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