]> git.saurik.com Git - bison.git/blob - src/scan-gram.l
NEWS: Internationalization.
[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 /* Quote only if escaping won't make the character visible. */
621 if (isspace ((unsigned char) *p) && isprint ((unsigned char) *p))
622 p = quote (p);
623 else
624 p = quotearg_style_mem (escape_quoting_style, p, 1);
625 complain_at (*loc, _("invalid character after \\-escape: %s"), p);
626 }
627 }
628
629 /*--------------------------------------------.
630 | Scanning user-code characters and strings. |
631 `--------------------------------------------*/
632
633 <SC_CHARACTER,SC_STRING>
634 {
635 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
636 }
637
638 <SC_CHARACTER>
639 {
640 "'" STRING_GROW; BEGIN context_state;
641 \n unexpected_newline (token_start, "'"); BEGIN context_state;
642 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
643 }
644
645 <SC_STRING>
646 {
647 "\"" STRING_GROW; BEGIN context_state;
648 \n unexpected_newline (token_start, "\""); BEGIN context_state;
649 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
650 }
651
652
653 /*---------------------------------------------------.
654 | Strings, comments etc. can be found in user code. |
655 `---------------------------------------------------*/
656
657 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
658 {
659 "'" {
660 STRING_GROW;
661 context_state = YY_START;
662 token_start = loc->start;
663 BEGIN SC_CHARACTER;
664 }
665 "\"" {
666 STRING_GROW;
667 context_state = YY_START;
668 token_start = loc->start;
669 BEGIN SC_STRING;
670 }
671 "/"{splice}"*" {
672 STRING_GROW;
673 context_state = YY_START;
674 token_start = loc->start;
675 BEGIN SC_COMMENT;
676 }
677 "/"{splice}"/" {
678 STRING_GROW;
679 context_state = YY_START;
680 BEGIN SC_LINE_COMMENT;
681 }
682 }
683
684
685
686 /*-----------------------------------------------------------.
687 | Scanning some code in braces (actions). The initial "{" is |
688 | already eaten. |
689 `-----------------------------------------------------------*/
690
691 <SC_BRACED_CODE>
692 {
693 "{"|"<"{splice}"%" STRING_GROW; nesting++;
694 "%"{splice}">" STRING_GROW; nesting--;
695 "}" {
696 obstack_1grow (&obstack_for_string, '}');
697
698 --nesting;
699 if (nesting < 0)
700 {
701 STRING_FINISH;
702 loc->start = code_start;
703 val->code = last_string;
704 BEGIN INITIAL;
705 return BRACED_CODE;
706 }
707 }
708
709 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
710 (as `<' `<%'). */
711 "<"{splice}"<" STRING_GROW;
712
713 <<EOF>> {
714 unexpected_eof (code_start, "}");
715 STRING_FINISH;
716 loc->start = code_start;
717 val->code = last_string;
718 BEGIN INITIAL;
719 return BRACED_CODE;
720 }
721 }
722
723
724 /*--------------------------------------------------------------.
725 | Scanning some prologue: from "%{" (already scanned) to "%}". |
726 `--------------------------------------------------------------*/
727
728 <SC_PROLOGUE>
729 {
730 "%}" {
731 STRING_FINISH;
732 loc->start = code_start;
733 val->chars = last_string;
734 BEGIN INITIAL;
735 return PROLOGUE;
736 }
737
738 <<EOF>> {
739 unexpected_eof (code_start, "%}");
740 STRING_FINISH;
741 loc->start = code_start;
742 val->chars = last_string;
743 BEGIN INITIAL;
744 return PROLOGUE;
745 }
746 }
747
748
749 /*---------------------------------------------------------------.
750 | Scanning the epilogue (everything after the second "%%", which |
751 | has already been eaten). |
752 `---------------------------------------------------------------*/
753
754 <SC_EPILOGUE>
755 {
756 <<EOF>> {
757 STRING_FINISH;
758 loc->start = code_start;
759 val->chars = last_string;
760 BEGIN INITIAL;
761 return EPILOGUE;
762 }
763 }
764
765
766 /*-----------------------------------------------------.
767 | By default, grow the string obstack with the input. |
768 `-----------------------------------------------------*/
769
770 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
771 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
772
773 %%
774
775 /* Read bytes from FP into buffer BUF of size SIZE. Return the
776 number of bytes read. Remove '\r' from input, treating \r\n
777 and isolated \r as \n. */
778
779 static size_t
780 no_cr_read (FILE *fp, char *buf, size_t size)
781 {
782 size_t bytes_read = fread (buf, 1, size, fp);
783 if (bytes_read)
784 {
785 char *w = memchr (buf, '\r', bytes_read);
786 if (w)
787 {
788 char const *r = ++w;
789 char const *lim = buf + bytes_read;
790
791 for (;;)
792 {
793 /* Found an '\r'. Treat it like '\n', but ignore any
794 '\n' that immediately follows. */
795 w[-1] = '\n';
796 if (r == lim)
797 {
798 int ch = getc (fp);
799 if (ch != '\n' && ungetc (ch, fp) != ch)
800 break;
801 }
802 else if (*r == '\n')
803 r++;
804
805 /* Copy until the next '\r'. */
806 do
807 {
808 if (r == lim)
809 return w - buf;
810 }
811 while ((*w++ = *r++) != '\r');
812 }
813
814 return w - buf;
815 }
816 }
817
818 return bytes_read;
819 }
820
821
822
823 /*------------------------------------------------------.
824 | Scan NUMBER for a base-BASE integer at location LOC. |
825 `------------------------------------------------------*/
826
827 static unsigned long int
828 scan_integer (char const *number, int base, location loc)
829 {
830 verify (INT_MAX < ULONG_MAX);
831 unsigned long int num = strtoul (number, NULL, base);
832
833 if (INT_MAX < num)
834 {
835 complain_at (loc, _("integer out of range: %s"), quote (number));
836 num = INT_MAX;
837 }
838
839 return num;
840 }
841
842
843 /*------------------------------------------------------------------.
844 | Convert universal character name UCN to a single-byte character, |
845 | and return that character. Return -1 if UCN does not correspond |
846 | to a single-byte character. |
847 `------------------------------------------------------------------*/
848
849 static int
850 convert_ucn_to_byte (char const *ucn)
851 {
852 verify (UCHAR_MAX <= INT_MAX);
853 unsigned long int code = strtoul (ucn + 2, NULL, 16);
854
855 /* FIXME: Currently we assume Unicode-compatible unibyte characters
856 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
857 non-ASCII hosts we support only the portable C character set.
858 These limitations should be removed once we add support for
859 multibyte characters. */
860
861 if (UCHAR_MAX < code)
862 return -1;
863
864 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
865 {
866 /* A non-ASCII host. Use CODE to index into a table of the C
867 basic execution character set, which is guaranteed to exist on
868 all Standard C platforms. This table also includes '$', '@',
869 and '`', which are not in the basic execution character set but
870 which are unibyte characters on all the platforms that we know
871 about. */
872 static signed char const table[] =
873 {
874 '\0', -1, -1, -1, -1, -1, -1, '\a',
875 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
876 -1, -1, -1, -1, -1, -1, -1, -1,
877 -1, -1, -1, -1, -1, -1, -1, -1,
878 ' ', '!', '"', '#', '$', '%', '&', '\'',
879 '(', ')', '*', '+', ',', '-', '.', '/',
880 '0', '1', '2', '3', '4', '5', '6', '7',
881 '8', '9', ':', ';', '<', '=', '>', '?',
882 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
883 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
884 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
885 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
886 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
887 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
888 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
889 'x', 'y', 'z', '{', '|', '}', '~'
890 };
891
892 code = code < sizeof table ? table[code] : -1;
893 }
894 #endif
895
896 return code;
897 }
898
899
900 /*----------------------------------------------------------------.
901 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
902 `----------------------------------------------------------------*/
903
904 static void
905 handle_syncline (char *args, location loc)
906 {
907 char *after_num;
908 unsigned long int lineno = strtoul (args, &after_num, 10);
909 char *file = strchr (after_num, '"') + 1;
910 *strchr (file, '"') = '\0';
911 if (INT_MAX <= lineno)
912 {
913 warn_at (loc, _("line number overflow"));
914 lineno = INT_MAX;
915 }
916 current_file = uniqstr_new (file);
917 boundary_set (&scanner_cursor, current_file, lineno, 1);
918 }
919
920
921 /*----------------------------------------------------------------.
922 | For a token or comment starting at START, report message MSGID, |
923 | which should say that an end marker was found before |
924 | the expected TOKEN_END. |
925 `----------------------------------------------------------------*/
926
927 static void
928 unexpected_end (boundary start, char const *msgid, char const *token_end)
929 {
930 location loc;
931 loc.start = start;
932 loc.end = scanner_cursor;
933 complain_at (loc, _(msgid), token_end);
934 }
935
936
937 /*------------------------------------------------------------------------.
938 | Report an unexpected EOF in a token or comment starting at START. |
939 | An end of file was encountered and the expected TOKEN_END was missing. |
940 `------------------------------------------------------------------------*/
941
942 static void
943 unexpected_eof (boundary start, char const *token_end)
944 {
945 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
946 }
947
948
949 /*----------------------------------------.
950 | Likewise, but for unexpected newlines. |
951 `----------------------------------------*/
952
953 static void
954 unexpected_newline (boundary start, char const *token_end)
955 {
956 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
957 }
958
959
960 /*-------------------------.
961 | Initialize the scanner. |
962 `-------------------------*/
963
964 void
965 gram_scanner_initialize (void)
966 {
967 obstack_init (&obstack_for_string);
968 }
969
970
971 /*-----------------------------------------------.
972 | Free all the memory allocated to the scanner. |
973 `-----------------------------------------------*/
974
975 void
976 gram_scanner_free (void)
977 {
978 obstack_free (&obstack_for_string, 0);
979 /* Reclaim Flex's buffers. */
980 yylex_destroy ();
981 }