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