From e95498f8e547b42233dfa2e1e756cac88ac4c5b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Zrounba?= <6691770+clement-z@users.noreply.github.com> Date: Fri, 3 Nov 2023 15:04:06 +0100 Subject: improve handling of multiple files --- src/main.cpp | 15 +++++++++++++++ src/parser/parser.l | 15 +++++++++++---- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 08cef21..e850e34 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,6 +27,7 @@ using namespace literals; #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 netlist_buf_fifo; @@ -42,13 +43,22 @@ FILE* open_netlist_file(const string &filename, yyscan_t &scanner) 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 @@ -56,6 +66,11 @@ int yywrap(yyscan_t scanner) 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; } } diff --git a/src/parser/parser.l b/src/parser/parser.l index 7c33630..3edda2f 100644 --- a/src/parser/parser.l +++ b/src/parser/parser.l @@ -93,6 +93,13 @@ static T from_string(const string &str) %% /*** Rules section ***/ +^.*{EOL} { + // print the netlist as it is read + // note: doesnt handle included files + if(false) + cout << yytext; REJECT; +} + ^\.include {yy_push_state(INCLUDE, yyscanner);} {WS}* /* eat the whitespace */ [^ ;\t\n\r]+ { /* got the include file name */ @@ -383,12 +390,12 @@ static T from_string(const string &str) <> { yypop_buffer_state(yyscanner); - if ( !YY_CURRENT_BUFFER ) - { + if ( !YY_CURRENT_BUFFER ) + { yyterminate(); - } - return '\n'; } + return '\n'; +} <> { exit(yylerror(yyscanner, "Unexpected end of file.")); -- cgit v1.2.3