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