diff options
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 74 |
1 files changed, 13 insertions, 61 deletions
diff --git a/src/main.cpp b/src/main.cpp index 015648c..96af216 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,6 +16,7 @@ #include <specs.h> #include "optical_output_port.h" #include "parser/parse_tree.h" +#include "parser/parser_state.h" class OpticalOutputPort; @@ -26,54 +27,10 @@ using namespace literals; #include "../build/parser/parser.tab.h" #include "../build/parser/parser.yy.h" extern int yydebug; -int yywrap(yyscan_t scanner); -extern "C" int yy_delete_buffer(yyscan_t scanner); - -static deque<YY_BUFFER_STATE> netlist_buf_fifo; - -FILE* open_netlist_file(const string &filename, yyscan_t &scanner) -{ - // Check whether the file exists - FILE *f; - if (filename == "-"s) { - cerr << "Error: stdin not supported." << endl; - exit(1); - } else if (!(f = fopen(filename.c_str(), "r"))) { - cerr << "Error: File not found \"" << filename << "\"" << endl; - exit(1); - } - - string header = "\n**** START of " + filename + " ****\n"; - netlist_buf_fifo.push_back(yy_scan_string(header.c_str(), scanner)); - - YY_BUFFER_STATE buf = yy_create_buffer(f, YY_BUF_SIZE, scanner); - netlist_buf_fifo.push_back(buf); - - string footer = "\n**** END of " + filename + " ****\n"; - netlist_buf_fifo.push_back(yy_scan_string(footer.c_str(), scanner)); - - return f; -} - -int yywrap(yyscan_t scanner) -{ - static YY_BUFFER_STATE *last_buffer = NULL; - - if (netlist_buf_fifo.empty()) - return 1; - else - { - YY_BUFFER_STATE buf = netlist_buf_fifo.front(); - netlist_buf_fifo.pop_front(); - yy_switch_to_buffer(buf, scanner); - if ( last_buffer ) - { - yy_delete_buffer( *last_buffer, scanner); - last_buffer = &buf; - } - return 0; - } -} +extern int yy_load_next_buf(yyscan_t scanner); +extern void yy_add_content_from_string(const string &str, const string &desc, yyscan_t &scanner); +extern void yy_add_content_from_file(const string &filename, yyscan_t &scanner); +//extern "C" int yy_delete_buffer(yyscan_t scanner); int do_list_tests() { @@ -121,9 +78,10 @@ int do_circuit(const string &filename, bool is_dry_run = false, const string& js cout << "╚═══════════════════╝" << endl; yyscan_t scanner; + ParserState *parser_state = new ParserState; YY_BUFFER_STATE buf; - yylex_init(&scanner); + yylex_init_extra(parser_state, &scanner); buf = yy_create_buffer(f, YY_BUF_SIZE, scanner); yy_switch_to_buffer(buf, scanner); @@ -132,6 +90,7 @@ int do_circuit(const string &filename, bool is_dry_run = false, const string& js //yy_delete_buffer(buf, scanner); yylex_destroy(scanner); + delete parser_state; // Close the file if (f) @@ -184,23 +143,19 @@ int do_circuit(const string &filename, bool is_dry_run = false, const string& js int build_circuit(ParseTree &pt, const vector<string> &filenames, string footer="") { yyscan_t scanner; - yylex_init(&scanner); + yylex_init_extra(new ParserState(), &scanner); - vector<FILE*> files; for(const auto &fname: filenames) - { - FILE * f = open_netlist_file(fname, scanner); - files.push_back(f); - } + yy_add_content_from_file(fname, scanner); // add footer - netlist_buf_fifo.push_back(yy_scan_string(footer.c_str(), scanner)); + yy_add_content_from_string(footer, "footer autogenerated from command line arguments", scanner); // Set up first file - int res = yywrap(scanner); + int res = yy_load_next_buf(scanner); if(res != 0) { - cerr << "Expected yywrap to return 0" << endl; + cerr << "Expected yy_load_next_buf to return 0" << endl; exit(1); } @@ -213,9 +168,6 @@ int build_circuit(ParseTree &pt, const vector<string> &filenames, string footer= //yy_delete_buffer(buf, scanner); yylex_destroy(scanner); - for (auto &f: files) - fclose(f); - // Return if unsuccessful if (parsing_result != 0) { return parsing_result; |
