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"
30 #include "src/complain.h"
31 #include "src/files.h"
32 #include "src/getargs.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>. */
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) \
69 #define ROLLBACK_CURRENT_TOKEN \
71 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0); \
72 scanner_cursor.byte -= yyleng; \
76 #define DEPRECATED_DIRECTIVE(Msg) \
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) \
86 #define STRING_GROW_ESCAPE(Char) \
88 verify (UCHAR_MAX < ULONG_MAX); \
90 bool valid = 0 < c && c <= UCHAR_MAX; \
92 complain (loc, complaint, \
93 _("invalid number after \\-escape: %s"), \
95 if (YY_START == SC_ESCAPED_CHARACTER) \
96 STRING_1GROW (valid ? c : '?'); \
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;
115 gram_scanner_last_string_free (void)
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. */
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. */
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])*
158 xint 0[xX][0-9abcdefABCDEF]+
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. */
176 /* Nesting level. Either for nested braces, or nested angle brackets
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
189 boundary code_start = scanner_cursor;
191 /* Where containing comment or string or character literal started,
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;
200 scanner_cursor = loc->start;
206 /*-----------------------.
207 | Scanning white space. |
208 `-----------------------*/
210 <INITIAL,SC_AFTER_IDENTIFIER,SC_BRACKETED_ID,SC_RETURN_BRACKETED_ID>
212 /* Comments and white space. */
214 complain (loc, Wother, _("stray ',' treated as white space"));
219 token_start = loc->start;
220 context_state = YY_START;
221 BEGIN SC_YACC_COMMENT;
224 ^"#line "{int}(" \"".*"\"")?{eol} {
225 handle_syncline (yytext + sizeof "#line " - 1, *loc);
230 /*----------------------------.
231 | Scanning Bison directives. |
232 `----------------------------*/
234 /* For directives that are also command line options, the regex must be
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. */
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");
309 complain (loc, complaint, _("invalid directive: %s"), quote (yytext));
316 ";" return SEMICOLON;
319 val->ID = uniqstr_new (yytext);
321 bracketed_id_str = NULL;
322 BEGIN SC_AFTER_IDENTIFIER;
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". */
331 complain (loc, complaint, _("invalid identifier: %s"), quote (yytext));
336 "'" token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
339 "\"" token_start = loc->start; STRING_1GROW ('"'); BEGIN SC_ESCAPED_STRING;
340 "_(\"" token_start = loc->start; STRING_1GROW ('"'); BEGIN SC_ESCAPED_TSTRING;
343 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
345 /* Code in between braces. */
349 code_start = loc->start;
350 BEGIN SC_BRACED_CODE;
353 /* Semantic predicate. */
354 "%?"([ \f\t\v]|{eol})*"{" {
356 code_start = loc->start;
361 "<*>" return TAG_ANY;
362 "<>" return TAG_NONE;
365 token_start = loc->start;
370 static int percent_percent_count;
371 if (++percent_percent_count == 2)
373 return PERCENT_PERCENT;
377 bracketed_id_str = NULL;
378 bracketed_id_start = loc->start;
379 bracketed_id_context_state = YY_START;
380 BEGIN SC_BRACKETED_ID;
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));
391 loc->start = loc->end = scanner_cursor;
397 /*--------------------------------------------------------------.
398 | Supporting \0 complexifies our implementation for no expected |
400 `--------------------------------------------------------------*/
402 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_TSTRING,SC_TAG>
405 complain (loc, complaint, _("invalid null character"));
413 /*-----------------------------------------------------------------.
414 | Scanning after an identifier, checking whether a colon is next. |
415 `-----------------------------------------------------------------*/
417 <SC_AFTER_IDENTIFIER>
420 if (bracketed_id_str)
422 ROLLBACK_CURRENT_TOKEN;
423 BEGIN SC_RETURN_BRACKETED_ID;
429 bracketed_id_start = loc->start;
430 bracketed_id_context_state = YY_START;
431 BEGIN SC_BRACKETED_ID;
435 ROLLBACK_CURRENT_TOKEN;
436 BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
441 ROLLBACK_CURRENT_TOKEN;
442 BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
447 BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
453 /*--------------------------------.
454 | Scanning bracketed identifiers. |
455 `--------------------------------*/
460 if (bracketed_id_str)
462 complain (loc, complaint,
463 _("unexpected identifier in bracketed name: %s"),
469 bracketed_id_str = uniqstr_new (yytext);
470 bracketed_id_loc = *loc;
474 BEGIN bracketed_id_context_state;
475 if (bracketed_id_str)
477 if (INITIAL == bracketed_id_context_state)
479 val->BRACKETED_ID = bracketed_id_str;
480 bracketed_id_str = 0;
481 *loc = bracketed_id_loc;
487 complain (loc, complaint, _("an identifier expected"));
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));
501 BEGIN bracketed_id_context_state;
502 unexpected_eof (bracketed_id_start, "]");
506 <SC_RETURN_BRACKETED_ID>
509 ROLLBACK_CURRENT_TOKEN;
510 val->BRACKETED_ID = bracketed_id_str;
511 bracketed_id_str = 0;
512 *loc = bracketed_id_loc;
519 /*---------------------------------------------------------------.
520 | Scanning a Yacc comment. The initial '/ *' is already eaten. |
521 `---------------------------------------------------------------*/
525 "*/" BEGIN context_state;
527 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
531 /*------------------------------------------------------------.
532 | Scanning a C comment. The initial '/ *' is already eaten. |
533 `------------------------------------------------------------*/
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 `--------------------------------------------------------------*/
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 `------------------------------------------------*/
565 loc->start = token_start;
566 complain (loc, Wyacc,
567 _("POSIX Yacc does not support string literals"));
568 RETURN_VALUE (STRING, last_string);
570 <<EOF>> unexpected_eof (token_start, "\"");
571 "\n" unexpected_newline (token_start, "\"");
580 loc->start = token_start;
581 complain (loc, Wyacc,
582 _("POSIX Yacc does not support string literals"));
583 RETURN_VALUE (TSTRING, last_string);
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>
601 loc->start = token_start;
603 if (last_string[0] == '\0')
605 complain (loc, complaint, _("empty character literal"));
609 else if (last_string[1] != '\0')
611 complain (loc, complaint, _("extra characters in character literal"));
617 val->CHAR_LITERAL = last_string[0];
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 `--------------------------------------------------------------*/
639 loc->start = token_start;
640 val->TAG = uniqstr_new (last_string);
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>
661 STRING_GROW_ESCAPE (strtol (yytext + 1, NULL, 8));
664 \\x[0-9abcdefABCDEF]+ {
665 STRING_GROW_ESCAPE (strtol (yytext + 2, NULL, 16));
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));
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))
689 p = quotearg_style_mem (escape_quoting_style, p, 1);
690 complain (loc, complaint, _("invalid character after \\-escape: %s"),
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 ? "?\""
705 /*--------------------------------------------.
706 | Scanning user-code characters and strings. |
707 `--------------------------------------------*/
709 <SC_CHARACTER,SC_STRING>
711 {splice}|\\{splice}[^\n\[\]] STRING_GROW ();
716 "'" STRING_GROW (); BEGIN context_state;
717 {eol} unexpected_newline (token_start, "'");
718 <<EOF>> unexpected_eof (token_start, "'");
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>
737 context_state = YY_START;
738 token_start = loc->start;
743 context_state = YY_START;
744 token_start = loc->start;
749 context_state = YY_START;
750 token_start = loc->start;
755 context_state = YY_START;
756 BEGIN SC_LINE_COMMENT;
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
774 "<"{splice}"<" STRING_GROW ();
776 <<EOF>> unexpected_eof (code_start, "}");
788 loc->start = code_start;
790 RETURN_VALUE (BRACED_CODE, last_string);
802 loc->start = code_start;
804 RETURN_VALUE (BRACED_PREDICATE, last_string);
811 /*--------------------------------------------------------------.
812 | Scanning some prologue: from "%{" (already scanned) to "%}". |
813 `--------------------------------------------------------------*/
819 loc->start = code_start;
821 RETURN_VALUE (PROLOGUE, last_string);
824 <<EOF>> unexpected_eof (code_start, "%}");
828 /*---------------------------------------------------------------.
829 | Scanning the epilogue (everything after the second "%%", which |
830 | has already been eaten). |
831 `---------------------------------------------------------------*/
837 loc->start = code_start;
839 RETURN_VALUE (EPILOGUE, last_string);
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
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 `------------------------------------------------------*/
867 scan_integer (char const *number, int base, location loc)
869 verify (INT_MAX < ULONG_MAX);
871 complain (&loc, Wyacc,
872 _("POSIX Yacc does not support hexadecimal literals"));
875 long num = strtol (number, NULL, base);
877 if (! (0 <= num && num <= INT_MAX && errno == 0))
879 complain (&loc, complaint, _("integer out of range: %s"),
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 `------------------------------------------------------------------*/
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))
909 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
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
917 static signed char const table[] =
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', '{', '|', '}', '~'
937 code = code < sizeof table ? table[code] : -1;
945 /*----------------------------------------------------------------------------.
946 | Handle '#line INT_LITERAL( "FILE")?\n'. ARGS has already skipped '#line '. |
947 `----------------------------------------------------------------------------*/
950 handle_syncline (char *args, location loc)
954 long lineno = strtol (args, &file, 10);
955 if (! (0 <= lineno && lineno <= INT_MAX && errno == 0))
957 complain (&loc, Wother, _("line number overflow"));
961 file = strchr (file, '"');
964 *strchr (file + 1, '"') = '\0';
965 current_file = uniqstr_new (file + 1);
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 `----------------------------------------------------------------*/
978 unexpected_end (boundary start, char const *msgid, char const *token_end)
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;
991 unput (token_end[--i]);
993 token_end = quote (token_end);
994 /* Instead of '\'', display "'". */
995 if (STREQ (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 `------------------------------------------------------------------------*/
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 `----------------------------------------*/
1019 unexpected_newline (boundary start, char const *token_end)
1021 unexpected_end (start, _("missing %s at end of line"), token_end);
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");
1037 gram_scanner_close (void)
1040 /* Reclaim Flex's buffers. */
1046 gram_scanner_free (void)
1048 obstack_free (&obstack_for_string, 0);