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