]>
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 | 56 | /* Location of scanner cursor. */ |
4a678af8 | 57 | static 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 | { | |
34f98f46 JD |
167 | "%after-header" return PERCENT_AFTER_HEADER; |
168 | "%before-header" return PERCENT_BEFORE_HEADER; | |
58d7a1a1 AD |
169 | "%binary" return PERCENT_NONASSOC; |
170 | "%debug" return PERCENT_DEBUG; | |
171 | "%default"[-_]"prec" return PERCENT_DEFAULT_PREC; | |
172 | "%define" return PERCENT_DEFINE; | |
173 | "%defines" return PERCENT_DEFINES; | |
174 | "%destructor" return PERCENT_DESTRUCTOR; | |
175 | "%dprec" return PERCENT_DPREC; | |
34f98f46 | 176 | "%end-header" return PERCENT_END_HEADER; |
58d7a1a1 AD |
177 | "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE; |
178 | "%expect" return PERCENT_EXPECT; | |
179 | "%expect"[-_]"rr" return PERCENT_EXPECT_RR; | |
180 | "%file-prefix" return PERCENT_FILE_PREFIX; | |
e9955c83 | 181 | "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC; |
58d7a1a1 AD |
182 | "%initial-action" return PERCENT_INITIAL_ACTION; |
183 | "%glr-parser" return PERCENT_GLR_PARSER; | |
184 | "%left" return PERCENT_LEFT; | |
185 | "%lex-param" return PERCENT_LEX_PARAM; | |
186 | "%locations" return PERCENT_LOCATIONS; | |
187 | "%merge" return PERCENT_MERGE; | |
188 | "%name"[-_]"prefix" return PERCENT_NAME_PREFIX; | |
189 | "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC; | |
190 | "%no"[-_]"lines" return PERCENT_NO_LINES; | |
191 | "%nonassoc" return PERCENT_NONASSOC; | |
192 | "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER; | |
193 | "%nterm" return PERCENT_NTERM; | |
194 | "%output" return PERCENT_OUTPUT; | |
195 | "%parse-param" return PERCENT_PARSE_PARAM; | |
196 | "%prec" return PERCENT_PREC; | |
197 | "%printer" return PERCENT_PRINTER; | |
198 | "%pure"[-_]"parser" return PERCENT_PURE_PARSER; | |
199 | "%require" return PERCENT_REQUIRE; | |
200 | "%right" return PERCENT_RIGHT; | |
201 | "%skeleton" return PERCENT_SKELETON; | |
202 | "%start" return PERCENT_START; | |
34f98f46 | 203 | "%start-header" return PERCENT_START_HEADER; |
58d7a1a1 AD |
204 | "%term" return PERCENT_TOKEN; |
205 | "%token" return PERCENT_TOKEN; | |
206 | "%token"[-_]"table" return PERCENT_TOKEN_TABLE; | |
207 | "%type" return PERCENT_TYPE; | |
208 | "%union" return PERCENT_UNION; | |
209 | "%verbose" return PERCENT_VERBOSE; | |
210 | "%yacc" return PERCENT_YACC; | |
e9955c83 | 211 | |
3f2d73f1 | 212 | {directive} { |
41141c56 | 213 | complain_at (*loc, _("invalid directive: %s"), quote (yytext)); |
412f8a59 | 214 | } |
900c5db5 | 215 | |
e9955c83 | 216 | "=" return EQUAL; |
e9071366 | 217 | "|" return PIPE; |
e9955c83 AD |
218 | ";" return SEMICOLON; |
219 | ||
3f2d73f1 | 220 | {id} { |
58d7a1a1 | 221 | val->uniqstr = uniqstr_new (yytext); |
3f2d73f1 | 222 | id_loc = *loc; |
3f2d73f1 | 223 | BEGIN SC_AFTER_IDENTIFIER; |
e9955c83 AD |
224 | } |
225 | ||
d8d3f94a | 226 | {int} { |
1452af69 PE |
227 | val->integer = scan_integer (yytext, 10, *loc); |
228 | return INT; | |
229 | } | |
230 | 0[xX][0-9abcdefABCDEF]+ { | |
231 | val->integer = scan_integer (yytext, 16, *loc); | |
d8d3f94a PE |
232 | return INT; |
233 | } | |
e9955c83 AD |
234 | |
235 | /* Characters. We don't check there is only one. */ | |
3f2d73f1 | 236 | "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER; |
e9955c83 AD |
237 | |
238 | /* Strings. */ | |
ca407bdf | 239 | "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING; |
e9955c83 AD |
240 | |
241 | /* Prologue. */ | |
3f2d73f1 | 242 | "%{" code_start = loc->start; BEGIN SC_PROLOGUE; |
e9955c83 AD |
243 | |
244 | /* Code in between braces. */ | |
3f2d73f1 PE |
245 | "{" { |
246 | STRING_GROW; | |
247 | braces_level = 0; | |
248 | code_start = loc->start; | |
249 | BEGIN SC_BRACED_CODE; | |
250 | } | |
e9955c83 AD |
251 | |
252 | /* A type. */ | |
d8d3f94a | 253 | "<"{tag}">" { |
223ff46e | 254 | obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2); |
41141c56 | 255 | STRING_FINISH; |
223ff46e | 256 | val->uniqstr = uniqstr_new (last_string); |
41141c56 | 257 | STRING_FREE; |
4cdb01db AD |
258 | return TYPE; |
259 | } | |
260 | ||
a706a1cc PE |
261 | "%%" { |
262 | static int percent_percent_count; | |
e9955c83 | 263 | if (++percent_percent_count == 2) |
a2bc9dbc | 264 | BEGIN SC_EPILOGUE; |
e9955c83 AD |
265 | return PERCENT_PERCENT; |
266 | } | |
267 | ||
a706a1cc | 268 | . { |
41141c56 | 269 | complain_at (*loc, _("invalid character: %s"), quote (yytext)); |
3f2d73f1 | 270 | } |
379f0ac8 PE |
271 | |
272 | <<EOF>> { | |
273 | loc->start = loc->end = scanner_cursor; | |
274 | yyterminate (); | |
275 | } | |
3f2d73f1 PE |
276 | } |
277 | ||
278 | ||
279 | /*-----------------------------------------------------------------. | |
280 | | Scanning after an identifier, checking whether a colon is next. | | |
281 | `-----------------------------------------------------------------*/ | |
282 | ||
283 | <SC_AFTER_IDENTIFIER> | |
284 | { | |
285 | ":" { | |
3f2d73f1 PE |
286 | *loc = id_loc; |
287 | BEGIN INITIAL; | |
288 | return ID_COLON; | |
289 | } | |
290 | . { | |
291 | scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0); | |
292 | yyless (0); | |
293 | *loc = id_loc; | |
294 | BEGIN INITIAL; | |
295 | return ID; | |
296 | } | |
297 | <<EOF>> { | |
298 | *loc = id_loc; | |
299 | BEGIN INITIAL; | |
300 | return ID; | |
e9955c83 AD |
301 | } |
302 | } | |
303 | ||
304 | ||
d8d3f94a PE |
305 | /*---------------------------------------------------------------. |
306 | | Scanning a Yacc comment. The initial `/ *' is already eaten. | | |
307 | `---------------------------------------------------------------*/ | |
e9955c83 | 308 | |
d8d3f94a | 309 | <SC_YACC_COMMENT> |
e9955c83 | 310 | { |
3f2d73f1 | 311 | "*/" BEGIN context_state; |
a706a1cc | 312 | .|\n ; |
aa418041 | 313 | <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state; |
d8d3f94a PE |
314 | } |
315 | ||
316 | ||
317 | /*------------------------------------------------------------. | |
318 | | Scanning a C comment. The initial `/ *' is already eaten. | | |
319 | `------------------------------------------------------------*/ | |
320 | ||
321 | <SC_COMMENT> | |
322 | { | |
3f2d73f1 | 323 | "*"{splice}"/" STRING_GROW; BEGIN context_state; |
aa418041 | 324 | <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state; |
e9955c83 AD |
325 | } |
326 | ||
327 | ||
d8d3f94a PE |
328 | /*--------------------------------------------------------------. |
329 | | Scanning a line comment. The initial `//' is already eaten. | | |
330 | `--------------------------------------------------------------*/ | |
331 | ||
332 | <SC_LINE_COMMENT> | |
333 | { | |
3f2d73f1 | 334 | "\n" STRING_GROW; BEGIN context_state; |
41141c56 | 335 | {splice} STRING_GROW; |
3f2d73f1 | 336 | <<EOF>> BEGIN context_state; |
d8d3f94a PE |
337 | } |
338 | ||
339 | ||
4febdd96 PE |
340 | /*------------------------------------------------. |
341 | | Scanning a Bison string, including its escapes. | | |
342 | | The initial quote is already eaten. | | |
343 | `------------------------------------------------*/ | |
e9955c83 AD |
344 | |
345 | <SC_ESCAPED_STRING> | |
346 | { | |
db2cc12f | 347 | "\"" { |
41141c56 | 348 | STRING_FINISH; |
3f2d73f1 | 349 | loc->start = token_start; |
223ff46e | 350 | val->chars = last_string; |
a706a1cc | 351 | BEGIN INITIAL; |
e9955c83 AD |
352 | return STRING; |
353 | } | |
4febdd96 PE |
354 | \n unexpected_newline (token_start, "\""); BEGIN INITIAL; |
355 | <<EOF>> unexpected_eof (token_start, "\""); BEGIN INITIAL; | |
e9955c83 AD |
356 | } |
357 | ||
4febdd96 PE |
358 | /*----------------------------------------------------------. |
359 | | Scanning a Bison character literal, decoding its escapes. | | |
360 | | The initial quote is already eaten. | | |
361 | `----------------------------------------------------------*/ | |
e9955c83 AD |
362 | |
363 | <SC_ESCAPED_CHARACTER> | |
364 | { | |
db2cc12f | 365 | "'" { |
41141c56 PE |
366 | STRING_GROW; |
367 | STRING_FINISH; | |
3f2d73f1 | 368 | loc->start = token_start; |
58d7a1a1 | 369 | val->character = last_string[1]; |
41141c56 | 370 | STRING_FREE; |
a706a1cc | 371 | BEGIN INITIAL; |
58d7a1a1 | 372 | return CHAR; |
e9955c83 | 373 | } |
4febdd96 PE |
374 | \n unexpected_newline (token_start, "'"); BEGIN INITIAL; |
375 | <<EOF>> unexpected_eof (token_start, "'"); BEGIN INITIAL; | |
376 | } | |
a706a1cc | 377 | |
4febdd96 PE |
378 | <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING> |
379 | { | |
92ac3705 | 380 | \0 complain_at (*loc, _("invalid null character")); |
e9955c83 AD |
381 | } |
382 | ||
383 | ||
384 | /*----------------------------. | |
385 | | Decode escaped characters. | | |
386 | `----------------------------*/ | |
387 | ||
388 | <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER> | |
389 | { | |
d8d3f94a | 390 | \\[0-7]{1,3} { |
4517da37 | 391 | unsigned long int c = strtoul (yytext + 1, NULL, 8); |
d8d3f94a | 392 | if (UCHAR_MAX < c) |
3f2d73f1 | 393 | complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext)); |
05ac60f3 | 394 | else if (! c) |
92ac3705 | 395 | complain_at (*loc, _("invalid null character: %s"), quote (yytext)); |
e9955c83 | 396 | else |
223ff46e | 397 | obstack_1grow (&obstack_for_string, c); |
e9955c83 AD |
398 | } |
399 | ||
6b0d38ab | 400 | \\x[0-9abcdefABCDEF]+ { |
4517da37 PE |
401 | verify (UCHAR_MAX < ULONG_MAX); |
402 | unsigned long int c = strtoul (yytext + 2, NULL, 16); | |
403 | if (UCHAR_MAX < c) | |
3f2d73f1 | 404 | complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext)); |
92ac3705 PE |
405 | else if (! c) |
406 | complain_at (*loc, _("invalid null character: %s"), quote (yytext)); | |
d8d3f94a | 407 | else |
223ff46e | 408 | obstack_1grow (&obstack_for_string, c); |
e9955c83 AD |
409 | } |
410 | ||
223ff46e PE |
411 | \\a obstack_1grow (&obstack_for_string, '\a'); |
412 | \\b obstack_1grow (&obstack_for_string, '\b'); | |
413 | \\f obstack_1grow (&obstack_for_string, '\f'); | |
414 | \\n obstack_1grow (&obstack_for_string, '\n'); | |
415 | \\r obstack_1grow (&obstack_for_string, '\r'); | |
416 | \\t obstack_1grow (&obstack_for_string, '\t'); | |
417 | \\v obstack_1grow (&obstack_for_string, '\v'); | |
412f8a59 PE |
418 | |
419 | /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */ | |
223ff46e | 420 | \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]); |
412f8a59 | 421 | |
6b0d38ab | 422 | \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} { |
d8d3f94a PE |
423 | int c = convert_ucn_to_byte (yytext); |
424 | if (c < 0) | |
3f2d73f1 | 425 | complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext)); |
92ac3705 PE |
426 | else if (! c) |
427 | complain_at (*loc, _("invalid null character: %s"), quote (yytext)); | |
d8d3f94a | 428 | else |
223ff46e | 429 | obstack_1grow (&obstack_for_string, c); |
d8d3f94a | 430 | } |
4f25ebb0 | 431 | \\(.|\n) { |
3f2d73f1 | 432 | complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext)); |
41141c56 | 433 | STRING_GROW; |
e9955c83 AD |
434 | } |
435 | } | |
436 | ||
4febdd96 PE |
437 | /*--------------------------------------------. |
438 | | Scanning user-code characters and strings. | | |
439 | `--------------------------------------------*/ | |
e9955c83 | 440 | |
4febdd96 PE |
441 | <SC_CHARACTER,SC_STRING> |
442 | { | |
e9071366 | 443 | {splice}|\\{splice}[^\n\[\]] STRING_GROW; |
4febdd96 | 444 | } |
e9955c83 AD |
445 | |
446 | <SC_CHARACTER> | |
447 | { | |
4febdd96 PE |
448 | "'" STRING_GROW; BEGIN context_state; |
449 | \n unexpected_newline (token_start, "'"); BEGIN context_state; | |
450 | <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state; | |
e9955c83 AD |
451 | } |
452 | ||
e9955c83 AD |
453 | <SC_STRING> |
454 | { | |
4febdd96 PE |
455 | "\"" STRING_GROW; BEGIN context_state; |
456 | \n unexpected_newline (token_start, "\""); BEGIN context_state; | |
457 | <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state; | |
e9955c83 AD |
458 | } |
459 | ||
460 | ||
461 | /*---------------------------------------------------. | |
462 | | Strings, comments etc. can be found in user code. | | |
463 | `---------------------------------------------------*/ | |
464 | ||
465 | <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE> | |
466 | { | |
3f2d73f1 PE |
467 | "'" { |
468 | STRING_GROW; | |
469 | context_state = YY_START; | |
470 | token_start = loc->start; | |
471 | BEGIN SC_CHARACTER; | |
472 | } | |
473 | "\"" { | |
474 | STRING_GROW; | |
475 | context_state = YY_START; | |
476 | token_start = loc->start; | |
477 | BEGIN SC_STRING; | |
478 | } | |
479 | "/"{splice}"*" { | |
480 | STRING_GROW; | |
481 | context_state = YY_START; | |
482 | token_start = loc->start; | |
483 | BEGIN SC_COMMENT; | |
484 | } | |
485 | "/"{splice}"/" { | |
486 | STRING_GROW; | |
487 | context_state = YY_START; | |
488 | BEGIN SC_LINE_COMMENT; | |
489 | } | |
e9955c83 AD |
490 | } |
491 | ||
492 | ||
624a35e2 | 493 | |
58d7a1a1 AD |
494 | /*-----------------------------------------------------------. |
495 | | Scanning some code in braces (actions). The initial "{" is | | |
496 | | already eaten. | | |
497 | `-----------------------------------------------------------*/ | |
e9955c83 AD |
498 | |
499 | <SC_BRACED_CODE> | |
500 | { | |
41141c56 PE |
501 | "{"|"<"{splice}"%" STRING_GROW; braces_level++; |
502 | "%"{splice}">" STRING_GROW; braces_level--; | |
e9955c83 | 503 | "}" { |
25522739 PE |
504 | obstack_1grow (&obstack_for_string, '}'); |
505 | ||
2346344a AD |
506 | --braces_level; |
507 | if (braces_level < 0) | |
e9955c83 | 508 | { |
41141c56 | 509 | STRING_FINISH; |
3f2d73f1 | 510 | loc->start = code_start; |
223ff46e | 511 | val->chars = last_string; |
e9071366 | 512 | gram_last_braced_code_loc = *loc; |
a706a1cc | 513 | BEGIN INITIAL; |
58d7a1a1 | 514 | return BRACED_CODE; |
e9955c83 AD |
515 | } |
516 | } | |
517 | ||
a706a1cc PE |
518 | /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly |
519 | (as `<' `<%'). */ | |
41141c56 | 520 | "<"{splice}"<" STRING_GROW; |
a706a1cc | 521 | |
aa418041 | 522 | <<EOF>> unexpected_eof (code_start, "}"); BEGIN INITIAL; |
e9955c83 AD |
523 | } |
524 | ||
525 | ||
526 | /*--------------------------------------------------------------. | |
527 | | Scanning some prologue: from "%{" (already scanned) to "%}". | | |
528 | `--------------------------------------------------------------*/ | |
529 | ||
530 | <SC_PROLOGUE> | |
531 | { | |
532 | "%}" { | |
41141c56 | 533 | STRING_FINISH; |
3f2d73f1 | 534 | loc->start = code_start; |
223ff46e | 535 | val->chars = last_string; |
a706a1cc | 536 | BEGIN INITIAL; |
e9955c83 AD |
537 | return PROLOGUE; |
538 | } | |
539 | ||
aa418041 | 540 | <<EOF>> unexpected_eof (code_start, "%}"); BEGIN INITIAL; |
e9955c83 AD |
541 | } |
542 | ||
543 | ||
544 | /*---------------------------------------------------------------. | |
545 | | Scanning the epilogue (everything after the second "%%", which | | |
d8d3f94a | 546 | | has already been eaten). | |
e9955c83 AD |
547 | `---------------------------------------------------------------*/ |
548 | ||
549 | <SC_EPILOGUE> | |
550 | { | |
e9955c83 | 551 | <<EOF>> { |
41141c56 | 552 | STRING_FINISH; |
3f2d73f1 | 553 | loc->start = code_start; |
223ff46e | 554 | val->chars = last_string; |
a706a1cc | 555 | BEGIN INITIAL; |
e9955c83 AD |
556 | return EPILOGUE; |
557 | } | |
558 | } | |
559 | ||
560 | ||
4febdd96 PE |
561 | /*-----------------------------------------------------. |
562 | | By default, grow the string obstack with the input. | | |
563 | `-----------------------------------------------------*/ | |
564 | ||
565 | <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. | | |
566 | <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW; | |
567 | ||
e9955c83 AD |
568 | %% |
569 | ||
6c30d641 PE |
570 | /* Read bytes from FP into buffer BUF of size SIZE. Return the |
571 | number of bytes read. Remove '\r' from input, treating \r\n | |
572 | and isolated \r as \n. */ | |
573 | ||
574 | static size_t | |
575 | no_cr_read (FILE *fp, char *buf, size_t size) | |
576 | { | |
a737b216 PE |
577 | size_t bytes_read = fread (buf, 1, size, fp); |
578 | if (bytes_read) | |
6c30d641 | 579 | { |
a737b216 | 580 | char *w = memchr (buf, '\r', bytes_read); |
6c30d641 PE |
581 | if (w) |
582 | { | |
583 | char const *r = ++w; | |
a737b216 | 584 | char const *lim = buf + bytes_read; |
6c30d641 PE |
585 | |
586 | for (;;) | |
587 | { | |
588 | /* Found an '\r'. Treat it like '\n', but ignore any | |
589 | '\n' that immediately follows. */ | |
590 | w[-1] = '\n'; | |
591 | if (r == lim) | |
592 | { | |
593 | int ch = getc (fp); | |
594 | if (ch != '\n' && ungetc (ch, fp) != ch) | |
595 | break; | |
596 | } | |
597 | else if (*r == '\n') | |
598 | r++; | |
599 | ||
600 | /* Copy until the next '\r'. */ | |
601 | do | |
602 | { | |
603 | if (r == lim) | |
604 | return w - buf; | |
605 | } | |
606 | while ((*w++ = *r++) != '\r'); | |
607 | } | |
608 | ||
609 | return w - buf; | |
610 | } | |
611 | } | |
612 | ||
a737b216 | 613 | return bytes_read; |
6c30d641 PE |
614 | } |
615 | ||
616 | ||
f25bfb75 | 617 | |
1452af69 PE |
618 | /*------------------------------------------------------. |
619 | | Scan NUMBER for a base-BASE integer at location LOC. | | |
620 | `------------------------------------------------------*/ | |
621 | ||
622 | static unsigned long int | |
623 | scan_integer (char const *number, int base, location loc) | |
624 | { | |
4517da37 PE |
625 | verify (INT_MAX < ULONG_MAX); |
626 | unsigned long int num = strtoul (number, NULL, base); | |
627 | ||
628 | if (INT_MAX < num) | |
1452af69 PE |
629 | { |
630 | complain_at (loc, _("integer out of range: %s"), quote (number)); | |
631 | num = INT_MAX; | |
632 | } | |
4517da37 | 633 | |
1452af69 PE |
634 | return num; |
635 | } | |
636 | ||
637 | ||
d8d3f94a PE |
638 | /*------------------------------------------------------------------. |
639 | | Convert universal character name UCN to a single-byte character, | | |
640 | | and return that character. Return -1 if UCN does not correspond | | |
641 | | to a single-byte character. | | |
642 | `------------------------------------------------------------------*/ | |
643 | ||
644 | static int | |
645 | convert_ucn_to_byte (char const *ucn) | |
646 | { | |
4517da37 PE |
647 | verify (UCHAR_MAX <= INT_MAX); |
648 | unsigned long int code = strtoul (ucn + 2, NULL, 16); | |
d8d3f94a PE |
649 | |
650 | /* FIXME: Currently we assume Unicode-compatible unibyte characters | |
651 | on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On | |
652 | non-ASCII hosts we support only the portable C character set. | |
653 | These limitations should be removed once we add support for | |
654 | multibyte characters. */ | |
655 | ||
656 | if (UCHAR_MAX < code) | |
657 | return -1; | |
658 | ||
659 | #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e) | |
660 | { | |
661 | /* A non-ASCII host. Use CODE to index into a table of the C | |
662 | basic execution character set, which is guaranteed to exist on | |
663 | all Standard C platforms. This table also includes '$', '@', | |
8e6ef483 | 664 | and '`', which are not in the basic execution character set but |
d8d3f94a PE |
665 | which are unibyte characters on all the platforms that we know |
666 | about. */ | |
667 | static signed char const table[] = | |
668 | { | |
669 | '\0', -1, -1, -1, -1, -1, -1, '\a', | |
670 | '\b', '\t', '\n', '\v', '\f', '\r', -1, -1, | |
671 | -1, -1, -1, -1, -1, -1, -1, -1, | |
672 | -1, -1, -1, -1, -1, -1, -1, -1, | |
673 | ' ', '!', '"', '#', '$', '%', '&', '\'', | |
674 | '(', ')', '*', '+', ',', '-', '.', '/', | |
675 | '0', '1', '2', '3', '4', '5', '6', '7', | |
676 | '8', '9', ':', ';', '<', '=', '>', '?', | |
677 | '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', | |
678 | 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', | |
679 | 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', | |
680 | 'X', 'Y', 'Z', '[', '\\', ']', '^', '_', | |
681 | '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', | |
682 | 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', | |
683 | 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', | |
684 | 'x', 'y', 'z', '{', '|', '}', '~' | |
685 | }; | |
686 | ||
687 | code = code < sizeof table ? table[code] : -1; | |
688 | } | |
689 | #endif | |
c4d720cd | 690 | |
d8d3f94a PE |
691 | return code; |
692 | } | |
693 | ||
694 | ||
900c5db5 AD |
695 | /*----------------------------------------------------------------. |
696 | | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. | | |
697 | `----------------------------------------------------------------*/ | |
698 | ||
699 | static void | |
4517da37 | 700 | handle_syncline (char *args, location loc) |
900c5db5 | 701 | { |
4517da37 PE |
702 | char *after_num; |
703 | unsigned long int lineno = strtoul (args, &after_num, 10); | |
704 | char *file = strchr (after_num, '"') + 1; | |
705 | *strchr (file, '"') = '\0'; | |
706 | if (INT_MAX <= lineno) | |
707 | { | |
708 | warn_at (loc, _("line number overflow")); | |
709 | lineno = INT_MAX; | |
710 | } | |
e9071366 | 711 | current_file = uniqstr_new (file); |
0c8e079f | 712 | boundary_set (&scanner_cursor, current_file, lineno, 1); |
4517da37 PE |
713 | } |
714 | ||
715 | ||
4febdd96 PE |
716 | /*----------------------------------------------------------------. |
717 | | For a token or comment starting at START, report message MSGID, | | |
718 | | which should say that an end marker was found before | | |
719 | | the expected TOKEN_END. | | |
720 | `----------------------------------------------------------------*/ | |
721 | ||
722 | static void | |
723 | unexpected_end (boundary start, char const *msgid, char const *token_end) | |
724 | { | |
725 | location loc; | |
726 | loc.start = start; | |
727 | loc.end = scanner_cursor; | |
728 | complain_at (loc, _(msgid), token_end); | |
729 | } | |
730 | ||
731 | ||
3f2d73f1 PE |
732 | /*------------------------------------------------------------------------. |
733 | | Report an unexpected EOF in a token or comment starting at START. | | |
734 | | An end of file was encountered and the expected TOKEN_END was missing. | | |
3f2d73f1 | 735 | `------------------------------------------------------------------------*/ |
a706a1cc PE |
736 | |
737 | static void | |
aa418041 | 738 | unexpected_eof (boundary start, char const *token_end) |
a706a1cc | 739 | { |
4febdd96 PE |
740 | unexpected_end (start, N_("missing `%s' at end of file"), token_end); |
741 | } | |
742 | ||
743 | ||
744 | /*----------------------------------------. | |
745 | | Likewise, but for unexpected newlines. | | |
746 | `----------------------------------------*/ | |
747 | ||
748 | static void | |
749 | unexpected_newline (boundary start, char const *token_end) | |
750 | { | |
751 | unexpected_end (start, N_("missing `%s' at end of line"), token_end); | |
a706a1cc PE |
752 | } |
753 | ||
754 | ||
f25bfb75 AD |
755 | /*-------------------------. |
756 | | Initialize the scanner. | | |
757 | `-------------------------*/ | |
758 | ||
1d6412ad | 759 | void |
e9071366 | 760 | gram_scanner_initialize (void) |
1d6412ad | 761 | { |
223ff46e | 762 | obstack_init (&obstack_for_string); |
1d6412ad AD |
763 | } |
764 | ||
765 | ||
f25bfb75 AD |
766 | /*-----------------------------------------------. |
767 | | Free all the memory allocated to the scanner. | | |
768 | `-----------------------------------------------*/ | |
769 | ||
4cdb01db | 770 | void |
e9071366 | 771 | gram_scanner_free (void) |
4cdb01db | 772 | { |
223ff46e | 773 | obstack_free (&obstack_for_string, 0); |
536545f3 AD |
774 | /* Reclaim Flex's buffers. */ |
775 | yy_delete_buffer (YY_CURRENT_BUFFER); | |
4cdb01db | 776 | } |