]>
Commit | Line | Data |
---|---|---|
1 | /* Bison Grammar Scanner -*- C -*- | |
2 | ||
3 | Copyright (C) 2002-2013 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 3 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, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | %option debug nodefault noinput noyywrap never-interactive | |
21 | %option prefix="gram_" outfile="lex.yy.c" | |
22 | ||
23 | %{ | |
24 | /* Work around a bug in flex 2.5.31. See Debian bug 333231 | |
25 | <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */ | |
26 | #undef gram_wrap | |
27 | #define gram_wrap() 1 | |
28 | ||
29 | #define FLEX_PREFIX(Id) gram_ ## Id | |
30 | #include <src/flex-scanner.h> | |
31 | ||
32 | #include <src/complain.h> | |
33 | #include <src/files.h> | |
34 | #include <src/getargs.h> | |
35 | #include <src/gram.h> | |
36 | #include <quotearg.h> | |
37 | #include <src/reader.h> | |
38 | #include <src/uniqstr.h> | |
39 | ||
40 | #include <c-ctype.h> | |
41 | #include <mbswidth.h> | |
42 | #include <quote.h> | |
43 | ||
44 | #include <src/scan-gram.h> | |
45 | ||
46 | #define YY_DECL GRAM_LEX_DECL | |
47 | ||
48 | #define YY_USER_INIT \ | |
49 | code_start = scanner_cursor = loc->start; \ | |
50 | ||
51 | /* Location of scanner cursor. */ | |
52 | static boundary scanner_cursor; | |
53 | ||
54 | #define YY_USER_ACTION location_compute (loc, &scanner_cursor, yytext, yyleng); | |
55 | ||
56 | static size_t no_cr_read (FILE *, char *, size_t); | |
57 | #define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size)) | |
58 | ||
59 | #define RETURN_PERCENT_PARAM(Value) \ | |
60 | RETURN_VALUE(PERCENT_PARAM, param, param_ ## Value) | |
61 | ||
62 | #define RETURN_PERCENT_FLAG(Value) \ | |
63 | RETURN_VALUE(PERCENT_FLAG, uniqstr, uniqstr_new (Value)) | |
64 | ||
65 | #define RETURN_VALUE(Token, Field, Value) \ | |
66 | do { \ | |
67 | val->Field = Value; \ | |
68 | return Token; \ | |
69 | } while (0) | |
70 | ||
71 | #define ROLLBACK_CURRENT_TOKEN \ | |
72 | do { \ | |
73 | scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0); \ | |
74 | yyless (0); \ | |
75 | } while (0) | |
76 | ||
77 | #define DEPRECATED(Msg) \ | |
78 | do { \ | |
79 | size_t i; \ | |
80 | deprecated_directive (loc, yytext, Msg); \ | |
81 | scanner_cursor.column -= mbsnwidth (Msg, strlen (Msg), 0); \ | |
82 | for (i = strlen (Msg); i != 0; --i) \ | |
83 | unput (Msg[i - 1]); \ | |
84 | } while (0) | |
85 | ||
86 | /* A string representing the most recently saved token. */ | |
87 | static char *last_string; | |
88 | ||
89 | /* Bracketed identifier. */ | |
90 | static uniqstr bracketed_id_str = 0; | |
91 | static location bracketed_id_loc; | |
92 | static boundary bracketed_id_start; | |
93 | static int bracketed_id_context_state = 0; | |
94 | ||
95 | void | |
96 | gram_scanner_last_string_free (void) | |
97 | { | |
98 | STRING_FREE; | |
99 | } | |
100 | ||
101 | static void handle_syncline (char *, location); | |
102 | static unsigned long int scan_integer (char const *p, int base, location loc); | |
103 | static int convert_ucn_to_byte (char const *hex_text); | |
104 | static void unexpected_eof (boundary, char const *); | |
105 | static void unexpected_newline (boundary, char const *); | |
106 | ||
107 | %} | |
108 | /* A C-like comment in directives/rules. */ | |
109 | %x SC_YACC_COMMENT | |
110 | /* Strings and characters in directives/rules. */ | |
111 | %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER | |
112 | /* A identifier was just read in directives/rules. Special state | |
113 | to capture the sequence 'identifier :'. */ | |
114 | %x SC_AFTER_IDENTIFIER | |
115 | ||
116 | /* POSIX says that a tag must be both an id and a C union member, but | |
117 | historically almost any character is allowed in a tag. We | |
118 | disallow NUL, as this simplifies our implementation. We match | |
119 | angle brackets in nested pairs: several languages use them for | |
120 | generics/template types. */ | |
121 | %x SC_TAG | |
122 | ||
123 | /* Four types of user code: | |
124 | - prologue (code between '%{' '%}' in the first section, before %%); | |
125 | - actions, printers, union, etc, (between braced in the middle section); | |
126 | - epilogue (everything after the second %%). | |
127 | - predicate (code between '%?{' and '{' in middle section); */ | |
128 | %x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE SC_PREDICATE | |
129 | /* C and C++ comments in code. */ | |
130 | %x SC_COMMENT SC_LINE_COMMENT | |
131 | /* Strings and characters in code. */ | |
132 | %x SC_STRING SC_CHARACTER | |
133 | /* Bracketed identifiers support. */ | |
134 | %x SC_BRACKETED_ID SC_RETURN_BRACKETED_ID | |
135 | ||
136 | letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_] | |
137 | notletter [^.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]{-}[%\{] | |
138 | id {letter}({letter}|[-0-9])* | |
139 | int [0-9]+ | |
140 | ||
141 | /* Zero or more instances of backslash-newline. Following GCC, allow | |
142 | white space between the backslash and the newline. */ | |
143 | splice (\\[ \f\t\v]*\n)* | |
144 | ||
145 | /* An equal sign, with optional leading whitespaces. This is used in some | |
146 | deprecated constructs. */ | |
147 | eqopt ([[:space:]]*=)? | |
148 | ||
149 | %% | |
150 | %{ | |
151 | /* Nesting level. Either for nested braces, or nested angle brackets | |
152 | (but not mixed). */ | |
153 | int nesting PACIFY_CC (= 0); | |
154 | ||
155 | /* Parent context state, when applicable. */ | |
156 | int context_state PACIFY_CC (= 0); | |
157 | ||
158 | /* Location of most recent identifier, when applicable. */ | |
159 | location id_loc PACIFY_CC (= empty_location); | |
160 | ||
161 | /* Where containing code started, when applicable. Its initial | |
162 | value is relevant only when yylex is invoked in the SC_EPILOGUE | |
163 | start condition. */ | |
164 | boundary code_start = scanner_cursor; | |
165 | ||
166 | /* Where containing comment or string or character literal started, | |
167 | when applicable. */ | |
168 | boundary token_start PACIFY_CC (= scanner_cursor); | |
169 | %} | |
170 | ||
171 | ||
172 | /*-----------------------. | |
173 | | Scanning white space. | | |
174 | `-----------------------*/ | |
175 | ||
176 | <INITIAL,SC_AFTER_IDENTIFIER,SC_BRACKETED_ID,SC_RETURN_BRACKETED_ID> | |
177 | { | |
178 | /* Comments and white space. */ | |
179 | "," { | |
180 | complain (loc, Wother, _("stray ',' treated as white space")); | |
181 | } | |
182 | [ \f\n\t\v] | | |
183 | "//".* continue; | |
184 | "/*" { | |
185 | token_start = loc->start; | |
186 | context_state = YY_START; | |
187 | BEGIN SC_YACC_COMMENT; | |
188 | } | |
189 | ||
190 | /* #line directives are not documented, and may be withdrawn or | |
191 | modified in future versions of Bison. */ | |
192 | ^"#line "{int}(" \"".*"\"")?"\n" { | |
193 | handle_syncline (yytext + sizeof "#line " - 1, *loc); | |
194 | } | |
195 | } | |
196 | ||
197 | ||
198 | /*----------------------------. | |
199 | | Scanning Bison directives. | | |
200 | `----------------------------*/ | |
201 | ||
202 | /* For directives that are also command line options, the regex must be | |
203 | "%..." | |
204 | after "[-_]"s are removed, and the directive must match the --long | |
205 | option name, with a single string argument. Otherwise, add exceptions | |
206 | to ../build-aux/cross-options.pl. */ | |
207 | ||
208 | <INITIAL> | |
209 | { | |
210 | "%binary" return PERCENT_NONASSOC; | |
211 | "%code" return PERCENT_CODE; | |
212 | "%debug" RETURN_PERCENT_FLAG("parse.trace"); | |
213 | "%default-prec" return PERCENT_DEFAULT_PREC; | |
214 | "%define" return PERCENT_DEFINE; | |
215 | "%defines" return PERCENT_DEFINES; | |
216 | "%destructor" return PERCENT_DESTRUCTOR; | |
217 | "%dprec" return PERCENT_DPREC; | |
218 | "%empty" return PERCENT_EMPTY; | |
219 | "%error-verbose" return PERCENT_ERROR_VERBOSE; | |
220 | "%expect" return PERCENT_EXPECT; | |
221 | "%expect-rr" return PERCENT_EXPECT_RR; | |
222 | "%file-prefix" return PERCENT_FILE_PREFIX; | |
223 | "%fixed-output-files" return PERCENT_YACC; | |
224 | "%initial-action" return PERCENT_INITIAL_ACTION; | |
225 | "%glr-parser" return PERCENT_GLR_PARSER; | |
226 | "%language" return PERCENT_LANGUAGE; | |
227 | "%left" return PERCENT_LEFT; | |
228 | "%lex-param" RETURN_PERCENT_PARAM(lex); | |
229 | "%locations" RETURN_PERCENT_FLAG("locations"); | |
230 | "%merge" return PERCENT_MERGE; | |
231 | "%name-prefix" return PERCENT_NAME_PREFIX; | |
232 | "%no-default-prec" return PERCENT_NO_DEFAULT_PREC; | |
233 | "%no-lines" return PERCENT_NO_LINES; | |
234 | "%nonassoc" return PERCENT_NONASSOC; | |
235 | "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER; | |
236 | "%nterm" return PERCENT_NTERM; | |
237 | "%output" return PERCENT_OUTPUT; | |
238 | "%param" RETURN_PERCENT_PARAM(both); | |
239 | "%parse-param" RETURN_PERCENT_PARAM(parse); | |
240 | "%prec" return PERCENT_PREC; | |
241 | "%precedence" return PERCENT_PRECEDENCE; | |
242 | "%printer" return PERCENT_PRINTER; | |
243 | "%pure-parser" RETURN_PERCENT_FLAG("api.pure"); | |
244 | "%require" return PERCENT_REQUIRE; | |
245 | "%right" return PERCENT_RIGHT; | |
246 | "%skeleton" return PERCENT_SKELETON; | |
247 | "%start" return PERCENT_START; | |
248 | "%term" return PERCENT_TOKEN; | |
249 | "%token" return PERCENT_TOKEN; | |
250 | "%token-table" return PERCENT_TOKEN_TABLE; | |
251 | "%type" return PERCENT_TYPE; | |
252 | "%union" return PERCENT_UNION; | |
253 | "%verbose" return PERCENT_VERBOSE; | |
254 | "%yacc" return PERCENT_YACC; | |
255 | ||
256 | /* deprecated */ | |
257 | "%default"[-_]"prec" DEPRECATED("%default-prec"); | |
258 | "%error"[-_]"verbose" DEPRECATED("%define parse.error verbose"); | |
259 | "%expect"[-_]"rr" DEPRECATED("%expect-rr"); | |
260 | "%file-prefix"{eqopt} DEPRECATED("%file-prefix"); | |
261 | "%fixed"[-_]"output"[-_]"files" DEPRECATED("%fixed-output-files"); | |
262 | "%name"[-_]"prefix"{eqopt} DEPRECATED("%name-prefix"); | |
263 | "%no"[-_]"default"[-_]"prec" DEPRECATED("%no-default-prec"); | |
264 | "%no"[-_]"lines" DEPRECATED("%no-lines"); | |
265 | "%output"{eqopt} DEPRECATED("%output"); | |
266 | "%pure"[-_]"parser" DEPRECATED("%pure-parser"); | |
267 | "%token"[-_]"table" DEPRECATED("%token-table"); | |
268 | ||
269 | "%"{id}|"%"{notletter}([[:graph:]])+ { | |
270 | complain (loc, complaint, _("invalid directive: %s"), quote (yytext)); | |
271 | } | |
272 | ||
273 | "=" return EQUAL; | |
274 | "|" return PIPE; | |
275 | ";" return SEMICOLON; | |
276 | ||
277 | {id} { | |
278 | val->uniqstr = uniqstr_new (yytext); | |
279 | id_loc = *loc; | |
280 | bracketed_id_str = NULL; | |
281 | BEGIN SC_AFTER_IDENTIFIER; | |
282 | } | |
283 | ||
284 | {int} { | |
285 | val->integer = scan_integer (yytext, 10, *loc); | |
286 | return INT; | |
287 | } | |
288 | 0[xX][0-9abcdefABCDEF]+ { | |
289 | val->integer = scan_integer (yytext, 16, *loc); | |
290 | return INT; | |
291 | } | |
292 | ||
293 | /* Identifiers may not start with a digit. Yet, don't silently | |
294 | accept "1FOO" as "1 FOO". */ | |
295 | {int}{id} { | |
296 | complain (loc, complaint, _("invalid identifier: %s"), quote (yytext)); | |
297 | } | |
298 | ||
299 | /* Characters. */ | |
300 | "'" token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER; | |
301 | ||
302 | /* Strings. */ | |
303 | "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING; | |
304 | ||
305 | /* Prologue. */ | |
306 | "%{" code_start = loc->start; BEGIN SC_PROLOGUE; | |
307 | ||
308 | /* Code in between braces. */ | |
309 | "{" { | |
310 | STRING_GROW; | |
311 | nesting = 0; | |
312 | code_start = loc->start; | |
313 | BEGIN SC_BRACED_CODE; | |
314 | } | |
315 | ||
316 | /* Semantic predicate. */ | |
317 | "%?"[ \f\n\t\v]*"{" { | |
318 | nesting = 0; | |
319 | code_start = loc->start; | |
320 | BEGIN SC_PREDICATE; | |
321 | } | |
322 | ||
323 | /* A type. */ | |
324 | "<*>" return TAG_ANY; | |
325 | "<>" return TAG_NONE; | |
326 | "<" { | |
327 | nesting = 0; | |
328 | token_start = loc->start; | |
329 | BEGIN SC_TAG; | |
330 | } | |
331 | ||
332 | "%%" { | |
333 | static int percent_percent_count; | |
334 | if (++percent_percent_count == 2) | |
335 | BEGIN SC_EPILOGUE; | |
336 | return PERCENT_PERCENT; | |
337 | } | |
338 | ||
339 | "[" { | |
340 | bracketed_id_str = NULL; | |
341 | bracketed_id_start = loc->start; | |
342 | bracketed_id_context_state = YY_START; | |
343 | BEGIN SC_BRACKETED_ID; | |
344 | } | |
345 | ||
346 | [^\[%A-Za-z0-9_<>{}\"\'*;|=/, \f\n\t\v]+|. { | |
347 | complain (loc, complaint, "%s: %s", | |
348 | ngettext ("invalid character", "invalid characters", yyleng), | |
349 | quote_mem (yytext, yyleng)); | |
350 | } | |
351 | ||
352 | <<EOF>> { | |
353 | loc->start = loc->end = scanner_cursor; | |
354 | yyterminate (); | |
355 | } | |
356 | } | |
357 | ||
358 | ||
359 | /*--------------------------------------------------------------. | |
360 | | Supporting \0 complexifies our implementation for no expected | | |
361 | | added value. | | |
362 | `--------------------------------------------------------------*/ | |
363 | ||
364 | <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING,SC_TAG> | |
365 | { | |
366 | \0 complain (loc, complaint, _("invalid null character")); | |
367 | } | |
368 | ||
369 | ||
370 | /*-----------------------------------------------------------------. | |
371 | | Scanning after an identifier, checking whether a colon is next. | | |
372 | `-----------------------------------------------------------------*/ | |
373 | ||
374 | <SC_AFTER_IDENTIFIER> | |
375 | { | |
376 | "[" { | |
377 | if (bracketed_id_str) | |
378 | { | |
379 | ROLLBACK_CURRENT_TOKEN; | |
380 | BEGIN SC_RETURN_BRACKETED_ID; | |
381 | *loc = id_loc; | |
382 | return ID; | |
383 | } | |
384 | else | |
385 | { | |
386 | bracketed_id_start = loc->start; | |
387 | bracketed_id_context_state = YY_START; | |
388 | BEGIN SC_BRACKETED_ID; | |
389 | } | |
390 | } | |
391 | ":" { | |
392 | BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL); | |
393 | *loc = id_loc; | |
394 | return ID_COLON; | |
395 | } | |
396 | . { | |
397 | ROLLBACK_CURRENT_TOKEN; | |
398 | BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL); | |
399 | *loc = id_loc; | |
400 | return ID; | |
401 | } | |
402 | <<EOF>> { | |
403 | BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL); | |
404 | *loc = id_loc; | |
405 | return ID; | |
406 | } | |
407 | } | |
408 | ||
409 | /*--------------------------------. | |
410 | | Scanning bracketed identifiers. | | |
411 | `--------------------------------*/ | |
412 | ||
413 | <SC_BRACKETED_ID> | |
414 | { | |
415 | {id} { | |
416 | if (bracketed_id_str) | |
417 | { | |
418 | complain (loc, complaint, | |
419 | _("unexpected identifier in bracketed name: %s"), | |
420 | quote (yytext)); | |
421 | } | |
422 | else | |
423 | { | |
424 | bracketed_id_str = uniqstr_new (yytext); | |
425 | bracketed_id_loc = *loc; | |
426 | } | |
427 | } | |
428 | "]" { | |
429 | BEGIN bracketed_id_context_state; | |
430 | if (bracketed_id_str) | |
431 | { | |
432 | if (INITIAL == bracketed_id_context_state) | |
433 | { | |
434 | val->uniqstr = bracketed_id_str; | |
435 | bracketed_id_str = 0; | |
436 | *loc = bracketed_id_loc; | |
437 | return BRACKETED_ID; | |
438 | } | |
439 | } | |
440 | else | |
441 | complain (loc, complaint, _("an identifier expected")); | |
442 | } | |
443 | ||
444 | [^\].A-Za-z0-9_/ \f\n\t\v]+|. { | |
445 | complain (loc, complaint, "%s: %s", | |
446 | ngettext ("invalid character in bracketed name", | |
447 | "invalid characters in bracketed name", yyleng), | |
448 | quote_mem (yytext, yyleng)); | |
449 | } | |
450 | ||
451 | <<EOF>> { | |
452 | BEGIN bracketed_id_context_state; | |
453 | unexpected_eof (bracketed_id_start, "]"); | |
454 | } | |
455 | } | |
456 | ||
457 | <SC_RETURN_BRACKETED_ID> | |
458 | { | |
459 | . { | |
460 | ROLLBACK_CURRENT_TOKEN; | |
461 | val->uniqstr = bracketed_id_str; | |
462 | bracketed_id_str = 0; | |
463 | *loc = bracketed_id_loc; | |
464 | BEGIN INITIAL; | |
465 | return BRACKETED_ID; | |
466 | } | |
467 | } | |
468 | ||
469 | ||
470 | /*---------------------------------------------------------------. | |
471 | | Scanning a Yacc comment. The initial '/ *' is already eaten. | | |
472 | `---------------------------------------------------------------*/ | |
473 | ||
474 | <SC_YACC_COMMENT> | |
475 | { | |
476 | "*/" BEGIN context_state; | |
477 | .|\n continue; | |
478 | <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state; | |
479 | } | |
480 | ||
481 | ||
482 | /*------------------------------------------------------------. | |
483 | | Scanning a C comment. The initial '/ *' is already eaten. | | |
484 | `------------------------------------------------------------*/ | |
485 | ||
486 | <SC_COMMENT> | |
487 | { | |
488 | "*"{splice}"/" STRING_GROW; BEGIN context_state; | |
489 | <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state; | |
490 | } | |
491 | ||
492 | ||
493 | /*--------------------------------------------------------------. | |
494 | | Scanning a line comment. The initial '//' is already eaten. | | |
495 | `--------------------------------------------------------------*/ | |
496 | ||
497 | <SC_LINE_COMMENT> | |
498 | { | |
499 | "\n" STRING_GROW; BEGIN context_state; | |
500 | {splice} STRING_GROW; | |
501 | <<EOF>> BEGIN context_state; | |
502 | } | |
503 | ||
504 | ||
505 | /*------------------------------------------------. | |
506 | | Scanning a Bison string, including its escapes. | | |
507 | | The initial quote is already eaten. | | |
508 | `------------------------------------------------*/ | |
509 | ||
510 | <SC_ESCAPED_STRING> | |
511 | { | |
512 | "\"" { | |
513 | STRING_FINISH; | |
514 | loc->start = token_start; | |
515 | val->chars = last_string; | |
516 | BEGIN INITIAL; | |
517 | return STRING; | |
518 | } | |
519 | <<EOF>> unexpected_eof (token_start, "\""); | |
520 | "\n" unexpected_newline (token_start, "\""); | |
521 | } | |
522 | ||
523 | /*----------------------------------------------------------. | |
524 | | Scanning a Bison character literal, decoding its escapes. | | |
525 | | The initial quote is already eaten. | | |
526 | `----------------------------------------------------------*/ | |
527 | ||
528 | <SC_ESCAPED_CHARACTER> | |
529 | { | |
530 | "'" { | |
531 | STRING_FINISH; | |
532 | loc->start = token_start; | |
533 | val->character = last_string[0]; | |
534 | ||
535 | /* FIXME: Eventually, make these errors. */ | |
536 | if (last_string[0] == '\0') | |
537 | { | |
538 | complain (loc, Wother, _("empty character literal")); | |
539 | /* '\0' seems dangerous even if we are about to complain. */ | |
540 | val->character = '\''; | |
541 | } | |
542 | else if (last_string[1] != '\0') | |
543 | complain (loc, Wother, | |
544 | _("extra characters in character literal")); | |
545 | STRING_FREE; | |
546 | BEGIN INITIAL; | |
547 | return CHAR; | |
548 | } | |
549 | "\n" unexpected_newline (token_start, "'"); | |
550 | <<EOF>> unexpected_eof (token_start, "'"); | |
551 | } | |
552 | ||
553 | ||
554 | ||
555 | /*--------------------------------------------------------------. | |
556 | | Scanning a tag. The initial angle bracket is already eaten. | | |
557 | `--------------------------------------------------------------*/ | |
558 | ||
559 | <SC_TAG> | |
560 | { | |
561 | ">" { | |
562 | --nesting; | |
563 | if (nesting < 0) | |
564 | { | |
565 | STRING_FINISH; | |
566 | loc->start = token_start; | |
567 | val->uniqstr = uniqstr_new (last_string); | |
568 | STRING_FREE; | |
569 | BEGIN INITIAL; | |
570 | return TAG; | |
571 | } | |
572 | STRING_GROW; | |
573 | } | |
574 | ||
575 | ([^<>]|->)+ STRING_GROW; | |
576 | "<"+ STRING_GROW; nesting += yyleng; | |
577 | ||
578 | <<EOF>> unexpected_eof (token_start, ">"); | |
579 | } | |
580 | ||
581 | /*----------------------------. | |
582 | | Decode escaped characters. | | |
583 | `----------------------------*/ | |
584 | ||
585 | <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER> | |
586 | { | |
587 | \\[0-7]{1,3} { | |
588 | unsigned long int c = strtoul (yytext + 1, NULL, 8); | |
589 | if (!c || UCHAR_MAX < c) | |
590 | complain (loc, complaint, _("invalid number after \\-escape: %s"), | |
591 | yytext+1); | |
592 | else | |
593 | obstack_1grow (&obstack_for_string, c); | |
594 | } | |
595 | ||
596 | \\x[0-9abcdefABCDEF]+ { | |
597 | verify (UCHAR_MAX < ULONG_MAX); | |
598 | unsigned long int c = strtoul (yytext + 2, NULL, 16); | |
599 | if (!c || UCHAR_MAX < c) | |
600 | complain (loc, complaint, _("invalid number after \\-escape: %s"), | |
601 | yytext+1); | |
602 | else | |
603 | obstack_1grow (&obstack_for_string, c); | |
604 | } | |
605 | ||
606 | \\a obstack_1grow (&obstack_for_string, '\a'); | |
607 | \\b obstack_1grow (&obstack_for_string, '\b'); | |
608 | \\f obstack_1grow (&obstack_for_string, '\f'); | |
609 | \\n obstack_1grow (&obstack_for_string, '\n'); | |
610 | \\r obstack_1grow (&obstack_for_string, '\r'); | |
611 | \\t obstack_1grow (&obstack_for_string, '\t'); | |
612 | \\v obstack_1grow (&obstack_for_string, '\v'); | |
613 | ||
614 | /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */ | |
615 | \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]); | |
616 | ||
617 | \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} { | |
618 | int c = convert_ucn_to_byte (yytext); | |
619 | if (c <= 0) | |
620 | complain (loc, complaint, _("invalid number after \\-escape: %s"), | |
621 | yytext+1); | |
622 | else | |
623 | obstack_1grow (&obstack_for_string, c); | |
624 | } | |
625 | \\(.|\n) { | |
626 | char const *p = yytext + 1; | |
627 | /* Quote only if escaping won't make the character visible. */ | |
628 | if (c_isspace ((unsigned char) *p) && c_isprint ((unsigned char) *p)) | |
629 | p = quote (p); | |
630 | else | |
631 | p = quotearg_style_mem (escape_quoting_style, p, 1); | |
632 | complain (loc, complaint, _("invalid character after \\-escape: %s"), | |
633 | p); | |
634 | } | |
635 | } | |
636 | ||
637 | /*--------------------------------------------. | |
638 | | Scanning user-code characters and strings. | | |
639 | `--------------------------------------------*/ | |
640 | ||
641 | <SC_CHARACTER,SC_STRING> | |
642 | { | |
643 | {splice}|\\{splice}[^\n\[\]] STRING_GROW; | |
644 | } | |
645 | ||
646 | <SC_CHARACTER> | |
647 | { | |
648 | "'" STRING_GROW; BEGIN context_state; | |
649 | \n unexpected_newline (token_start, "'"); | |
650 | <<EOF>> unexpected_eof (token_start, "'"); | |
651 | } | |
652 | ||
653 | <SC_STRING> | |
654 | { | |
655 | "\"" STRING_GROW; BEGIN context_state; | |
656 | \n unexpected_newline (token_start, "\""); | |
657 | <<EOF>> unexpected_eof (token_start, "\""); | |
658 | } | |
659 | ||
660 | ||
661 | /*---------------------------------------------------. | |
662 | | Strings, comments etc. can be found in user code. | | |
663 | `---------------------------------------------------*/ | |
664 | ||
665 | <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_PREDICATE> | |
666 | { | |
667 | "'" { | |
668 | STRING_GROW; | |
669 | context_state = YY_START; | |
670 | token_start = loc->start; | |
671 | BEGIN SC_CHARACTER; | |
672 | } | |
673 | "\"" { | |
674 | STRING_GROW; | |
675 | context_state = YY_START; | |
676 | token_start = loc->start; | |
677 | BEGIN SC_STRING; | |
678 | } | |
679 | "/"{splice}"*" { | |
680 | STRING_GROW; | |
681 | context_state = YY_START; | |
682 | token_start = loc->start; | |
683 | BEGIN SC_COMMENT; | |
684 | } | |
685 | "/"{splice}"/" { | |
686 | STRING_GROW; | |
687 | context_state = YY_START; | |
688 | BEGIN SC_LINE_COMMENT; | |
689 | } | |
690 | } | |
691 | ||
692 | ||
693 | ||
694 | /*-----------------------------------------------------------. | |
695 | | Scanning some code in braces (actions, predicates). The | | |
696 | | initial "{" is already eaten. | | |
697 | `-----------------------------------------------------------*/ | |
698 | ||
699 | <SC_BRACED_CODE,SC_PREDICATE> | |
700 | { | |
701 | "{"|"<"{splice}"%" STRING_GROW; nesting++; | |
702 | "%"{splice}">" STRING_GROW; nesting--; | |
703 | ||
704 | /* Tokenize '<<%' correctly (as '<<' '%') rather than incorrrectly | |
705 | (as '<' '<%'). */ | |
706 | "<"{splice}"<" STRING_GROW; | |
707 | ||
708 | <<EOF>> unexpected_eof (code_start, "}"); | |
709 | } | |
710 | ||
711 | <SC_BRACED_CODE> | |
712 | { | |
713 | "}" { | |
714 | obstack_1grow (&obstack_for_string, '}'); | |
715 | ||
716 | --nesting; | |
717 | if (nesting < 0) | |
718 | { | |
719 | STRING_FINISH; | |
720 | loc->start = code_start; | |
721 | val->code = last_string; | |
722 | BEGIN INITIAL; | |
723 | return BRACED_CODE; | |
724 | } | |
725 | } | |
726 | } | |
727 | ||
728 | <SC_PREDICATE> | |
729 | { | |
730 | "}" { | |
731 | --nesting; | |
732 | if (nesting < 0) | |
733 | { | |
734 | STRING_FINISH; | |
735 | loc->start = code_start; | |
736 | val->code = last_string; | |
737 | BEGIN INITIAL; | |
738 | return BRACED_PREDICATE; | |
739 | } | |
740 | else | |
741 | obstack_1grow (&obstack_for_string, '}'); | |
742 | } | |
743 | } | |
744 | ||
745 | /*--------------------------------------------------------------. | |
746 | | Scanning some prologue: from "%{" (already scanned) to "%}". | | |
747 | `--------------------------------------------------------------*/ | |
748 | ||
749 | <SC_PROLOGUE> | |
750 | { | |
751 | "%}" { | |
752 | STRING_FINISH; | |
753 | loc->start = code_start; | |
754 | val->chars = last_string; | |
755 | BEGIN INITIAL; | |
756 | return PROLOGUE; | |
757 | } | |
758 | ||
759 | <<EOF>> unexpected_eof (code_start, "%}"); | |
760 | } | |
761 | ||
762 | ||
763 | /*---------------------------------------------------------------. | |
764 | | Scanning the epilogue (everything after the second "%%", which | | |
765 | | has already been eaten). | | |
766 | `---------------------------------------------------------------*/ | |
767 | ||
768 | <SC_EPILOGUE> | |
769 | { | |
770 | <<EOF>> { | |
771 | STRING_FINISH; | |
772 | loc->start = code_start; | |
773 | val->chars = last_string; | |
774 | BEGIN INITIAL; | |
775 | return EPILOGUE; | |
776 | } | |
777 | } | |
778 | ||
779 | ||
780 | /*-----------------------------------------------------. | |
781 | | By default, grow the string obstack with the input. | | |
782 | `-----------------------------------------------------*/ | |
783 | ||
784 | <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PREDICATE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. | | |
785 | <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PREDICATE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW; | |
786 | ||
787 | %% | |
788 | ||
789 | /* Read bytes from FP into buffer BUF of size SIZE. Return the | |
790 | number of bytes read. Remove '\r' from input, treating \r\n | |
791 | and isolated \r as \n. */ | |
792 | ||
793 | static size_t | |
794 | no_cr_read (FILE *fp, char *buf, size_t size) | |
795 | { | |
796 | size_t bytes_read = fread (buf, 1, size, fp); | |
797 | if (bytes_read) | |
798 | { | |
799 | char *w = memchr (buf, '\r', bytes_read); | |
800 | if (w) | |
801 | { | |
802 | char const *r = ++w; | |
803 | char const *lim = buf + bytes_read; | |
804 | ||
805 | for (;;) | |
806 | { | |
807 | /* Found an '\r'. Treat it like '\n', but ignore any | |
808 | '\n' that immediately follows. */ | |
809 | w[-1] = '\n'; | |
810 | if (r == lim) | |
811 | { | |
812 | int ch = getc (fp); | |
813 | if (ch != '\n' && ungetc (ch, fp) != ch) | |
814 | break; | |
815 | } | |
816 | else if (*r == '\n') | |
817 | r++; | |
818 | ||
819 | /* Copy until the next '\r'. */ | |
820 | do | |
821 | { | |
822 | if (r == lim) | |
823 | return w - buf; | |
824 | } | |
825 | while ((*w++ = *r++) != '\r'); | |
826 | } | |
827 | ||
828 | return w - buf; | |
829 | } | |
830 | } | |
831 | ||
832 | return bytes_read; | |
833 | } | |
834 | ||
835 | ||
836 | ||
837 | /*------------------------------------------------------. | |
838 | | Scan NUMBER for a base-BASE integer at location LOC. | | |
839 | `------------------------------------------------------*/ | |
840 | ||
841 | static unsigned long int | |
842 | scan_integer (char const *number, int base, location loc) | |
843 | { | |
844 | verify (INT_MAX < ULONG_MAX); | |
845 | unsigned long int num = strtoul (number, NULL, base); | |
846 | ||
847 | if (INT_MAX < num) | |
848 | { | |
849 | complain (&loc, complaint, _("integer out of range: %s"), | |
850 | quote (number)); | |
851 | num = INT_MAX; | |
852 | } | |
853 | ||
854 | return num; | |
855 | } | |
856 | ||
857 | ||
858 | /*------------------------------------------------------------------. | |
859 | | Convert universal character name UCN to a single-byte character, | | |
860 | | and return that character. Return -1 if UCN does not correspond | | |
861 | | to a single-byte character. | | |
862 | `------------------------------------------------------------------*/ | |
863 | ||
864 | static int | |
865 | convert_ucn_to_byte (char const *ucn) | |
866 | { | |
867 | verify (UCHAR_MAX <= INT_MAX); | |
868 | unsigned long int code = strtoul (ucn + 2, NULL, 16); | |
869 | ||
870 | /* FIXME: Currently we assume Unicode-compatible unibyte characters | |
871 | on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On | |
872 | non-ASCII hosts we support only the portable C character set. | |
873 | These limitations should be removed once we add support for | |
874 | multibyte characters. */ | |
875 | ||
876 | if (UCHAR_MAX < code) | |
877 | return -1; | |
878 | ||
879 | #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e) | |
880 | { | |
881 | /* A non-ASCII host. Use CODE to index into a table of the C | |
882 | basic execution character set, which is guaranteed to exist on | |
883 | all Standard C platforms. This table also includes '$', '@', | |
884 | and '`', which are not in the basic execution character set but | |
885 | which are unibyte characters on all the platforms that we know | |
886 | about. */ | |
887 | static signed char const table[] = | |
888 | { | |
889 | '\0', -1, -1, -1, -1, -1, -1, '\a', | |
890 | '\b', '\t', '\n', '\v', '\f', '\r', -1, -1, | |
891 | -1, -1, -1, -1, -1, -1, -1, -1, | |
892 | -1, -1, -1, -1, -1, -1, -1, -1, | |
893 | ' ', '!', '"', '#', '$', '%', '&', '\'', | |
894 | '(', ')', '*', '+', ',', '-', '.', '/', | |
895 | '0', '1', '2', '3', '4', '5', '6', '7', | |
896 | '8', '9', ':', ';', '<', '=', '>', '?', | |
897 | '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', | |
898 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', | |
899 | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', | |
900 | 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', | |
901 | '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', | |
902 | 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', | |
903 | 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', | |
904 | 'x', 'y', 'z', '{', '|', '}', '~' | |
905 | }; | |
906 | ||
907 | code = code < sizeof table ? table[code] : -1; | |
908 | } | |
909 | #endif | |
910 | ||
911 | return code; | |
912 | } | |
913 | ||
914 | ||
915 | /*---------------------------------------------------------------------. | |
916 | | Handle '#line INT( "FILE")?\n'. ARGS has already skipped '#line '. | | |
917 | `---------------------------------------------------------------------*/ | |
918 | ||
919 | static void | |
920 | handle_syncline (char *args, location loc) | |
921 | { | |
922 | char *file; | |
923 | unsigned long int lineno = strtoul (args, &file, 10); | |
924 | if (INT_MAX <= lineno) | |
925 | { | |
926 | complain (&loc, Wother, _("line number overflow")); | |
927 | lineno = INT_MAX; | |
928 | } | |
929 | ||
930 | file = strchr (file, '"'); | |
931 | if (file) | |
932 | { | |
933 | *strchr (file + 1, '"') = '\0'; | |
934 | current_file = uniqstr_new (file + 1); | |
935 | } | |
936 | boundary_set (&scanner_cursor, current_file, lineno, 1); | |
937 | } | |
938 | ||
939 | ||
940 | /*----------------------------------------------------------------. | |
941 | | For a token or comment starting at START, report message MSGID, | | |
942 | | which should say that an end marker was found before the | | |
943 | | expected TOKEN_END. Then, pretend that TOKEN_END was found. | | |
944 | `----------------------------------------------------------------*/ | |
945 | ||
946 | static void | |
947 | unexpected_end (boundary start, char const *msgid, char const *token_end) | |
948 | { | |
949 | location loc; | |
950 | loc.start = start; | |
951 | loc.end = scanner_cursor; | |
952 | size_t i = strlen (token_end); | |
953 | ||
954 | /* Adjust scanner cursor so that any later message does not count | |
955 | the characters about to be inserted. */ | |
956 | scanner_cursor.column -= i; | |
957 | ||
958 | while (i != 0) | |
959 | unput (token_end[--i]); | |
960 | ||
961 | token_end = quote (token_end); | |
962 | /* Instead of '\'', display "'". */ | |
963 | if (STREQ (token_end, "'\\''")) | |
964 | token_end = "\"'\""; | |
965 | complain (&loc, complaint, _(msgid), token_end); | |
966 | } | |
967 | ||
968 | ||
969 | /*------------------------------------------------------------------------. | |
970 | | Report an unexpected EOF in a token or comment starting at START. | | |
971 | | An end of file was encountered and the expected TOKEN_END was missing. | | |
972 | | After reporting the problem, pretend that TOKEN_END was found. | | |
973 | `------------------------------------------------------------------------*/ | |
974 | ||
975 | static void | |
976 | unexpected_eof (boundary start, char const *token_end) | |
977 | { | |
978 | unexpected_end (start, N_("missing %s at end of file"), token_end); | |
979 | } | |
980 | ||
981 | ||
982 | /*----------------------------------------. | |
983 | | Likewise, but for unexpected newlines. | | |
984 | `----------------------------------------*/ | |
985 | ||
986 | static void | |
987 | unexpected_newline (boundary start, char const *token_end) | |
988 | { | |
989 | unexpected_end (start, N_("missing %s at end of line"), token_end); | |
990 | } | |
991 | ||
992 | ||
993 | /*-------------------------. | |
994 | | Initialize the scanner. | | |
995 | `-------------------------*/ | |
996 | ||
997 | void | |
998 | gram_scanner_initialize (void) | |
999 | { | |
1000 | obstack_init (&obstack_for_string); | |
1001 | } | |
1002 | ||
1003 | ||
1004 | /*-----------------------------------------------. | |
1005 | | Free all the memory allocated to the scanner. | | |
1006 | `-----------------------------------------------*/ | |
1007 | ||
1008 | void | |
1009 | gram_scanner_free (void) | |
1010 | { | |
1011 | obstack_free (&obstack_for_string, 0); | |
1012 | /* Reclaim Flex's buffers. */ | |
1013 | yylex_destroy (); | |
1014 | } |