skeletons: make the eof token translatable if i18n is enabled

* src/output.c (has_translations): New.
(prepare_symbol_names): Translate endtoken if the user already
translated tokens.

* examples/c/bistromathic/parse.y, src/parse-gram.y: Simplify.
This commit is contained in:
Akim Demaille
2020-04-12 11:07:03 +02:00
parent 72c9fa4510
commit 05be0fef95
5 changed files with 45 additions and 34 deletions

View File

@@ -81,7 +81,6 @@
RPAREN ")" RPAREN ")"
EQUAL "=" EQUAL "="
EXIT "exit" EXIT "exit"
EOF 0 _("end of file")
<double> <double>
NUM _("double precision number") NUM _("double precision number")
<symrec*> <symrec*>
@@ -236,7 +235,7 @@ yylex (const char **line, YYSTYPE *yylval, YYLTYPE *yylloc)
case '(': return TOK_LPAREN; case '(': return TOK_LPAREN;
case ')': return TOK_RPAREN; case ')': return TOK_RPAREN;
case 0: return TOK_EOF; case 0: return TOK_YYEOF;
default: default:
// Numbers. // Numbers.

View File

@@ -186,6 +186,16 @@ xescape_trigraphs (const char *src)
return buf; return buf;
} }
/* Whether some symbol requires internationalization. */
static bool
has_translations (void)
{
for (int i = 0; i < nsyms; i++)
if (symbols[i]->translatable)
return true;
return false;
}
/* 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). */
@@ -195,9 +205,10 @@ prepare_symbol_names (char const *muscle_name)
const bool eof_is_user_defined const bool eof_is_user_defined
= !endtoken->alias || STRNEQ (endtoken->alias->tag, "$end"); = !endtoken->alias || STRNEQ (endtoken->alias->tag, "$end");
/* We assume that the table will be output starting at column 2. */
const bool quote = STREQ (muscle_name, "tname"); const bool quote = STREQ (muscle_name, "tname");
bool has_translations = false; const bool with_translations = !quote && has_translations ();
/* We assume that the table will be output starting at column 2. */
int j = 2; int j = 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);
@@ -205,11 +216,16 @@ prepare_symbol_names (char const *muscle_name)
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 /* Use "end of file" rather than "$end". But keep "$end" in the
reports, it's shorter and more consistent. */ reports, it's shorter and more consistent. Support i18n if
const char *tag the user already uses it. */
= !eof_is_user_defined && symbols[i]->content == endtoken->content const char *tag = symbols[i]->tag;
? "\"end of file\"" bool translatable = with_translations && symbols[i]->translatable;
: symbols[i]->tag; if (!eof_is_user_defined && symbols[i]->content == endtoken->content)
{
tag = "\"end of file\"";
translatable = with_translations;
}
char *cp char *cp
= tag[0] == '"' && !quote = tag[0] == '"' && !quote
? xescape_trigraphs (tag) ? xescape_trigraphs (tag)
@@ -218,7 +234,7 @@ prepare_symbol_names (char const *muscle_name)
comma and the space. */ comma and the space. */
int width int width
= strlen (cp) + 2 = strlen (cp) + 2
+ (!quote && symbols[i]->translatable ? strlen ("N_()") : 0); + (translatable ? strlen ("N_()") : 0);
if (j + width > 75) if (j + width > 75)
{ {
@@ -228,13 +244,10 @@ prepare_symbol_names (char const *muscle_name)
if (i) if (i)
obstack_1grow (&format_obstack, ' '); obstack_1grow (&format_obstack, ' ');
if (!quote && symbols[i]->translatable) if (translatable)
{ obstack_sgrow (&format_obstack, "]b4_symbol_translate([");
has_translations = true;
obstack_sgrow (&format_obstack, "]b4_symbol_translate([");
}
obstack_escape (&format_obstack, cp); obstack_escape (&format_obstack, cp);
if (!quote && symbols[i]->translatable) if (translatable)
obstack_sgrow (&format_obstack, "])["); obstack_sgrow (&format_obstack, "])[");
free (cp); free (cp);
obstack_1grow (&format_obstack, ','); obstack_1grow (&format_obstack, ',');
@@ -248,7 +261,7 @@ prepare_symbol_names (char const *muscle_name)
/* Announce whether translation support is needed. */ /* Announce whether translation support is needed. */
if (!quote) if (!quote)
MUSCLE_INSERT_BOOL ("has_translations", has_translations); MUSCLE_INSERT_BOOL ("has_translations", with_translations);
} }

