From e34fe4dde7c37e7bd35da32ad8211995d678552e Mon Sep 17 00:00:00 2001 From: Ulya Trofimovich Date: Thu, 29 Aug 2019 22:27:01 +0100 Subject: [PATCH] libre2c: added prefix 'TOKEN_' to all parser tokens to avoid name collisions. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Apparently some versions of bison generate token definitions as macros, not as enum members. If a bison-generated header is included into another file that contains identifiers identical to the names of parser tokens, all such identifiers are macro-substituted to a numeric constant. Adding a prefix to token names reduces the chances of name collision. This fixes bug #261: "lib/parse.h:49:15: 错误:expected unqualified-id before numeric constant" reported by qidizi. --- bootstrap/lib/lex.cc | 20 ++++++++++---------- bootstrap/lib/parse.cc | 12 ++++++------ bootstrap/lib/parse.h | 6 +++--- lib/lex.re | 18 +++++++++--------- lib/parse.ypp | 20 ++++++++++---------- 5 files changed, 38 insertions(+), 38 deletions(-) diff --git a/bootstrap/lib/lex.cc b/bootstrap/lib/lex.cc index f6778930..f8a7e995 100644 --- a/bootstrap/lib/lex.cc +++ b/bootstrap/lib/lex.cc @@ -1,4 +1,4 @@ -/* Generated by re2c 1.1.1 on Sat Jul 13 19:06:36 2019 */ +/* Generated by re2c 1.2.1 on Thu Aug 29 22:25:45 2019 */ #line 1 "../lib/lex.re" #include @@ -109,7 +109,7 @@ yy5: std::vector *str = new std::vector; str->push_back(c); yylval.regexp = ast_str(NOWHERE, str, false); - return REGEXP; + return TOKEN_REGEXP; } #line 115 "lib/lex.cc" yy6: @@ -117,7 +117,7 @@ yy6: #line 46 "../lib/lex.re" { error("anchors are not supported"); - return ERROR; + return TOKEN_ERROR; } #line 123 "lib/lex.cc" yy8: @@ -130,7 +130,7 @@ yy10: #line 72 "../lib/lex.re" { yylval.regexp = ast_dot(NOWHERE); - return REGEXP; + return TOKEN_REGEXP; } #line 136 "lib/lex.cc" yy12: @@ -178,7 +178,7 @@ yy21: { if (!s_to_u32_unsafe(x, cur - 1, yylval.bounds.min)) goto err_cnt; yylval.bounds.max = yylval.bounds.min; - return COUNT; + return TOKEN_COUNT; } #line 184 "lib/lex.cc" yy23: @@ -194,7 +194,7 @@ yy25: { if (!s_to_u32_unsafe(x, cur - 2, yylval.bounds.min)) goto err_cnt; yylval.bounds.max = AST::MANY; - return COUNT; + return TOKEN_COUNT; } #line 200 "lib/lex.cc" yy27: @@ -205,7 +205,7 @@ yy27: { if (!s_to_u32_unsafe(x, y - 1, yylval.bounds.min) || !s_to_u32_unsafe(y, cur - 1, yylval.bounds.max)) goto err_cnt; - return COUNT; + return TOKEN_COUNT; } #line 211 "lib/lex.cc" } @@ -257,7 +257,7 @@ yy39: std::vector *p = new std::vector; p->swap(cls); yylval.regexp = ast_cls(NOWHERE, p, neg); - return REGEXP; + return TOKEN_REGEXP; } #line 263 "lib/lex.cc" } @@ -266,11 +266,11 @@ yy39: err: error("syntax error: %s\n", cur); - return ERROR; + return TOKEN_ERROR; err_cnt: error("repetition count overflow"); - return ERROR; + return TOKEN_ERROR; } int32_t lex_cls_chr(const char *&cur, uint32_t &c) diff --git a/bootstrap/lib/parse.cc b/bootstrap/lib/parse.cc index 413de35d..e8738534 100644 --- a/bootstrap/lib/parse.cc +++ b/bootstrap/lib/parse.cc @@ -128,9 +128,9 @@ extern int yydebug; # define YYTOKENTYPE enum yytokentype { - COUNT = 258, - ERROR = 259, - REGEXP = 260 + TOKEN_COUNT = 258, + TOKEN_ERROR = 259, + TOKEN_REGEXP = 260 }; #endif @@ -469,9 +469,9 @@ static const yytype_uint8 yyrline[] = First, the terminals, then, starting at YYNTOKENS, nonterminals. */ static const char *const yytname[] = { - "$end", "error", "$undefined", "COUNT", "ERROR", "REGEXP", "'|'", "'*'", - "'+'", "'?'", "'('", "')'", "$accept", "regexp", "expr", "term", - "factor", "primary", YY_NULLPTR + "$end", "error", "$undefined", "TOKEN_COUNT", "TOKEN_ERROR", + "TOKEN_REGEXP", "'|'", "'*'", "'+'", "'?'", "'('", "')'", "$accept", + "regexp", "expr", "term", "factor", "primary", YY_NULLPTR }; #endif diff --git a/bootstrap/lib/parse.h b/bootstrap/lib/parse.h index 9be559c7..10864f11 100644 --- a/bootstrap/lib/parse.h +++ b/bootstrap/lib/parse.h @@ -45,9 +45,9 @@ extern int yydebug; # define YYTOKENTYPE enum yytokentype { - COUNT = 258, - ERROR = 259, - REGEXP = 260 + TOKEN_COUNT = 258, + TOKEN_ERROR = 259, + TOKEN_REGEXP = 260 }; #endif diff --git a/lib/lex.re b/lib/lex.re index 754d4c90..1dc4c701 100644 --- a/lib/lex.re +++ b/lib/lex.re @@ -45,7 +45,7 @@ int lex(const char *&cur) [$^] { error("anchors are not supported"); - return ERROR; + return TOKEN_ERROR; } "[^" { neg = true; goto cls; } @@ -54,24 +54,24 @@ int lex(const char *&cur) "{" @x num "}" { if (!s_to_u32_unsafe(x, cur - 1, yylval.bounds.min)) goto err_cnt; yylval.bounds.max = yylval.bounds.min; - return COUNT; + return TOKEN_COUNT; } "{" @x num "," @y num "}" { if (!s_to_u32_unsafe(x, y - 1, yylval.bounds.min) || !s_to_u32_unsafe(y, cur - 1, yylval.bounds.max)) goto err_cnt; - return COUNT; + return TOKEN_COUNT; } "{" @x num ",}" { if (!s_to_u32_unsafe(x, cur - 2, yylval.bounds.min)) goto err_cnt; yylval.bounds.max = AST::MANY; - return COUNT; + return TOKEN_COUNT; } "." { yylval.regexp = ast_dot(NOWHERE); - return REGEXP; + return TOKEN_REGEXP; } [^] \ nil { @@ -79,7 +79,7 @@ int lex(const char *&cur) std::vector *str = new std::vector; str->push_back(c); yylval.regexp = ast_str(NOWHERE, str, false); - return REGEXP; + return TOKEN_REGEXP; } */ @@ -98,17 +98,17 @@ add: std::vector *p = new std::vector; p->swap(cls); yylval.regexp = ast_cls(NOWHERE, p, neg); - return REGEXP; + return TOKEN_REGEXP; } */ err: error("syntax error: %s\n", cur); - return ERROR; + return TOKEN_ERROR; err_cnt: error("repetition count overflow"); - return ERROR; + return TOKEN_ERROR; } int32_t lex_cls_chr(const char *&cur, uint32_t &c) diff --git a/lib/parse.ypp b/lib/parse.ypp index 1288e38f..e10209db 100644 --- a/lib/parse.ypp +++ b/lib/parse.ypp @@ -39,12 +39,12 @@ void yyerror(const char *pattern, const char*) RE2C_ATTR((noreturn)); re2c::ASTBounds bounds; }; -%token COUNT -%token ERROR -%token REGEXP +%token TOKEN_COUNT +%token TOKEN_ERROR +%token TOKEN_REGEXP -%type REGEXP regexp expr term factor primary -%type COUNT +%type TOKEN_REGEXP regexp expr term factor primary +%type TOKEN_COUNT %% @@ -62,14 +62,14 @@ term factor : primary -| primary '*' { $$ = ast_iter($1, 0, AST::MANY); } -| primary '+' { $$ = ast_iter($1, 1, AST::MANY); } -| primary '?' { $$ = ast_iter($1, 0, 1); } -| primary COUNT { $$ = ast_iter($1, $2.min, $2.max); } +| primary '*' { $$ = ast_iter($1, 0, AST::MANY); } +| primary '+' { $$ = ast_iter($1, 1, AST::MANY); } +| primary '?' { $$ = ast_iter($1, 0, 1); } +| primary TOKEN_COUNT { $$ = ast_iter($1, $2.min, $2.max); } ; primary -: REGEXP +: TOKEN_REGEXP | '(' ')' { $$ = ast_cap(ast_nil(NOWHERE)); } | '(' expr ')' { $$ = ast_cap($2); } ; -- 2.40.0