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