View File

@@ -104,7 +104,7 @@
enum yysymbol_kind_t enum yysymbol_kind_t
{ {
YYSYMBOL_YYEMPTY = -2, YYSYMBOL_YYEMPTY = -2,
YYSYMBOL_YYEOF = 0, /* "end of file" */ YYSYMBOL_YYEOF = 0, /* $end */
YYSYMBOL_YYERROR = 1, /* error */ YYSYMBOL_YYERROR = 1, /* error */
YYSYMBOL_YYUNDEF = 2, /* $undefined */ YYSYMBOL_YYUNDEF = 2, /* $undefined */
YYSYMBOL_STRING = 3, /* "string" */ YYSYMBOL_STRING = 3, /* "string" */
@@ -614,19 +614,19 @@ union yyalloc
/* YYRLINE[YYN] -- Source line where rule number YYN was defined. */ /* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
static const yytype_int16 yyrline[] = static const yytype_int16 yyrline[] =
{ {
0, 288, 288, 297, 298, 302, 303, 309, 313, 318, 0, 287, 287, 296, 297, 301, 302, 308, 312, 317,
319, 324, 325, 326, 327, 328, 333, 338, 339, 340, 318, 323, 324, 325, 326, 327, 332, 337, 338, 339,
341, 342, 343, 343, 344, 345, 346, 347, 348, 349, 340, 341, 342, 342, 343, 344, 345, 346, 347, 348,
350, 351, 355, 356, 365, 366, 370, 381, 385, 389, 349, 350, 354, 355, 364, 365, 369, 380, 384, 388,
397, 407, 408, 418, 419, 425, 438, 438, 443, 443, 396, 406, 407, 417, 418, 424, 437, 437, 442, 442,
448, 452, 462, 463, 464, 465, 469, 470, 475, 476, 447, 451, 461, 462, 463, 464, 468, 469, 474, 475,
480, 481, 485, 486, 487, 500, 509, 513, 517, 525, 479, 480, 484, 485, 486, 499, 508, 512, 516, 524,
526, 530, 543, 544, 549, 550, 551, 569, 573, 577, 525, 529, 542, 543, 548, 549, 550, 568, 572, 576,
585, 587, 592, 599, 609, 613, 617, 625, 630, 642, 584, 586, 591, 598, 608, 612, 616, 624, 629, 641,
643, 649, 650, 651, 658, 658, 666, 667, 668, 673, 642, 648, 649, 650, 657, 657, 665, 666, 667, 672,
676, 678, 680, 682, 684, 686, 688, 690, 692, 697, 675, 677, 679, 681, 683, 685, 687, 689, 691, 696,
698, 707, 731, 732, 733, 734, 746, 748, 772, 777, 697, 706, 730, 731, 732, 733, 745, 747, 771, 776,
778, 783, 791, 792 777, 782, 790, 791
}; };
#endif #endif
@@ -670,7 +670,7 @@ yysymbol_name (yysymbol_kind_t yysymbol)
internationalizable. */ internationalizable. */
static yytype_int8 yytranslatable[] = static yytype_int8 yytranslatable[] =
{ {
1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

View File

@@ -77,7 +77,7 @@ extern int gram_debug;
# define GRAM_TOKENTYPE # define GRAM_TOKENTYPE
enum gram_tokentype enum gram_tokentype
{ {
GRAM_EOF = 0, /* "end of file" */ GRAM_EOF = 0, /* $end */
GRAM_ERRCODE = 1, /* error */ GRAM_ERRCODE = 1, /* error */
GRAM_UNDEF = 2, /* $undefined */ GRAM_UNDEF = 2, /* $undefined */
STRING = 3, /* "string" */ STRING = 3, /* "string" */

View File

@@ -141,7 +141,6 @@
} }
%token %token
GRAM_EOF 0 _("end of file")
STRING _("string") STRING _("string")
TSTRING _("translatable string") TSTRING _("translatable string")