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