]> git.saurik.com Git - bison.git/blob - src/scan-gram.l
dbe40b9c13bc8ba6c81cf587037c8fb72c54b97a
[bison.git] / src / scan-gram.l
1 /* Bison Grammar Scanner -*- C -*-
2
3 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA
21 */
22
23 %option debug nodefault nounput noyywrap never-interactive
24 %option prefix="gram_" outfile="lex.yy.c"
25
26 %{
27 /* Work around a bug in flex 2.5.31. See Debian bug 333231
28 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
29 #undef gram_wrap
30 #define gram_wrap() 1
31
32 #define FLEX_PREFIX(Id) gram_ ## Id
33 #include "flex-scanner.h"
34
35 #include "complain.h"
36 #include "files.h"
37 #include "getargs.h" /* yacc_flag */
38 #include "gram.h"
39 #include "quotearg.h"
40 #include "reader.h"
41 #include "uniqstr.h"
42
43 #include <mbswidth.h>
44 #include <quote.h>
45
46 #include "scan-gram.h"
47
48 #define YY_DECL GRAM_LEX_DECL
49
50 #define YY_USER_INIT \
51 code_start = scanner_cursor = loc->start; \
52
53 /* Location of scanner cursor. */
54 boundary scanner_cursor;
55
56 #define YY_USER_ACTION location_compute (loc, &scanner_cursor, yytext, yyleng);
57
58 static size_t no_cr_read (FILE *, char *, size_t);
59 #define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size))
60
61 /* A string representing the most recently saved token. */
62 char *last_string;
63
64 void
65 gram_scanner_last_string_free (void)
66 {
67 STRING_FREE;
68 }
69
70 /* The location of the most recently saved token, if it was a
71 BRACED_CODE token; otherwise, this has an unspecified value. */
72 location gram_last_braced_code_loc;
73
74 static void handle_syncline (char *, location);
75 static unsigned long int scan_integer (char const *p, int base, location loc);
76 static int convert_ucn_to_byte (char const *hex_text);
77 static void unexpected_eof (boundary, char const *);
78 static void unexpected_newline (boundary, char const *);
79
80 %}
81 /* A C-like comment in directives/rules. */
82 %x SC_YACC_COMMENT
83 /* Strings and characters in directives/rules. */
84 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
85 /* A identifier was just read in directives/rules. Special state
86 to capture the sequence `identifier :'. */
87 %x SC_AFTER_IDENTIFIER
88 /* A keyword that should be followed by some code was read (e.g.
89 %printer). */
90 %x SC_PRE_CODE
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 /* Token type to return, when applicable. */
125 int token_type IF_LINT (= 0);
126
127 /* Location of most recent identifier, when applicable. */
128 location id_loc IF_LINT (= empty_location);
129
130 /* Where containing code started, when applicable. Its initial
131 value is relevant only when yylex is invoked in the SC_EPILOGUE
132 start condition. */
133 boundary code_start = scanner_cursor;
134
135 /* Where containing comment or string or character literal started,
136 when applicable. */
137 boundary token_start IF_LINT (= scanner_cursor);
138 %}
139
140
141 /*-----------------------.
142 | Scanning white space. |
143 `-----------------------*/
144
145 <INITIAL,SC_AFTER_IDENTIFIER,SC_PRE_CODE>
146 {
147 /* Comments and white space. */
148 "," warn_at (*loc, _("stray `,' treated as white space"));
149 [ \f\n\t\v] |
150 "//".* ;
151 "/*" {
152 token_start = loc->start;
153 context_state = YY_START;
154 BEGIN SC_YACC_COMMENT;
155 }
156
157 /* #line directives are not documented, and may be withdrawn or
158 modified in future versions of Bison. */
159 ^"#line "{int}" \"".*"\"\n" {
160 handle_syncline (yytext + sizeof "#line " - 1, *loc);
161 }
162 }
163
164
165 /*----------------------------.
166 | Scanning Bison directives. |
167 `----------------------------*/
168 <INITIAL>
169 {
170 "%binary" return PERCENT_NONASSOC;
171 "%debug" return PERCENT_DEBUG;
172 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
173 "%define" return PERCENT_DEFINE;
174 "%defines" return PERCENT_DEFINES;
175 "%destructor" /* FIXME: Remove once %union handled differently. */ token_type = BRACED_CODE; return PERCENT_DESTRUCTOR;
176 "%dprec" return PERCENT_DPREC;
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" /* FIXME: Remove once %union handled differently. */ token_type = BRACED_CODE; return PERCENT_INITIAL_ACTION;
183 "%glr-parser" return PERCENT_GLR_PARSER;
184 "%left" return PERCENT_LEFT;
185 "%lex-param" /* FIXME: Remove once %union handled differently. */ token_type = BRACED_CODE; 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" /* FIXME: Remove once %union handled differently. */ token_type = BRACED_CODE; return PERCENT_PARSE_PARAM;
196 "%prec" return PERCENT_PREC;
197 "%printer" /* FIXME: Remove once %union handled differently. */ token_type = BRACED_CODE; 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 "%term" return PERCENT_TOKEN;
204 "%token" return PERCENT_TOKEN;
205 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
206 "%type" return PERCENT_TYPE;
207 "%union" token_type = PERCENT_UNION; BEGIN SC_PRE_CODE;
208 "%verbose" return PERCENT_VERBOSE;
209 "%yacc" return PERCENT_YACC;
210
211 {directive} {
212 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
213 }
214
215 "=" return EQUAL;
216 "|" return PIPE;
217 ";" return SEMICOLON;
218
219 {id} {
220 val->symbol = symbol_get (yytext, *loc);
221 id_loc = *loc;
222 BEGIN SC_AFTER_IDENTIFIER;
223 }
224
225 {int} {
226 val->integer = scan_integer (yytext, 10, *loc);
227 return INT;
228 }
229 0[xX][0-9abcdefABCDEF]+ {
230 val->integer = scan_integer (yytext, 16, *loc);
231 return INT;
232 }
233
234 /* Characters. We don't check there is only one. */
235 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
236
237 /* Strings. */
238 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
239
240 /* Prologue. */
241 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
242
243 /* Code in between braces. */
244 "{" {
245 if (current_rule && current_rule->action)
246 grammar_midrule_action ();
247 STRING_GROW;
248 token_type = BRACED_CODE;
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 unsigned char last_string_1;
369 STRING_GROW;
370 STRING_FINISH;
371 loc->start = token_start;
372 val->symbol = symbol_get (quotearg_style (escape_quoting_style,
373 last_string),
374 *loc);
375 symbol_class_set (val->symbol, token_sym, *loc, false);
376 last_string_1 = last_string[1];
377 symbol_user_token_number_set (val->symbol, last_string_1, *loc);
378 STRING_FREE;
379 BEGIN INITIAL;
380 return ID;
381 }
382 \n unexpected_newline (token_start, "'"); BEGIN INITIAL;
383 <<EOF>> unexpected_eof (token_start, "'"); BEGIN INITIAL;
384 }
385
386 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
387 {
388 \0 complain_at (*loc, _("invalid null character"));
389 }
390
391
392 /*----------------------------.
393 | Decode escaped characters. |
394 `----------------------------*/
395
396 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
397 {
398 \\[0-7]{1,3} {
399 unsigned long int c = strtoul (yytext + 1, NULL, 8);
400 if (UCHAR_MAX < c)
401 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
402 else if (! c)
403 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
404 else
405 obstack_1grow (&obstack_for_string, c);
406 }
407
408 \\x[0-9abcdefABCDEF]+ {
409 verify (UCHAR_MAX < ULONG_MAX);
410 unsigned long int c = strtoul (yytext + 2, NULL, 16);
411 if (UCHAR_MAX < c)
412 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
413 else if (! c)
414 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
415 else
416 obstack_1grow (&obstack_for_string, c);
417 }
418
419 \\a obstack_1grow (&obstack_for_string, '\a');
420 \\b obstack_1grow (&obstack_for_string, '\b');
421 \\f obstack_1grow (&obstack_for_string, '\f');
422 \\n obstack_1grow (&obstack_for_string, '\n');
423 \\r obstack_1grow (&obstack_for_string, '\r');
424 \\t obstack_1grow (&obstack_for_string, '\t');
425 \\v obstack_1grow (&obstack_for_string, '\v');
426
427 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
428 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
429
430 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
431 int c = convert_ucn_to_byte (yytext);
432 if (c < 0)
433 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
434 else if (! c)
435 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
436 else
437 obstack_1grow (&obstack_for_string, c);
438 }
439 \\(.|\n) {
440 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
441 STRING_GROW;
442 }
443 }
444
445 /*--------------------------------------------.
446 | Scanning user-code characters and strings. |
447 `--------------------------------------------*/
448
449 <SC_CHARACTER,SC_STRING>
450 {
451 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
452 }
453
454 <SC_CHARACTER>
455 {
456 "'" STRING_GROW; BEGIN context_state;
457 \n unexpected_newline (token_start, "'"); BEGIN context_state;
458 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
459 }
460
461 <SC_STRING>
462 {
463 "\"" STRING_GROW; BEGIN context_state;
464 \n unexpected_newline (token_start, "\""); BEGIN context_state;
465 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
466 }
467
468
469 /*---------------------------------------------------.
470 | Strings, comments etc. can be found in user code. |
471 `---------------------------------------------------*/
472
473 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
474 {
475 "'" {
476 STRING_GROW;
477 context_state = YY_START;
478 token_start = loc->start;
479 BEGIN SC_CHARACTER;
480 }
481 "\"" {
482 STRING_GROW;
483 context_state = YY_START;
484 token_start = loc->start;
485 BEGIN SC_STRING;
486 }
487 "/"{splice}"*" {
488 STRING_GROW;
489 context_state = YY_START;
490 token_start = loc->start;
491 BEGIN SC_COMMENT;
492 }
493 "/"{splice}"/" {
494 STRING_GROW;
495 context_state = YY_START;
496 BEGIN SC_LINE_COMMENT;
497 }
498 }
499
500
501 /*---------------------------------------------------------------.
502 | Scanning after %union etc., possibly followed by white space. |
503 | For %union only, allow arbitrary C code to appear before the |
504 | following brace, as an extension to POSIX. |
505 `---------------------------------------------------------------*/
506
507 <SC_PRE_CODE>
508 {
509 . {
510 bool valid = yytext[0] == '{' || token_type == PERCENT_UNION;
511 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
512 yyless (0);
513
514 if (valid)
515 {
516 braces_level = -1;
517 code_start = loc->start;
518 BEGIN SC_BRACED_CODE;
519 }
520 else
521 {
522 complain_at (*loc, _("missing `{' in %s"),
523 token_name (token_type));
524 obstack_sgrow (&obstack_for_string, "{}");
525 STRING_FINISH;
526 val->chars = last_string;
527 BEGIN INITIAL;
528 return token_type;
529 }
530 }
531
532 <<EOF>> unexpected_eof (scanner_cursor, "{}"); BEGIN INITIAL;
533 }
534
535
536 /*---------------------------------------------------------------.
537 | Scanning some code in braces (%union and actions). The initial |
538 | "{" is already eaten. |
539 `---------------------------------------------------------------*/
540
541 <SC_BRACED_CODE>
542 {
543 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
544 "%"{splice}">" STRING_GROW; braces_level--;
545 "}" {
546 obstack_1grow (&obstack_for_string, '}');
547
548 --braces_level;
549 if (braces_level < 0)
550 {
551 STRING_FINISH;
552 loc->start = code_start;
553 val->chars = last_string;
554 gram_last_braced_code_loc = *loc;
555 BEGIN INITIAL;
556 return token_type;
557 }
558 }
559
560 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
561 (as `<' `<%'). */
562 "<"{splice}"<" STRING_GROW;
563
564 <<EOF>> unexpected_eof (code_start, "}"); BEGIN INITIAL;
565 }
566
567
568 /*--------------------------------------------------------------.
569 | Scanning some prologue: from "%{" (already scanned) to "%}". |
570 `--------------------------------------------------------------*/
571
572 <SC_PROLOGUE>
573 {
574 "%}" {
575 STRING_FINISH;
576 loc->start = code_start;
577 val->chars = last_string;
578 BEGIN INITIAL;
579 return PROLOGUE;
580 }
581
582 <<EOF>> unexpected_eof (code_start, "%}"); BEGIN INITIAL;
583 }
584
585
586 /*---------------------------------------------------------------.
587 | Scanning the epilogue (everything after the second "%%", which |
588 | has already been eaten). |
589 `---------------------------------------------------------------*/
590
591 <SC_EPILOGUE>
592 {
593 <<EOF>> {
594 STRING_FINISH;
595 loc->start = code_start;
596 val->chars = last_string;
597 BEGIN INITIAL;
598 return EPILOGUE;
599 }
600 }
601
602
603 /*-----------------------------------------------------.
604 | By default, grow the string obstack with the input. |
605 `-----------------------------------------------------*/
606
607 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
608 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
609
610 %%
611
612 /* Read bytes from FP into buffer BUF of size SIZE. Return the
613 number of bytes read. Remove '\r' from input, treating \r\n
614 and isolated \r as \n. */
615
616 static size_t
617 no_cr_read (FILE *fp, char *buf, size_t size)
618 {
619 size_t bytes_read = fread (buf, 1, size, fp);
620 if (bytes_read)
621 {
622 char *w = memchr (buf, '\r', bytes_read);
623 if (w)
624 {
625 char const *r = ++w;
626 char const *lim = buf + bytes_read;
627
628 for (;;)
629 {
630 /* Found an '\r'. Treat it like '\n', but ignore any
631 '\n' that immediately follows. */
632 w[-1] = '\n';
633 if (r == lim)
634 {
635 int ch = getc (fp);
636 if (ch != '\n' && ungetc (ch, fp) != ch)
637 break;
638 }
639 else if (*r == '\n')
640 r++;
641
642 /* Copy until the next '\r'. */
643 do
644 {
645 if (r == lim)
646 return w - buf;
647 }
648 while ((*w++ = *r++) != '\r');
649 }
650
651 return w - buf;
652 }
653 }
654
655 return bytes_read;
656 }
657
658
659
660 /*------------------------------------------------------.
661 | Scan NUMBER for a base-BASE integer at location LOC. |
662 `------------------------------------------------------*/
663
664 static unsigned long int
665 scan_integer (char const *number, int base, location loc)
666 {
667 verify (INT_MAX < ULONG_MAX);
668 unsigned long int num = strtoul (number, NULL, base);
669
670 if (INT_MAX < num)
671 {
672 complain_at (loc, _("integer out of range: %s"), quote (number));
673 num = INT_MAX;
674 }
675
676 return num;
677 }
678
679
680 /*------------------------------------------------------------------.
681 | Convert universal character name UCN to a single-byte character, |
682 | and return that character. Return -1 if UCN does not correspond |
683 | to a single-byte character. |
684 `------------------------------------------------------------------*/
685
686 static int
687 convert_ucn_to_byte (char const *ucn)
688 {
689 verify (UCHAR_MAX <= INT_MAX);
690 unsigned long int code = strtoul (ucn + 2, NULL, 16);
691
692 /* FIXME: Currently we assume Unicode-compatible unibyte characters
693 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
694 non-ASCII hosts we support only the portable C character set.
695 These limitations should be removed once we add support for
696 multibyte characters. */
697
698 if (UCHAR_MAX < code)
699 return -1;
700
701 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
702 {
703 /* A non-ASCII host. Use CODE to index into a table of the C
704 basic execution character set, which is guaranteed to exist on
705 all Standard C platforms. This table also includes '$', '@',
706 and '`', which are not in the basic execution character set but
707 which are unibyte characters on all the platforms that we know
708 about. */
709 static signed char const table[] =
710 {
711 '\0', -1, -1, -1, -1, -1, -1, '\a',
712 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
713 -1, -1, -1, -1, -1, -1, -1, -1,
714 -1, -1, -1, -1, -1, -1, -1, -1,
715 ' ', '!', '"', '#', '$', '%', '&', '\'',
716 '(', ')', '*', '+', ',', '-', '.', '/',
717 '0', '1', '2', '3', '4', '5', '6', '7',
718 '8', '9', ':', ';', '<', '=', '>', '?',
719 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
720 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
721 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
722 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
723 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
724 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
725 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
726 'x', 'y', 'z', '{', '|', '}', '~'
727 };
728
729 code = code < sizeof table ? table[code] : -1;
730 }
731 #endif
732
733 return code;
734 }
735
736
737 /*----------------------------------------------------------------.
738 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
739 `----------------------------------------------------------------*/
740
741 static void
742 handle_syncline (char *args, location loc)
743 {
744 char *after_num;
745 unsigned long int lineno = strtoul (args, &after_num, 10);
746 char *file = strchr (after_num, '"') + 1;
747 *strchr (file, '"') = '\0';
748 if (INT_MAX <= lineno)
749 {
750 warn_at (loc, _("line number overflow"));
751 lineno = INT_MAX;
752 }
753 current_file = uniqstr_new (file);
754 boundary_set, (&scanner_cursor, current_file, lineno, 1);
755 }
756
757
758 /*----------------------------------------------------------------.
759 | For a token or comment starting at START, report message MSGID, |
760 | which should say that an end marker was found before |
761 | the expected TOKEN_END. |
762 `----------------------------------------------------------------*/
763
764 static void
765 unexpected_end (boundary start, char const *msgid, char const *token_end)
766 {
767 location loc;
768 loc.start = start;
769 loc.end = scanner_cursor;
770 complain_at (loc, _(msgid), token_end);
771 }
772
773
774 /*------------------------------------------------------------------------.
775 | Report an unexpected EOF in a token or comment starting at START. |
776 | An end of file was encountered and the expected TOKEN_END was missing. |
777 `------------------------------------------------------------------------*/
778
779 static void
780 unexpected_eof (boundary start, char const *token_end)
781 {
782 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
783 }
784
785
786 /*----------------------------------------.
787 | Likewise, but for unexpected newlines. |
788 `----------------------------------------*/
789
790 static void
791 unexpected_newline (boundary start, char const *token_end)
792 {
793 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
794 }
795
796
797 /*-------------------------.
798 | Initialize the scanner. |
799 `-------------------------*/
800
801 void
802 gram_scanner_initialize (void)
803 {
804 obstack_init (&obstack_for_string);
805 }
806
807
808 /*-----------------------------------------------.
809 | Free all the memory allocated to the scanner. |
810 `-----------------------------------------------*/
811
812 void
813 gram_scanner_free (void)
814 {
815 obstack_free (&obstack_for_string, 0);
816 /* Reclaim Flex's buffers. */
817 yy_delete_buffer (YY_CURRENT_BUFFER);
818 }