]> git.saurik.com Git - bison.git/blob - src/scan-gram.l
style changes.
[bison.git] / src / scan-gram.l
1 /* Bison Grammar Scanner -*- C -*-
2
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free
4 Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 %option debug nodefault nounput noyywrap never-interactive
22 %option prefix="gram_" outfile="lex.yy.c"
23
24 %{
25 /* Work around a bug in flex 2.5.31. See Debian bug 333231
26 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
27 #undef gram_wrap
28 #define gram_wrap() 1
29
30 #define FLEX_PREFIX(Id) gram_ ## Id
31 #include "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 %{id}
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
156 /* For directives that are also command line options, the regex must be
157 "%..."
158 after "[-_]"s are removed, and the directive must match the --long
159 option name, with a single string argument. Otherwise, add exceptions
160 to ../build-aux/cross-options.pl. */
161
162 <INITIAL>
163 {
164 "%binary" return PERCENT_NONASSOC;
165 "%code" return PERCENT_CODE;
166 "%debug" return PERCENT_DEBUG;
167 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
168 "%define" return PERCENT_DEFINE;
169 "%defines" return PERCENT_DEFINES;
170 "%destructor" return PERCENT_DESTRUCTOR;
171 "%dprec" return PERCENT_DPREC;
172 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
173 "%expect" return PERCENT_EXPECT;
174 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
175 "%file-prefix" return PERCENT_FILE_PREFIX;
176 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
177 "%initial-action" return PERCENT_INITIAL_ACTION;
178 "%glr-parser" return PERCENT_GLR_PARSER;
179 "%language" return PERCENT_LANGUAGE;
180 "%left" return PERCENT_LEFT;
181 "%lex-param" return PERCENT_LEX_PARAM;
182 "%locations" return PERCENT_LOCATIONS;
183 "%merge" return PERCENT_MERGE;
184 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
185 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
186 "%no"[-_]"lines" return PERCENT_NO_LINES;
187 "%nonassoc" return PERCENT_NONASSOC;
188 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
189 "%nterm" return PERCENT_NTERM;
190 "%output" return PERCENT_OUTPUT;
191 "%parse-param" return PERCENT_PARSE_PARAM;
192 "%prec" return PERCENT_PREC;
193 "%printer" return PERCENT_PRINTER;
194 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
195 "%require" return PERCENT_REQUIRE;
196 "%right" return PERCENT_RIGHT;
197 "%skeleton" return PERCENT_SKELETON;
198 "%start" return PERCENT_START;
199 "%term" return PERCENT_TOKEN;
200 "%token" return PERCENT_TOKEN;
201 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
202 "%type" return PERCENT_TYPE;
203 "%union" return PERCENT_UNION;
204 "%verbose" return PERCENT_VERBOSE;
205 "%yacc" return PERCENT_YACC;
206
207 {directive} {
208 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
209 }
210
211 "=" return EQUAL;
212 "|" return PIPE;
213 ";" return SEMICOLON;
214 "<*>" return TYPE_TAG_ANY;
215 "<>" return TYPE_TAG_NONE;
216
217 {id} {
218 val->uniqstr = uniqstr_new (yytext);
219 id_loc = *loc;
220 BEGIN SC_AFTER_IDENTIFIER;
221 }
222
223 {int} {
224 val->integer = scan_integer (yytext, 10, *loc);
225 return INT;
226 }
227 0[xX][0-9abcdefABCDEF]+ {
228 val->integer = scan_integer (yytext, 16, *loc);
229 return INT;
230 }
231
232 /* Identifiers may not start with a digit. Yet, don't silently
233 accept "1FOO" as "1 FOO". */
234 {int}{id} {
235 complain_at (*loc, _("invalid identifier: %s"), quote (yytext));
236 }
237
238 /* Characters. We don't check there is only one. */
239 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
240
241 /* Strings. */
242 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
243
244 /* Prologue. */
245 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
246
247 /* Code in between braces. */
248 "{" {
249 STRING_GROW;
250 braces_level = 0;
251 code_start = loc->start;
252 BEGIN SC_BRACED_CODE;
253 }
254
255 /* A type. */
256 "<"{tag}">" {
257 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
258 STRING_FINISH;
259 val->uniqstr = uniqstr_new (last_string);
260 STRING_FREE;
261 return TYPE;
262 }
263
264 "%%" {
265 static int percent_percent_count;
266 if (++percent_percent_count == 2)
267 BEGIN SC_EPILOGUE;
268 return PERCENT_PERCENT;
269 }
270
271 . {
272 complain_at (*loc, _("invalid character: %s"), quote (yytext));
273 }
274
275 <<EOF>> {
276 loc->start = loc->end = scanner_cursor;
277 yyterminate ();
278 }
279 }
280
281
282 /*-----------------------------------------------------------------.
283 | Scanning after an identifier, checking whether a colon is next. |
284 `-----------------------------------------------------------------*/
285
286 <SC_AFTER_IDENTIFIER>
287 {
288 ":" {
289 *loc = id_loc;
290 BEGIN INITIAL;
291 return ID_COLON;
292 }
293 . {
294 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
295 yyless (0);
296 *loc = id_loc;
297 BEGIN INITIAL;
298 return ID;
299 }
300 <<EOF>> {
301 *loc = id_loc;
302 BEGIN INITIAL;
303 return ID;
304 }
305 }
306
307
308 /*---------------------------------------------------------------.
309 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
310 `---------------------------------------------------------------*/
311
312 <SC_YACC_COMMENT>
313 {
314 "*/" BEGIN context_state;
315 .|\n ;
316 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
317 }
318
319
320 /*------------------------------------------------------------.
321 | Scanning a C comment. The initial `/ *' is already eaten. |
322 `------------------------------------------------------------*/
323
324 <SC_COMMENT>
325 {
326 "*"{splice}"/" STRING_GROW; BEGIN context_state;
327 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
328 }
329
330
331 /*--------------------------------------------------------------.
332 | Scanning a line comment. The initial `//' is already eaten. |
333 `--------------------------------------------------------------*/
334
335 <SC_LINE_COMMENT>
336 {
337 "\n" STRING_GROW; BEGIN context_state;
338 {splice} STRING_GROW;
339 <<EOF>> BEGIN context_state;
340 }
341
342
343 /*------------------------------------------------.
344 | Scanning a Bison string, including its escapes. |
345 | The initial quote is already eaten. |
346 `------------------------------------------------*/
347
348 <SC_ESCAPED_STRING>
349 {
350 "\""|"\n" {
351 if (yytext[0] == '\n')
352 unexpected_newline (token_start, "\"");
353 STRING_FINISH;
354 loc->start = token_start;
355 val->chars = last_string;
356 BEGIN INITIAL;
357 return STRING;
358 }
359 <<EOF>> {
360 unexpected_eof (token_start, "\"");
361 STRING_FINISH;
362 loc->start = token_start;
363 val->chars = last_string;
364 BEGIN INITIAL;
365 return STRING;
366 }
367 }
368
369 /*----------------------------------------------------------.
370 | Scanning a Bison character literal, decoding its escapes. |
371 | The initial quote is already eaten. |
372 `----------------------------------------------------------*/
373
374 <SC_ESCAPED_CHARACTER>
375 {
376 "'"|"\n" {
377 if (yytext[0] == '\n')
378 unexpected_newline (token_start, "'");
379 STRING_GROW;
380 STRING_FINISH;
381 loc->start = token_start;
382 val->character = last_string[1];
383 STRING_FREE;
384 BEGIN INITIAL;
385 return CHAR;
386 }
387 <<EOF>> {
388 unexpected_eof (token_start, "'");
389 STRING_FINISH;
390 loc->start = token_start;
391 if (strlen (last_string) > 1)
392 val->character = last_string[1];
393 else
394 val->character = last_string[0];
395 STRING_FREE;
396 BEGIN INITIAL;
397 return CHAR;
398 }
399 }
400
401 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
402 {
403 \0 complain_at (*loc, _("invalid null character"));
404 }
405
406
407 /*----------------------------.
408 | Decode escaped characters. |
409 `----------------------------*/
410
411 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
412 {
413 \\[0-7]{1,3} {
414 unsigned long int c = strtoul (yytext + 1, NULL, 8);
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 \\x[0-9abcdefABCDEF]+ {
424 verify (UCHAR_MAX < ULONG_MAX);
425 unsigned long int c = strtoul (yytext + 2, NULL, 16);
426 if (UCHAR_MAX < c)
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
434 \\a obstack_1grow (&obstack_for_string, '\a');
435 \\b obstack_1grow (&obstack_for_string, '\b');
436 \\f obstack_1grow (&obstack_for_string, '\f');
437 \\n obstack_1grow (&obstack_for_string, '\n');
438 \\r obstack_1grow (&obstack_for_string, '\r');
439 \\t obstack_1grow (&obstack_for_string, '\t');
440 \\v obstack_1grow (&obstack_for_string, '\v');
441
442 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
443 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
444
445 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
446 int c = convert_ucn_to_byte (yytext);
447 if (c < 0)
448 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
449 else if (! c)
450 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
451 else
452 obstack_1grow (&obstack_for_string, c);
453 }
454 \\(.|\n) {
455 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
456 STRING_GROW;
457 }
458 }
459
460 /*--------------------------------------------.
461 | Scanning user-code characters and strings. |
462 `--------------------------------------------*/
463
464 <SC_CHARACTER,SC_STRING>
465 {
466 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
467 }
468
469 <SC_CHARACTER>
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 <SC_STRING>
477 {
478 "\"" STRING_GROW; BEGIN context_state;
479 \n unexpected_newline (token_start, "\""); BEGIN context_state;
480 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
481 }
482
483
484 /*---------------------------------------------------.
485 | Strings, comments etc. can be found in user code. |
486 `---------------------------------------------------*/
487
488 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
489 {
490 "'" {
491 STRING_GROW;
492 context_state = YY_START;
493 token_start = loc->start;
494 BEGIN SC_CHARACTER;
495 }
496 "\"" {
497 STRING_GROW;
498 context_state = YY_START;
499 token_start = loc->start;
500 BEGIN SC_STRING;
501 }
502 "/"{splice}"*" {
503 STRING_GROW;
504 context_state = YY_START;
505 token_start = loc->start;
506 BEGIN SC_COMMENT;
507 }
508 "/"{splice}"/" {
509 STRING_GROW;
510 context_state = YY_START;
511 BEGIN SC_LINE_COMMENT;
512 }
513 }
514
515
516
517 /*-----------------------------------------------------------.
518 | Scanning some code in braces (actions). The initial "{" is |
519 | already eaten. |
520 `-----------------------------------------------------------*/
521
522 <SC_BRACED_CODE>
523 {
524 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
525 "%"{splice}">" STRING_GROW; braces_level--;
526 "}" {
527 obstack_1grow (&obstack_for_string, '}');
528
529 --braces_level;
530 if (braces_level < 0)
531 {
532 STRING_FINISH;
533 loc->start = code_start;
534 val->code = last_string;
535 BEGIN INITIAL;
536 return BRACED_CODE;
537 }
538 }
539
540 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
541 (as `<' `<%'). */
542 "<"{splice}"<" STRING_GROW;
543
544 <<EOF>> {
545 unexpected_eof (code_start, "}");
546 STRING_FINISH;
547 loc->start = code_start;
548 val->code = last_string;
549 BEGIN INITIAL;
550 return BRACED_CODE;
551 }
552 }
553
554
555 /*--------------------------------------------------------------.
556 | Scanning some prologue: from "%{" (already scanned) to "%}". |
557 `--------------------------------------------------------------*/
558
559 <SC_PROLOGUE>
560 {
561 "%}" {
562 STRING_FINISH;
563 loc->start = code_start;
564 val->chars = last_string;
565 BEGIN INITIAL;
566 return PROLOGUE;
567 }
568
569 <<EOF>> {
570 unexpected_eof (code_start, "%}");
571 STRING_FINISH;
572 loc->start = code_start;
573 val->chars = last_string;
574 BEGIN INITIAL;
575 return PROLOGUE;
576 }
577 }
578
579
580 /*---------------------------------------------------------------.
581 | Scanning the epilogue (everything after the second "%%", which |
582 | has already been eaten). |
583 `---------------------------------------------------------------*/
584
585 <SC_EPILOGUE>
586 {
587 <<EOF>> {
588 STRING_FINISH;
589 loc->start = code_start;
590 val->chars = last_string;
591 BEGIN INITIAL;
592 return EPILOGUE;
593 }
594 }
595
596
597 /*-----------------------------------------------------.
598 | By default, grow the string obstack with the input. |
599 `-----------------------------------------------------*/
600
601 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
602 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
603
604 %%
605
606 /* Read bytes from FP into buffer BUF of size SIZE. Return the
607 number of bytes read. Remove '\r' from input, treating \r\n
608 and isolated \r as \n. */
609
610 static size_t
611 no_cr_read (FILE *fp, char *buf, size_t size)
612 {
613 size_t bytes_read = fread (buf, 1, size, fp);
614 if (bytes_read)
615 {
616 char *w = memchr (buf, '\r', bytes_read);
617 if (w)
618 {
619 char const *r = ++w;
620 char const *lim = buf + bytes_read;
621
622 for (;;)
623 {
624 /* Found an '\r'. Treat it like '\n', but ignore any
625 '\n' that immediately follows. */
626 w[-1] = '\n';
627 if (r == lim)
628 {
629 int ch = getc (fp);
630 if (ch != '\n' && ungetc (ch, fp) != ch)
631 break;
632 }
633 else if (*r == '\n')
634 r++;
635
636 /* Copy until the next '\r'. */
637 do
638 {
639 if (r == lim)
640 return w - buf;
641 }
642 while ((*w++ = *r++) != '\r');
643 }
644
645 return w - buf;
646 }
647 }
648
649 return bytes_read;
650 }
651
652
653
654 /*------------------------------------------------------.
655 | Scan NUMBER for a base-BASE integer at location LOC. |
656 `------------------------------------------------------*/
657
658 static unsigned long int
659 scan_integer (char const *number, int base, location loc)
660 {
661 verify (INT_MAX < ULONG_MAX);
662 unsigned long int num = strtoul (number, NULL, base);
663
664 if (INT_MAX < num)
665 {
666 complain_at (loc, _("integer out of range: %s"), quote (number));
667 num = INT_MAX;
668 }
669
670 return num;
671 }
672
673
674 /*------------------------------------------------------------------.
675 | Convert universal character name UCN to a single-byte character, |
676 | and return that character. Return -1 if UCN does not correspond |
677 | to a single-byte character. |
678 `------------------------------------------------------------------*/
679
680 static int
681 convert_ucn_to_byte (char const *ucn)
682 {
683 verify (UCHAR_MAX <= INT_MAX);
684 unsigned long int code = strtoul (ucn + 2, NULL, 16);
685
686 /* FIXME: Currently we assume Unicode-compatible unibyte characters
687 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
688 non-ASCII hosts we support only the portable C character set.
689 These limitations should be removed once we add support for
690 multibyte characters. */
691
692 if (UCHAR_MAX < code)
693 return -1;
694
695 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
696 {
697 /* A non-ASCII host. Use CODE to index into a table of the C
698 basic execution character set, which is guaranteed to exist on
699 all Standard C platforms. This table also includes '$', '@',
700 and '`', which are not in the basic execution character set but
701 which are unibyte characters on all the platforms that we know
702 about. */
703 static signed char const table[] =
704 {
705 '\0', -1, -1, -1, -1, -1, -1, '\a',
706 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
707 -1, -1, -1, -1, -1, -1, -1, -1,
708 -1, -1, -1, -1, -1, -1, -1, -1,
709 ' ', '!', '"', '#', '$', '%', '&', '\'',
710 '(', ')', '*', '+', ',', '-', '.', '/',
711 '0', '1', '2', '3', '4', '5', '6', '7',
712 '8', '9', ':', ';', '<', '=', '>', '?',
713 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
714 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
715 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
716 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
717 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
718 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
719 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
720 'x', 'y', 'z', '{', '|', '}', '~'
721 };
722
723 code = code < sizeof table ? table[code] : -1;
724 }
725 #endif
726
727 return code;
728 }
729
730
731 /*----------------------------------------------------------------.
732 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
733 `----------------------------------------------------------------*/
734
735 static void
736 handle_syncline (char *args, location loc)
737 {
738 char *after_num;
739 unsigned long int lineno = strtoul (args, &after_num, 10);
740 char *file = strchr (after_num, '"') + 1;
741 *strchr (file, '"') = '\0';
742 if (INT_MAX <= lineno)
743 {
744 warn_at (loc, _("line number overflow"));
745 lineno = INT_MAX;
746 }
747 current_file = uniqstr_new (file);
748 boundary_set (&scanner_cursor, current_file, lineno, 1);
749 }
750
751
752 /*----------------------------------------------------------------.
753 | For a token or comment starting at START, report message MSGID, |
754 | which should say that an end marker was found before |
755 | the expected TOKEN_END. |
756 `----------------------------------------------------------------*/
757
758 static void
759 unexpected_end (boundary start, char const *msgid, char const *token_end)
760 {
761 location loc;
762 loc.start = start;
763 loc.end = scanner_cursor;
764 complain_at (loc, _(msgid), token_end);
765 }
766
767
768 /*------------------------------------------------------------------------.
769 | Report an unexpected EOF in a token or comment starting at START. |
770 | An end of file was encountered and the expected TOKEN_END was missing. |
771 `------------------------------------------------------------------------*/
772
773 static void
774 unexpected_eof (boundary start, char const *token_end)
775 {
776 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
777 }
778
779
780 /*----------------------------------------.
781 | Likewise, but for unexpected newlines. |
782 `----------------------------------------*/
783
784 static void
785 unexpected_newline (boundary start, char const *token_end)
786 {
787 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
788 }
789
790
791 /*-------------------------.
792 | Initialize the scanner. |
793 `-------------------------*/
794
795 void
796 gram_scanner_initialize (void)
797 {
798 obstack_init (&obstack_for_string);
799 }
800
801
802 /*-----------------------------------------------.
803 | Free all the memory allocated to the scanner. |
804 `-----------------------------------------------*/
805
806 void
807 gram_scanner_free (void)
808 {
809 obstack_free (&obstack_for_string, 0);
810 /* Reclaim Flex's buffers. */
811 yylex_destroy ();
812 }