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