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