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