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