From ffa46e651686a684bd4bf95e7a1cb9ae8989915e Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sun, 12 Apr 2020 13:44:57 +0200 Subject: [PATCH] skeletons: clarify the tag of special tokens From GRAM_EOF = 0, /* $end */ GRAM_ERRCODE = 1, /* error */ GRAM_UNDEF = 2, /* $undefined */ to GRAM_EOF = 0, /* "end of file" */ GRAM_ERRCODE = 1, /* error */ GRAM_UNDEF = 2, /* "invalid token" */ * src/output.c (symbol_tag): New. Use it to pass the token names and the symbol tags to the skeletons. * tests/input.at: Adjust. --- src/output.c | 48 ++++++++++++++++++++++++++++-------------------- src/parse-gram.c | 4 ++-- src/parse-gram.h | 4 ++-- tests/input.at | 4 ++-- 4 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/output.c b/src/output.c index 1a7aa906..01669f5a 100644 --- a/src/output.c +++ b/src/output.c @@ -196,6 +196,23 @@ has_translations (void) return false; } +/* The tag to show in the generated parsers. Use "end of file" rather + than "$end". But keep "$end" in the reports, it's shorter and more + consistent. Support i18n if the user already uses it. */ +static const char * +symbol_tag (const symbol *sym) +{ + const bool eof_is_user_defined + = !endtoken->alias || STRNEQ (endtoken->alias->tag, "$end"); + + if (!eof_is_user_defined && sym->content == endtoken->content) + return "\"end of file\""; + else if (sym->content == undeftoken->content) + return "\"invalid token\""; + else + return sym->tag; +} + /* Generate the b4_ (e.g., b4_tname) table with the symbol names (aka tags). */ @@ -209,27 +226,18 @@ prepare_symbol_names (char const *muscle_name) const bool with_translations = !quote && has_translations (); /* We assume that the table will be output starting at column 2. */ - int j = 2; + int col = 2; struct quoting_options *qo = clone_quoting_options (0); set_quoting_style (qo, c_quoting_style); set_quoting_flags (qo, QA_SPLIT_TRIGRAPHS); for (int i = 0; i < nsyms; i++) { - /* Use "end of file" rather than "$end". But keep "$end" in the - reports, it's shorter and more consistent. Support i18n if - the user already uses it. */ - const char *tag = symbols[i]->tag; - bool translatable = with_translations && symbols[i]->translatable; - if (!eof_is_user_defined && symbols[i]->content == endtoken->content) - { - tag = "\"end of file\""; - translatable = with_translations; - } - else if (symbols[i]->content == undeftoken->content) - { - tag = "\"invalid token\""; - translatable = with_translations; - } + const char *tag = symbol_tag (symbols[i]); + bool translatable = + with_translations + && (symbols[i]->translatable + || (!eof_is_user_defined && symbols[i]->content == endtoken->content) + || symbols[i]->content == undeftoken->content); char *cp = tag[0] == '"' && !quote @@ -241,10 +249,10 @@ prepare_symbol_names (char const *muscle_name) = strlen (cp) + 2 + (translatable ? strlen ("N_()") : 0); - if (j + width > 75) + if (col + width > 75) { obstack_sgrow (&format_obstack, "\n "); - j = 1; + col = 1; } if (i) @@ -256,7 +264,7 @@ prepare_symbol_names (char const *muscle_name) obstack_sgrow (&format_obstack, "])["); free (cp); obstack_1grow (&format_obstack, ','); - j += width; + col += width; } free (qo); obstack_sgrow (&format_obstack, " ]b4_null["); @@ -570,7 +578,7 @@ prepare_symbol_definitions (void) /* Its tag. Typically for documentation purpose. */ SET_KEY ("tag"); - MUSCLE_INSERT_STRING (key, sym->tag); + MUSCLE_INSERT_STRING (key, symbol_tag (sym)); SET_KEY ("user_number"); MUSCLE_INSERT_INT (key, sym->content->user_token_number); diff --git a/src/parse-gram.c b/src/parse-gram.c index be7e599d..fb74a582 100644 --- a/src/parse-gram.c +++ b/src/parse-gram.c @@ -104,9 +104,9 @@ enum yysymbol_kind_t { YYSYMBOL_YYEMPTY = -2, - YYSYMBOL_YYEOF = 0, /* $end */ + YYSYMBOL_YYEOF = 0, /* "end of file" */ YYSYMBOL_YYERROR = 1, /* error */ - YYSYMBOL_YYUNDEF = 2, /* $undefined */ + YYSYMBOL_YYUNDEF = 2, /* "invalid token" */ YYSYMBOL_STRING = 3, /* "string" */ YYSYMBOL_TSTRING = 4, /* "translatable string" */ YYSYMBOL_PERCENT_TOKEN = 5, /* "%token" */ diff --git a/src/parse-gram.h b/src/parse-gram.h index ac501c5f..b40347ba 100644 --- a/src/parse-gram.h +++ b/src/parse-gram.h @@ -77,9 +77,9 @@ extern int gram_debug; # define GRAM_TOKENTYPE enum gram_tokentype { - GRAM_EOF = 0, /* $end */ + GRAM_EOF = 0, /* "end of file" */ GRAM_ERRCODE = 1, /* error */ - GRAM_UNDEF = 2, /* $undefined */ + GRAM_UNDEF = 2, /* "invalid token" */ STRING = 3, /* "string" */ TSTRING = 4, /* "translatable string" */ PERCENT_TOKEN = 5, /* "%token" */ diff --git a/tests/input.at b/tests/input.at index 7cf2eccb..f1cab823 100644 --- a/tests/input.at +++ b/tests/input.at @@ -431,9 +431,9 @@ exp:; 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, YYEOF, 0, , +0, Token, "end of file", YYEOF, 0, , 1, Token, error, YYERRCODE, 256, , -2, Token, $undefined, YYUNDEF, 257, , +2, Token, "invalid token", YYUNDEF, 257, , 3, Token, 'a', , 97, , 4, Token, "A1", A1, 1, , 5, Token, A2, A2, 258, ,