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