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