[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 08/10] skeletons: make the eof token translatable if i18n is enab
From: |
Akim Demaille |
Subject: |
[PATCH 08/10] skeletons: make the eof token translatable if i18n is enabled |
Date: |
Sun, 12 Apr 2020 14:22:35 +0200 |
* 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.
---
examples/c/bistromathic/parse.y | 3 +--
src/output.c | 43 +++++++++++++++++++++------------
src/parse-gram.c | 30 +++++++++++------------
src/parse-gram.h | 2 +-
src/parse-gram.y | 1 -
5 files changed, 45 insertions(+), 34 deletions(-)
diff --git a/examples/c/bistromathic/parse.y b/examples/c/bistromathic/parse.y
index 6082f80a..4d4b1e45 100644
--- a/examples/c/bistromathic/parse.y
+++ b/examples/c/bistromathic/parse.y
@@ -81,7 +81,6 @@
RPAREN ")"
EQUAL "="
EXIT "exit"
- EOF 0 _("end of file")
<double>
NUM _("double precision number")
<symrec*>
@@ -236,7 +235,7 @@ yylex (const char **line, YYSTYPE *yylval, YYLTYPE *yylloc)
case '(': return TOK_LPAREN;
case ')': return TOK_RPAREN;
- case 0: return TOK_EOF;
+ case 0: return TOK_YYEOF;
default:
// Numbers.
diff --git a/src/output.c b/src/output.c
index 8689c6dd..5348aac4 100644
--- a/src/output.c
+++ b/src/output.c
@@ -186,6 +186,16 @@ xescape_trigraphs (const char *src)
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
symbol names (aka tags). */
@@ -195,9 +205,10 @@ prepare_symbol_names (char const *muscle_name)
const bool eof_is_user_defined
= !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");
- 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;
struct quoting_options *qo = clone_quoting_options (0);
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++)
{
/* Use "end of file" rather than "$end". But keep "$end" in the
- reports, it's shorter and more consistent. */
- const char *tag
- = !eof_is_user_defined && symbols[i]->content == endtoken->content
- ? "\"end of file\""
- : symbols[i]->tag;
+ 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;
+ }
+
char *cp
= tag[0] == '"' && !quote
? xescape_trigraphs (tag)
@@ -218,7 +234,7 @@ prepare_symbol_names (char const *muscle_name)
comma and the space. */
int width
= strlen (cp) + 2
- + (!quote && symbols[i]->translatable ? strlen ("N_()") : 0);
+ + (translatable ? strlen ("N_()") : 0);
if (j + width > 75)
{
@@ -228,13 +244,10 @@ prepare_symbol_names (char const *muscle_name)
if (i)
obstack_1grow (&format_obstack, ' ');
- if (!quote && symbols[i]->translatable)
- {
- has_translations = true;
- obstack_sgrow (&format_obstack, "]b4_symbol_translate([");
- }
+ if (translatable)
+ obstack_sgrow (&format_obstack, "]b4_symbol_translate([");
obstack_escape (&format_obstack, cp);
- if (!quote && symbols[i]->translatable)
+ if (translatable)
obstack_sgrow (&format_obstack, "])[");
free (cp);
obstack_1grow (&format_obstack, ',');
@@ -248,7 +261,7 @@ prepare_symbol_names (char const *muscle_name)
/* Announce whether translation support is needed. */
if (!quote)
- MUSCLE_INSERT_BOOL ("has_translations", has_translations);
+ MUSCLE_INSERT_BOOL ("has_translations", with_translations);
}
diff --git a/src/parse-gram.c b/src/parse-gram.c
index d3bb2ea4..993ad11a 100644
--- a/src/parse-gram.c
+++ b/src/parse-gram.c
@@ -104,7 +104,7 @@
enum yysymbol_kind_t
{
YYSYMBOL_YYEMPTY = -2,
- YYSYMBOL_YYEOF = 0, /* "end of file" */
+ YYSYMBOL_YYEOF = 0, /* $end */
YYSYMBOL_YYERROR = 1, /* error */
YYSYMBOL_YYUNDEF = 2, /* $undefined */
YYSYMBOL_STRING = 3, /* "string" */
@@ -614,19 +614,19 @@ union yyalloc
/* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
static const yytype_int16 yyrline[] =
{
- 0, 288, 288, 297, 298, 302, 303, 309, 313, 318,
- 319, 324, 325, 326, 327, 328, 333, 338, 339, 340,
- 341, 342, 343, 343, 344, 345, 346, 347, 348, 349,
- 350, 351, 355, 356, 365, 366, 370, 381, 385, 389,
- 397, 407, 408, 418, 419, 425, 438, 438, 443, 443,
- 448, 452, 462, 463, 464, 465, 469, 470, 475, 476,
- 480, 481, 485, 486, 487, 500, 509, 513, 517, 525,
- 526, 530, 543, 544, 549, 550, 551, 569, 573, 577,
- 585, 587, 592, 599, 609, 613, 617, 625, 630, 642,
- 643, 649, 650, 651, 658, 658, 666, 667, 668, 673,
- 676, 678, 680, 682, 684, 686, 688, 690, 692, 697,
- 698, 707, 731, 732, 733, 734, 746, 748, 772, 777,
- 778, 783, 791, 792
+ 0, 287, 287, 296, 297, 301, 302, 308, 312, 317,
+ 318, 323, 324, 325, 326, 327, 332, 337, 338, 339,
+ 340, 341, 342, 342, 343, 344, 345, 346, 347, 348,
+ 349, 350, 354, 355, 364, 365, 369, 380, 384, 388,
+ 396, 406, 407, 417, 418, 424, 437, 437, 442, 442,
+ 447, 451, 461, 462, 463, 464, 468, 469, 474, 475,
+ 479, 480, 484, 485, 486, 499, 508, 512, 516, 524,
+ 525, 529, 542, 543, 548, 549, 550, 568, 572, 576,
+ 584, 586, 591, 598, 608, 612, 616, 624, 629, 641,
+ 642, 648, 649, 650, 657, 657, 665, 666, 667, 672,
+ 675, 677, 679, 681, 683, 685, 687, 689, 691, 696,
+ 697, 706, 730, 731, 732, 733, 745, 747, 771, 776,
+ 777, 782, 790, 791
};
#endif
@@ -670,7 +670,7 @@ yysymbol_name (yysymbol_kind_t yysymbol)
internationalizable. */
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,
diff --git a/src/parse-gram.h b/src/parse-gram.h
index ab0f6d80..ac501c5f 100644
--- a/src/parse-gram.h
+++ b/src/parse-gram.h
@@ -77,7 +77,7 @@ extern int gram_debug;
# define GRAM_TOKENTYPE
enum gram_tokentype
{
- GRAM_EOF = 0, /* "end of file" */
+ GRAM_EOF = 0, /* $end */
GRAM_ERRCODE = 1, /* error */
GRAM_UNDEF = 2, /* $undefined */
STRING = 3, /* "string" */
diff --git a/src/parse-gram.y b/src/parse-gram.y
index 148d48b8..9980355a 100644
--- a/src/parse-gram.y
+++ b/src/parse-gram.y
@@ -141,7 +141,6 @@
}
%token
- GRAM_EOF 0 _("end of file")
STRING _("string")
TSTRING _("translatable string")
--
2.26.0
- [PATCH 00/10] Clean up the handling of the tokens, Akim Demaille, 2020/04/12
- [PATCH 01/10] tokens: style: minor fixes, Akim Demaille, 2020/04/12
- [PATCH 02/10] tokens: properly define the "error" token kind, Akim Demaille, 2020/04/12
- [PATCH 03/10] tokens: define the "$undefined" token kind, Akim Demaille, 2020/04/12
- [PATCH 05/10] c++: remove the yy prefix from some functions, Akim Demaille, 2020/04/12
- [PATCH 08/10] skeletons: make the eof token translatable if i18n is enabled,
Akim Demaille <=
- [PATCH 04/10] tokens: properly define the YYEOF token kind, Akim Demaille, 2020/04/12
- [PATCH 06/10] diagnostics: replace "user token number" by "token code", Akim Demaille, 2020/04/12
- [PATCH 09/10] skeletons: use "invalid token" instead of "$undefined", Akim Demaille, 2020/04/12
- [PATCH 10/10] skeletons: clarify the tag of special tokens, Akim Demaille, 2020/04/12
- [PATCH 07/10] skeletons: use "end of file" instead of "$end", Akim Demaille, 2020/04/12