]> git.saurik.com Git - bison.git/blob - src/scan-code.l
Extract the parsing of user actions from the grammar scanner.
[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 /* This scanner is special: it is invoked only once, henceforth
79 is expected to return only once. This initialization is
80 therefore done once per action to translate. */
81 assert (sc_context == SC_SYMBOL_ACTION
82 || sc_context == SC_RULE_ACTION
83 || sc_context == INITIAL);
84 BEGIN sc_context;
85 %}
86
87 /*------------------------------------------------------------.
88 | Scanning a C comment. The initial `/ *' is already eaten. |
89 `------------------------------------------------------------*/
90
91 <SC_COMMENT>
92 {
93 "*"{splice}"/" STRING_GROW; BEGIN sc_context;
94 }
95
96
97 /*--------------------------------------------------------------.
98 | Scanning a line comment. The initial `//' is already eaten. |
99 `--------------------------------------------------------------*/
100
101 <SC_LINE_COMMENT>
102 {
103 "\n" STRING_GROW; BEGIN sc_context;
104 {splice} STRING_GROW;
105 }
106
107
108 /*--------------------------------------------.
109 | Scanning user-code characters and strings. |
110 `--------------------------------------------*/
111
112 <SC_CHARACTER,SC_STRING>
113 {
114 {splice}|\\{splice}. STRING_GROW;
115 }
116
117 <SC_CHARACTER>
118 {
119 "'" STRING_GROW; BEGIN sc_context;
120 }
121
122 <SC_STRING>
123 {
124 "\"" STRING_GROW; BEGIN sc_context;
125 }
126
127
128 <SC_RULE_ACTION,SC_SYMBOL_ACTION>{
129 "'" {
130 STRING_GROW;
131 BEGIN SC_CHARACTER;
132 }
133 "\"" {
134 STRING_GROW;
135 BEGIN SC_STRING;
136 }
137 "/"{splice}"*" {
138 STRING_GROW;
139 BEGIN SC_COMMENT;
140 }
141 "/"{splice}"/" {
142 STRING_GROW;
143 BEGIN SC_LINE_COMMENT;
144 }
145 }
146
147 <SC_RULE_ACTION>
148 {
149 "$"("<"{tag}">")?(-?[0-9]+|"$") handle_action_dollar (yytext, *loc);
150 "@"(-?[0-9]+|"$") handle_action_at (yytext, *loc);
151
152 "$" {
153 warn_at (*loc, _("stray `$'"));
154 obstack_sgrow (&obstack_for_string, "$][");
155 }
156 "@" {
157 warn_at (*loc, _("stray `@'"));
158 obstack_sgrow (&obstack_for_string, "@@");
159 }
160 }
161
162 <SC_SYMBOL_ACTION>
163 {
164 "$$" obstack_sgrow (&obstack_for_string, "]b4_dollar_dollar[");
165 "@$" obstack_sgrow (&obstack_for_string, "]b4_at_dollar[");
166 }
167
168
169 /*-----------------------------------------.
170 | Escape M4 quoting characters in C code. |
171 `-----------------------------------------*/
172
173 <*>
174 {
175 \$ obstack_sgrow (&obstack_for_string, "$][");
176 \@ obstack_sgrow (&obstack_for_string, "@@");
177 \[ obstack_sgrow (&obstack_for_string, "@{");
178 \] obstack_sgrow (&obstack_for_string, "@}");
179 }
180
181 /*-----------------------------------------------------.
182 | By default, grow the string obstack with the input. |
183 `-----------------------------------------------------*/
184
185 <*>.|\n STRING_GROW;
186
187 /* End of processing. */
188 <*><<EOF>> {
189 obstack_1grow (&obstack_for_string, '\0');
190 return obstack_finish (&obstack_for_string);
191 }
192
193 %%
194
195 /* Keeps track of the maximum number of semantic values to the left of
196 a handle (those referenced by $0, $-1, etc.) are required by the
197 semantic actions of this grammar. */
198 int max_left_semantic_context = 0;
199
200
201 /*------------------------------------------------------------------.
202 | TEXT is pointing to a wannabee semantic value (i.e., a `$'). |
203 | |
204 | Possible inputs: $[<TYPENAME>]($|integer) |
205 | |
206 | Output to OBSTACK_FOR_STRING a reference to this semantic value. |
207 `------------------------------------------------------------------*/
208
209 static void
210 handle_action_dollar (char *text, location loc)
211 {
212 const char *type_name = NULL;
213 char *cp = text + 1;
214 int rule_length = symbol_list_length (current_rule->next);
215
216 /* Get the type name if explicit. */
217 if (*cp == '<')
218 {
219 type_name = ++cp;
220 while (*cp != '>')
221 ++cp;
222 *cp = '\0';
223 ++cp;
224 }
225
226 if (*cp == '$')
227 {
228 if (!type_name)
229 type_name = symbol_list_n_type_name_get (current_rule, loc, 0);
230 if (!type_name && typed)
231 complain_at (loc, _("$$ of `%s' has no declared type"),
232 current_rule->sym->tag);
233 if (!type_name)
234 type_name = "";
235 obstack_fgrow1 (&obstack_for_string,
236 "]b4_lhs_value([%s])[", type_name);
237 current_rule->used = true;
238 }
239 else
240 {
241 long int num;
242 set_errno (0);
243 num = strtol (cp, 0, 10);
244 if (INT_MIN <= num && num <= rule_length && ! get_errno ())
245 {
246 int n = num;
247 if (1-n > max_left_semantic_context)
248 max_left_semantic_context = 1-n;
249 if (!type_name && n > 0)
250 type_name = symbol_list_n_type_name_get (current_rule, loc, n);
251 if (!type_name && typed)
252 complain_at (loc, _("$%d of `%s' has no declared type"),
253 n, current_rule->sym->tag);
254 if (!type_name)
255 type_name = "";
256 obstack_fgrow3 (&obstack_for_string,
257 "]b4_rhs_value(%d, %d, [%s])[",
258 rule_length, n, type_name);
259 symbol_list_n_used_set (current_rule, n, true);
260 }
261 else
262 complain_at (loc, _("integer out of range: %s"), quote (text));
263 }
264 }
265
266
267 /*------------------------------------------------------.
268 | TEXT is a location token (i.e., a `@...'). Output to |
269 | OBSTACK_FOR_STRING a reference to this location. |
270 `------------------------------------------------------*/
271
272 static void
273 handle_action_at (char *text, location loc)
274 {
275 char *cp = text + 1;
276 int rule_length = symbol_list_length (current_rule->next);
277 locations_flag = true;
278
279 if (*cp == '$')
280 obstack_sgrow (&obstack_for_string, "]b4_lhs_location[");
281 else
282 {
283 long int num;
284 set_errno (0);
285 num = strtol (cp, 0, 10);
286
287 if (INT_MIN <= num && num <= rule_length && ! get_errno ())
288 {
289 int n = num;
290 obstack_fgrow2 (&obstack_for_string, "]b4_rhs_location(%d, %d)[",
291 rule_length, n);
292 }
293 else
294 complain_at (loc, _("integer out of range: %s"), quote (text));
295 }
296 }
297
298
299 /*-------------------------.
300 | Initialize the scanner. |
301 `-------------------------*/
302
303 /* Translate the dollars and ats in \a a, whose location is l.
304 Depending on the \a sc_context (SC_RULE_ACTION, SC_SYMBOL_ACTION,
305 INITIAL), the processing is different. */
306
307 static const char *
308 translate_action (int sc_context, const char *a, location l)
309 {
310 const char *res;
311 static bool initialized = false;
312 if (!initialized)
313 {
314 obstack_init (&obstack_for_string);
315 /* The initial buffer, never used. */
316 yy_delete_buffer (YY_CURRENT_BUFFER);
317 yy_flex_debug = 0;
318 initialized = true;
319 }
320
321 loc->start = loc->end = l.start;
322 yy_switch_to_buffer (yy_scan_string (a));
323 res = code_lex (sc_context);
324 yy_delete_buffer (YY_CURRENT_BUFFER);
325
326 return res;
327 }
328
329 const char *
330 translate_rule_action (symbol_list *r, const char *a, location l)
331 {
332 current_rule = r;
333 return translate_action (SC_RULE_ACTION, a, l);
334 }
335
336 const char *
337 translate_symbol_action (const char *a, location l)
338 {
339 return translate_action (SC_SYMBOL_ACTION, a, l);
340 }
341
342 const char *
343 translate_code (const char *a, location l)
344 {
345 return translate_action (INITIAL, a, l);
346 }
347
348 /*-----------------------------------------------.
349 | Free all the memory allocated to the scanner. |
350 `-----------------------------------------------*/
351
352 void
353 code_scanner_free (void)
354 {
355 obstack_free (&obstack_for_string, 0);
356 /* Reclaim Flex's buffers. */
357 yy_delete_buffer (YY_CURRENT_BUFFER);
358 }