aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorClément Zrounba <clement.zrounba@ec-lyon.fr>2023-11-14 17:45:45 +0000
committerClément Zrounba <clement.zrounba@ec-lyon.fr>2023-11-14 18:17:32 +0000
commitc8450b94be661a6d9d9d2718cc16befa4a5abae9 (patch)
tree099732f9f114d2cb3d20765664bfdbebe9fd719b /src/main.cpp
parent1b0780d225707f2cd71e82eaab8df5318cd72bae (diff)
downloadspecs-c8450b94be661a6d9d9d2718cc16befa4a5abae9.tar.gz
specs-c8450b94be661a6d9d9d2718cc16befa4a5abae9.zip
Improve parsing
- Make multi-file more robust - Error reporting is more accurate (shows file:line and prints offending string) - Allow `.include` directives in subcircuits
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp74
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;