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