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