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