]>
Commit | Line | Data |
---|---|---|
e9071366 AD |
1 | /* Bison Action Scanner -*- C -*- |
2 | ||
3 | Copyright (C) 2006 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 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 | |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
20 | 02110-1301 USA | |
21 | */ | |
22 | ||
23 | %option debug nodefault nounput noyywrap never-interactive | |
24 | %option prefix="code_" outfile="lex.yy.c" | |
25 | ||
26 | %{ | |
9e668899 JD |
27 | #include <config.h> |
28 | #include "system.h" | |
29 | ||
e9071366 AD |
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 code_wrap | |
33 | #define code_wrap() 1 | |
34 | ||
35 | #define FLEX_PREFIX(Id) code_ ## Id | |
0c8e079f | 36 | #include "complain.h" |
e9071366 AD |
37 | #include "flex-scanner.h" |
38 | #include "reader.h" | |
39 | #include "getargs.h" | |
40 | #include <assert.h> | |
41 | #include <get-errno.h> | |
42 | #include <quote.h> | |
43 | ||
44 | #include "scan-code.h" | |
45 | ||
46 | /* The current calling start condition: SC_RULE_ACTION or | |
47 | SC_SYMBOL_ACTION. */ | |
4210cd0b | 48 | # define YY_DECL const char *code_lex (int sc_context, symbol_list *rule) |
e9071366 AD |
49 | YY_DECL; |
50 | ||
51 | #define YY_USER_ACTION location_compute (loc, &loc->end, yytext, yyleng); | |
52 | ||
4210cd0b JD |
53 | static void handle_action_dollar (symbol_list *rule, char *cp, |
54 | location dollar_loc); | |
55 | static void handle_action_at (symbol_list *rule, char *cp, location at_loc); | |
e9071366 AD |
56 | static location the_location; |
57 | static location *loc = &the_location; | |
e9071366 AD |
58 | %} |
59 | /* C and C++ comments in code. */ | |
60 | %x SC_COMMENT SC_LINE_COMMENT | |
61 | /* Strings and characters in code. */ | |
62 | %x SC_STRING SC_CHARACTER | |
63 | /* Whether in a rule or symbol action. Specifies the translation | |
64 | of $ and @. */ | |
65 | %x SC_RULE_ACTION SC_SYMBOL_ACTION | |
66 | ||
67 | ||
68 | /* POSIX says that a tag must be both an id and a C union member, but | |
69 | historically almost any character is allowed in a tag. We disallow | |
70 | NUL and newline, as this simplifies our implementation. */ | |
71 | tag [^\0\n>]+ | |
72 | ||
73 | /* Zero or more instances of backslash-newline. Following GCC, allow | |
74 | white space between the backslash and the newline. */ | |
75 | splice (\\[ \f\t\v]*\n)* | |
76 | ||
77 | %% | |
78 | ||
79 | %{ | |
2346344a AD |
80 | /* Nesting level of the current code in braces. */ |
81 | int braces_level IF_LINT (= 0); | |
82 | ||
e9071366 AD |
83 | /* This scanner is special: it is invoked only once, henceforth |
84 | is expected to return only once. This initialization is | |
85 | therefore done once per action to translate. */ | |
86 | assert (sc_context == SC_SYMBOL_ACTION | |
87 | || sc_context == SC_RULE_ACTION | |
88 | || sc_context == INITIAL); | |
89 | BEGIN sc_context; | |
90 | %} | |
91 | ||
92 | /*------------------------------------------------------------. | |
93 | | Scanning a C comment. The initial `/ *' is already eaten. | | |
94 | `------------------------------------------------------------*/ | |
95 | ||
96 | <SC_COMMENT> | |
97 | { | |
98 | "*"{splice}"/" STRING_GROW; BEGIN sc_context; | |
99 | } | |
100 | ||
101 | ||
102 | /*--------------------------------------------------------------. | |
103 | | Scanning a line comment. The initial `//' is already eaten. | | |
104 | `--------------------------------------------------------------*/ | |
105 | ||
106 | <SC_LINE_COMMENT> | |
107 | { | |
108 | "\n" STRING_GROW; BEGIN sc_context; | |
109 | {splice} STRING_GROW; | |
110 | } | |
111 | ||
112 | ||
113 | /*--------------------------------------------. | |
114 | | Scanning user-code characters and strings. | | |
115 | `--------------------------------------------*/ | |
116 | ||
117 | <SC_CHARACTER,SC_STRING> | |
118 | { | |
119 | {splice}|\\{splice}. STRING_GROW; | |
120 | } | |
121 | ||
122 | <SC_CHARACTER> | |
123 | { | |
124 | "'" STRING_GROW; BEGIN sc_context; | |
125 | } | |
126 | ||
127 | <SC_STRING> | |
128 | { | |
129 | "\"" STRING_GROW; BEGIN sc_context; | |
130 | } | |
131 | ||
132 | ||
133 | <SC_RULE_ACTION,SC_SYMBOL_ACTION>{ | |
134 | "'" { | |
135 | STRING_GROW; | |
136 | BEGIN SC_CHARACTER; | |
137 | } | |
138 | "\"" { | |
139 | STRING_GROW; | |
140 | BEGIN SC_STRING; | |
141 | } | |
142 | "/"{splice}"*" { | |
143 | STRING_GROW; | |
144 | BEGIN SC_COMMENT; | |
145 | } | |
146 | "/"{splice}"/" { | |
147 | STRING_GROW; | |
148 | BEGIN SC_LINE_COMMENT; | |
149 | } | |
150 | } | |
151 | ||
152 | <SC_RULE_ACTION> | |
153 | { | |
4210cd0b JD |
154 | "$"("<"{tag}">")?(-?[0-9]+|"$") handle_action_dollar (rule, yytext, *loc); |
155 | "@"(-?[0-9]+|"$") handle_action_at (rule, yytext, *loc); | |
e9071366 AD |
156 | |
157 | "$" { | |
158 | warn_at (*loc, _("stray `$'")); | |
159 | obstack_sgrow (&obstack_for_string, "$]["); | |
160 | } | |
161 | "@" { | |
162 | warn_at (*loc, _("stray `@'")); | |
163 | obstack_sgrow (&obstack_for_string, "@@"); | |
164 | } | |
2346344a AD |
165 | |
166 | "{" STRING_GROW; ++braces_level; | |
167 | "}" { | |
168 | bool outer_brace = --braces_level < 0; | |
169 | ||
170 | /* As an undocumented Bison extension, append `;' before the last | |
171 | brace in braced code, so that the user code can omit trailing | |
172 | `;'. But do not append `;' if emulating Yacc, since Yacc does | |
173 | not append one. | |
174 | ||
175 | FIXME: Bison should warn if a semicolon seems to be necessary | |
176 | here, and should omit the semicolon if it seems unnecessary | |
177 | (e.g., after ';', '{', or '}', each followed by comments or | |
178 | white space). Such a warning shouldn't depend on --yacc; it | |
179 | should depend on a new --pedantic option, which would cause | |
180 | Bison to warn if it detects an extension to POSIX. --pedantic | |
181 | should also diagnose other Bison extensions like %yacc. | |
182 | Perhaps there should also be a GCC-style --pedantic-errors | |
183 | option, so that such warnings are diagnosed as errors. */ | |
184 | if (outer_brace && ! yacc_flag) | |
185 | obstack_1grow (&obstack_for_string, ';'); | |
186 | ||
187 | STRING_GROW; | |
188 | } | |
e9071366 AD |
189 | } |
190 | ||
191 | <SC_SYMBOL_ACTION> | |
192 | { | |
193 | "$$" obstack_sgrow (&obstack_for_string, "]b4_dollar_dollar["); | |
194 | "@$" obstack_sgrow (&obstack_for_string, "]b4_at_dollar["); | |
195 | } | |
196 | ||
197 | ||
198 | /*-----------------------------------------. | |
199 | | Escape M4 quoting characters in C code. | | |
200 | `-----------------------------------------*/ | |
201 | ||
202 | <*> | |
203 | { | |
204 | \$ obstack_sgrow (&obstack_for_string, "$]["); | |
205 | \@ obstack_sgrow (&obstack_for_string, "@@"); | |
206 | \[ obstack_sgrow (&obstack_for_string, "@{"); | |
207 | \] obstack_sgrow (&obstack_for_string, "@}"); | |
208 | } | |
209 | ||
210 | /*-----------------------------------------------------. | |
211 | | By default, grow the string obstack with the input. | | |
212 | `-----------------------------------------------------*/ | |
213 | ||
214 | <*>.|\n STRING_GROW; | |
215 | ||
216 | /* End of processing. */ | |
217 | <*><<EOF>> { | |
218 | obstack_1grow (&obstack_for_string, '\0'); | |
219 | return obstack_finish (&obstack_for_string); | |
220 | } | |
221 | ||
222 | %% | |
223 | ||
224 | /* Keeps track of the maximum number of semantic values to the left of | |
225 | a handle (those referenced by $0, $-1, etc.) are required by the | |
226 | semantic actions of this grammar. */ | |
227 | int max_left_semantic_context = 0; | |
228 | ||
229 | ||
230 | /*------------------------------------------------------------------. | |
231 | | TEXT is pointing to a wannabee semantic value (i.e., a `$'). | | |
232 | | | | |
233 | | Possible inputs: $[<TYPENAME>]($|integer) | | |
234 | | | | |
235 | | Output to OBSTACK_FOR_STRING a reference to this semantic value. | | |
236 | `------------------------------------------------------------------*/ | |
237 | ||
238 | static void | |
4210cd0b | 239 | handle_action_dollar (symbol_list *rule, char *text, location dollar_loc) |
e9071366 AD |
240 | { |
241 | const char *type_name = NULL; | |
242 | char *cp = text + 1; | |
ffa4ba3a JD |
243 | symbol_list *effective_rule; |
244 | int effective_rule_length; | |
245 | ||
246 | if (rule->midrule_parent_rule) | |
247 | { | |
248 | effective_rule = rule->midrule_parent_rule; | |
249 | effective_rule_length = rule->midrule_parent_rhs_index - 1; | |
250 | } | |
251 | else | |
252 | { | |
253 | effective_rule = rule; | |
254 | effective_rule_length = symbol_list_length (rule->next); | |
255 | } | |
e9071366 AD |
256 | |
257 | /* Get the type name if explicit. */ | |
258 | if (*cp == '<') | |
259 | { | |
260 | type_name = ++cp; | |
261 | while (*cp != '>') | |
262 | ++cp; | |
263 | *cp = '\0'; | |
264 | ++cp; | |
265 | } | |
266 | ||
267 | if (*cp == '$') | |
268 | { | |
269 | if (!type_name) | |
4210cd0b | 270 | type_name = symbol_list_n_type_name_get (rule, dollar_loc, 0); |
e9071366 | 271 | if (!type_name && typed) |
ad6b1efa JD |
272 | { |
273 | if (rule->midrule_parent_rule) | |
274 | complain_at (dollar_loc, | |
275 | _("$$ for the midrule at $%d of `%s' has no declared" | |
276 | " type"), | |
277 | rule->midrule_parent_rhs_index, | |
278 | effective_rule->sym->tag); | |
279 | else | |
280 | complain_at (dollar_loc, _("$$ of `%s' has no declared type"), | |
281 | rule->sym->tag); | |
282 | } | |
e9071366 AD |
283 | if (!type_name) |
284 | type_name = ""; | |
285 | obstack_fgrow1 (&obstack_for_string, | |
286 | "]b4_lhs_value([%s])[", type_name); | |
4210cd0b | 287 | rule->used = true; |
e9071366 AD |
288 | } |
289 | else | |
290 | { | |
291 | long int num; | |
292 | set_errno (0); | |
293 | num = strtol (cp, 0, 10); | |
ffa4ba3a | 294 | if (INT_MIN <= num && num <= effective_rule_length && ! get_errno ()) |
e9071366 AD |
295 | { |
296 | int n = num; | |
297 | if (1-n > max_left_semantic_context) | |
298 | max_left_semantic_context = 1-n; | |
299 | if (!type_name && n > 0) | |
0c8e079f | 300 | type_name = |
ffa4ba3a | 301 | symbol_list_n_type_name_get (effective_rule, dollar_loc, n); |
e9071366 | 302 | if (!type_name && typed) |
0c8e079f | 303 | complain_at (dollar_loc, _("$%d of `%s' has no declared type"), |
ffa4ba3a | 304 | n, effective_rule->sym->tag); |
e9071366 AD |
305 | if (!type_name) |
306 | type_name = ""; | |
307 | obstack_fgrow3 (&obstack_for_string, | |
308 | "]b4_rhs_value(%d, %d, [%s])[", | |
ffa4ba3a JD |
309 | effective_rule_length, n, type_name); |
310 | symbol_list_n_used_set (effective_rule, n, true); | |
e9071366 AD |
311 | } |
312 | else | |
0c8e079f | 313 | complain_at (dollar_loc, _("integer out of range: %s"), quote (text)); |
e9071366 AD |
314 | } |
315 | } | |
316 | ||
317 | ||
318 | /*------------------------------------------------------. | |
319 | | TEXT is a location token (i.e., a `@...'). Output to | | |
320 | | OBSTACK_FOR_STRING a reference to this location. | | |
321 | `------------------------------------------------------*/ | |
322 | ||
323 | static void | |
4210cd0b | 324 | handle_action_at (symbol_list *rule, char *text, location at_loc) |
e9071366 AD |
325 | { |
326 | char *cp = text + 1; | |
e9071366 | 327 | locations_flag = true; |
ffa4ba3a JD |
328 | int effective_rule_length; |
329 | ||
330 | if (rule->midrule_parent_rule) | |
331 | effective_rule_length = rule->midrule_parent_rhs_index - 1; | |
332 | else | |
333 | effective_rule_length = symbol_list_length (rule->next); | |
e9071366 AD |
334 | |
335 | if (*cp == '$') | |
336 | obstack_sgrow (&obstack_for_string, "]b4_lhs_location["); | |
337 | else | |
338 | { | |
339 | long int num; | |
340 | set_errno (0); | |
341 | num = strtol (cp, 0, 10); | |
342 | ||
ffa4ba3a | 343 | if (INT_MIN <= num && num <= effective_rule_length && ! get_errno ()) |
e9071366 AD |
344 | { |
345 | int n = num; | |
346 | obstack_fgrow2 (&obstack_for_string, "]b4_rhs_location(%d, %d)[", | |
ffa4ba3a | 347 | effective_rule_length, n); |
e9071366 AD |
348 | } |
349 | else | |
0c8e079f | 350 | complain_at (at_loc, _("integer out of range: %s"), quote (text)); |
e9071366 AD |
351 | } |
352 | } | |
353 | ||
354 | ||
355 | /*-------------------------. | |
356 | | Initialize the scanner. | | |
357 | `-------------------------*/ | |
358 | ||
359 | /* Translate the dollars and ats in \a a, whose location is l. | |
360 | Depending on the \a sc_context (SC_RULE_ACTION, SC_SYMBOL_ACTION, | |
361 | INITIAL), the processing is different. */ | |
362 | ||
363 | static const char * | |
4210cd0b | 364 | translate_action (int sc_context, symbol_list *rule, const char *a, location l) |
e9071366 AD |
365 | { |
366 | const char *res; | |
367 | static bool initialized = false; | |
368 | if (!initialized) | |
369 | { | |
370 | obstack_init (&obstack_for_string); | |
371 | /* The initial buffer, never used. */ | |
372 | yy_delete_buffer (YY_CURRENT_BUFFER); | |
373 | yy_flex_debug = 0; | |
374 | initialized = true; | |
375 | } | |
376 | ||
377 | loc->start = loc->end = l.start; | |
378 | yy_switch_to_buffer (yy_scan_string (a)); | |
4210cd0b | 379 | res = code_lex (sc_context, rule); |
e9071366 AD |
380 | yy_delete_buffer (YY_CURRENT_BUFFER); |
381 | ||
382 | return res; | |
383 | } | |
384 | ||
385 | const char * | |
e256e17f | 386 | translate_rule_action (symbol_list *rule) |
e9071366 | 387 | { |
e256e17f JD |
388 | return translate_action (SC_RULE_ACTION, rule, rule->action, |
389 | rule->action_location); | |
e9071366 AD |
390 | } |
391 | ||
392 | const char * | |
393 | translate_symbol_action (const char *a, location l) | |
394 | { | |
4210cd0b | 395 | return translate_action (SC_SYMBOL_ACTION, NULL, a, l); |
e9071366 AD |
396 | } |
397 | ||
398 | const char * | |
399 | translate_code (const char *a, location l) | |
400 | { | |
4210cd0b | 401 | return translate_action (INITIAL, NULL, a, l); |
e9071366 AD |
402 | } |
403 | ||
404 | /*-----------------------------------------------. | |
405 | | Free all the memory allocated to the scanner. | | |
406 | `-----------------------------------------------*/ | |
407 | ||
408 | void | |
409 | code_scanner_free (void) | |
410 | { | |
411 | obstack_free (&obstack_for_string, 0); | |
412 | /* Reclaim Flex's buffers. */ | |
413 | yy_delete_buffer (YY_CURRENT_BUFFER); | |
414 | } |