parsers: support translatable token aliases

In addition to

    %token NUM "number"

accept

    %token NUM _("number")

in which case the token will be translated in error messages.
Do not use _() in the output if there are no translatable tokens.

* src/symtab.h, src/symtab.c (symbol): Add a 'translatable' member.
* src/parse-gram.y (TSTRING): New token.
(string_as_id.opt): Replace with...
(alias): this.
Use it.
* src/scan-gram.l (SC_ESCAPED_TSTRING): New start conditions, to match
TSTRINGs.
* src/output.c (prepare_symbols): Define b4_translatable if there are
translatable strings.

* data/skeletons/glr.c, data/skeletons/lalr1.cc,
* data/skeletons/yacc.c (yytnamerr): Receive b4_translatable, and use it.
This commit is contained in:
Akim Demaille
2018-12-28 08:47:04 +01:00
parent e9d404415a
commit 9096955fba
6 changed files with 79 additions and 18 deletions

View File

@@ -143,6 +143,7 @@
/* Define the tokens together with their human representation. */
%token GRAM_EOF 0 "end of file"
%token STRING "string"
TSTRING "translatable string"
%token PERCENT_TOKEN "%token"
%token PERCENT_NTERM "%nterm"
@@ -216,8 +217,8 @@
%type <unsigned char> CHAR
%printer { fputs (char_name ($$), yyo); } <unsigned char>
%type <char*> "{...}" "%?{...}" "%{...%}" EPILOGUE STRING
%printer { fputs ($$, yyo); } <char*>
%type <char*> "{...}" "%?{...}" "%{...%}" EPILOGUE STRING TSTRING
%printer { fputs ($$, yyo); } <char*>
%type <uniqstr>
BRACKETED_ID ID ID_COLON
@@ -532,7 +533,7 @@ token_decl.1:
// One symbol declaration for %token or %nterm.
token_decl:
id int.opt[num] string_as_id.opt[alias]
id int.opt[num] alias
{
$$ = $id;
symbol_class_set ($id, current_class, @id, true);
@@ -549,6 +550,19 @@ int.opt:
| INT
;
%type <symbol*> alias;
alias:
%empty { $$ = NULL; }
| string_as_id { $$ = $1; }
| TSTRING
{
$$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
symbol_class_set ($$, token_sym, @1, false);
$$->translatable = true;
}
;
/*-------------------------------------.
| token_decls_for_prec (%left, etc.). |
`-------------------------------------*/
@@ -782,12 +796,6 @@ string_as_id:
}
;
%type <symbol*> string_as_id.opt;
string_as_id.opt:
%empty { $$ = NULL; }
| string_as_id
;
epilogue.opt:
%empty
| "%%" EPILOGUE