]> git.saurik.com Git - bison.git/blame - src/scan-gram.l
2007-01-30 Paolo Bonzini <bonzini@gnu.org>
[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;
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;
190 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
7172e23e
JD
191 "%push-parser" return PERCENT_PUSH_PARSER;
192 "%push-pull-parser" return PERCENT_PUSH_PULL_PARSER;
58d7a1a1
AD
193 "%require" return PERCENT_REQUIRE;
194 "%right" return PERCENT_RIGHT;
195 "%skeleton" return PERCENT_SKELETON;
196 "%start" return PERCENT_START;
197 "%term" return PERCENT_TOKEN;
198 "%token" return PERCENT_TOKEN;
199 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
200 "%type" return PERCENT_TYPE;
201 "%union" return PERCENT_UNION;
202 "%verbose" return PERCENT_VERBOSE;
203 "%yacc" return PERCENT_YACC;
e9955c83 204
3f2d73f1 205 {directive} {
41141c56 206 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
412f8a59 207 }
900c5db5 208
e9955c83 209 "=" return EQUAL;
e9071366 210 "|" return PIPE;
e9955c83 211 ";" return SEMICOLON;
12e35840 212 "<*>" return TYPE_TAG_ANY;
3ebecc24 213 "<>" return TYPE_TAG_NONE;
e9955c83 214
3f2d73f1 215 {id} {
58d7a1a1 216 val->uniqstr = uniqstr_new (yytext);
3f2d73f1 217 id_loc = *loc;
3f2d73f1 218 BEGIN SC_AFTER_IDENTIFIER;
e9955c83
AD
219 }
220
d8d3f94a 221 {int} {
1452af69
PE
222 val->integer = scan_integer (yytext, 10, *loc);
223 return INT;
224 }
225 0[xX][0-9abcdefABCDEF]+ {
226 val->integer = scan_integer (yytext, 16, *loc);
d8d3f94a
PE
227 return INT;
228 }
e9955c83
AD
229
230 /* Characters. We don't check there is only one. */
3f2d73f1 231 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
e9955c83
AD
232
233 /* Strings. */
ca407bdf 234 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
e9955c83
AD
235
236 /* Prologue. */
3f2d73f1 237 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
e9955c83
AD
238
239 /* Code in between braces. */
3f2d73f1
PE
240 "{" {
241 STRING_GROW;
242 braces_level = 0;
243 code_start = loc->start;
244 BEGIN SC_BRACED_CODE;
245 }
e9955c83
AD
246
247 /* A type. */
d8d3f94a 248 "<"{tag}">" {
223ff46e 249 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
41141c56 250 STRING_FINISH;
223ff46e 251 val->uniqstr = uniqstr_new (last_string);
41141c56 252 STRING_FREE;
4cdb01db
AD
253 return TYPE;
254 }
255
a706a1cc
PE
256 "%%" {
257 static int percent_percent_count;
e9955c83 258 if (++percent_percent_count == 2)
a2bc9dbc 259 BEGIN SC_EPILOGUE;
e9955c83
AD
260 return PERCENT_PERCENT;
261 }
262
a706a1cc 263 . {
41141c56 264 complain_at (*loc, _("invalid character: %s"), quote (yytext));
3f2d73f1 265 }
379f0ac8
PE
266
267 <<EOF>> {
268 loc->start = loc->end = scanner_cursor;
269 yyterminate ();
270 }
3f2d73f1
PE
271}
272
273
274 /*-----------------------------------------------------------------.
275 | Scanning after an identifier, checking whether a colon is next. |
276 `-----------------------------------------------------------------*/
277
278<SC_AFTER_IDENTIFIER>
279{
280 ":" {
3f2d73f1
PE
281 *loc = id_loc;
282 BEGIN INITIAL;
283 return ID_COLON;
284 }
285 . {
286 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0);
287 yyless (0);
288 *loc = id_loc;
289 BEGIN INITIAL;
290 return ID;
291 }
292 <<EOF>> {
293 *loc = id_loc;
294 BEGIN INITIAL;
295 return ID;
e9955c83
AD
296 }
297}
298
299
d8d3f94a
PE
300 /*---------------------------------------------------------------.
301 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
302 `---------------------------------------------------------------*/
e9955c83 303
d8d3f94a 304<SC_YACC_COMMENT>
e9955c83 305{
3f2d73f1 306 "*/" BEGIN context_state;
a706a1cc 307 .|\n ;
aa418041 308 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
d8d3f94a
PE
309}
310
311
312 /*------------------------------------------------------------.
313 | Scanning a C comment. The initial `/ *' is already eaten. |
314 `------------------------------------------------------------*/
315
316<SC_COMMENT>
317{
3f2d73f1 318 "*"{splice}"/" STRING_GROW; BEGIN context_state;
aa418041 319 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
e9955c83
AD
320}
321
322
d8d3f94a
PE
323 /*--------------------------------------------------------------.
324 | Scanning a line comment. The initial `//' is already eaten. |
325 `--------------------------------------------------------------*/
326
327<SC_LINE_COMMENT>
328{
3f2d73f1 329 "\n" STRING_GROW; BEGIN context_state;
41141c56 330 {splice} STRING_GROW;
3f2d73f1 331 <<EOF>> BEGIN context_state;
d8d3f94a
PE
332}
333
334
4febdd96
PE
335 /*------------------------------------------------.
336 | Scanning a Bison string, including its escapes. |
337 | The initial quote is already eaten. |
338 `------------------------------------------------*/
e9955c83
AD
339
340<SC_ESCAPED_STRING>
341{
47aee066
JD
342 "\""|"\n" {
343 if (yytext[0] == '\n')
344 unexpected_newline (token_start, "\"");
345 STRING_FINISH;
346 loc->start = token_start;
347 val->chars = last_string;
348 BEGIN INITIAL;
349 return STRING;
350 }
351 <<EOF>> {
352 unexpected_eof (token_start, "\"");
41141c56 353 STRING_FINISH;
3f2d73f1 354 loc->start = token_start;
223ff46e 355 val->chars = last_string;
a706a1cc 356 BEGIN INITIAL;
e9955c83
AD
357 return STRING;
358 }
e9955c83
AD
359}
360
4febdd96
PE
361 /*----------------------------------------------------------.
362 | Scanning a Bison character literal, decoding its escapes. |
363 | The initial quote is already eaten. |
364 `----------------------------------------------------------*/
e9955c83
AD
365
366<SC_ESCAPED_CHARACTER>
367{
47aee066
JD
368 "'"|"\n" {
369 if (yytext[0] == '\n')
370 unexpected_newline (token_start, "'");
41141c56
PE
371 STRING_GROW;
372 STRING_FINISH;
3f2d73f1 373 loc->start = token_start;
58d7a1a1 374 val->character = last_string[1];
41141c56 375 STRING_FREE;
a706a1cc 376 BEGIN INITIAL;
58d7a1a1 377 return CHAR;
e9955c83 378 }
47aee066
JD
379 <<EOF>> {
380 unexpected_eof (token_start, "'");
381 STRING_FINISH;
382 loc->start = token_start;
383 if (strlen(last_string) > 1)
384 val->character = last_string[1];
385 else
386 val->character = last_string[0];
387 STRING_FREE;
388 BEGIN INITIAL;
389 return CHAR;
390 }
4febdd96 391}
a706a1cc 392
4febdd96
PE
393<SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
394{
92ac3705 395 \0 complain_at (*loc, _("invalid null character"));
e9955c83
AD
396}
397
398
399 /*----------------------------.
400 | Decode escaped characters. |
401 `----------------------------*/
402
403<SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
404{
d8d3f94a 405 \\[0-7]{1,3} {
4517da37 406 unsigned long int c = strtoul (yytext + 1, NULL, 8);
d8d3f94a 407 if (UCHAR_MAX < c)
3f2d73f1 408 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
05ac60f3 409 else if (! c)
92ac3705 410 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
e9955c83 411 else
223ff46e 412 obstack_1grow (&obstack_for_string, c);
e9955c83
AD
413 }
414
6b0d38ab 415 \\x[0-9abcdefABCDEF]+ {
4517da37
PE
416 verify (UCHAR_MAX < ULONG_MAX);
417 unsigned long int c = strtoul (yytext + 2, NULL, 16);
418 if (UCHAR_MAX < c)
3f2d73f1 419 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
92ac3705
PE
420 else if (! c)
421 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
d8d3f94a 422 else
223ff46e 423 obstack_1grow (&obstack_for_string, c);
e9955c83
AD
424 }
425
223ff46e
PE
426 \\a obstack_1grow (&obstack_for_string, '\a');
427 \\b obstack_1grow (&obstack_for_string, '\b');
428 \\f obstack_1grow (&obstack_for_string, '\f');
429 \\n obstack_1grow (&obstack_for_string, '\n');
430 \\r obstack_1grow (&obstack_for_string, '\r');
431 \\t obstack_1grow (&obstack_for_string, '\t');
432 \\v obstack_1grow (&obstack_for_string, '\v');
412f8a59
PE
433
434 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
223ff46e 435 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
412f8a59 436
6b0d38ab 437 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
d8d3f94a
PE
438 int c = convert_ucn_to_byte (yytext);
439 if (c < 0)
3f2d73f1 440 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
92ac3705
PE
441 else if (! c)
442 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
d8d3f94a 443 else
223ff46e 444 obstack_1grow (&obstack_for_string, c);
d8d3f94a 445 }
4f25ebb0 446 \\(.|\n) {
3f2d73f1 447 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
41141c56 448 STRING_GROW;
e9955c83
AD
449 }
450}
451
4febdd96
PE
452 /*--------------------------------------------.
453 | Scanning user-code characters and strings. |
454 `--------------------------------------------*/
e9955c83 455
4febdd96
PE
456<SC_CHARACTER,SC_STRING>
457{
e9071366 458 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
4febdd96 459}
e9955c83
AD
460
461<SC_CHARACTER>
462{
4febdd96
PE
463 "'" STRING_GROW; BEGIN context_state;
464 \n unexpected_newline (token_start, "'"); BEGIN context_state;
465 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
e9955c83
AD
466}
467
e9955c83
AD
468<SC_STRING>
469{
4febdd96
PE
470 "\"" STRING_GROW; BEGIN context_state;
471 \n unexpected_newline (token_start, "\""); BEGIN context_state;
472 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
e9955c83
AD
473}
474
475
476 /*---------------------------------------------------.
477 | Strings, comments etc. can be found in user code. |
478 `---------------------------------------------------*/
479
480<SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
481{
3f2d73f1
PE
482 "'" {
483 STRING_GROW;
484 context_state = YY_START;
485 token_start = loc->start;
486 BEGIN SC_CHARACTER;
487 }
488 "\"" {
489 STRING_GROW;
490 context_state = YY_START;
491 token_start = loc->start;
492 BEGIN SC_STRING;
493 }
494 "/"{splice}"*" {
495 STRING_GROW;
496 context_state = YY_START;
497 token_start = loc->start;
498 BEGIN SC_COMMENT;
499 }
500 "/"{splice}"/" {
501 STRING_GROW;
502 context_state = YY_START;
503 BEGIN SC_LINE_COMMENT;
504 }
e9955c83
AD
505}
506
507
624a35e2 508
58d7a1a1
AD
509 /*-----------------------------------------------------------.
510 | Scanning some code in braces (actions). The initial "{" is |
511 | already eaten. |
512 `-----------------------------------------------------------*/
e9955c83
AD
513
514<SC_BRACED_CODE>
515{
41141c56
PE
516 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
517 "%"{splice}">" STRING_GROW; braces_level--;
e9955c83 518 "}" {
25522739
PE
519 obstack_1grow (&obstack_for_string, '}');
520
2346344a
AD
521 --braces_level;
522 if (braces_level < 0)
e9955c83 523 {
41141c56 524 STRING_FINISH;
3f2d73f1 525 loc->start = code_start;
eb095650 526 val->code = last_string;
a706a1cc 527 BEGIN INITIAL;
58d7a1a1 528 return BRACED_CODE;
e9955c83
AD
529 }
530 }
531
a706a1cc
PE
532 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
533 (as `<' `<%'). */
41141c56 534 "<"{splice}"<" STRING_GROW;
a706a1cc 535
47aee066
JD
536 <<EOF>> {
537 unexpected_eof (code_start, "}");
538 STRING_FINISH;
539 loc->start = code_start;
eb095650 540 val->code = last_string;
47aee066
JD
541 BEGIN INITIAL;
542 return BRACED_CODE;
543 }
e9955c83
AD
544}
545
546
547 /*--------------------------------------------------------------.
548 | Scanning some prologue: from "%{" (already scanned) to "%}". |
549 `--------------------------------------------------------------*/
550
551<SC_PROLOGUE>
552{
553 "%}" {
41141c56 554 STRING_FINISH;
3f2d73f1 555 loc->start = code_start;
223ff46e 556 val->chars = last_string;
a706a1cc 557 BEGIN INITIAL;
e9955c83
AD
558 return PROLOGUE;
559 }
560
47aee066
JD
561 <<EOF>> {
562 unexpected_eof (code_start, "%}");
563 STRING_FINISH;
564 loc->start = code_start;
565 val->chars = last_string;
566 BEGIN INITIAL;
567 return PROLOGUE;
568 }
e9955c83
AD
569}
570
571
572 /*---------------------------------------------------------------.
573 | Scanning the epilogue (everything after the second "%%", which |
d8d3f94a 574 | has already been eaten). |
e9955c83
AD
575 `---------------------------------------------------------------*/
576
577<SC_EPILOGUE>
578{
e9955c83 579 <<EOF>> {
41141c56 580 STRING_FINISH;
3f2d73f1 581 loc->start = code_start;
223ff46e 582 val->chars = last_string;
a706a1cc 583 BEGIN INITIAL;
e9955c83
AD
584 return EPILOGUE;
585 }
586}
587
588
4febdd96
PE
589 /*-----------------------------------------------------.
590 | By default, grow the string obstack with the input. |
591 `-----------------------------------------------------*/
592
593<SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
594<SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
595
e9955c83
AD
596%%
597
6c30d641
PE
598/* Read bytes from FP into buffer BUF of size SIZE. Return the
599 number of bytes read. Remove '\r' from input, treating \r\n
600 and isolated \r as \n. */
601
602static size_t
603no_cr_read (FILE *fp, char *buf, size_t size)
604{
a737b216
PE
605 size_t bytes_read = fread (buf, 1, size, fp);
606 if (bytes_read)
6c30d641 607 {
a737b216 608 char *w = memchr (buf, '\r', bytes_read);
6c30d641
PE
609 if (w)
610 {
611 char const *r = ++w;
a737b216 612 char const *lim = buf + bytes_read;
6c30d641
PE
613
614 for (;;)
615 {
616 /* Found an '\r'. Treat it like '\n', but ignore any
617 '\n' that immediately follows. */
618 w[-1] = '\n';
619 if (r == lim)
620 {
621 int ch = getc (fp);
622 if (ch != '\n' && ungetc (ch, fp) != ch)
623 break;
624 }
625 else if (*r == '\n')
626 r++;
627
628 /* Copy until the next '\r'. */
629 do
630 {
631 if (r == lim)
632 return w - buf;
633 }
634 while ((*w++ = *r++) != '\r');
635 }
636
637 return w - buf;
638 }
639 }
640
a737b216 641 return bytes_read;
6c30d641
PE
642}
643
644
f25bfb75 645
1452af69
PE
646/*------------------------------------------------------.
647| Scan NUMBER for a base-BASE integer at location LOC. |
648`------------------------------------------------------*/
649
650static unsigned long int
651scan_integer (char const *number, int base, location loc)
652{
4517da37
PE
653 verify (INT_MAX < ULONG_MAX);
654 unsigned long int num = strtoul (number, NULL, base);
655
656 if (INT_MAX < num)
1452af69
PE
657 {
658 complain_at (loc, _("integer out of range: %s"), quote (number));
659 num = INT_MAX;
660 }
4517da37 661
1452af69
PE
662 return num;
663}
664
665
d8d3f94a
PE
666/*------------------------------------------------------------------.
667| Convert universal character name UCN to a single-byte character, |
668| and return that character. Return -1 if UCN does not correspond |
669| to a single-byte character. |
670`------------------------------------------------------------------*/
671
672static int
673convert_ucn_to_byte (char const *ucn)
674{
4517da37
PE
675 verify (UCHAR_MAX <= INT_MAX);
676 unsigned long int code = strtoul (ucn + 2, NULL, 16);
d8d3f94a
PE
677
678 /* FIXME: Currently we assume Unicode-compatible unibyte characters
679 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
680 non-ASCII hosts we support only the portable C character set.
681 These limitations should be removed once we add support for
682 multibyte characters. */
683
684 if (UCHAR_MAX < code)
685 return -1;
686
687#if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
688 {
689 /* A non-ASCII host. Use CODE to index into a table of the C
690 basic execution character set, which is guaranteed to exist on
691 all Standard C platforms. This table also includes '$', '@',
8e6ef483 692 and '`', which are not in the basic execution character set but
d8d3f94a
PE
693 which are unibyte characters on all the platforms that we know
694 about. */
695 static signed char const table[] =
696 {
697 '\0', -1, -1, -1, -1, -1, -1, '\a',
698 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
699 -1, -1, -1, -1, -1, -1, -1, -1,
700 -1, -1, -1, -1, -1, -1, -1, -1,
701 ' ', '!', '"', '#', '$', '%', '&', '\'',
702 '(', ')', '*', '+', ',', '-', '.', '/',
703 '0', '1', '2', '3', '4', '5', '6', '7',
704 '8', '9', ':', ';', '<', '=', '>', '?',
705 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
706 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
707 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
708 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
709 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
710 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
711 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
712 'x', 'y', 'z', '{', '|', '}', '~'
713 };
714
715 code = code < sizeof table ? table[code] : -1;
716 }
717#endif
c4d720cd 718
d8d3f94a
PE
719 return code;
720}
721
722
900c5db5
AD
723/*----------------------------------------------------------------.
724| Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
725`----------------------------------------------------------------*/
726
727static void
4517da37 728handle_syncline (char *args, location loc)
900c5db5 729{
4517da37
PE
730 char *after_num;
731 unsigned long int lineno = strtoul (args, &after_num, 10);
732 char *file = strchr (after_num, '"') + 1;
733 *strchr (file, '"') = '\0';
734 if (INT_MAX <= lineno)
735 {
736 warn_at (loc, _("line number overflow"));
737 lineno = INT_MAX;
738 }
e9071366 739 current_file = uniqstr_new (file);
0c8e079f 740 boundary_set (&scanner_cursor, current_file, lineno, 1);
4517da37
PE
741}
742
743
4febdd96
PE
744/*----------------------------------------------------------------.
745| For a token or comment starting at START, report message MSGID, |
746| which should say that an end marker was found before |
747| the expected TOKEN_END. |
748`----------------------------------------------------------------*/
749
750static void
751unexpected_end (boundary start, char const *msgid, char const *token_end)
752{
753 location loc;
754 loc.start = start;
755 loc.end = scanner_cursor;
756 complain_at (loc, _(msgid), token_end);
757}
758
759
3f2d73f1
PE
760/*------------------------------------------------------------------------.
761| Report an unexpected EOF in a token or comment starting at START. |
762| An end of file was encountered and the expected TOKEN_END was missing. |
3f2d73f1 763`------------------------------------------------------------------------*/
a706a1cc
PE
764
765static void
aa418041 766unexpected_eof (boundary start, char const *token_end)
a706a1cc 767{
4febdd96
PE
768 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
769}
770
771
772/*----------------------------------------.
773| Likewise, but for unexpected newlines. |
774`----------------------------------------*/
775
776static void
777unexpected_newline (boundary start, char const *token_end)
778{
779 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
a706a1cc
PE
780}
781
782
f25bfb75
AD
783/*-------------------------.
784| Initialize the scanner. |
785`-------------------------*/
786
1d6412ad 787void
e9071366 788gram_scanner_initialize (void)
1d6412ad 789{
223ff46e 790 obstack_init (&obstack_for_string);
1d6412ad
AD
791}
792
793
f25bfb75
AD
794/*-----------------------------------------------.
795| Free all the memory allocated to the scanner. |
796`-----------------------------------------------*/
797
4cdb01db 798void
e9071366 799gram_scanner_free (void)
4cdb01db 800{
223ff46e 801 obstack_free (&obstack_for_string, 0);
536545f3 802 /* Reclaim Flex's buffers. */
580b8926 803 yylex_destroy ();
4cdb01db 804}