From e030fe46256c666ab4aa6566e491d7fc593dd47c Mon Sep 17 00:00:00 2001 From: Alexandre Bique Date: Thu, 29 Jan 2009 23:36:49 +0100 Subject: [PATCH] Fixed location tracking for flex/bison --- src/ast/generator-ascii-visitor.cc | 11 ++++++---- src/ast/generator-browse-visitor.cc | 27 ++++++++++++----------- src/lang/mugiwara/bison-parser/lexer.l | 11 +++++++--- src/lang/mugiwara/bison-parser/parser.y | 39 +-------------------------------- tests/lang/mugiwara/input/cast.mgw | 1 + 5 files changed, 31 insertions(+), 58 deletions(-) diff --git a/src/ast/generator-ascii-visitor.cc b/src/ast/generator-ascii-visitor.cc index 44e65c8..87d268b 100644 --- a/src/ast/generator-ascii-visitor.cc +++ b/src/ast/generator-ascii-visitor.cc @@ -99,17 +99,20 @@ generateAsciiVisitorSource(const char * working_directory) << " std::cout << name_ << \" \";" NL << " std::cout << \""cchev"<"ctype << node->name << cchev">\e[m\" NL;" NL; - int i = 0; + unsigned int i = 0; std::vector allAttrs = ag::allAttributes(node); BOOST_FOREACH(const ag::Attribute * attr, allAttrs) { ++i; out << " name_ = \"" << attr->name << "\";" NL; if (ag::isNode(attr->type)) - out << " prefix_ = oldPrefix;" NL - << " prefix_.append(\" " + out << " if (node." << attr->name << ")" NL + << " {" NL + << " prefix_ = oldPrefix;" NL + << " prefix_.append(\" " << (i < allAttrs.size() ? "|" : " ") << "\");" NL - << " node." << attr->name << "->accept(*this);" NL; + << " node." << attr->name << "->accept(*this);" NL + << " }" NL; else if (ag::isVectorOfNode(attr->type)) { out << " i = 0;" NL diff --git a/src/ast/generator-browse-visitor.cc b/src/ast/generator-browse-visitor.cc index 0d6bf89..1a5d138 100644 --- a/src/ast/generator-browse-visitor.cc +++ b/src/ast/generator-browse-visitor.cc @@ -24,9 +24,9 @@ generateBrowseVisitorHeader(const char * working_directory) ag::appendGeneratedWarning(out); out << "#ifndef AST_BROWSE_VISITOR_HH" NL << "# define AST_BROWSE_VISITOR_HH" NL - NL + NL << "#include " NL - NL + NL << "namespace ast" NL << "{" NL; @@ -40,11 +40,11 @@ generateBrowseVisitorHeader(const char * working_directory) << " {" NL \ << " public:" NL \ << " virtual ~"Prefix"BrowseVisitor();" NL \ - NL; \ + NL; \ \ for (node = ag::nodes; !node->name.empty() > 0; node++) \ out << " virtual void visit("Const << node->name << " & node);" \ - NL NL; \ + NL NL; \ \ out << " };" NL NL; @@ -80,28 +80,29 @@ generateBrowseVisitorSource(const char * working_directory) << "{" NL; #define BROWSE_VISITOR_CC(Name, Const) \ - out << " "Name"::~"Name"() {}" NL NL; \ + out << " "Name"::~"Name"() {}" NL NL; \ \ for (node = ag::nodes; !node->name.empty() > 0; node++) \ { \ out << " void "Name"::visit("Const << node->name \ - << " & node __attribute__((unused)))" NL \ - << " {" NL; \ + << " & node __attribute__((unused)))" NL \ + << " {" NL; \ \ BOOST_FOREACH(const ag::Attribute * attr, allAttributes(node)) \ { \ if (ag::isNode(attr->type)) \ - out << " node." << attr->name << "->accept(*this);" NL; \ + out << " if (node." << attr->name << ")" NL \ + << " node." << attr->name << "->accept(*this);" NL; \ else if (ag::isVectorOfNode(attr->type)) \ - out << " if (node." << attr->name << ")" NL \ + out << " if (node." << attr->name << ")" NL \ << " BOOST_FOREACH(Node * n, (*node." << attr->name \ - << ")) {" NL \ + << ")) {" NL \ << " assert(n);" \ - << " n->accept(*this);" NL \ - << " }" NL; \ + << " n->accept(*this);" NL \ + << " }" NL; \ } \ \ - out << " }" NL NL; \ + out << " }" NL NL; \ } BROWSE_VISITOR_CC("BrowseVisitor", ""); diff --git a/src/lang/mugiwara/bison-parser/lexer.l b/src/lang/mugiwara/bison-parser/lexer.l index bd5a9d2..af34f13 100644 --- a/src/lang/mugiwara/bison-parser/lexer.l +++ b/src/lang/mugiwara/bison-parser/lexer.l @@ -9,12 +9,17 @@ #include #include "parser.hh" -//# define YY_USER_ACTION yylloc->columns (yyleng); +# define YY_USER_ACTION \ + yylloc->first_line = yylineno; \ + yylloc->last_line = yylineno; \ + yylloc->first_column = yylloc->last_column; \ + yylloc->last_column += yyleng; + %} %% -\n /* ignore end of line */ printf("lineno: %d\n", yylineno); +\n /* ignore end of line */yylloc->last_column = 0; [ \t]+ /* ignore whitespace */; \/\/.*$ /* line comment */ @@ -60,6 +65,6 @@ goto return GOTO; cast return CAST; (([0-9]+)?\.)?[0-9]+ yylval->number = strtod(yytext, 0); return NUMBER; -[a-zA-Z]+ yylval->string = strdup(yytext); return ID; +[a-zA-Z][a-zA-Z0-9_]+ yylval->string = strdup(yytext); return ID; %% diff --git a/src/lang/mugiwara/bison-parser/parser.y b/src/lang/mugiwara/bison-parser/parser.y index 9c39b91..557e437 100644 --- a/src/lang/mugiwara/bison-parser/parser.y +++ b/src/lang/mugiwara/bison-parser/parser.y @@ -3,6 +3,7 @@ %locations %start file %defines +%error-verbose %lex-param {yyscan_t yyscanner} %parse-param {yyscan_t yyscanner} @@ -17,16 +18,6 @@ #include #include -#define YYLTYPE YYLTYPE - -typedef struct YYLTYPE -{ - int first_line; - int first_column; - int last_line; - int last_column; -} YYLTYPE; - #include "parser.hh" #include "lexer.hh" @@ -39,34 +30,6 @@ void yyerror(YYLTYPE *yyloccp, yyscan_t yyscanner, ast::File *& file, const char << ": " << str << std::endl; } -// int yywrap(yyscan_t /*yyscanner*/) -// { -// return 1; -// } - -# define YYLLOC_DEFAULT(Current, Rhs, N) \ - do { \ - if (true) \ - { \ - (Current).first_line = YYRHSLOC(Rhs, 1).first_line; \ - (Current).first_column = YYRHSLOC(Rhs, 1).first_column; \ - (Current).last_line = YYRHSLOC(Rhs, N).last_line; \ - (Current).last_column = YYRHSLOC(Rhs, N).last_column; \ - } \ - else \ - { \ - (Current).first_line = (Current).last_line = \ - YYRHSLOC(Rhs, 0).last_line; \ - (Current).first_column = (Current).last_column = \ - YYRHSLOC(Rhs, 0).last_column; \ - } \ - std::cerr << "N: " << N << " " \ - << (Current).first_line << "." << (Current).first_column \ - << "-" \ - << (Current).last_line << "." << (Current).last_column \ - << std::endl; \ - } while (0) - #define MAKE_BINARY_EXP(Type, Out, Left, Right) \ do { \ ast::Type * exp = new ast::Type(); \ diff --git a/tests/lang/mugiwara/input/cast.mgw b/tests/lang/mugiwara/input/cast.mgw index 56891b8..157fdbc 100644 --- a/tests/lang/mugiwara/input/cast.mgw +++ b/tests/lang/mugiwara/input/cast.mgw @@ -7,3 +7,4 @@ int main() cast(uint16, 43); cast(uint8, 43); } + \ No newline at end of file -- 2.11.4.GIT