From a4ed94bc13de329773be1cc065176da15061fd6c Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 10 Apr 2020 07:30:24 +0200 Subject: [PATCH] tokens: properly define the "error" token kind There are people out there that do use YYERRCODE (the token kind of the error token). See for instance https://github.com/borbolla-automation/SPC_Machines/blob/3812012bb782bfdfe7b325950a35cd337925fcad/unixODBC-2.3.2/Drivers/nn/yylex.c. Currently, YYERRCODE is defined by yacc.c in an adhoc way as a #define in the *.c file only. It belongs with the other token kinds. YYERRCODE is not a nice name, it does not fit in our naming scheme. YYERROR would be more logical, but it collides with the YYERROR macro. Shall we keep the same name in all the skeletons? Besides, to avoid collisions in C, we need to apply the api prefix: YYERRCODE is actually ERRCODE. This is not needed in the other languages. * data/skeletons/bison.m4 (b4_symbol_token_kind): New. Map the error token to "YYERRCODE". * data/skeletons/yacc.c (YYERRCODE): Don't define it, it's handled by... * src/output.c (prepare_symbol_definitions): this. * tests/input.at (Redefining the error token): Check it. --- TODO | 1 + data/skeletons/bison.m4 | 16 ++++++++++++++-- data/skeletons/yacc.c | 3 --- src/output.c | 2 +- src/parse-gram.c | 3 --- src/parse-gram.h | 1 + tests/input.at | 34 ++++++++++++++++++++++++---------- 7 files changed, 41 insertions(+), 19 deletions(-) diff --git a/TODO b/TODO index 554283df..37a80460 100644 --- a/TODO +++ b/TODO @@ -6,6 +6,7 @@ should not have to dispatch to several APIs. ** Documentation - yyexpected_tokens in all the languages. - YYENOMEM +- YYERRCODE? - i18n in Java - symbol.type_get should be kind_get, and it's not documented. diff --git a/data/skeletons/bison.m4 b/data/skeletons/bison.m4 index a2bb1cf5..09dc4456 100644 --- a/data/skeletons/bison.m4 +++ b/data/skeletons/bison.m4 @@ -405,6 +405,14 @@ m4_define([_b4_symbol], [__b4_symbol([$1], [$2])])]) +# b4_symbol_token_kind(NUM) +# ------------------------- +# The token kind of this symbol. +m4_define([b4_symbol_token_kind], +[b4_percent_define_get([api.token.prefix])dnl +_b4_symbol([$1], [id])]) + + # b4_symbol_kind(NUM) # ------------------- # Build the name of the kind of this symbol. It must always exist, @@ -432,8 +440,7 @@ m4_case([$1], # If FIELD = id, prepend the token prefix. m4_define([b4_symbol], [m4_case([$2], - [id], [m4_do([b4_percent_define_get([api.token.prefix])], - [_b4_symbol([$1], [id])])], + [id], [b4_symbol_token_kind([$1])], [kind], [b4_symbol_kind([$1])], [_b4_symbol($@)])]) @@ -449,6 +456,10 @@ m4_define([b4_symbol_if], [m4_fatal([$0: field $2 of $1 is not a Boolean:] b4_symbol([$1], [$2]))])]) +# Give the error token a better name. +m4_define([b4_symbol(1, id)], [b4_api_PREFIX[][ERRCODE]]) + + # b4_symbol_tag_comment(SYMBOL-NUM) # --------------------------------- # Issue a comment giving the tag of symbol NUM. @@ -534,6 +545,7 @@ m4_define([b4_token_visible_if], [b4_symbol_if([$1], [has_id], [$2], [$3])], [$3])]) + # b4_token_has_definition(NUM) # ---------------------------- # 1 if NUM is visible, nothing otherwise. diff --git a/data/skeletons/yacc.c b/data/skeletons/yacc.c index 2c47cf21..12bb7fe1 100644 --- a/data/skeletons/yacc.c +++ b/data/skeletons/yacc.c @@ -743,9 +743,6 @@ enum { YYENOMEM = -2 }; } \ while (0) -/* Error token external number. */ -#define YYERRCODE ]b4_symbol(1, user_number)[ - ]b4_locations_if([[ ]b4_yylloc_default_define[ #define YYRHSLOC(Rhs, K) ((Rhs)[K]) diff --git a/src/output.c b/src/output.c index 451d92ee..f759fc8f 100644 --- a/src/output.c +++ b/src/output.c @@ -550,7 +550,7 @@ prepare_symbol_definitions (void) SET_KEY ("is_token"); MUSCLE_INSERT_INT (key, - i < ntokens && sym != errtoken && sym != undeftoken); + i < ntokens && sym != undeftoken); SET_KEY ("number"); MUSCLE_INSERT_INT (key, sym->content->number); diff --git a/src/parse-gram.c b/src/parse-gram.c index 186d838e..ed6794b2 100644 --- a/src/parse-gram.c +++ b/src/parse-gram.c @@ -927,9 +927,6 @@ enum { YYENOMEM = -2 }; } \ while (0) -/* Error token external number. */ -#define YYERRCODE 256 - /* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N]. If N is 0, then set CURRENT to the empty location which ends diff --git a/src/parse-gram.h b/src/parse-gram.h index 55912057..ae498897 100644 --- a/src/parse-gram.h +++ b/src/parse-gram.h @@ -78,6 +78,7 @@ extern int gram_debug; enum gram_tokentype { GRAM_EOF = 0, /* "end of file" */ + GRAM_ERRCODE = 1, /* error */ STRING = 3, /* "string" */ TSTRING = 4, /* "translatable string" */ PERCENT_TOKEN = 5, /* "%token" */ diff --git a/tests/input.at b/tests/input.at index 4f196d7b..d9cfc3f7 100644 --- a/tests/input.at +++ b/tests/input.at @@ -306,6 +306,7 @@ AT_SETUP([Redefining the error token]) # We used to crash when trying to display the original definition of # "error", which is a builtin without any location. +AT_BISON_OPTION_PUSHDEFS AT_DATA([input.y], [[%token error 123 @@ -327,20 +328,31 @@ input.y:2.14-16: error: redefining user token number of error ]]) # While at it, make sure we properly used the user's number for -# "error". +# "error". I don't see what it buys us, but... AT_DATA([input.y], -[[%token error 123 +[[%{ +#include +#include +]AT_YYERROR_DEFINE[ +]AT_YYLEX_DEFINE[ +%} + +%token error 123 %% exp: +%% +int main (void) +{ + assert (YYERRCODE == 123); + assert (YYTRANSLATE (YYERRCODE) == YYSYMBOL_YYERROR); + return 0; +} ]]) -AT_BISON_CHECK([input.y]) - -AT_CHECK([$EGREP -E '123|256' input.tab.c], [], -[[ 0, 123, 257 -#define YYERRCODE 123 -]]) +AT_FULL_COMPILE([input]) +AT_PARSER_CHECK([input], 0) +AT_BISON_OPTION_POPDEFS AT_CLEANUP @@ -377,7 +389,9 @@ AT_CLEANUP AT_SETUP([Symbol declarations]) AT_DATA([dump-symbols.m4], -[[m4@&t@_define([b4_symbol_dump], +[[m4@&t@_define([b4_api_PREFIX], [YY]) + +m4@&t@_define([b4_symbol_dump], [$1, d@&t@nl b4_symbol_if([$1], [is_token], [Token], [Nonterminal]), d@&t@nl b4_symbol([$1], [tag]), d@&t@nl @@ -416,7 +430,7 @@ AT_BISON_CHECK([-Wno-other -S./dump-symbols.m4 input.y]) AT_CHECK([cat symbols.csv], [], [[number, class, tag, id, user_number, type, 0, Token, $end, , 0, , -1, Nonterminal, error, error, 256, , +1, Token, error, YYERRCODE, 256, , 2, Nonterminal, $undefined, , 257, , 3, Token, 'a', , 97, , 4, Token, "A1", A1, 1, ,