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