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