]> git.saurik.com Git - bison.git/blame - src/scan-gram.l
* src/parse-gram.y: Add `%expect 0' so we don't overlook conflicts.
[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
e9071366
AD
69/* The location of the most recently saved token, if it was a
70 BRACED_CODE token; otherwise, this has an unspecified value. */
71location gram_last_braced_code_loc;
4517da37 72
4517da37 73static void handle_syncline (char *, location);
1452af69 74static unsigned long int scan_integer (char const *p, int base, location loc);
d8d3f94a 75static int convert_ucn_to_byte (char const *hex_text);
aa418041 76static void unexpected_eof (boundary, char const *);
4febdd96 77static void unexpected_newline (boundary, char const *);
e9955c83
AD
78
79%}
e9071366
AD
80 /* A C-like comment in directives/rules. */
81%x SC_YACC_COMMENT
82 /* Strings and characters in directives/rules. */
e9955c83 83%x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
e9071366
AD
84 /* A identifier was just read in directives/rules. Special state
85 to capture the sequence `identifier :'. */
86%x SC_AFTER_IDENTIFIER
e9071366
AD
87
88 /* Three types of user code:
89 - prologue (code between `%{' `%}' in the first section, before %%);
90 - actions, printers, union, etc, (between braced in the middle section);
91 - epilogue (everything after the second %%). */
92%x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE
93 /* C and C++ comments in code. */
94%x SC_COMMENT SC_LINE_COMMENT
95 /* Strings and characters in code. */
96%x SC_STRING SC_CHARACTER
e9955c83 97
29c01725
AD
98letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
99id {letter}({letter}|[0-9])*
100directive %{letter}({letter}|[0-9]|-)*
624a35e2 101int [0-9]+
d8d3f94a
PE
102
103/* POSIX says that a tag must be both an id and a C union member, but
104 historically almost any character is allowed in a tag. We disallow
105 NUL and newline, as this simplifies our implementation. */
106tag [^\0\n>]+
107
108/* Zero or more instances of backslash-newline. Following GCC, allow
109 white space between the backslash and the newline. */
110splice (\\[ \f\t\v]*\n)*
e9955c83
AD
111
112%%
113%{
a706a1cc 114 /* Nesting level of the current code in braces. */
1a9e39f1
PE
115 int braces_level IF_LINT (= 0);
116
3f2d73f1
PE
117 /* Parent context state, when applicable. */
118 int context_state IF_LINT (= 0);
a706a1cc 119
3f2d73f1 120 /* Location of most recent identifier, when applicable. */
a2bc9dbc 121 location id_loc IF_LINT (= empty_location);
3f2d73f1 122
a2bc9dbc
PE
123 /* Where containing code started, when applicable. Its initial
124 value is relevant only when yylex is invoked in the SC_EPILOGUE
125 start condition. */
126 boundary code_start = scanner_cursor;
3f2d73f1 127
223ff46e
PE
128 /* Where containing comment or string or character literal started,
129 when applicable. */
a2bc9dbc 130 boundary token_start IF_LINT (= scanner_cursor);
e9955c83
AD
131%}
132
133
3f2d73f1
PE
134 /*-----------------------.
135 | Scanning white space. |
136 `-----------------------*/
137
58d7a1a1 138<INITIAL,SC_AFTER_IDENTIFIER>
3f2d73f1 139{
4febdd96 140 /* Comments and white space. */
83adb046 141 "," warn_at (*loc, _("stray `,' treated as white space"));
4febdd96 142 [ \f\n\t\v] |
3f2d73f1 143 "//".* ;
83adb046
PE
144 "/*" {
145 token_start = loc->start;
146 context_state = YY_START;
147 BEGIN SC_YACC_COMMENT;
148 }
3f2d73f1
PE
149
150 /* #line directives are not documented, and may be withdrawn or
151 modified in future versions of Bison. */
152 ^"#line "{int}" \"".*"\"\n" {
4517da37 153 handle_syncline (yytext + sizeof "#line " - 1, *loc);
3f2d73f1
PE
154 }
155}
156
157
e9955c83
AD
158 /*----------------------------.
159 | Scanning Bison directives. |
160 `----------------------------*/
161<INITIAL>
162{
34f98f46
JD
163 "%after-header" return PERCENT_AFTER_HEADER;
164 "%before-header" return PERCENT_BEFORE_HEADER;
58d7a1a1
AD
165 "%binary" return PERCENT_NONASSOC;
166 "%debug" return PERCENT_DEBUG;
167 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
168 "%define" return PERCENT_DEFINE;
169 "%defines" return PERCENT_DEFINES;
170 "%destructor" return PERCENT_DESTRUCTOR;
171 "%dprec" return PERCENT_DPREC;
34f98f46 172 "%end-header" return PERCENT_END_HEADER;
58d7a1a1
AD
173 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
174 "%expect" return PERCENT_EXPECT;
175 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
176 "%file-prefix" return PERCENT_FILE_PREFIX;
e9955c83 177 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
58d7a1a1
AD
178 "%initial-action" return PERCENT_INITIAL_ACTION;
179 "%glr-parser" return PERCENT_GLR_PARSER;
180 "%left" return PERCENT_LEFT;
181 "%lex-param" return PERCENT_LEX_PARAM;
182 "%locations" return PERCENT_LOCATIONS;
183 "%merge" return PERCENT_MERGE;
184 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
185 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
186 "%no"[-_]"lines" return PERCENT_NO_LINES;
187 "%nonassoc" return PERCENT_NONASSOC;
188 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
189 "%nterm" return PERCENT_NTERM;
190 "%output" return PERCENT_OUTPUT;
191 "%parse-param" return PERCENT_PARSE_PARAM;
192 "%prec" return PERCENT_PREC;
193 "%printer" return PERCENT_PRINTER;
194 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
195 "%require" return PERCENT_REQUIRE;
196 "%right" return PERCENT_RIGHT;
197 "%skeleton" return PERCENT_SKELETON;
198 "%start" return PERCENT_START;
34f98f46 199 "%start-header" return PERCENT_START_HEADER;
58d7a1a1
AD
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
AD
214 ";" return SEMICOLON;
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{
db2cc12f 343 "\"" {
41141c56 344 STRING_FINISH;
3f2d73f1 345 loc->start = token_start;
223ff46e 346 val->chars = last_string;
a706a1cc 347 BEGIN INITIAL;
e9955c83
AD
348 return STRING;
349 }
4febdd96
PE
350 \n unexpected_newline (token_start, "\""); BEGIN INITIAL;
351 <<EOF>> unexpected_eof (token_start, "\""); BEGIN INITIAL;
e9955c83
AD
352}
353
4febdd96
PE
354 /*----------------------------------------------------------.
355 | Scanning a Bison character literal, decoding its escapes. |
356 | The initial quote is already eaten. |
357 `----------------------------------------------------------*/
e9955c83
AD
358
359<SC_ESCAPED_CHARACTER>
360{
db2cc12f 361 "'" {
41141c56
PE
362 STRING_GROW;
363 STRING_FINISH;
3f2d73f1 364 loc->start = token_start;
58d7a1a1 365 val->character = last_string[1];
41141c56 366 STRING_FREE;
a706a1cc 367 BEGIN INITIAL;
58d7a1a1 368 return CHAR;
e9955c83 369 }
4febdd96
PE
370 \n unexpected_newline (token_start, "'"); BEGIN INITIAL;
371 <<EOF>> unexpected_eof (token_start, "'"); BEGIN INITIAL;
372}
a706a1cc 373
4febdd96
PE
374<SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
375{
92ac3705 376 \0 complain_at (*loc, _("invalid null character"));
e9955c83
AD
377}
378
379
380 /*----------------------------.
381 | Decode escaped characters. |
382 `----------------------------*/
383
384<SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
385{
d8d3f94a 386 \\[0-7]{1,3} {
4517da37 387 unsigned long int c = strtoul (yytext + 1, NULL, 8);
d8d3f94a 388 if (UCHAR_MAX < c)
3f2d73f1 389 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
05ac60f3 390 else if (! c)
92ac3705 391 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
e9955c83 392 else
223ff46e 393 obstack_1grow (&obstack_for_string, c);
e9955c83
AD
394 }
395
6b0d38ab 396 \\x[0-9abcdefABCDEF]+ {
4517da37
PE
397 verify (UCHAR_MAX < ULONG_MAX);
398 unsigned long int c = strtoul (yytext + 2, NULL, 16);
399 if (UCHAR_MAX < c)
3f2d73f1 400 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
92ac3705
PE
401 else if (! c)
402 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
d8d3f94a 403 else
223ff46e 404 obstack_1grow (&obstack_for_string, c);
e9955c83
AD
405 }
406
223ff46e
PE
407 \\a obstack_1grow (&obstack_for_string, '\a');
408 \\b obstack_1grow (&obstack_for_string, '\b');
409 \\f obstack_1grow (&obstack_for_string, '\f');
410 \\n obstack_1grow (&obstack_for_string, '\n');
411 \\r obstack_1grow (&obstack_for_string, '\r');
412 \\t obstack_1grow (&obstack_for_string, '\t');
413 \\v obstack_1grow (&obstack_for_string, '\v');
412f8a59
PE
414
415 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
223ff46e 416 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
412f8a59 417
6b0d38ab 418 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
d8d3f94a
PE
419 int c = convert_ucn_to_byte (yytext);
420 if (c < 0)
3f2d73f1 421 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
92ac3705
PE
422 else if (! c)
423 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
d8d3f94a 424 else
223ff46e 425 obstack_1grow (&obstack_for_string, c);
d8d3f94a 426 }
4f25ebb0 427 \\(.|\n) {
3f2d73f1 428 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
41141c56 429 STRING_GROW;
e9955c83
AD
430 }
431}
432
4febdd96
PE
433 /*--------------------------------------------.
434 | Scanning user-code characters and strings. |
435 `--------------------------------------------*/
e9955c83 436
4febdd96
PE
437<SC_CHARACTER,SC_STRING>
438{
e9071366 439 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
4febdd96 440}
e9955c83
AD
441
442<SC_CHARACTER>
443{
4febdd96
PE
444 "'" STRING_GROW; BEGIN context_state;
445 \n unexpected_newline (token_start, "'"); BEGIN context_state;
446 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
e9955c83
AD
447}
448
e9955c83
AD
449<SC_STRING>
450{
4febdd96
PE
451 "\"" STRING_GROW; BEGIN context_state;
452 \n unexpected_newline (token_start, "\""); BEGIN context_state;
453 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
e9955c83
AD
454}
455
456
457 /*---------------------------------------------------.
458 | Strings, comments etc. can be found in user code. |
459 `---------------------------------------------------*/
460
461<SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
462{
3f2d73f1
PE
463 "'" {
464 STRING_GROW;
465 context_state = YY_START;
466 token_start = loc->start;
467 BEGIN SC_CHARACTER;
468 }
469 "\"" {
470 STRING_GROW;
471 context_state = YY_START;
472 token_start = loc->start;
473 BEGIN SC_STRING;
474 }
475 "/"{splice}"*" {
476 STRING_GROW;
477 context_state = YY_START;
478 token_start = loc->start;
479 BEGIN SC_COMMENT;
480 }
481 "/"{splice}"/" {
482 STRING_GROW;
483 context_state = YY_START;
484 BEGIN SC_LINE_COMMENT;
485 }
e9955c83
AD
486}
487
488
624a35e2 489
58d7a1a1
AD
490 /*-----------------------------------------------------------.
491 | Scanning some code in braces (actions). The initial "{" is |
492 | already eaten. |
493 `-----------------------------------------------------------*/
e9955c83
AD
494
495<SC_BRACED_CODE>
496{
41141c56
PE
497 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
498 "%"{splice}">" STRING_GROW; braces_level--;
e9955c83 499 "}" {
25522739
PE
500 obstack_1grow (&obstack_for_string, '}');
501
2346344a
AD
502 --braces_level;
503 if (braces_level < 0)
e9955c83 504 {
41141c56 505 STRING_FINISH;
3f2d73f1 506 loc->start = code_start;
223ff46e 507 val->chars = last_string;
e9071366 508 gram_last_braced_code_loc = *loc;
a706a1cc 509 BEGIN INITIAL;
58d7a1a1 510 return BRACED_CODE;
e9955c83
AD
511 }
512 }
513
a706a1cc
PE
514 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
515 (as `<' `<%'). */
41141c56 516 "<"{splice}"<" STRING_GROW;
a706a1cc 517
aa418041 518 <<EOF>> unexpected_eof (code_start, "}"); BEGIN INITIAL;
e9955c83
AD
519}
520
521
522 /*--------------------------------------------------------------.
523 | Scanning some prologue: from "%{" (already scanned) to "%}". |
524 `--------------------------------------------------------------*/
525
526<SC_PROLOGUE>
527{
528 "%}" {
41141c56 529 STRING_FINISH;
3f2d73f1 530 loc->start = code_start;
223ff46e 531 val->chars = last_string;
a706a1cc 532 BEGIN INITIAL;
e9955c83
AD
533 return PROLOGUE;
534 }
535
aa418041 536 <<EOF>> unexpected_eof (code_start, "%}"); BEGIN INITIAL;
e9955c83
AD
537}
538
539
540 /*---------------------------------------------------------------.
541 | Scanning the epilogue (everything after the second "%%", which |
d8d3f94a 542 | has already been eaten). |
e9955c83
AD
543 `---------------------------------------------------------------*/
544
545<SC_EPILOGUE>
546{
e9955c83 547 <<EOF>> {
41141c56 548 STRING_FINISH;
3f2d73f1 549 loc->start = code_start;
223ff46e 550 val->chars = last_string;
a706a1cc 551 BEGIN INITIAL;
e9955c83
AD
552 return EPILOGUE;
553 }
554}
555
556
4febdd96
PE
557 /*-----------------------------------------------------.
558 | By default, grow the string obstack with the input. |
559 `-----------------------------------------------------*/
560
561<SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
562<SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
563
e9955c83
AD
564%%
565
6c30d641
PE
566/* Read bytes from FP into buffer BUF of size SIZE. Return the
567 number of bytes read. Remove '\r' from input, treating \r\n
568 and isolated \r as \n. */
569
570static size_t
571no_cr_read (FILE *fp, char *buf, size_t size)
572{
a737b216
PE
573 size_t bytes_read = fread (buf, 1, size, fp);
574 if (bytes_read)
6c30d641 575 {
a737b216 576 char *w = memchr (buf, '\r', bytes_read);
6c30d641
PE
577 if (w)
578 {
579 char const *r = ++w;
a737b216 580 char const *lim = buf + bytes_read;
6c30d641
PE
581
582 for (;;)
583 {
584 /* Found an '\r'. Treat it like '\n', but ignore any
585 '\n' that immediately follows. */
586 w[-1] = '\n';
587 if (r == lim)
588 {
589 int ch = getc (fp);
590 if (ch != '\n' && ungetc (ch, fp) != ch)
591 break;
592 }
593 else if (*r == '\n')
594 r++;
595
596 /* Copy until the next '\r'. */
597 do
598 {
599 if (r == lim)
600 return w - buf;
601 }
602 while ((*w++ = *r++) != '\r');
603 }
604
605 return w - buf;
606 }
607 }
608
a737b216 609 return bytes_read;
6c30d641
PE
610}
611
612
f25bfb75 613
1452af69
PE
614/*------------------------------------------------------.
615| Scan NUMBER for a base-BASE integer at location LOC. |
616`------------------------------------------------------*/
617
618static unsigned long int
619scan_integer (char const *number, int base, location loc)
620{
4517da37
PE
621 verify (INT_MAX < ULONG_MAX);
622 unsigned long int num = strtoul (number, NULL, base);
623
624 if (INT_MAX < num)
1452af69
PE
625 {
626 complain_at (loc, _("integer out of range: %s"), quote (number));
627 num = INT_MAX;
628 }
4517da37 629
1452af69
PE
630 return num;
631}
632
633
d8d3f94a
PE
634/*------------------------------------------------------------------.
635| Convert universal character name UCN to a single-byte character, |
636| and return that character. Return -1 if UCN does not correspond |
637| to a single-byte character. |
638`------------------------------------------------------------------*/
639
640static int
641convert_ucn_to_byte (char const *ucn)
642{
4517da37
PE
643 verify (UCHAR_MAX <= INT_MAX);
644 unsigned long int code = strtoul (ucn + 2, NULL, 16);
d8d3f94a
PE
645
646 /* FIXME: Currently we assume Unicode-compatible unibyte characters
647 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
648 non-ASCII hosts we support only the portable C character set.
649 These limitations should be removed once we add support for
650 multibyte characters. */
651
652 if (UCHAR_MAX < code)
653 return -1;
654
655#if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
656 {
657 /* A non-ASCII host. Use CODE to index into a table of the C
658 basic execution character set, which is guaranteed to exist on
659 all Standard C platforms. This table also includes '$', '@',
8e6ef483 660 and '`', which are not in the basic execution character set but
d8d3f94a
PE
661 which are unibyte characters on all the platforms that we know
662 about. */
663 static signed char const table[] =
664 {
665 '\0', -1, -1, -1, -1, -1, -1, '\a',
666 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
667 -1, -1, -1, -1, -1, -1, -1, -1,
668 -1, -1, -1, -1, -1, -1, -1, -1,
669 ' ', '!', '"', '#', '$', '%', '&', '\'',
670 '(', ')', '*', '+', ',', '-', '.', '/',
671 '0', '1', '2', '3', '4', '5', '6', '7',
672 '8', '9', ':', ';', '<', '=', '>', '?',
673 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
674 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
675 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
676 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
677 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
678 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
679 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
680 'x', 'y', 'z', '{', '|', '}', '~'
681 };
682
683 code = code < sizeof table ? table[code] : -1;
684 }
685#endif
c4d720cd 686
d8d3f94a
PE
687 return code;
688}
689
690
900c5db5
AD
691/*----------------------------------------------------------------.
692| Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
693`----------------------------------------------------------------*/
694
695static void
4517da37 696handle_syncline (char *args, location loc)
900c5db5 697{
4517da37
PE
698 char *after_num;
699 unsigned long int lineno = strtoul (args, &after_num, 10);
700 char *file = strchr (after_num, '"') + 1;
701 *strchr (file, '"') = '\0';
702 if (INT_MAX <= lineno)
703 {
704 warn_at (loc, _("line number overflow"));
705 lineno = INT_MAX;
706 }
e9071366 707 current_file = uniqstr_new (file);
0c8e079f 708 boundary_set (&scanner_cursor, current_file, lineno, 1);
4517da37
PE
709}
710
711
4febdd96
PE
712/*----------------------------------------------------------------.
713| For a token or comment starting at START, report message MSGID, |
714| which should say that an end marker was found before |
715| the expected TOKEN_END. |
716`----------------------------------------------------------------*/
717
718static void
719unexpected_end (boundary start, char const *msgid, char const *token_end)
720{
721 location loc;
722 loc.start = start;
723 loc.end = scanner_cursor;
724 complain_at (loc, _(msgid), token_end);
725}
726
727
3f2d73f1
PE
728/*------------------------------------------------------------------------.
729| Report an unexpected EOF in a token or comment starting at START. |
730| An end of file was encountered and the expected TOKEN_END was missing. |
3f2d73f1 731`------------------------------------------------------------------------*/
a706a1cc
PE
732
733static void
aa418041 734unexpected_eof (boundary start, char const *token_end)
a706a1cc 735{
4febdd96
PE
736 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
737}
738
739
740/*----------------------------------------.
741| Likewise, but for unexpected newlines. |
742`----------------------------------------*/
743
744static void
745unexpected_newline (boundary start, char const *token_end)
746{
747 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
a706a1cc
PE
748}
749
750
f25bfb75
AD
751/*-------------------------.
752| Initialize the scanner. |
753`-------------------------*/
754
1d6412ad 755void
e9071366 756gram_scanner_initialize (void)
1d6412ad 757{
223ff46e 758 obstack_init (&obstack_for_string);
1d6412ad
AD
759}
760
761
f25bfb75
AD
762/*-----------------------------------------------.
763| Free all the memory allocated to the scanner. |
764`-----------------------------------------------*/
765
4cdb01db 766void
e9071366 767gram_scanner_free (void)
4cdb01db 768{
223ff46e 769 obstack_free (&obstack_for_string, 0);
536545f3
AD
770 /* Reclaim Flex's buffers. */
771 yy_delete_buffer (YY_CURRENT_BUFFER);
4cdb01db 772}