From 5771aa9a1e089cef76ab17df14ad3c3fbde76310 Mon Sep 17 00:00:00 2001 From: sebastien <sebastien@ac1d8469-bf42-47a9-8791-bf33cf982152> Date: Wed, 2 Apr 2008 07:40:09 +0000 Subject: [PATCH] v4 preprocessor/macro: bugfix related to nested @for and @if structures git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1770 ac1d8469-bf42-47a9-8791-bf33cf982152 --- macro/MacroFlex.ll | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll index d3aa2406..d1d2fc9e 100644 --- a/macro/MacroFlex.ll +++ b/macro/MacroFlex.ll @@ -59,6 +59,7 @@ typedef Macro::parser::token token; SPC [ \t]+ EOL (\r)?\n +CONT \\\\ %% /* Code put at the beginning of yylex() */ @@ -90,7 +91,7 @@ EOL (\r)?\n <MACRO>{SPC}+ { yylloc->step(); } <MACRO>@ { BEGIN(INITIAL); return token::EOL; } -<MACRO>\\\\{SPC}*{EOL} { yylloc->lines(1); yylloc->step(); } +<MACRO>{CONT}{SPC}*{EOL} { yylloc->lines(1); yylloc->step(); } <MACRO>{EOL} { yylloc->lines(1); yylloc->step(); @@ -171,7 +172,12 @@ EOL (\r)?\n <MACRO><<EOF>> { driver.error(*yylloc, "Unexpected end of file while parsing a macro expression"); } <FOR_BODY>{EOL} { yylloc->lines(1); yylloc->step(); for_body_tmp.append(yytext); } -<FOR_BODY>@{SPC}*for { nested_for_nb++; for_body_tmp.append(yytext); } +<FOR_BODY>^{SPC}*@{SPC}*for({SPC}|{CONT}) { + /* In order to catch nested @for, it is necessary to start from the beginning of + the line (otherwise we could catch something like "@var@ for" */ + nested_for_nb++; + for_body_tmp.append(yytext); + } <FOR_BODY>. { for_body_tmp.append(yytext); } <FOR_BODY><<EOF>> { driver.error(*yylloc, "Unexpected end of file: @for loop not matched by an @endfor"); } <FOR_BODY>@{SPC}*endfor{SPC}*{EOL} { @@ -199,7 +205,12 @@ EOL (\r)?\n } <THEN_BODY>{EOL} { yylloc->lines(1); yylloc->step(); then_body_tmp.append(yytext); } -<THEN_BODY>@{SPC}*if { nested_if_nb++; then_body_tmp.append(yytext); } +<THEN_BODY>^{SPC}*@{SPC}*if({SPC}|{CONT}) { + /* In order to catch nested @if, it is necessary to start from the beginning of + the line (otherwise we could catch something like "@var@ if" */ + nested_if_nb++; + then_body_tmp.append(yytext); + } <THEN_BODY>. { then_body_tmp.append(yytext); } <THEN_BODY><<EOF>> { driver.error(*yylloc, "Unexpected end of file: @if not matched by an @endif"); } <THEN_BODY>@{SPC}*else{SPC}*{EOL} { @@ -237,7 +248,12 @@ EOL (\r)?\n } <ELSE_BODY>{EOL} { yylloc->lines(1); yylloc->step(); else_body_tmp.append(yytext); } -<ELSE_BODY>@{SPC}*if { nested_if_nb++; else_body_tmp.append(yytext); } +<ELSE_BODY>^{SPC}*@{SPC}*if({SPC}|{CONT}) { + /* In order to catch nested @if, it is necessary to start from the beginning of + the line (otherwise we could catch something like "@var@ if" */ + nested_if_nb++; + else_body_tmp.append(yytext); + } <ELSE_BODY>. { else_body_tmp.append(yytext); } <ELSE_BODY><<EOF>> { driver.error(*yylloc, "Unexpected end of file: @if not matched by an @endif"); } -- GitLab