glr2.cc: prefer unnamed namespace to 'static'
[bison.git] / src / scan-gram.l
blobf55429ede73361bea57f8373790b254698c02b8c
1 /* Bison Grammar Scanner                             -*- C -*-
3    Copyright (C) 2002-2015, 2018-2021 Free Software Foundation, Inc.
5    This file is part of Bison, the GNU Compiler Compiler.
7    This program is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation, either version 3 of the License, or
10    (at your option) any later version.
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
20 %option debug nodefault noinput noyywrap never-interactive
21 %option prefix="gram_" outfile="lex.yy.c"
24 #include <c-ctype.h>
25 #include <errno.h>
26 #include <mbswidth.h>
27 #include <quote.h>
28 #include <quotearg.h>
30 #include "src/complain.h"
31 #include "src/files.h"
32 #include "src/getargs.h"
33 #include "src/gram.h"
34 #include "src/reader.h"
35 #include "src/scan-gram.h"
36 #include "src/uniqstr.h"
38 #define FLEX_PREFIX(Id) gram_ ## Id
39 #include "src/flex-scanner.h"
41 /* Work around a bug in flex 2.5.31.  See Debian bug 333231
42    <https://bugs.debian.org/333231>.  */
43 #undef gram_wrap
44 #define gram_wrap() 1
46 #define YY_DECL GRAM_LEX_DECL
48 /* Location of scanner cursor.  */
49 static boundary scanner_cursor;
51 #define YY_USER_ACTION  location_compute (loc, &scanner_cursor, yytext, yyleng);
53 /* Report that yytext is an extension, and evaluate to its token kind.  */
54 #define BISON_DIRECTIVE(Directive)                              \
55   (bison_directive (loc, yytext), PERCENT_ ## Directive)
57 #define RETURN_PERCENT_PARAM(Value)             \
58   RETURN_VALUE(PERCENT_PARAM, param_ ## Value)
60 #define RETURN_PERCENT_FLAG(Value)                      \
61   RETURN_VALUE(PERCENT_FLAG, uniqstr_new (Value))
63 #define RETURN_VALUE(Token, Value)              \
64   do {                                          \
65     val->Token = Value;                         \
66     return Token;                               \
67   } while (0)
69 #define ROLLBACK_CURRENT_TOKEN                                  \
70   do {                                                          \
71     scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);     \
72     scanner_cursor.byte -= yyleng;                              \
73     yyless (0);                                                 \
74   } while (0)
76 #define DEPRECATED_DIRECTIVE(Msg)                               \
77   do {                                                          \
78     deprecated_directive (loc, yytext, Msg);                    \
79     scanner_cursor.column -= mbsnwidth (Msg, strlen (Msg), 0);  \
80     scanner_cursor.byte -= strlen (Msg);                        \
81     for (size_t i = strlen (Msg); i != 0; --i)                  \
82       unput (Msg[i - 1]);                                       \
83   } while (0)
86 #define STRING_GROW_ESCAPE(Char)                                \
87   do {                                                          \
88     verify (UCHAR_MAX < ULONG_MAX);                             \
89     long c = Char;                                              \
90     bool valid = 0 < c && c <= UCHAR_MAX;                       \
91     if (!valid)                                                 \
92       complain (loc, complaint,                                 \
93                 _("invalid number after \\-escape: %s"),        \
94                 yytext + 1);                                    \
95     if (YY_START == SC_ESCAPED_CHARACTER)                       \
96       STRING_1GROW (valid ? c : '?');                           \
97     else                                                        \
98       STRING_GROW ();                                           \
99   } while (0)
102 /* The current file name.  Might change with #line.  */
103 static uniqstr current_file = NULL;
105 /* A string representing the most recently saved token.  */
106 static char *last_string = NULL;
108 /* Bracketed identifier. */
109 static uniqstr bracketed_id_str = NULL;
110 static location bracketed_id_loc;
111 static boundary bracketed_id_start;
112 static int bracketed_id_context_state = 0;
114 void
115 gram_scanner_last_string_free (void)
117   STRING_FREE ();
120 static void handle_syncline (char *, location);
121 static int scan_integer (char const *p, int base, location loc);
122 static int convert_ucn_to_byte (char const *hex_text);
123 static void unexpected_eof (boundary, char const *);
124 static void unexpected_newline (boundary, char const *);
127  /* A C-like comment in directives/rules. */
128 %x SC_YACC_COMMENT
129  /* Characters and strings in directives/rules. */
130 %x SC_ESCAPED_CHARACTER SC_ESCAPED_STRING SC_ESCAPED_TSTRING
131  /* A identifier was just read in directives/rules.  Special state
132     to capture the sequence 'identifier :'. */
133 %x SC_AFTER_IDENTIFIER
135  /* POSIX says that a tag must be both an id and a C union member, but
136     historically almost any character is allowed in a tag.  We
137     disallow NUL, as this simplifies our implementation.  We match
138     angle brackets in nested pairs: several languages use them for
139     generics/template types.  */
140 %x SC_TAG
142  /* Four types of user code:
143     - prologue (code between '%{' '%}' in the first section, before %%);
144     - actions, printers, union, etc, (between braced in the middle section);
145     - epilogue (everything after the second %%).
146     - predicate (code between '%?{' and '{' in middle section); */
147 %x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE SC_PREDICATE
148  /* C and C++ comments in code. */
149 %x SC_COMMENT SC_LINE_COMMENT
150  /* Strings and characters in code. */
151 %x SC_STRING SC_CHARACTER
152  /* Bracketed identifiers support. */
153 %x SC_BRACKETED_ID SC_RETURN_BRACKETED_ID
155 letter    [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
156 id        {letter}({letter}|[-0-9])*
157 int       [0-9]+
158 xint      0[xX][0-9abcdefABCDEF]+
160 eol       \n|\r\n
162  /* UTF-8 Encoded Unicode Code Point, from Flex's documentation. */
163 mbchar    [\x09\x0A\x0D\x20-\x7E]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF]([\x80-\xBF]{2})|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF]([\x80-\xBF]{2})|[\xF1-\xF3]([\x80-\xBF]{3})|\xF4[\x80-\x8F]([\x80-\xBF]{2})
165 /* Zero or more instances of backslash-newline.  Following GCC, allow
166    white space between the backslash and the newline.  */
167 splice   (\\[ \f\t\v]*{eol})*
169 /* An equal sign, with optional leading whitespaces. This is used in some
170    deprecated constructs. */
171 sp       [[:space:]]*
172 eqopt    ({sp}=)?
176   /* Nesting level.  Either for nested braces, or nested angle brackets
177      (but not mixed).  */
178   int nesting PACIFY_CC (= 0);
180   /* Parent context state, when applicable.  */
181   int context_state PACIFY_CC (= 0);
183   /* Location of most recent identifier, when applicable.  */
184   location id_loc PACIFY_CC (= empty_loc);
186   /* Where containing code started, when applicable.  Its initial
187      value is relevant only when yylex is invoked in the SC_EPILOGUE
188      start condition.  */
189   boundary code_start = scanner_cursor;
191   /* Where containing comment or string or character literal started,
192      when applicable.  */
193   boundary token_start PACIFY_CC (= scanner_cursor);
195   /* We cannot trust YY_USER_INIT, whose semantics changes over time
196      (it moved in Flex 2.5.38).  */
197   static bool first = true;
198   if (first)
199     {
200       scanner_cursor = loc->start;
201       first = false;
202     }
206   /*-----------------------.
207   | Scanning white space.  |
208   `-----------------------*/
210 <INITIAL,SC_AFTER_IDENTIFIER,SC_BRACKETED_ID,SC_RETURN_BRACKETED_ID>
212   /* Comments and white space.  */
213   "," {
214      complain (loc, Wother, _("stray ',' treated as white space"));
215   }
216   [ \f\t\v\r]|{eol}  |
217   "//".*       continue;
218   "/*" {
219     token_start = loc->start;
220     context_state = YY_START;
221     BEGIN SC_YACC_COMMENT;
222   }
224   ^"#line "{int}(" \"".*"\"")?{eol} {
225     handle_syncline (yytext + sizeof "#line " - 1, *loc);
226   }
230   /*----------------------------.
231   | Scanning Bison directives.  |
232   `----------------------------*/
234   /* For directives that are also command line options, the regex must be
235         "%..."
236      after "[-_]"s are removed, and the directive must match the --long
237      option name, with a single string argument.  Otherwise, add exceptions
238      to ../build-aux/cross-options.pl.  */
240 <INITIAL>
242   "%binary"                         return BISON_DIRECTIVE (NONASSOC);
243   "%code"                           return BISON_DIRECTIVE (CODE);
244   "%debug"                          RETURN_PERCENT_FLAG ("parse.trace");
245   "%default-prec"                   return BISON_DIRECTIVE (DEFAULT_PREC);
246   "%define"                         return BISON_DIRECTIVE (DEFINE);
247   "%defines"                        return BISON_DIRECTIVE (HEADER); // Deprecated in 3.8.
248   "%destructor"                     return BISON_DIRECTIVE (DESTRUCTOR);
249   "%dprec"                          return BISON_DIRECTIVE (DPREC);
250   "%empty"                          return BISON_DIRECTIVE (EMPTY);
251   "%expect"                         return BISON_DIRECTIVE (EXPECT);
252   "%expect-rr"                      return BISON_DIRECTIVE (EXPECT_RR);
253   "%file-prefix"                    RETURN_VALUE (PERCENT_FILE_PREFIX, uniqstr_new (yytext));
254   "%glr-parser"                     return BISON_DIRECTIVE (GLR_PARSER);
255   "%header"                         return BISON_DIRECTIVE (HEADER);
256   "%initial-action"                 return BISON_DIRECTIVE (INITIAL_ACTION);
257   "%language"                       return BISON_DIRECTIVE (LANGUAGE);
258   "%left"                           return PERCENT_LEFT;
259   "%lex-param"                      RETURN_PERCENT_PARAM (lex);
260   "%locations"                      RETURN_PERCENT_FLAG ("locations");
261   "%merge"                          return BISON_DIRECTIVE (MERGE);
262   "%no-default-prec"                return BISON_DIRECTIVE (NO_DEFAULT_PREC);
263   "%no-lines"                       return BISON_DIRECTIVE (NO_LINES);
264   "%nonassoc"                       return PERCENT_NONASSOC;
265   "%nondeterministic-parser"        return BISON_DIRECTIVE (NONDETERMINISTIC_PARSER);
266   "%nterm"                          return BISON_DIRECTIVE (NTERM);
267   "%output"                         return BISON_DIRECTIVE (OUTPUT);
268   "%param"                          RETURN_PERCENT_PARAM (both);
269   "%parse-param"                    RETURN_PERCENT_PARAM (parse);
270   "%prec"                           return PERCENT_PREC;
271   "%precedence"                     return BISON_DIRECTIVE (PRECEDENCE);
272   "%printer"                        return BISON_DIRECTIVE (PRINTER);
273   "%require"                        return BISON_DIRECTIVE (REQUIRE);
274   "%right"                          return PERCENT_RIGHT;
275   "%skeleton"                       return BISON_DIRECTIVE (SKELETON);
276   "%start"                          return PERCENT_START;
277   "%term"                           return BISON_DIRECTIVE (TOKEN);
278   "%token"                          return PERCENT_TOKEN;
279   "%token-table"                    return BISON_DIRECTIVE (TOKEN_TABLE);
280   "%type"                           return PERCENT_TYPE;
281   "%union"                          return PERCENT_UNION;
282   "%verbose"                        return BISON_DIRECTIVE (VERBOSE);
283   "%yacc"                           return PERCENT_YACC;
285   /* Deprecated since Bison 2.3b (2008-05-27), but the warning is
286      issued only since Bison 3.4. */
287   "%pure"[-_]"parser"               RETURN_VALUE (PERCENT_PURE_PARSER, uniqstr_new (yytext));
289   /* Deprecated since Bison 3.0 (2013-07-25), but the warning is
290      issued only since Bison 3.3. */
291   "%error-verbose"                  RETURN_VALUE (PERCENT_ERROR_VERBOSE, uniqstr_new (yytext));
293   /* Deprecated since Bison 2.6 (2012-07-19), but the warning is
294      issued only since Bison 3.3. */
295   "%name"[-_]"prefix"{eqopt}{sp}    RETURN_VALUE (PERCENT_NAME_PREFIX, uniqstr_new (yytext));
297   /* Deprecated since Bison 2.7.90, 2012. */
298   "%default"[-_]"prec"              DEPRECATED_DIRECTIVE ("%default-prec");
299   "%error"[-_]"verbose"             RETURN_VALUE (PERCENT_ERROR_VERBOSE, uniqstr_new (yytext));
300   "%expect"[-_]"rr"                 DEPRECATED_DIRECTIVE ("%expect-rr");
301   "%file-prefix"{eqopt}             RETURN_VALUE (PERCENT_FILE_PREFIX, uniqstr_new (yytext));
302   "%fixed"[-_]"output"[-_]"files"   DEPRECATED_DIRECTIVE ("%output \"y.tab.c\"");
303   "%no"[-_]"default"[-_]"prec"      DEPRECATED_DIRECTIVE ("%no-default-prec");
304   "%no"[-_]"lines"                  DEPRECATED_DIRECTIVE ("%no-lines");
305   "%output"{eqopt}                  DEPRECATED_DIRECTIVE ("%output");
306   "%token"[-_]"table"               DEPRECATED_DIRECTIVE ("%token-table");
308   "%"{id} {
309     complain (loc, complaint, _("invalid directive: %s"), quote (yytext));
310     return GRAM_error;
311   }
313   ":"                     return COLON;
314   "="                     return EQUAL;
315   "|"                     return PIPE;
316   ";"                     return SEMICOLON;
318   {id} {
319     val->ID = uniqstr_new (yytext);
320     id_loc = *loc;
321     bracketed_id_str = NULL;
322     BEGIN SC_AFTER_IDENTIFIER;
323   }
325   {int}      RETURN_VALUE (INT_LITERAL, scan_integer (yytext, 10, *loc));
326   {xint}     RETURN_VALUE (INT_LITERAL, scan_integer (yytext, 16, *loc));
328   /* Identifiers may not start with a digit.  Yet, don't silently
329      accept "1FOO" as "1 FOO".  */
330   {int}{id} {
331     complain (loc, complaint, _("invalid identifier: %s"), quote (yytext));
332     return GRAM_error;
333   }
335   /* Characters.  */
336   "'"         token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
338   /* Strings. */
339   "\""        token_start = loc->start; STRING_1GROW ('"'); BEGIN SC_ESCAPED_STRING;
340   "_(\""      token_start = loc->start; STRING_1GROW ('"'); BEGIN SC_ESCAPED_TSTRING;
342   /* Prologue. */
343   "%{"        code_start = loc->start; BEGIN SC_PROLOGUE;
345   /* Code in between braces.  */
346   "{" {
347     STRING_GROW ();
348     nesting = 0;
349     code_start = loc->start;
350     BEGIN SC_BRACED_CODE;
351   }
353   /* Semantic predicate. */
354   "%?"([ \f\t\v]|{eol})*"{" {
355     nesting = 0;
356     code_start = loc->start;
357     BEGIN SC_PREDICATE;
358   }
360   /* A type. */
361   "<*>"       return TAG_ANY;
362   "<>"        return TAG_NONE;
363   "<"         {
364     nesting = 0;
365     token_start = loc->start;
366     BEGIN SC_TAG;
367   }
369   "%%" {
370     static int percent_percent_count;
371     if (++percent_percent_count == 2)
372       BEGIN SC_EPILOGUE;
373     return PERCENT_PERCENT;
374   }
376   "[" {
377     bracketed_id_str = NULL;
378     bracketed_id_start = loc->start;
379     bracketed_id_context_state = YY_START;
380     BEGIN SC_BRACKETED_ID;
381   }
383   [^\[%A-Za-z0-9_<>{}""''*;|=/, \f\r\n\t\v]+|. {
384     complain (loc, complaint, "%s: %s",
385               ngettext ("invalid character", "invalid characters", yyleng),
386               quote_mem (yytext, yyleng));
387     return GRAM_error;
388   }
390   <<EOF>> {
391     loc->start = loc->end = scanner_cursor;
392     yyterminate ();
393   }
397   /*--------------------------------------------------------------.
398   | Supporting \0 complexifies our implementation for no expected |
399   | added value.                                                  |
400   `--------------------------------------------------------------*/
402 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_TSTRING,SC_TAG>
404   \0         {
405     complain (loc, complaint, _("invalid null character"));
406     STRING_FINISH ();
407     STRING_FREE ();
408     return GRAM_error;
409   }
413   /*-----------------------------------------------------------------.
414   | Scanning after an identifier, checking whether a colon is next.  |
415   `-----------------------------------------------------------------*/
417 <SC_AFTER_IDENTIFIER>
419   "[" {
420     if (bracketed_id_str)
421       {
422         ROLLBACK_CURRENT_TOKEN;
423         BEGIN SC_RETURN_BRACKETED_ID;
424         *loc = id_loc;
425         return ID;
426       }
427     else
428       {
429         bracketed_id_start = loc->start;
430         bracketed_id_context_state = YY_START;
431         BEGIN SC_BRACKETED_ID;
432       }
433   }
434   ":" {
435     ROLLBACK_CURRENT_TOKEN;
436     BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
437     *loc = id_loc;
438     return ID_COLON;
439   }
440   . {
441     ROLLBACK_CURRENT_TOKEN;
442     BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
443     *loc = id_loc;
444     return ID;
445   }
446   <<EOF>> {
447     BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
448     *loc = id_loc;
449     return ID;
450   }
453   /*--------------------------------.
454   | Scanning bracketed identifiers. |
455   `--------------------------------*/
457 <SC_BRACKETED_ID>
459   {id} {
460     if (bracketed_id_str)
461       {
462         complain (loc, complaint,
463                   _("unexpected identifier in bracketed name: %s"),
464                   quote (yytext));
465         return GRAM_error;
466       }
467     else
468       {
469         bracketed_id_str = uniqstr_new (yytext);
470         bracketed_id_loc = *loc;
471       }
472   }
473   "]" {
474     BEGIN bracketed_id_context_state;
475     if (bracketed_id_str)
476       {
477         if (INITIAL == bracketed_id_context_state)
478           {
479             val->BRACKETED_ID = bracketed_id_str;
480             bracketed_id_str = 0;
481             *loc = bracketed_id_loc;
482             return BRACKETED_ID;
483           }
484       }
485     else
486       {
487         complain (loc, complaint, _("an identifier expected"));
488         return GRAM_error;
489       }
490   }
492   [^\].A-Za-z0-9_/ \f\r\n\t\v]+|. {
493     complain (loc, complaint, "%s: %s",
494               ngettext ("invalid character in bracketed name",
495                         "invalid characters in bracketed name", yyleng),
496               quote_mem (yytext, yyleng));
497     return GRAM_error;
498   }
500   <<EOF>> {
501     BEGIN bracketed_id_context_state;
502     unexpected_eof (bracketed_id_start, "]");
503   }
506 <SC_RETURN_BRACKETED_ID>
508   . {
509     ROLLBACK_CURRENT_TOKEN;
510     val->BRACKETED_ID = bracketed_id_str;
511     bracketed_id_str = 0;
512     *loc = bracketed_id_loc;
513     BEGIN INITIAL;
514     return BRACKETED_ID;
515   }
519   /*---------------------------------------------------------------.
520   | Scanning a Yacc comment.  The initial '/ *' is already eaten.  |
521   `---------------------------------------------------------------*/
523 <SC_YACC_COMMENT>
525   "*/"     BEGIN context_state;
526   .|{eol}  continue;
527   <<EOF>>  unexpected_eof (token_start, "*/"); BEGIN context_state;
531   /*------------------------------------------------------------.
532   | Scanning a C comment.  The initial '/ *' is already eaten.  |
533   `------------------------------------------------------------*/
535 <SC_COMMENT>
537   "*"{splice}"/"  STRING_GROW (); BEGIN context_state;
538   <<EOF>>         unexpected_eof (token_start, "*/"); BEGIN context_state;
542   /*--------------------------------------------------------------.
543   | Scanning a line comment.  The initial '//' is already eaten.  |
544   `--------------------------------------------------------------*/
546 <SC_LINE_COMMENT>
548   {eol}          STRING_GROW (); BEGIN context_state;
549   {splice}       STRING_GROW ();
550   <<EOF>>        BEGIN context_state;
554   /*------------------------------------------------.
555   | Scanning a Bison string, including its escapes. |
556   | The initial quote is already eaten.             |
557   `------------------------------------------------*/
559 <SC_ESCAPED_STRING>
561   "\"" {
562     STRING_1GROW ('"');
563     STRING_FINISH ();
564     BEGIN INITIAL;
565     loc->start = token_start;
566     complain (loc, Wyacc,
567               _("POSIX Yacc does not support string literals"));
568     RETURN_VALUE (STRING, last_string);
569   }
570   <<EOF>>   unexpected_eof (token_start, "\"");
571   "\n"      unexpected_newline (token_start, "\"");
574 <SC_ESCAPED_TSTRING>
576   "\")" {
577     STRING_1GROW ('"');
578     STRING_FINISH ();
579     BEGIN INITIAL;
580     loc->start = token_start;
581     complain (loc, Wyacc,
582               _("POSIX Yacc does not support string literals"));
583     RETURN_VALUE (TSTRING, last_string);
584   }
585   <<EOF>>   unexpected_eof (token_start, "\")");
586   "\n"      unexpected_newline (token_start, "\")");
591   /*----------------------------------------------------------.
592   | Scanning a Bison character literal, decoding its escapes. |
593   | The initial quote is already eaten.                       |
594   `----------------------------------------------------------*/
596 <SC_ESCAPED_CHARACTER>
598   "'" {
599     STRING_FINISH ();
600     BEGIN INITIAL;
601     loc->start = token_start;
603     if (last_string[0] == '\0')
604       {
605         complain (loc, complaint, _("empty character literal"));
606         STRING_FREE ();
607         return GRAM_error;
608       }
609     else if (last_string[1] != '\0')
610       {
611         complain (loc, complaint, _("extra characters in character literal"));
612         STRING_FREE ();
613         return GRAM_error;
614       }
615     else
616       {
617         val->CHAR_LITERAL = last_string[0];
618         STRING_FREE ();
619         return CHAR_LITERAL;
620       }
621   }
622   {eol}     unexpected_newline (token_start, "'");
623   <<EOF>>   unexpected_eof (token_start, "'");
628   /*--------------------------------------------------------------.
629   | Scanning a tag.  The initial angle bracket is already eaten.  |
630   `--------------------------------------------------------------*/
632 <SC_TAG>
634   ">" {
635     --nesting;
636     if (nesting < 0)
637       {
638         STRING_FINISH ();
639         loc->start = token_start;
640         val->TAG = uniqstr_new (last_string);
641         STRING_FREE ();
642         BEGIN INITIAL;
643         return TAG;
644       }
645     STRING_GROW ();
646   }
648   ([^<>]|->)+ STRING_GROW ();
649   "<"+   STRING_GROW (); nesting += yyleng;
651   <<EOF>>   unexpected_eof (token_start, ">");
654   /*----------------------------.
655   | Decode escaped characters.  |
656   `----------------------------*/
658 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_TSTRING>
660   \\[0-7]{1,3} {
661     STRING_GROW_ESCAPE (strtol (yytext + 1, NULL, 8));
662   }
664   \\x[0-9abcdefABCDEF]+ {
665     STRING_GROW_ESCAPE (strtol (yytext + 2, NULL, 16));
666   }
668   \\a   STRING_GROW_ESCAPE ('\a');
669   \\b   STRING_GROW_ESCAPE ('\b');
670   \\f   STRING_GROW_ESCAPE ('\f');
671   \\n   STRING_GROW_ESCAPE ('\n');
672   \\r   STRING_GROW_ESCAPE ('\r');
673   \\t   STRING_GROW_ESCAPE ('\t');
674   \\v   STRING_GROW_ESCAPE ('\v');
676   /* \\[\"\'?\\] would be shorter, but it confuses xgettext.  */
677   \\("\""|"'"|"?"|"\\")  STRING_GROW_ESCAPE (yytext[1]);
679   \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
680     STRING_GROW_ESCAPE (convert_ucn_to_byte (yytext));
681   }
683   \\(.|{eol})      {
684     char const *p = yytext + 1;
685     /* Quote only if escaping won't make the character visible.  */
686     if (c_isspace ((unsigned char) *p) && c_isprint ((unsigned char) *p))
687       p = quote (p);
688     else
689       p = quotearg_style_mem (escape_quoting_style, p, 1);
690     complain (loc, complaint, _("invalid character after \\-escape: %s"),
691               p);
692     STRING_1GROW ('?');
693   }
695   "\\"             {
696     // None of the other rules matched: the last character of this
697     // file is "\".  But Flex does not support "\\<<EOF>>".
698     unexpected_eof (token_start,
699                     YY_START == SC_ESCAPED_CHARACTER ? "?'"
700                     : YY_START == SC_ESCAPED_STRING ? "?\""
701                     : "?\")");
702   }
705   /*--------------------------------------------.
706   | Scanning user-code characters and strings.  |
707   `--------------------------------------------*/
709 <SC_CHARACTER,SC_STRING>
711   {splice}|\\{splice}[^\n\[\]]  STRING_GROW ();
714 <SC_CHARACTER>
716   "'"           STRING_GROW (); BEGIN context_state;
717   {eol}         unexpected_newline (token_start, "'");
718   <<EOF>>       unexpected_eof (token_start, "'");
721 <SC_STRING>
723   "\""          STRING_GROW (); BEGIN context_state;
724   {eol}         unexpected_newline (token_start, "\"");
725   <<EOF>>       unexpected_eof (token_start, "\"");
729   /*---------------------------------------------------.
730   | Strings, comments etc. can be found in user code.  |
731   `---------------------------------------------------*/
733 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_PREDICATE>
735   "'" {
736     STRING_GROW ();
737     context_state = YY_START;
738     token_start = loc->start;
739     BEGIN SC_CHARACTER;
740   }
741   "\"" {
742     STRING_GROW ();
743     context_state = YY_START;
744     token_start = loc->start;
745     BEGIN SC_STRING;
746   }
747   "/"{splice}"*" {
748     STRING_GROW ();
749     context_state = YY_START;
750     token_start = loc->start;
751     BEGIN SC_COMMENT;
752   }
753   "/"{splice}"/" {
754     STRING_GROW ();
755     context_state = YY_START;
756     BEGIN SC_LINE_COMMENT;
757   }
762   /*-----------------------------------------------------------.
763   | Scanning some code in braces (actions, predicates). The    |
764   | initial "{" is already eaten.                              |
765   `-----------------------------------------------------------*/
767 <SC_BRACED_CODE,SC_PREDICATE>
769   "{"|"<"{splice}"%"  STRING_GROW (); nesting++;
770   "%"{splice}">"      STRING_GROW (); nesting--;
772   /* Tokenize '<<%' correctly (as '<<' '%') rather than incorrectly
773      (as '<' '<%').  */
774   "<"{splice}"<"  STRING_GROW ();
776   <<EOF>>   unexpected_eof (code_start, "}");
779 <SC_BRACED_CODE>
781   "}" {
782     STRING_1GROW ('}');
784     --nesting;
785     if (nesting < 0)
786       {
787         STRING_FINISH ();
788         loc->start = code_start;
789         BEGIN INITIAL;
790         RETURN_VALUE (BRACED_CODE, last_string);
791       }
792   }
795 <SC_PREDICATE>
797   "}" {
798     --nesting;
799     if (nesting < 0)
800       {
801         STRING_FINISH ();
802         loc->start = code_start;
803         BEGIN INITIAL;
804         RETURN_VALUE (BRACED_PREDICATE, last_string);
805       }
806     else
807       STRING_1GROW ('}');
808   }
811   /*--------------------------------------------------------------.
812   | Scanning some prologue: from "%{" (already scanned) to "%}".  |
813   `--------------------------------------------------------------*/
815 <SC_PROLOGUE>
817   "%}" {
818     STRING_FINISH ();
819     loc->start = code_start;
820     BEGIN INITIAL;
821     RETURN_VALUE (PROLOGUE, last_string);
822   }
824   <<EOF>>   unexpected_eof (code_start, "%}");
828   /*---------------------------------------------------------------.
829   | Scanning the epilogue (everything after the second "%%", which |
830   | has already been eaten).                                       |
831   `---------------------------------------------------------------*/
833 <SC_EPILOGUE>
835   <<EOF>> {
836     STRING_FINISH ();
837     loc->start = code_start;
838     BEGIN INITIAL;
839     RETURN_VALUE (EPILOGUE, last_string);
840   }
844   /*-----------------------------------------------------.
845   | By default, grow the string obstack with the input.  |
846   `-----------------------------------------------------*/
848 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PREDICATE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_TSTRING>
850   /* Accept multibyte characters in one block instead of byte after
851      byte, so that add_column_width and mbsnwidth can compute correct
852      screen width.
854      Add a fallthrough "|." so that non UTF-8 input is still accepted
855      and does not jam the scanner.  */
856   {mbchar}|.   STRING_GROW ();
862 /*------------------------------------------------------.
863 | Scan NUMBER for a base-BASE integer at location LOC.  |
864 `------------------------------------------------------*/
866 static int
867 scan_integer (char const *number, int base, location loc)
869   verify (INT_MAX < ULONG_MAX);
870   if (base == 16)
871     complain (&loc, Wyacc,
872               _("POSIX Yacc does not support hexadecimal literals"));
874   errno = 0;
875   long num = strtol (number, NULL, base);
877   if (! (0 <= num && num <= INT_MAX && errno == 0))
878     {
879       complain (&loc, complaint, _("integer out of range: %s"),
880                 quote (number));
881       num = INT_MAX;
882     }
884   return num;
888 /*------------------------------------------------------------------.
889 | Convert universal character name UCN to a single-byte character,  |
890 | and return that character.  Return -1 if UCN does not correspond  |
891 | to a single-byte character.                                       |
892 `------------------------------------------------------------------*/
894 static int
895 convert_ucn_to_byte (char const *ucn)
897   verify (UCHAR_MAX <= INT_MAX);
898   long code = strtol (ucn + 2, NULL, 16);
900   /* FIXME: Currently we assume Unicode-compatible unibyte characters
901      on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes).  On
902      non-ASCII hosts we support only the portable C character set.
903      These limitations should be removed once we add support for
904      multibyte characters.  */
906   if (! (0 <= code && code <= UCHAR_MAX))
907     return -1;
909 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
910   {
911     /* A non-ASCII host.  Use CODE to index into a table of the C
912        basic execution character set, which is guaranteed to exist on
913        all Standard C platforms.  This table also includes '$', '@',
914        and '`', which are not in the basic execution character set but
915        which are unibyte characters on all the platforms that we know
916        about.  */
917     static signed char const table[] =
918       {
919         '\0',   -1,   -1,   -1,   -1,   -1,   -1, '\a',
920         '\b', '\t', '\n', '\v', '\f', '\r',   -1,   -1,
921           -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
922           -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
923          ' ',  '!',  '"',  '#',  '$',  '%',  '&', '\'',
924          '(',  ')',  '*',  '+',  ',',  '-',  '.',  '/',
925          '0',  '1',  '2',  '3',  '4',  '5',  '6',  '7',
926          '8',  '9',  ':',  ';',  '<',  '=',  '>',  '?',
927          '@',  'A',  'B',  'C',  'D',  'E',  'F',  'G',
928          'H',  'I',  'J',  'K',  'L',  'M',  'N',  'O',
929          'P',  'Q',  'R',  'S',  'T',  'U',  'V',  'W',
930          'X',  'Y',  'Z',  '[', '\\',  ']',  '^',  '_',
931          '`',  'a',  'b',  'c',  'd',  'e',  'f',  'g',
932          'h',  'i',  'j',  'k',  'l',  'm',  'n',  'o',
933          'p',  'q',  'r',  's',  't',  'u',  'v',  'w',
934          'x',  'y',  'z',  '{',  '|',  '}',  '~'
935       };
937     code = code < sizeof table ? table[code] : -1;
938   }
939 #endif
941   return code;
945 /*----------------------------------------------------------------------------.
946 | Handle '#line INT_LITERAL( "FILE")?\n'.  ARGS has already skipped '#line '. |
947 `----------------------------------------------------------------------------*/
949 static void
950 handle_syncline (char *args, location loc)
952   char *file;
953   errno = 0;
954   long lineno = strtol (args, &file, 10);
955   if (! (0 <= lineno && lineno <= INT_MAX && errno == 0))
956     {
957       complain (&loc, Wother, _("line number overflow"));
958       lineno = INT_MAX;
959     }
961   file = strchr (file, '"');
962   if (file)
963     {
964       *strchr (file + 1, '"') = '\0';
965       current_file = uniqstr_new (file + 1);
966     }
967   boundary_set (&scanner_cursor, current_file, lineno, 1, 1);
971 /*----------------------------------------------------------------.
972 | For a token or comment starting at START, report message MSGID, |
973 | which should say that an end marker was found before the        |
974 | expected TOKEN_END. Then, pretend that TOKEN_END was found.     |
975 `----------------------------------------------------------------*/
977 static void
978 unexpected_end (boundary start, char const *msgid, char const *token_end)
980   location loc;
981   loc.start = start;
982   loc.end = scanner_cursor;
983   size_t i = strlen (token_end);
985   /* Adjust scanner cursor so that any later message does not count
986      the characters about to be inserted.  */
987   scanner_cursor.column -= i;
988   scanner_cursor.byte -= i;
990   while (i != 0)
991     unput (token_end[--i]);
993   token_end = quote (token_end);
994   /* Instead of '\'', display "'".  */
995   if (STREQ (token_end, "'\\''"))
996     token_end = "\"'\"";
997   complain (&loc, complaint, msgid, token_end);
1001 /*------------------------------------------------------------------------.
1002 | Report an unexpected EOF in a token or comment starting at START.       |
1003 | An end of file was encountered and the expected TOKEN_END was missing.  |
1004 | After reporting the problem, pretend that TOKEN_END was found.          |
1005 `------------------------------------------------------------------------*/
1007 static void
1008 unexpected_eof (boundary start, char const *token_end)
1010   unexpected_end (start, _("missing %s at end of file"), token_end);
1014 /*----------------------------------------.
1015 | Likewise, but for unexpected newlines.  |
1016 `----------------------------------------*/
1018 static void
1019 unexpected_newline (boundary start, char const *token_end)
1021   unexpected_end (start, _("missing %s at end of line"), token_end);
1025 void
1026 gram_scanner_open (const char *gram)
1028   gram__flex_debug = trace_flag & trace_scan;
1029   gram_debug = trace_flag & trace_parse;
1030   obstack_init (&obstack_for_string);
1031   current_file = gram;
1032   gram_in = xfopen (gram, "r");
1036 void
1037 gram_scanner_close (void)
1039   xfclose (gram_in);
1040   /* Reclaim Flex's buffers.  */
1041   yylex_destroy ();
1045 void
1046 gram_scanner_free (void)
1048   obstack_free (&obstack_for_string, 0);