]> git.saurik.com Git - bison.git/blame_incremental - src/scan-gram.l
carets: activate by default
[bison.git] / src / scan-gram.l
... / ...
CommitLineData
1/* Bison Grammar Scanner -*- C -*-
2
3 Copyright (C) 2002-2012 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 3 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, see <http://www.gnu.org/licenses/>. */
19
20%option debug nodefault noinput noyywrap never-interactive
21%option prefix="gram_" outfile="lex.yy.c"
22
23%{
24/* Work around a bug in flex 2.5.31. See Debian bug 333231
25 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
26#undef gram_wrap
27#define gram_wrap() 1
28
29#define FLEX_PREFIX(Id) gram_ ## Id
30#include <src/flex-scanner.h>
31
32#include <src/complain.h>
33#include <src/files.h>
34#include <src/getargs.h>
35#include <src/gram.h>
36#include <quotearg.h>
37#include <src/reader.h>
38#include <src/uniqstr.h>
39
40#include <c-ctype.h>
41#include <mbswidth.h>
42#include <quote.h>
43
44#include <src/scan-gram.h>
45
46#define YY_DECL GRAM_LEX_DECL
47
48#define YY_USER_INIT \
49 code_start = scanner_cursor = loc->start; \
50
51/* Location of scanner cursor. */
52static boundary scanner_cursor;
53
54#define YY_USER_ACTION location_compute (loc, &scanner_cursor, yytext, yyleng);
55
56static size_t no_cr_read (FILE *, char *, size_t);
57#define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size))
58
59#define RETURN_PERCENT_PARAM(Value) \
60 RETURN_VALUE(PERCENT_PARAM, param, param_ ## Value)
61
62#define RETURN_PERCENT_FLAG(Value) \
63 RETURN_VALUE(PERCENT_FLAG, uniqstr, uniqstr_new (Value))
64
65#define RETURN_VALUE(Token, Field, Value) \
66 do { \
67 val->Field = Value; \
68 return Token; \
69 } while (0)
70
71#define ROLLBACK_CURRENT_TOKEN \
72 do { \
73 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0); \
74 yyless (0); \
75 } while (0)
76
77#define DEPRECATED(Msg) \
78 do { \
79 size_t i; \
80 deprecated_directive (loc, yytext, Msg); \
81 scanner_cursor.column -= mbsnwidth (Msg, strlen (Msg), 0); \
82 for (i = strlen (Msg); i != 0; --i) \
83 unput (Msg[i - 1]); \
84 } while (0)
85
86/* A string representing the most recently saved token. */
87static char *last_string;
88
89/* Bracketed identifier. */
90static uniqstr bracketed_id_str = 0;
91static location bracketed_id_loc;
92static boundary bracketed_id_start;
93static int bracketed_id_context_state = 0;
94
95void
96gram_scanner_last_string_free (void)
97{
98 STRING_FREE;
99}
100
101static void handle_syncline (char *, location);
102static unsigned long int scan_integer (char const *p, int base, location loc);
103static int convert_ucn_to_byte (char const *hex_text);
104static void unexpected_eof (boundary, char const *);
105static void unexpected_newline (boundary, char const *);
106
107%}
108 /* A C-like comment in directives/rules. */
109%x SC_YACC_COMMENT
110 /* Strings and characters in directives/rules. */
111%x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
112 /* A identifier was just read in directives/rules. Special state
113 to capture the sequence 'identifier :'. */
114%x SC_AFTER_IDENTIFIER
115 /* A complex tag, with nested angles brackets. */
116%x SC_TAG
117
118 /* Four types of user code:
119 - prologue (code between '%{' '%}' in the first section, before %%);
120 - actions, printers, union, etc, (between braced in the middle section);
121 - epilogue (everything after the second %%).
122 - predicate (code between '%?{' and '{' in middle section); */
123%x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE SC_PREDICATE
124 /* C and C++ comments in code. */
125%x SC_COMMENT SC_LINE_COMMENT
126 /* Strings and characters in code. */
127%x SC_STRING SC_CHARACTER
128 /* Bracketed identifiers support. */
129%x SC_BRACKETED_ID SC_RETURN_BRACKETED_ID
130
131letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
132notletter [^.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]{-}[%\{]
133id {letter}({letter}|[-0-9])*
134int [0-9]+
135
136/* POSIX says that a tag must be both an id and a C union member, but
137 historically almost any character is allowed in a tag. We disallow
138 NUL, as this simplifies our implementation. We disallow angle
139 bracket to match them in nested pairs: several languages use them
140 for generics/template types. */
141tag [^\0<>]+
142
143/* Zero or more instances of backslash-newline. Following GCC, allow
144 white space between the backslash and the newline. */
145splice (\\[ \f\t\v]*\n)*
146
147/* An equal sign, with optional leading whitespaces. This is used in some
148 deprecated constructs. */
149eqopt ([[:space:]]*=)?
150
151%%
152%{
153 /* Nesting level. Either for nested braces, or nested angle brackets
154 (but not mixed). */
155 int nesting PACIFY_CC (= 0);
156
157 /* Parent context state, when applicable. */
158 int context_state PACIFY_CC (= 0);
159
160 /* Location of most recent identifier, when applicable. */
161 location id_loc PACIFY_CC (= empty_location);
162
163 /* Where containing code started, when applicable. Its initial
164 value is relevant only when yylex is invoked in the SC_EPILOGUE
165 start condition. */
166 boundary code_start = scanner_cursor;
167
168 /* Where containing comment or string or character literal started,
169 when applicable. */
170 boundary token_start PACIFY_CC (= scanner_cursor);
171%}
172
173
174 /*-----------------------.
175 | Scanning white space. |
176 `-----------------------*/
177
178<INITIAL,SC_AFTER_IDENTIFIER,SC_BRACKETED_ID,SC_RETURN_BRACKETED_ID>
179{
180 /* Comments and white space. */
181 "," {
182 complain (loc, Wother, _("stray ',' treated as white space"));
183 }
184 [ \f\n\t\v] |
185 "//".* continue;
186 "/*" {
187 token_start = loc->start;
188 context_state = YY_START;
189 BEGIN SC_YACC_COMMENT;
190 }
191
192 /* #line directives are not documented, and may be withdrawn or
193 modified in future versions of Bison. */
194 ^"#line "{int}(" \"".*"\"")?"\n" {
195 handle_syncline (yytext + sizeof "#line " - 1, *loc);
196 }
197}
198
199
200 /*----------------------------.
201 | Scanning Bison directives. |
202 `----------------------------*/
203
204 /* For directives that are also command line options, the regex must be
205 "%..."
206 after "[-_]"s are removed, and the directive must match the --long
207 option name, with a single string argument. Otherwise, add exceptions
208 to ../build-aux/cross-options.pl. */
209
210<INITIAL>
211{
212 "%binary" return PERCENT_NONASSOC;
213 "%code" return PERCENT_CODE;
214 "%debug" RETURN_PERCENT_FLAG("parse.trace");
215 "%default-prec" return PERCENT_DEFAULT_PREC;
216 "%define" return PERCENT_DEFINE;
217 "%defines" return PERCENT_DEFINES;
218 "%destructor" return PERCENT_DESTRUCTOR;
219 "%dprec" return PERCENT_DPREC;
220 "%error-verbose" return PERCENT_ERROR_VERBOSE;
221 "%expect" return PERCENT_EXPECT;
222 "%expect-rr" return PERCENT_EXPECT_RR;
223 "%file-prefix" return PERCENT_FILE_PREFIX;
224 "%fixed-output-files" return PERCENT_YACC;
225 "%initial-action" return PERCENT_INITIAL_ACTION;
226 "%glr-parser" return PERCENT_GLR_PARSER;
227 "%language" return PERCENT_LANGUAGE;
228 "%left" return PERCENT_LEFT;
229 "%lex-param" RETURN_PERCENT_PARAM(lex);
230 "%locations" RETURN_PERCENT_FLAG("locations");
231 "%merge" return PERCENT_MERGE;
232 "%name-prefix" return PERCENT_NAME_PREFIX;
233 "%no-default-prec" return PERCENT_NO_DEFAULT_PREC;
234 "%no-lines" return PERCENT_NO_LINES;
235 "%nonassoc" return PERCENT_NONASSOC;
236 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
237 "%nterm" return PERCENT_NTERM;
238 "%output" return PERCENT_OUTPUT;
239 "%param" RETURN_PERCENT_PARAM(both);
240 "%parse-param" RETURN_PERCENT_PARAM(parse);
241 "%prec" return PERCENT_PREC;
242 "%precedence" return PERCENT_PRECEDENCE;
243 "%printer" return PERCENT_PRINTER;
244 "%pure-parser" RETURN_PERCENT_FLAG("api.pure");
245 "%require" return PERCENT_REQUIRE;
246 "%right" return PERCENT_RIGHT;
247 "%skeleton" return PERCENT_SKELETON;
248 "%start" return PERCENT_START;
249 "%term" return PERCENT_TOKEN;
250 "%token" return PERCENT_TOKEN;
251 "%token-table" return PERCENT_TOKEN_TABLE;
252 "%type" return PERCENT_TYPE;
253 "%union" return PERCENT_UNION;
254 "%verbose" return PERCENT_VERBOSE;
255 "%yacc" return PERCENT_YACC;
256
257 /* deprecated */
258 "%default"[-_]"prec" DEPRECATED("%default-prec");
259 "%error"[-_]"verbose" DEPRECATED("%define parse.error verbose");
260 "%expect"[-_]"rr" DEPRECATED("%expect-rr");
261 "%file-prefix"{eqopt} DEPRECATED("%file-prefix");
262 "%fixed"[-_]"output"[-_]"files" DEPRECATED("%fixed-output-files");
263 "%name"[-_]"prefix"{eqopt} DEPRECATED("%name-prefix");
264 "%no"[-_]"default"[-_]"prec" DEPRECATED("%no-default-prec");
265 "%no"[-_]"lines" DEPRECATED("%no-lines");
266 "%output"{eqopt} DEPRECATED("%output");
267 "%pure"[-_]"parser" DEPRECATED("%pure-parser");
268 "%token"[-_]"table" DEPRECATED("%token-table");
269
270 "%"{id}|"%"{notletter}([[:graph:]])+ {
271 complain (loc, complaint, _("invalid directive: %s"), quote (yytext));
272 }
273
274 "=" return EQUAL;
275 "|" return PIPE;
276 ";" return SEMICOLON;
277
278 {id} {
279 val->uniqstr = uniqstr_new (yytext);
280 id_loc = *loc;
281 bracketed_id_str = NULL;
282 BEGIN SC_AFTER_IDENTIFIER;
283 }
284
285 {int} {
286 val->integer = scan_integer (yytext, 10, *loc);
287 return INT;
288 }
289 0[xX][0-9abcdefABCDEF]+ {
290 val->integer = scan_integer (yytext, 16, *loc);
291 return INT;
292 }
293
294 /* Identifiers may not start with a digit. Yet, don't silently
295 accept "1FOO" as "1 FOO". */
296 {int}{id} {
297 complain (loc, complaint, _("invalid identifier: %s"), quote (yytext));
298 }
299
300 /* Characters. */
301 "'" token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
302
303 /* Strings. */
304 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
305
306 /* Prologue. */
307 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
308
309 /* Code in between braces. */
310 "{" {
311 STRING_GROW;
312 nesting = 0;
313 code_start = loc->start;
314 BEGIN SC_BRACED_CODE;
315 }
316
317 /* Semantic predicate. */
318 "%?"[ \f\n\t\v]*"{" {
319 nesting = 0;
320 code_start = loc->start;
321 BEGIN SC_PREDICATE;
322 }
323
324 /* A type. */
325 "<*>" return TAG_ANY;
326 "<>" return TAG_NONE;
327 "<"{tag}">" {
328 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
329 STRING_FINISH;
330 val->uniqstr = uniqstr_new (last_string);
331 STRING_FREE;
332 return TAG;
333 }
334 "<" {
335 nesting = 0;
336 token_start = loc->start;
337 BEGIN SC_TAG;
338 }
339
340 "%%" {
341 static int percent_percent_count;
342 if (++percent_percent_count == 2)
343 BEGIN SC_EPILOGUE;
344 return PERCENT_PERCENT;
345 }
346
347 "[" {
348 bracketed_id_str = NULL;
349 bracketed_id_start = loc->start;
350 bracketed_id_context_state = YY_START;
351 BEGIN SC_BRACKETED_ID;
352 }
353
354 [^\[%A-Za-z0-9_<>{}\"\'*;|=/, \f\n\t\v]+|. {
355 complain (loc, complaint, "%s: %s",
356 ngettext ("invalid character", "invalid characters", yyleng),
357 quote_mem (yytext, yyleng));
358 }
359
360 <<EOF>> {
361 loc->start = loc->end = scanner_cursor;
362 yyterminate ();
363 }
364}
365
366
367 /*--------------------------------------------------------------.
368 | Supporting \0 complexifies our implementation for no expected |
369 | added value. |
370 `--------------------------------------------------------------*/
371
372<SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING,SC_TAG>
373{
374 \0 complain (loc, complaint, _("invalid null character"));
375}
376
377
378 /*-----------------------------------------------------------------.
379 | Scanning after an identifier, checking whether a colon is next. |
380 `-----------------------------------------------------------------*/
381
382<SC_AFTER_IDENTIFIER>
383{
384 "[" {
385 if (bracketed_id_str)
386 {
387 ROLLBACK_CURRENT_TOKEN;
388 BEGIN SC_RETURN_BRACKETED_ID;
389 *loc = id_loc;
390 return ID;
391 }
392 else
393 {
394 bracketed_id_start = loc->start;
395 bracketed_id_context_state = YY_START;
396 BEGIN SC_BRACKETED_ID;
397 }
398 }
399 ":" {
400 BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
401 *loc = id_loc;
402 return ID_COLON;
403 }
404 . {
405 ROLLBACK_CURRENT_TOKEN;
406 BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
407 *loc = id_loc;
408 return ID;
409 }
410 <<EOF>> {
411 BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
412 *loc = id_loc;
413 return ID;
414 }
415}
416
417 /*--------------------------------.
418 | Scanning bracketed identifiers. |
419 `--------------------------------*/
420
421<SC_BRACKETED_ID>
422{
423 {id} {
424 if (bracketed_id_str)
425 {
426 complain (loc, complaint,
427 _("unexpected identifier in bracketed name: %s"),
428 quote (yytext));
429 }
430 else
431 {
432 bracketed_id_str = uniqstr_new (yytext);
433 bracketed_id_loc = *loc;
434 }
435 }
436 "]" {
437 BEGIN bracketed_id_context_state;
438 if (bracketed_id_str)
439 {
440 if (INITIAL == bracketed_id_context_state)
441 {
442 val->uniqstr = bracketed_id_str;
443 bracketed_id_str = 0;
444 *loc = bracketed_id_loc;
445 return BRACKETED_ID;
446 }
447 }
448 else
449 complain (loc, complaint, _("an identifier expected"));
450 }
451
452 [^\].A-Za-z0-9_/ \f\n\t\v]+|. {
453 complain (loc, complaint, "%s: %s",
454 ngettext ("invalid character in bracketed name",
455 "invalid characters in bracketed name", yyleng),
456 quote_mem (yytext, yyleng));
457 }
458
459 <<EOF>> {
460 BEGIN bracketed_id_context_state;
461 unexpected_eof (bracketed_id_start, "]");
462 }
463}
464
465<SC_RETURN_BRACKETED_ID>
466{
467 . {
468 ROLLBACK_CURRENT_TOKEN;
469 val->uniqstr = bracketed_id_str;
470 bracketed_id_str = 0;
471 *loc = bracketed_id_loc;
472 BEGIN INITIAL;
473 return BRACKETED_ID;
474 }
475}
476
477
478 /*---------------------------------------------------------------.
479 | Scanning a Yacc comment. The initial '/ *' is already eaten. |
480 `---------------------------------------------------------------*/
481
482<SC_YACC_COMMENT>
483{
484 "*/" BEGIN context_state;
485 .|\n continue;
486 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
487}
488
489
490 /*------------------------------------------------------------.
491 | Scanning a C comment. The initial '/ *' is already eaten. |
492 `------------------------------------------------------------*/
493
494<SC_COMMENT>
495{
496 "*"{splice}"/" STRING_GROW; BEGIN context_state;
497 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
498}
499
500
501 /*--------------------------------------------------------------.
502 | Scanning a line comment. The initial '//' is already eaten. |
503 `--------------------------------------------------------------*/
504
505<SC_LINE_COMMENT>
506{
507 "\n" STRING_GROW; BEGIN context_state;
508 {splice} STRING_GROW;
509 <<EOF>> BEGIN context_state;
510}
511
512
513 /*------------------------------------------------.
514 | Scanning a Bison string, including its escapes. |
515 | The initial quote is already eaten. |
516 `------------------------------------------------*/
517
518<SC_ESCAPED_STRING>
519{
520 "\"" {
521 STRING_FINISH;
522 loc->start = token_start;
523 val->chars = last_string;
524 BEGIN INITIAL;
525 return STRING;
526 }
527 <<EOF>> unexpected_eof (token_start, "\"");
528 "\n" unexpected_newline (token_start, "\"");
529}
530
531 /*----------------------------------------------------------.
532 | Scanning a Bison character literal, decoding its escapes. |
533 | The initial quote is already eaten. |
534 `----------------------------------------------------------*/
535
536<SC_ESCAPED_CHARACTER>
537{
538 "'" {
539 STRING_FINISH;
540 loc->start = token_start;
541 val->character = last_string[0];
542
543 /* FIXME: Eventually, make these errors. */
544 if (last_string[0] == '\0')
545 {
546 complain (loc, Wother, _("empty character literal"));
547 /* '\0' seems dangerous even if we are about to complain. */
548 val->character = '\'';
549 }
550 else if (last_string[1] != '\0')
551 complain (loc, Wother,
552 _("extra characters in character literal"));
553 STRING_FREE;
554 BEGIN INITIAL;
555 return CHAR;
556 }
557 "\n" unexpected_newline (token_start, "'");
558 <<EOF>> unexpected_eof (token_start, "'");
559}
560
561 /*-----------------------------------------------------------.
562 | Scanning a Bison nested tag. The initial angle bracket is |
563 | already eaten. |
564 `-----------------------------------------------------------*/
565
566<SC_TAG>
567{
568 ">" {
569 --nesting;
570 if (nesting < 0)
571 {
572 STRING_FINISH;
573 loc->start = token_start;
574 val->uniqstr = uniqstr_new (last_string);
575 STRING_FREE;
576 BEGIN INITIAL;
577 return TAG;
578 }
579 STRING_GROW;
580 }
581
582 [^<>]+ STRING_GROW;
583 "<"+ STRING_GROW; nesting += yyleng;
584
585 <<EOF>> unexpected_eof (token_start, ">");
586}
587
588 /*----------------------------.
589 | Decode escaped characters. |
590 `----------------------------*/
591
592<SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
593{
594 \\[0-7]{1,3} {
595 unsigned long int c = strtoul (yytext + 1, NULL, 8);
596 if (!c || UCHAR_MAX < c)
597 complain (loc, complaint, _("invalid number after \\-escape: %s"),
598 yytext+1);
599 else
600 obstack_1grow (&obstack_for_string, c);
601 }
602
603 \\x[0-9abcdefABCDEF]+ {
604 verify (UCHAR_MAX < ULONG_MAX);
605 unsigned long int c = strtoul (yytext + 2, NULL, 16);
606 if (!c || UCHAR_MAX < c)
607 complain (loc, complaint, _("invalid number after \\-escape: %s"),
608 yytext+1);
609 else
610 obstack_1grow (&obstack_for_string, c);
611 }
612
613 \\a obstack_1grow (&obstack_for_string, '\a');
614 \\b obstack_1grow (&obstack_for_string, '\b');
615 \\f obstack_1grow (&obstack_for_string, '\f');
616 \\n obstack_1grow (&obstack_for_string, '\n');
617 \\r obstack_1grow (&obstack_for_string, '\r');
618 \\t obstack_1grow (&obstack_for_string, '\t');
619 \\v obstack_1grow (&obstack_for_string, '\v');
620
621 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
622 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
623
624 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
625 int c = convert_ucn_to_byte (yytext);
626 if (c <= 0)
627 complain (loc, complaint, _("invalid number after \\-escape: %s"),
628 yytext+1);
629 else
630 obstack_1grow (&obstack_for_string, c);
631 }
632 \\(.|\n) {
633 char const *p = yytext + 1;
634 /* Quote only if escaping won't make the character visible. */
635 if (c_isspace ((unsigned char) *p) && c_isprint ((unsigned char) *p))
636 p = quote (p);
637 else
638 p = quotearg_style_mem (escape_quoting_style, p, 1);
639 complain (loc, complaint, _("invalid character after \\-escape: %s"),
640 p);
641 }
642}
643
644 /*--------------------------------------------.
645 | Scanning user-code characters and strings. |
646 `--------------------------------------------*/
647
648<SC_CHARACTER,SC_STRING>
649{
650 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
651}
652
653<SC_CHARACTER>
654{
655 "'" STRING_GROW; BEGIN context_state;
656 \n unexpected_newline (token_start, "'");
657 <<EOF>> unexpected_eof (token_start, "'");
658}
659
660<SC_STRING>
661{
662 "\"" STRING_GROW; BEGIN context_state;
663 \n unexpected_newline (token_start, "\"");
664 <<EOF>> unexpected_eof (token_start, "\"");
665}
666
667
668 /*---------------------------------------------------.
669 | Strings, comments etc. can be found in user code. |
670 `---------------------------------------------------*/
671
672<SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_PREDICATE>
673{
674 "'" {
675 STRING_GROW;
676 context_state = YY_START;
677 token_start = loc->start;
678 BEGIN SC_CHARACTER;
679 }
680 "\"" {
681 STRING_GROW;
682 context_state = YY_START;
683 token_start = loc->start;
684 BEGIN SC_STRING;
685 }
686 "/"{splice}"*" {
687 STRING_GROW;
688 context_state = YY_START;
689 token_start = loc->start;
690 BEGIN SC_COMMENT;
691 }
692 "/"{splice}"/" {
693 STRING_GROW;
694 context_state = YY_START;
695 BEGIN SC_LINE_COMMENT;
696 }
697}
698
699
700
701 /*-----------------------------------------------------------.
702 | Scanning some code in braces (actions, predicates). The |
703 | initial "{" is already eaten. |
704 `-----------------------------------------------------------*/
705
706<SC_BRACED_CODE,SC_PREDICATE>
707{
708 "{"|"<"{splice}"%" STRING_GROW; nesting++;
709 "%"{splice}">" STRING_GROW; nesting--;
710
711 /* Tokenize '<<%' correctly (as '<<' '%') rather than incorrrectly
712 (as '<' '<%'). */
713 "<"{splice}"<" STRING_GROW;
714
715 <<EOF>> unexpected_eof (code_start, "}");
716}
717
718<SC_BRACED_CODE>
719{
720 "}" {
721 obstack_1grow (&obstack_for_string, '}');
722
723 --nesting;
724 if (nesting < 0)
725 {
726 STRING_FINISH;
727 loc->start = code_start;
728 val->code = last_string;
729 BEGIN INITIAL;
730 return BRACED_CODE;
731 }
732 }
733}
734
735<SC_PREDICATE>
736{
737 "}" {
738 --nesting;
739 if (nesting < 0)
740 {
741 STRING_FINISH;
742 loc->start = code_start;
743 val->code = last_string;
744 BEGIN INITIAL;
745 return BRACED_PREDICATE;
746 }
747 else
748 obstack_1grow (&obstack_for_string, '}');
749 }
750}
751
752 /*--------------------------------------------------------------.
753 | Scanning some prologue: from "%{" (already scanned) to "%}". |
754 `--------------------------------------------------------------*/
755
756<SC_PROLOGUE>
757{
758 "%}" {
759 STRING_FINISH;
760 loc->start = code_start;
761 val->chars = last_string;
762 BEGIN INITIAL;
763 return PROLOGUE;
764 }
765
766 <<EOF>> unexpected_eof (code_start, "%}");
767}
768
769
770 /*---------------------------------------------------------------.
771 | Scanning the epilogue (everything after the second "%%", which |
772 | has already been eaten). |
773 `---------------------------------------------------------------*/
774
775<SC_EPILOGUE>
776{
777 <<EOF>> {
778 STRING_FINISH;
779 loc->start = code_start;
780 val->chars = last_string;
781 BEGIN INITIAL;
782 return EPILOGUE;
783 }
784}
785
786
787 /*-----------------------------------------------------.
788 | By default, grow the string obstack with the input. |
789 `-----------------------------------------------------*/
790
791<SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PREDICATE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
792 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PREDICATE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
793
794%%
795
796/* Read bytes from FP into buffer BUF of size SIZE. Return the
797 number of bytes read. Remove '\r' from input, treating \r\n
798 and isolated \r as \n. */
799
800static size_t
801no_cr_read (FILE *fp, char *buf, size_t size)
802{
803 size_t bytes_read = fread (buf, 1, size, fp);
804 if (bytes_read)
805 {
806 char *w = memchr (buf, '\r', bytes_read);
807 if (w)
808 {
809 char const *r = ++w;
810 char const *lim = buf + bytes_read;
811
812 for (;;)
813 {
814 /* Found an '\r'. Treat it like '\n', but ignore any
815 '\n' that immediately follows. */
816 w[-1] = '\n';
817 if (r == lim)
818 {
819 int ch = getc (fp);
820 if (ch != '\n' && ungetc (ch, fp) != ch)
821 break;
822 }
823 else if (*r == '\n')
824 r++;
825
826 /* Copy until the next '\r'. */
827 do
828 {
829 if (r == lim)
830 return w - buf;
831 }
832 while ((*w++ = *r++) != '\r');
833 }
834
835 return w - buf;
836 }
837 }
838
839 return bytes_read;
840}
841
842
843
844/*------------------------------------------------------.
845| Scan NUMBER for a base-BASE integer at location LOC. |
846`------------------------------------------------------*/
847
848static unsigned long int
849scan_integer (char const *number, int base, location loc)
850{
851 verify (INT_MAX < ULONG_MAX);
852 unsigned long int num = strtoul (number, NULL, base);
853
854 if (INT_MAX < num)
855 {
856 complain (&loc, complaint, _("integer out of range: %s"),
857 quote (number));
858 num = INT_MAX;
859 }
860
861 return num;
862}
863
864
865/*------------------------------------------------------------------.
866| Convert universal character name UCN to a single-byte character, |
867| and return that character. Return -1 if UCN does not correspond |
868| to a single-byte character. |
869`------------------------------------------------------------------*/
870
871static int
872convert_ucn_to_byte (char const *ucn)
873{
874 verify (UCHAR_MAX <= INT_MAX);
875 unsigned long int code = strtoul (ucn + 2, NULL, 16);
876
877 /* FIXME: Currently we assume Unicode-compatible unibyte characters
878 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
879 non-ASCII hosts we support only the portable C character set.
880 These limitations should be removed once we add support for
881 multibyte characters. */
882
883 if (UCHAR_MAX < code)
884 return -1;
885
886#if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
887 {
888 /* A non-ASCII host. Use CODE to index into a table of the C
889 basic execution character set, which is guaranteed to exist on
890 all Standard C platforms. This table also includes '$', '@',
891 and '`', which are not in the basic execution character set but
892 which are unibyte characters on all the platforms that we know
893 about. */
894 static signed char const table[] =
895 {
896 '\0', -1, -1, -1, -1, -1, -1, '\a',
897 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
898 -1, -1, -1, -1, -1, -1, -1, -1,
899 -1, -1, -1, -1, -1, -1, -1, -1,
900 ' ', '!', '"', '#', '$', '%', '&', '\'',
901 '(', ')', '*', '+', ',', '-', '.', '/',
902 '0', '1', '2', '3', '4', '5', '6', '7',
903 '8', '9', ':', ';', '<', '=', '>', '?',
904 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
905 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
906 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
907 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
908 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
909 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
910 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
911 'x', 'y', 'z', '{', '|', '}', '~'
912 };
913
914 code = code < sizeof table ? table[code] : -1;
915 }
916#endif
917
918 return code;
919}
920
921
922/*---------------------------------------------------------------------.
923| Handle '#line INT( "FILE")?\n'. ARGS has already skipped '#line '. |
924`---------------------------------------------------------------------*/
925
926static void
927handle_syncline (char *args, location loc)
928{
929 char *file;
930 unsigned long int lineno = strtoul (args, &file, 10);
931 if (INT_MAX <= lineno)
932 {
933 complain (&loc, Wother, _("line number overflow"));
934 lineno = INT_MAX;
935 }
936
937 file = strchr (file, '"');
938 if (file)
939 {
940 *strchr (file + 1, '"') = '\0';
941 current_file = uniqstr_new (file + 1);
942 }
943 boundary_set (&scanner_cursor, current_file, lineno, 1);
944}
945
946
947/*----------------------------------------------------------------.
948| For a token or comment starting at START, report message MSGID, |
949| which should say that an end marker was found before the |
950| expected TOKEN_END. Then, pretend that TOKEN_END was found. |
951`----------------------------------------------------------------*/
952
953static void
954unexpected_end (boundary start, char const *msgid, char const *token_end)
955{
956 location loc;
957 loc.start = start;
958 loc.end = scanner_cursor;
959 size_t i = strlen (token_end);
960
961/* Adjust scanner cursor so that any later message does not count
962 the characters about to be inserted. */
963 scanner_cursor.column -= i;
964
965 while (i != 0)
966 unput (token_end[--i]);
967
968 token_end = quote (token_end);
969 /* Instead of '\'', display "'". */
970 if (STREQ (token_end, "'\\''"))
971 token_end = "\"'\"";
972 complain (&loc, complaint, _(msgid), token_end);
973}
974
975
976/*------------------------------------------------------------------------.
977| Report an unexpected EOF in a token or comment starting at START. |
978| An end of file was encountered and the expected TOKEN_END was missing. |
979| After reporting the problem, pretend that TOKEN_END was found. |
980`------------------------------------------------------------------------*/
981
982static void
983unexpected_eof (boundary start, char const *token_end)
984{
985 unexpected_end (start, N_("missing %s at end of file"), token_end);
986}
987
988
989/*----------------------------------------.
990| Likewise, but for unexpected newlines. |
991`----------------------------------------*/
992
993static void
994unexpected_newline (boundary start, char const *token_end)
995{
996 unexpected_end (start, N_("missing %s at end of line"), token_end);
997}
998
999
1000/*-------------------------.
1001| Initialize the scanner. |
1002`-------------------------*/
1003
1004void
1005gram_scanner_initialize (void)
1006{
1007 obstack_init (&obstack_for_string);
1008}
1009
1010
1011/*-----------------------------------------------.
1012| Free all the memory allocated to the scanner. |
1013`-----------------------------------------------*/
1014
1015void
1016gram_scanner_free (void)
1017{
1018 obstack_free (&obstack_for_string, 0);
1019 /* Reclaim Flex's buffers. */
1020 yylex_destroy ();
1021}