]> git.saurik.com Git - bison.git/blob - src/scan-gram.l
bison: factoring.
[bison.git] / src / scan-gram.l
1 /* Bison Grammar Scanner -*- C -*-
2
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 %option debug nodefault nounput noyywrap never-interactive
22 %option prefix="gram_" outfile="lex.yy.c"
23
24 %{
25 /* Work around a bug in flex 2.5.31. See Debian bug 333231
26 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
27 #undef gram_wrap
28 #define gram_wrap() 1
29
30 #define FLEX_PREFIX(Id) gram_ ## Id
31 #include <src/flex-scanner.h>
32
33 #include <src/complain.h>
34 #include <src/files.h>
35 #include <src/gram.h>
36 #include <quotearg.h>
37 #include <src/reader.h>
38 #include <src/uniqstr.h>
39
40 #include <mbswidth.h>
41 #include <quote.h>
42
43 #include <src/scan-gram.h>
44
45 #define YY_DECL GRAM_LEX_DECL
46
47 #define YY_USER_INIT \
48 code_start = scanner_cursor = loc->start; \
49
50 /* Location of scanner cursor. */
51 static boundary scanner_cursor;
52
53 #define YY_USER_ACTION location_compute (loc, &scanner_cursor, yytext, yyleng);
54
55 static size_t no_cr_read (FILE *, char *, size_t);
56 #define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size))
57
58 #define RETURN_PERCENT_FLAG(Value) \
59 do { \
60 val->uniqstr = uniqstr_new (Value); \
61 return PERCENT_FLAG; \
62 } while (0)
63
64
65 /* A string representing the most recently saved token. */
66 static char *last_string;
67
68 void
69 gram_scanner_last_string_free (void)
70 {
71 STRING_FREE;
72 }
73
74 static void handle_syncline (char *, location);
75 static unsigned long int scan_integer (char const *p, int base, location loc);
76 static int convert_ucn_to_byte (char const *hex_text);
77 static void unexpected_eof (boundary, char const *);
78 static void unexpected_newline (boundary, char const *);
79
80 %}
81 /* A C-like comment in directives/rules. */
82 %x SC_YACC_COMMENT
83 /* Strings and characters in directives/rules. */
84 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
85 /* A identifier was just read in directives/rules. Special state
86 to capture the sequence `identifier :'. */
87 %x SC_AFTER_IDENTIFIER
88 /* A complex tag, with nested angles brackets. */
89 %x SC_TAG
90
91 /* Three types of user code:
92 - prologue (code between `%{' `%}' in the first section, before %%);
93 - actions, printers, union, etc, (between braced in the middle section);
94 - epilogue (everything after the second %%). */
95 %x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE
96 /* C and C++ comments in code. */
97 %x SC_COMMENT SC_LINE_COMMENT
98 /* Strings and characters in code. */
99 %x SC_STRING SC_CHARACTER
100
101 letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
102 id {letter}({letter}|[0-9]|-)*
103 directive %{id}
104 int [0-9]+
105
106 /* POSIX says that a tag must be both an id and a C union member, but
107 historically almost any character is allowed in a tag. We disallow
108 NUL, as this simplifies our implementation. We disallow angle
109 bracket to match them in nested pairs: several languages use them
110 for generics/template types. */
111 tag [^\0<>]+
112
113 /* Zero or more instances of backslash-newline. Following GCC, allow
114 white space between the backslash and the newline. */
115 splice (\\[ \f\t\v]*\n)*
116
117 %%
118 %{
119 /* Nesting level. Either for nested braces, or nested angle brackets
120 (but not mixed). */
121 int nesting IF_LINT (= 0);
122
123 /* Parent context state, when applicable. */
124 int context_state IF_LINT (= 0);
125
126 /* Location of most recent identifier, when applicable. */
127 location id_loc IF_LINT (= empty_location);
128
129 /* Where containing code started, when applicable. Its initial
130 value is relevant only when yylex is invoked in the SC_EPILOGUE
131 start condition. */
132 boundary code_start = scanner_cursor;
133
134 /* Where containing comment or string or character literal started,
135 when applicable. */
136 boundary token_start IF_LINT (= scanner_cursor);
137 %}
138
139
140 /*-----------------------.
141 | Scanning white space. |
142 `-----------------------*/
143
144 <INITIAL,SC_AFTER_IDENTIFIER>
145 {
146 /* Comments and white space. */
147 "," warn_at (*loc, _("stray `,' treated as white space"));
148 [ \f\n\t\v] |
149 "//".* ;
150 "/*" {
151 token_start = loc->start;
152 context_state = YY_START;
153 BEGIN SC_YACC_COMMENT;
154 }
155
156 /* #line directives are not documented, and may be withdrawn or
157 modified in future versions of Bison. */
158 ^"#line "{int}" \"".*"\"\n" {
159 handle_syncline (yytext + sizeof "#line " - 1, *loc);
160 }
161 }
162
163
164 /*----------------------------.
165 | Scanning Bison directives. |
166 `----------------------------*/
167
168 /* For directives that are also command line options, the regex must be
169 "%..."
170 after "[-_]"s are removed, and the directive must match the --long
171 option name, with a single string argument. Otherwise, add exceptions
172 to ../build-aux/cross-options.pl. */
173
174 <INITIAL>
175 {
176 "%binary" return PERCENT_NONASSOC;
177 "%code" return PERCENT_CODE;
178 "%debug" RETURN_PERCENT_FLAG("parse.trace");
179 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
180 "%define" return PERCENT_DEFINE;
181 "%defines" return PERCENT_DEFINES;
182 "%destructor" return PERCENT_DESTRUCTOR;
183 "%dprec" return PERCENT_DPREC;
184 "%error"[-_]"verbose" RETURN_PERCENT_FLAG("error-verbose");
185 "%expect" return PERCENT_EXPECT;
186 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
187 "%file-prefix" return PERCENT_FILE_PREFIX;
188 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
189 "%initial-action" return PERCENT_INITIAL_ACTION;
190 "%glr-parser" return PERCENT_GLR_PARSER;
191 "%language" return PERCENT_LANGUAGE;
192 "%left" return PERCENT_LEFT;
193 "%lex-param" return PERCENT_LEX_PARAM;
194 "%locations" RETURN_PERCENT_FLAG("locations");
195 "%merge" return PERCENT_MERGE;
196 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
197 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
198 "%no"[-_]"lines" return PERCENT_NO_LINES;
199 "%nonassoc" return PERCENT_NONASSOC;
200 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
201 "%nterm" return PERCENT_NTERM;
202 "%output" return PERCENT_OUTPUT;
203 "%parse-param" return PERCENT_PARSE_PARAM;
204 "%prec" return PERCENT_PREC;
205 "%precedence" return PERCENT_PRECEDENCE;
206 "%printer" return PERCENT_PRINTER;
207 "%pure"[-_]"parser" RETURN_PERCENT_FLAG("api.pure");
208 "%require" return PERCENT_REQUIRE;
209 "%right" return PERCENT_RIGHT;
210 "%skeleton" return PERCENT_SKELETON;
211 "%start" return PERCENT_START;
212 "%term" return PERCENT_TOKEN;
213 "%token" return PERCENT_TOKEN;
214 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
215 "%type" return PERCENT_TYPE;
216 "%union" return PERCENT_UNION;
217 "%verbose" return PERCENT_VERBOSE;
218 "%yacc" return PERCENT_YACC;
219
220 {directive} {
221 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
222 }
223
224 "=" return EQUAL;
225 "|" return PIPE;
226 ";" return SEMICOLON;
227
228 {id} {
229 val->uniqstr = uniqstr_new (yytext);
230 id_loc = *loc;
231 BEGIN SC_AFTER_IDENTIFIER;
232 }
233
234 {int} {
235 val->integer = scan_integer (yytext, 10, *loc);
236 return INT;
237 }
238 0[xX][0-9abcdefABCDEF]+ {
239 val->integer = scan_integer (yytext, 16, *loc);
240 return INT;
241 }
242
243 /* Characters. We don't check there is only one. */
244 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
245
246 /* Strings. */
247 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
248
249 /* Prologue. */
250 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
251
252 /* Code in between braces. */
253 "{" {
254 STRING_GROW;
255 nesting = 0;
256 code_start = loc->start;
257 BEGIN SC_BRACED_CODE;
258 }
259
260 /* A type. */
261 "<*>" return TAG_ANY;
262 "<>" return TAG_NONE;
263 "<"{tag}">" {
264 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
265 STRING_FINISH;
266 val->uniqstr = uniqstr_new (last_string);
267 STRING_FREE;
268 return TAG;
269 }
270 "<" {
271 nesting = 0;
272 token_start = loc->start;
273 BEGIN SC_TAG;
274 }
275
276 "%%" {
277 static int percent_percent_count;
278 if (++percent_percent_count == 2)
279 BEGIN SC_EPILOGUE;
280 return PERCENT_PERCENT;
281 }
282
283 . {
284 complain_at (*loc, _("invalid character: %s"), quote (yytext));
285 }
286
287 <<EOF>> {
288 loc->start = loc->end = scanner_cursor;
289 yyterminate ();
290 }
291 }
292
293
294 /*--------------------------------------------------------------.
295 | Supporting \0 complexifies our implementation for no expected |
296 | added value. |
297 `--------------------------------------------------------------*/
298
299 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING,SC_TAG>
300 {
301 \0 complain_at (*loc, _("invalid null character"));
302 }
303
304
305 /*-----------------------------------------------------------------.
306 | Scanning after an identifier, checking whether a colon is next. |
307 `-----------------------------------------------------------------*/
308
309 <SC_AFTER_IDENTIFIER>
310 {
311 ":" {
312 *loc = id_loc;
313 BEGIN INITIAL;
314 return ID_COLON;
315 }
316 . {
317 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
318 yyless (0);
319 *loc = id_loc;
320 BEGIN INITIAL;
321 return ID;
322 }
323 <<EOF>> {
324 *loc = id_loc;
325 BEGIN INITIAL;
326 return ID;
327 }
328 }
329
330
331 /*---------------------------------------------------------------.
332 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
333 `---------------------------------------------------------------*/
334
335 <SC_YACC_COMMENT>
336 {
337 "*/" BEGIN context_state;
338 .|\n ;
339 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
340 }
341
342
343 /*------------------------------------------------------------.
344 | Scanning a C comment. The initial `/ *' is already eaten. |
345 `------------------------------------------------------------*/
346
347 <SC_COMMENT>
348 {
349 "*"{splice}"/" STRING_GROW; BEGIN context_state;
350 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
351 }
352
353
354 /*--------------------------------------------------------------.
355 | Scanning a line comment. The initial `//' is already eaten. |
356 `--------------------------------------------------------------*/
357
358 <SC_LINE_COMMENT>
359 {
360 "\n" STRING_GROW; BEGIN context_state;
361 {splice} STRING_GROW;
362 <<EOF>> BEGIN context_state;
363 }
364
365
366 /*------------------------------------------------.
367 | Scanning a Bison string, including its escapes. |
368 | The initial quote is already eaten. |
369 `------------------------------------------------*/
370
371 <SC_ESCAPED_STRING>
372 {
373 "\""|"\n" {
374 if (yytext[0] == '\n')
375 unexpected_newline (token_start, "\"");
376 STRING_FINISH;
377 loc->start = token_start;
378 val->chars = last_string;
379 BEGIN INITIAL;
380 return STRING;
381 }
382 <<EOF>> {
383 unexpected_eof (token_start, "\"");
384 STRING_FINISH;
385 loc->start = token_start;
386 val->chars = last_string;
387 BEGIN INITIAL;
388 return STRING;
389 }
390 }
391
392 /*----------------------------------------------------------.
393 | Scanning a Bison character literal, decoding its escapes. |
394 | The initial quote is already eaten. |
395 `----------------------------------------------------------*/
396
397 <SC_ESCAPED_CHARACTER>
398 {
399 "'"|"\n" {
400 if (yytext[0] == '\n')
401 unexpected_newline (token_start, "'");
402 STRING_GROW;
403 STRING_FINISH;
404 loc->start = token_start;
405 val->character = last_string[1];
406 STRING_FREE;
407 BEGIN INITIAL;
408 return CHAR;
409 }
410 <<EOF>> {
411 unexpected_eof (token_start, "'");
412 STRING_FINISH;
413 loc->start = token_start;
414 if (strlen (last_string) > 1)
415 val->character = last_string[1];
416 else
417 val->character = last_string[0];
418 STRING_FREE;
419 BEGIN INITIAL;
420 return CHAR;
421 }
422 }
423
424 /*-----------------------------------------------------------.
425 | Scanning a Bison nested tag. The initial angle bracket is |
426 | already eaten. |
427 `-----------------------------------------------------------*/
428
429 <SC_TAG>
430 {
431 ">" {
432 --nesting;
433 if (nesting < 0)
434 {
435 STRING_FINISH;
436 loc->start = token_start;
437 val->uniqstr = uniqstr_new (last_string);
438 STRING_FREE;
439 BEGIN INITIAL;
440 return TAG;
441 }
442 STRING_GROW;
443 }
444
445 [^<>]+ STRING_GROW;
446 "<"+ STRING_GROW; nesting += yyleng;
447
448 <<EOF>> {
449 unexpected_eof (token_start, ">");
450 STRING_FINISH;
451 loc->start = token_start;
452 val->uniqstr = uniqstr_new (last_string);
453 STRING_FREE;
454 BEGIN INITIAL;
455 return TAG;
456 }
457 }
458
459 /*----------------------------.
460 | Decode escaped characters. |
461 `----------------------------*/
462
463 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
464 {
465 \\[0-7]{1,3} {
466 unsigned long int c = strtoul (yytext + 1, NULL, 8);
467 if (UCHAR_MAX < c)
468 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
469 else if (! c)
470 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
471 else
472 obstack_1grow (&obstack_for_string, c);
473 }
474
475 \\x[0-9abcdefABCDEF]+ {
476 verify (UCHAR_MAX < ULONG_MAX);
477 unsigned long int c = strtoul (yytext + 2, NULL, 16);
478 if (UCHAR_MAX < c)
479 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
480 else if (! c)
481 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
482 else
483 obstack_1grow (&obstack_for_string, c);
484 }
485
486 \\a obstack_1grow (&obstack_for_string, '\a');
487 \\b obstack_1grow (&obstack_for_string, '\b');
488 \\f obstack_1grow (&obstack_for_string, '\f');
489 \\n obstack_1grow (&obstack_for_string, '\n');
490 \\r obstack_1grow (&obstack_for_string, '\r');
491 \\t obstack_1grow (&obstack_for_string, '\t');
492 \\v obstack_1grow (&obstack_for_string, '\v');
493
494 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
495 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
496
497 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
498 int c = convert_ucn_to_byte (yytext);
499 if (c < 0)
500 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
501 else if (! c)
502 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
503 else
504 obstack_1grow (&obstack_for_string, c);
505 }
506 \\(.|\n) {
507 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
508 STRING_GROW;
509 }
510 }
511
512 /*--------------------------------------------.
513 | Scanning user-code characters and strings. |
514 `--------------------------------------------*/
515
516 <SC_CHARACTER,SC_STRING>
517 {
518 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
519 }
520
521 <SC_CHARACTER>
522 {
523 "'" STRING_GROW; BEGIN context_state;
524 \n unexpected_newline (token_start, "'"); BEGIN context_state;
525 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
526 }
527
528 <SC_STRING>
529 {
530 "\"" STRING_GROW; BEGIN context_state;
531 \n unexpected_newline (token_start, "\""); BEGIN context_state;
532 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
533 }
534
535
536 /*---------------------------------------------------.
537 | Strings, comments etc. can be found in user code. |
538 `---------------------------------------------------*/
539
540 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
541 {
542 "'" {
543 STRING_GROW;
544 context_state = YY_START;
545 token_start = loc->start;
546 BEGIN SC_CHARACTER;
547 }
548 "\"" {
549 STRING_GROW;
550 context_state = YY_START;
551 token_start = loc->start;
552 BEGIN SC_STRING;
553 }
554 "/"{splice}"*" {
555 STRING_GROW;
556 context_state = YY_START;
557 token_start = loc->start;
558 BEGIN SC_COMMENT;
559 }
560 "/"{splice}"/" {
561 STRING_GROW;
562 context_state = YY_START;
563 BEGIN SC_LINE_COMMENT;
564 }
565 }
566
567
568
569 /*-----------------------------------------------------------.
570 | Scanning some code in braces (actions). The initial "{" is |
571 | already eaten. |
572 `-----------------------------------------------------------*/
573
574 <SC_BRACED_CODE>
575 {
576 "{"|"<"{splice}"%" STRING_GROW; nesting++;
577 "%"{splice}">" STRING_GROW; nesting--;
578 "}" {
579 obstack_1grow (&obstack_for_string, '}');
580
581 --nesting;
582 if (nesting < 0)
583 {
584 STRING_FINISH;
585 loc->start = code_start;
586 val->code = last_string;
587 BEGIN INITIAL;
588 return BRACED_CODE;
589 }
590 }
591
592 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
593 (as `<' `<%'). */
594 "<"{splice}"<" STRING_GROW;
595
596 <<EOF>> {
597 unexpected_eof (code_start, "}");
598 STRING_FINISH;
599 loc->start = code_start;
600 val->code = last_string;
601 BEGIN INITIAL;
602 return BRACED_CODE;
603 }
604 }
605
606
607 /*--------------------------------------------------------------.
608 | Scanning some prologue: from "%{" (already scanned) to "%}". |
609 `--------------------------------------------------------------*/
610
611 <SC_PROLOGUE>
612 {
613 "%}" {
614 STRING_FINISH;
615 loc->start = code_start;
616 val->chars = last_string;
617 BEGIN INITIAL;
618 return PROLOGUE;
619 }
620
621 <<EOF>> {
622 unexpected_eof (code_start, "%}");
623 STRING_FINISH;
624 loc->start = code_start;
625 val->chars = last_string;
626 BEGIN INITIAL;
627 return PROLOGUE;
628 }
629 }
630
631
632 /*---------------------------------------------------------------.
633 | Scanning the epilogue (everything after the second "%%", which |
634 | has already been eaten). |
635 `---------------------------------------------------------------*/
636
637 <SC_EPILOGUE>
638 {
639 <<EOF>> {
640 STRING_FINISH;
641 loc->start = code_start;
642 val->chars = last_string;
643 BEGIN INITIAL;
644 return EPILOGUE;
645 }
646 }
647
648
649 /*-----------------------------------------------------.
650 | By default, grow the string obstack with the input. |
651 `-----------------------------------------------------*/
652
653 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
654 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
655
656 %%
657
658 /* Read bytes from FP into buffer BUF of size SIZE. Return the
659 number of bytes read. Remove '\r' from input, treating \r\n
660 and isolated \r as \n. */
661
662 static size_t
663 no_cr_read (FILE *fp, char *buf, size_t size)
664 {
665 size_t bytes_read = fread (buf, 1, size, fp);
666 if (bytes_read)
667 {
668 char *w = memchr (buf, '\r', bytes_read);
669 if (w)
670 {
671 char const *r = ++w;
672 char const *lim = buf + bytes_read;
673
674 for (;;)
675 {
676 /* Found an '\r'. Treat it like '\n', but ignore any
677 '\n' that immediately follows. */
678 w[-1] = '\n';
679 if (r == lim)
680 {
681 int ch = getc (fp);
682 if (ch != '\n' && ungetc (ch, fp) != ch)
683 break;
684 }
685 else if (*r == '\n')
686 r++;
687
688 /* Copy until the next '\r'. */
689 do
690 {
691 if (r == lim)
692 return w - buf;
693 }
694 while ((*w++ = *r++) != '\r');
695 }
696
697 return w - buf;
698 }
699 }
700
701 return bytes_read;
702 }
703
704
705
706 /*------------------------------------------------------.
707 | Scan NUMBER for a base-BASE integer at location LOC. |
708 `------------------------------------------------------*/
709
710 static unsigned long int
711 scan_integer (char const *number, int base, location loc)
712 {
713 verify (INT_MAX < ULONG_MAX);
714 unsigned long int num = strtoul (number, NULL, base);
715
716 if (INT_MAX < num)
717 {
718 complain_at (loc, _("integer out of range: %s"), quote (number));
719 num = INT_MAX;
720 }
721
722 return num;
723 }
724
725
726 /*------------------------------------------------------------------.
727 | Convert universal character name UCN to a single-byte character, |
728 | and return that character. Return -1 if UCN does not correspond |
729 | to a single-byte character. |
730 `------------------------------------------------------------------*/
731
732 static int
733 convert_ucn_to_byte (char const *ucn)
734 {
735 verify (UCHAR_MAX <= INT_MAX);
736 unsigned long int code = strtoul (ucn + 2, NULL, 16);
737
738 /* FIXME: Currently we assume Unicode-compatible unibyte characters
739 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
740 non-ASCII hosts we support only the portable C character set.
741 These limitations should be removed once we add support for
742 multibyte characters. */
743
744 if (UCHAR_MAX < code)
745 return -1;
746
747 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
748 {
749 /* A non-ASCII host. Use CODE to index into a table of the C
750 basic execution character set, which is guaranteed to exist on
751 all Standard C platforms. This table also includes '$', '@',
752 and '`', which are not in the basic execution character set but
753 which are unibyte characters on all the platforms that we know
754 about. */
755 static signed char const table[] =
756 {
757 '\0', -1, -1, -1, -1, -1, -1, '\a',
758 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
759 -1, -1, -1, -1, -1, -1, -1, -1,
760 -1, -1, -1, -1, -1, -1, -1, -1,
761 ' ', '!', '"', '#', '$', '%', '&', '\'',
762 '(', ')', '*', '+', ',', '-', '.', '/',
763 '0', '1', '2', '3', '4', '5', '6', '7',
764 '8', '9', ':', ';', '<', '=', '>', '?',
765 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
766 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
767 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
768 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
769 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
770 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
771 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
772 'x', 'y', 'z', '{', '|', '}', '~'
773 };
774
775 code = code < sizeof table ? table[code] : -1;
776 }
777 #endif
778
779 return code;
780 }
781
782
783 /*----------------------------------------------------------------.
784 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
785 `----------------------------------------------------------------*/
786
787 static void
788 handle_syncline (char *args, location loc)
789 {
790 char *after_num;
791 unsigned long int lineno = strtoul (args, &after_num, 10);
792 char *file = strchr (after_num, '"') + 1;
793 *strchr (file, '"') = '\0';
794 if (INT_MAX <= lineno)
795 {
796 warn_at (loc, _("line number overflow"));
797 lineno = INT_MAX;
798 }
799 current_file = uniqstr_new (file);
800 boundary_set (&scanner_cursor, current_file, lineno, 1);
801 }
802
803
804 /*----------------------------------------------------------------.
805 | For a token or comment starting at START, report message MSGID, |
806 | which should say that an end marker was found before |
807 | the expected TOKEN_END. |
808 `----------------------------------------------------------------*/
809
810 static void
811 unexpected_end (boundary start, char const *msgid, char const *token_end)
812 {
813 location loc;
814 loc.start = start;
815 loc.end = scanner_cursor;
816 complain_at (loc, _(msgid), token_end);
817 }
818
819
820 /*------------------------------------------------------------------------.
821 | Report an unexpected EOF in a token or comment starting at START. |
822 | An end of file was encountered and the expected TOKEN_END was missing. |
823 `------------------------------------------------------------------------*/
824
825 static void
826 unexpected_eof (boundary start, char const *token_end)
827 {
828 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
829 }
830
831
832 /*----------------------------------------.
833 | Likewise, but for unexpected newlines. |
834 `----------------------------------------*/
835
836 static void
837 unexpected_newline (boundary start, char const *token_end)
838 {
839 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
840 }
841
842
843 /*-------------------------.
844 | Initialize the scanner. |
845 `-------------------------*/
846
847 void
848 gram_scanner_initialize (void)
849 {
850 obstack_init (&obstack_for_string);
851 }
852
853
854 /*-----------------------------------------------.
855 | Free all the memory allocated to the scanner. |
856 `-----------------------------------------------*/
857
858 void
859 gram_scanner_free (void)
860 {
861 obstack_free (&obstack_for_string, 0);
862 /* Reclaim Flex's buffers. */
863 yylex_destroy ();
864 }