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