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.
This commit is contained in:
Akim Demaille
2020-04-12 13:44:57 +02:00
parent ff50f6f223
commit ffa46e6516
4 changed files with 34 additions and 26 deletions

View File

@@ -196,6 +196,23 @@ has_translations (void)
return false; 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_<MUSCLE_NAME> (e.g., b4_tname) table with the /* Generate the b4_<MUSCLE_NAME> (e.g., b4_tname) table with the
symbol names (aka tags). */ symbol names (aka tags). */
@@ -209,27 +226,18 @@ prepare_symbol_names (char const *muscle_name)
const bool with_translations = !quote && has_translations (); const bool with_translations = !quote && has_translations ();
/* We assume that the table will be output starting at column 2. */ /* 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); struct quoting_options *qo = clone_quoting_options (0);
set_quoting_style (qo, c_quoting_style); set_quoting_style (qo, c_quoting_style);
set_quoting_flags (qo, QA_SPLIT_TRIGRAPHS); set_quoting_flags (qo, QA_SPLIT_TRIGRAPHS);
for (int i = 0; i < nsyms; i++) for (int i = 0; i < nsyms; i++)
{ {
/* Use "end of file" rather than "$end". But keep "$end" in the const char *tag = symbol_tag (symbols[i]);
reports, it's shorter and more consistent. Support i18n if bool translatable =
the user already uses it. */ with_translations
const char *tag = symbols[i]->tag; && (symbols[i]->translatable
bool translatable = with_translations && symbols[i]->translatable; || (!eof_is_user_defined && symbols[i]->content == endtoken->content)
if (!eof_is_user_defined && symbols[i]->content == endtoken->content) || symbols[i]->content == undeftoken->content);
{
tag = "\"end of file\"";
translatable = with_translations;
}
else if (symbols[i]->content == undeftoken->content)
{
tag = "\"invalid token\"";
translatable = with_translations;
}
char *cp char *cp
= tag[0] == '"' && !quote = tag[0] == '"' && !quote
@@ -241,10 +249,10 @@ prepare_symbol_names (char const *muscle_name)
= strlen (cp) + 2 = strlen (cp) + 2
+ (translatable ? strlen ("N_()") : 0); + (translatable ? strlen ("N_()") : 0);
if (j + width > 75) if (col + width > 75)
{ {
obstack_sgrow (&format_obstack, "\n "); obstack_sgrow (&format_obstack, "\n ");
j = 1; col = 1;
} }
if (i) if (i)
@@ -256,7 +264,7 @@ prepare_symbol_names (char const *muscle_name)
obstack_sgrow (&format_obstack, "])["); obstack_sgrow (&format_obstack, "])[");
free (cp); free (cp);
obstack_1grow (&format_obstack, ','); obstack_1grow (&format_obstack, ',');
j += width; col += width;
} }
free (qo); free (qo);
obstack_sgrow (&format_obstack, " ]b4_null["); obstack_sgrow (&format_obstack, " ]b4_null[");
@@ -570,7 +578,7 @@ prepare_symbol_definitions (void)
/* Its tag. Typically for documentation purpose. */ /* Its tag. Typically for documentation purpose. */
SET_KEY ("tag"); SET_KEY ("tag");
MUSCLE_INSERT_STRING (key, sym->tag); MUSCLE_INSERT_STRING (key, symbol_tag (sym));
SET_KEY ("user_number"); SET_KEY ("user_number");
MUSCLE_INSERT_INT (key, sym->content->user_token_number); MUSCLE_INSERT_INT (key, sym->content->user_token_number);

View File

@@ -104,9 +104,9 @@
enum yysymbol_kind_t enum yysymbol_kind_t
{ {
YYSYMBOL_YYEMPTY = -2, YYSYMBOL_YYEMPTY = -2,
YYSYMBOL_YYEOF = 0, /* $end */ YYSYMBOL_YYEOF = 0, /* "end of file" */
YYSYMBOL_YYERROR = 1, /* error */ YYSYMBOL_YYERROR = 1, /* error */
YYSYMBOL_YYUNDEF = 2, /* $undefined */ YYSYMBOL_YYUNDEF = 2, /* "invalid token" */
YYSYMBOL_STRING = 3, /* "string" */ YYSYMBOL_STRING = 3, /* "string" */
YYSYMBOL_TSTRING = 4, /* "translatable string" */ YYSYMBOL_TSTRING = 4, /* "translatable string" */
YYSYMBOL_PERCENT_TOKEN = 5, /* "%token" */ YYSYMBOL_PERCENT_TOKEN = 5, /* "%token" */

View File

@@ -77,9 +77,9 @@ extern int gram_debug;
# define GRAM_TOKENTYPE # define GRAM_TOKENTYPE
enum gram_tokentype enum gram_tokentype
{ {
GRAM_EOF = 0, /* $end */ GRAM_EOF = 0, /* "end of file" */
GRAM_ERRCODE = 1, /* error */ GRAM_ERRCODE = 1, /* error */
GRAM_UNDEF = 2, /* $undefined */ GRAM_UNDEF = 2, /* "invalid token" */
STRING = 3, /* "string" */ STRING = 3, /* "string" */
TSTRING = 4, /* "translatable string" */ TSTRING = 4, /* "translatable string" */
PERCENT_TOKEN = 5, /* "%token" */ PERCENT_TOKEN = 5, /* "%token" */

View File

@@ -431,9 +431,9 @@ exp:;
AT_BISON_CHECK([-Wno-other -S./dump-symbols.m4 input.y]) AT_BISON_CHECK([-Wno-other -S./dump-symbols.m4 input.y])
AT_CHECK([cat symbols.csv], [], AT_CHECK([cat symbols.csv], [],
[[number, class, tag, id, user_number, type, [[number, class, tag, id, user_number, type,
0, Token, $end, YYEOF, 0, , 0, Token, "end of file", YYEOF, 0, ,
1, Token, error, YYERRCODE, 256, , 1, Token, error, YYERRCODE, 256, ,
2, Token, $undefined, YYUNDEF, 257, , 2, Token, "invalid token", YYUNDEF, 257, ,
3, Token, 'a', , 97, , 3, Token, 'a', , 97, ,
4, Token, "A1", A1, 1, , 4, Token, "A1", A1, 1, ,
5, Token, A2, A2, 258, , 5, Token, A2, A2, 258, ,