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