]>
Commit | Line | Data |
---|---|---|
1 | /* Bison Action Scanner -*- C -*- | |
2 | ||
3 | Copyright (C) 2006-2013 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 3 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, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | %option debug nodefault noinput nounput noyywrap never-interactive | |
21 | %option prefix="code_" outfile="lex.yy.c" | |
22 | ||
23 | %{ | |
24 | /* Work around a bug in flex 2.5.31. See Debian bug 333231 | |
25 | <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */ | |
26 | #undef code_wrap | |
27 | #define code_wrap() 1 | |
28 | ||
29 | #define FLEX_PREFIX(Id) code_ ## Id | |
30 | #include <src/flex-scanner.h> | |
31 | ||
32 | #include <src/complain.h> | |
33 | #include <src/reader.h> | |
34 | #include <src/getargs.h> | |
35 | #include <src/muscle-tab.h> | |
36 | #include <src/scan-code.h> | |
37 | #include <src/symlist.h> | |
38 | ||
39 | #include <c-ctype.h> | |
40 | #include <get-errno.h> | |
41 | #include <quote.h> | |
42 | ||
43 | /* The current calling start condition: SC_RULE_ACTION or | |
44 | SC_SYMBOL_ACTION. */ | |
45 | # define YY_DECL static char *code_lex (code_props *self, int sc_context) | |
46 | YY_DECL; | |
47 | ||
48 | #define YY_USER_ACTION location_compute (loc, &loc->end, yytext, yyleng); | |
49 | ||
50 | static char *fetch_type_name (char *cp, char const **type_name, | |
51 | location dollar_loc); | |
52 | ||
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); | |
56 | ||
57 | /* A string to be pushed to obstack after dollar/at has been handled. */ | |
58 | static char *ref_tail_fields; | |
59 | ||
60 | static location the_location; | |
61 | static location *loc = &the_location; | |
62 | ||
63 | /* A string representing the most recent translation. */ | |
64 | static char *last_string; | |
65 | ||
66 | /* True if an untyped $$ or $n was seen. */ | |
67 | static bool untyped_var_seen; | |
68 | ||
69 | %} | |
70 | /* C and C++ comments in code. */ | |
71 | %x SC_COMMENT SC_LINE_COMMENT | |
72 | /* Strings and characters in code. */ | |
73 | %x SC_STRING SC_CHARACTER | |
74 | /* Whether in a rule or symbol action. Specifies the translation | |
75 | of $ and @. */ | |
76 | %x SC_RULE_ACTION SC_SYMBOL_ACTION | |
77 | ||
78 | ||
79 | /* POSIX says that a tag must be both an id and a C union member, but | |
80 | historically almost any character is allowed in a tag. We disallow | |
81 | NUL and newline, as this simplifies our implementation. We allow | |
82 | "->" as a means to dereference a pointer. */ | |
83 | tag ([^\0\n>]|->)+ | |
84 | ||
85 | /* Zero or more instances of backslash-newline. Following GCC, allow | |
86 | white space between the backslash and the newline. */ | |
87 | splice (\\[ \f\t\v]*\n)* | |
88 | ||
89 | /* C style identifier. Must start with letter. Will be used for | |
90 | named symbol references. Shall be kept synchronized with | |
91 | scan-gram.l "letter" and "id". */ | |
92 | letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_] | |
93 | id {letter}({letter}|[-0-9])* | |
94 | ref -?[0-9]+|{id}|"["{id}"]"|"$" | |
95 | ||
96 | %% | |
97 | ||
98 | %{ | |
99 | /* This scanner is special: it is invoked only once, henceforth | |
100 | is expected to return only once. This initialization is | |
101 | therefore done once per action to translate. */ | |
102 | aver (sc_context == SC_SYMBOL_ACTION | |
103 | || sc_context == SC_RULE_ACTION | |
104 | || sc_context == INITIAL); | |
105 | BEGIN sc_context; | |
106 | %} | |
107 | ||
108 | /*------------------------------------------------------------. | |
109 | | Scanning a C comment. The initial '/ *' is already eaten. | | |
110 | `------------------------------------------------------------*/ | |
111 | ||
112 | <SC_COMMENT> | |
113 | { | |
114 | "*"{splice}"/" STRING_GROW; BEGIN sc_context; | |
115 | } | |
116 | ||
117 | ||
118 | /*--------------------------------------------------------------. | |
119 | | Scanning a line comment. The initial '//' is already eaten. | | |
120 | `--------------------------------------------------------------*/ | |
121 | ||
122 | <SC_LINE_COMMENT> | |
123 | { | |
124 | "\n" STRING_GROW; BEGIN sc_context; | |
125 | {splice} STRING_GROW; | |
126 | } | |
127 | ||
128 | ||
129 | /*--------------------------------------------. | |
130 | | Scanning user-code characters and strings. | | |
131 | `--------------------------------------------*/ | |
132 | ||
133 | <SC_CHARACTER,SC_STRING> | |
134 | { | |
135 | {splice}|\\{splice}. STRING_GROW; | |
136 | } | |
137 | ||
138 | <SC_CHARACTER> | |
139 | { | |
140 | "'" STRING_GROW; BEGIN sc_context; | |
141 | } | |
142 | ||
143 | <SC_STRING> | |
144 | { | |
145 | "\"" STRING_GROW; BEGIN sc_context; | |
146 | } | |
147 | ||
148 | ||
149 | <SC_RULE_ACTION,SC_SYMBOL_ACTION> | |
150 | { | |
151 | "'" STRING_GROW; BEGIN SC_CHARACTER; | |
152 | "\"" STRING_GROW; BEGIN SC_STRING; | |
153 | "/"{splice}"*" STRING_GROW; BEGIN SC_COMMENT; | |
154 | "/"{splice}"/" STRING_GROW; BEGIN SC_LINE_COMMENT; | |
155 | ||
156 | [$@] { | |
157 | complain (loc, Wother, _("stray '%s'"), yytext); | |
158 | obstack_escape (&obstack_for_string, yytext); | |
159 | } | |
160 | } | |
161 | ||
162 | <SC_RULE_ACTION> | |
163 | { | |
164 | "$"("<"{tag}">")?{ref} { | |
165 | ref_tail_fields = NULL; | |
166 | handle_action_dollar (self->rule, yytext, *loc); | |
167 | if (ref_tail_fields) | |
168 | obstack_sgrow (&obstack_for_string, ref_tail_fields); | |
169 | } | |
170 | "@"{ref} { | |
171 | ref_tail_fields = NULL; | |
172 | handle_action_at (self->rule, yytext, *loc); | |
173 | if (ref_tail_fields) | |
174 | obstack_sgrow (&obstack_for_string, ref_tail_fields); | |
175 | } | |
176 | } | |
177 | ||
178 | <SC_SYMBOL_ACTION> | |
179 | { | |
180 | "$"("<"{tag}">")?"$" { | |
181 | const char *type_name = NULL; | |
182 | fetch_type_name (yytext + 1, &type_name, *loc)[-1] = 0; | |
183 | obstack_sgrow (&obstack_for_string, "]b4_dollar_dollar("); | |
184 | obstack_quote (&obstack_for_string, type_name); | |
185 | obstack_sgrow (&obstack_for_string, ")["); | |
186 | self->is_value_used = true; | |
187 | } | |
188 | "@$" { | |
189 | obstack_sgrow (&obstack_for_string, "]b4_at_dollar["); | |
190 | muscle_percent_define_ensure("locations", the_location, true); | |
191 | } | |
192 | } | |
193 | ||
194 | ||
195 | <*> | |
196 | { | |
197 | /* Escape M4 quoting characters in C code. */ | |
198 | [$@\[\]] obstack_escape (&obstack_for_string, yytext); | |
199 | ||
200 | /* By default, grow the string obstack with the input. */ | |
201 | .|\n STRING_GROW; | |
202 | ||
203 | /* End of processing. */ | |
204 | <<EOF>> STRING_FINISH; return last_string; | |
205 | } | |
206 | ||
207 | %% | |
208 | ||
209 | static inline bool | |
210 | is_dot_or_dash (char ch) | |
211 | { | |
212 | return ch == '.' || ch == '-'; | |
213 | } | |
214 | ||
215 | static inline bool | |
216 | contains_dot_or_dash (const char* p) | |
217 | { | |
218 | for (; *p; ++p) | |
219 | if (is_dot_or_dash (*p)) | |
220 | return true; | |
221 | return false; | |
222 | } | |
223 | ||
224 | /* Defines a variant of a symbolic name resolution. */ | |
225 | typedef struct | |
226 | { | |
227 | /* Index in symbol list. */ | |
228 | unsigned symbol_index; | |
229 | ||
230 | /* Matched symbol id and loc. */ | |
231 | uniqstr id; | |
232 | location loc; | |
233 | ||
234 | /* Hiding named reference. */ | |
235 | named_ref* hidden_by; | |
236 | ||
237 | /* Error flags. May contain zero (no errors) or | |
238 | a combination of VARIANT_* values. */ | |
239 | unsigned err; | |
240 | } variant; | |
241 | ||
242 | /* Set when the variant refers to a symbol hidden | |
243 | by an explicit symbol reference. */ | |
244 | #define VARIANT_HIDDEN (1 << 0) | |
245 | ||
246 | /* Set when the variant refers to a symbol containing | |
247 | dots or dashes. Will require explicit bracketing. */ | |
248 | #define VARIANT_BAD_BRACKETING (1 << 1) | |
249 | ||
250 | /* Set when the variant refers to a symbol which is | |
251 | not visible from current midrule. */ | |
252 | #define VARIANT_NOT_VISIBLE_FROM_MIDRULE (1 << 2) | |
253 | ||
254 | static variant *variant_table = NULL; | |
255 | static unsigned variant_table_size = 0; | |
256 | static unsigned variant_count = 0; | |
257 | ||
258 | static variant * | |
259 | variant_table_grow (void) | |
260 | { | |
261 | ++variant_count; | |
262 | if (variant_count > variant_table_size) | |
263 | { | |
264 | while (variant_count > variant_table_size) | |
265 | variant_table_size = 2 * variant_table_size + 3; | |
266 | variant_table = xnrealloc (variant_table, variant_table_size, | |
267 | sizeof *variant_table); | |
268 | } | |
269 | return &variant_table[variant_count - 1]; | |
270 | } | |
271 | ||
272 | static void | |
273 | variant_table_free (void) | |
274 | { | |
275 | free (variant_table); | |
276 | variant_table = NULL; | |
277 | variant_table_size = variant_count = 0; | |
278 | } | |
279 | ||
280 | static char * | |
281 | find_prefix_end (const char *prefix, char *begin, char *end) | |
282 | { | |
283 | char *ptr = begin; | |
284 | ||
285 | for (; *prefix && ptr != end; ++prefix, ++ptr) | |
286 | if (*prefix != *ptr) | |
287 | return 0; | |
288 | ||
289 | if (*prefix) | |
290 | return 0; | |
291 | ||
292 | return ptr; | |
293 | } | |
294 | ||
295 | static variant * | |
296 | variant_add (uniqstr id, location id_loc, unsigned symbol_index, | |
297 | char *cp, char *cp_end, bool explicit_bracketing) | |
298 | { | |
299 | char *prefix_end; | |
300 | ||
301 | prefix_end = find_prefix_end (id, cp, cp_end); | |
302 | if (prefix_end && | |
303 | (prefix_end == cp_end || | |
304 | (!explicit_bracketing && is_dot_or_dash (*prefix_end)))) | |
305 | { | |
306 | variant *r = variant_table_grow (); | |
307 | r->symbol_index = symbol_index; | |
308 | r->id = id; | |
309 | r->loc = id_loc; | |
310 | r->hidden_by = NULL; | |
311 | r->err = 0; | |
312 | return r; | |
313 | } | |
314 | else | |
315 | return NULL; | |
316 | } | |
317 | ||
318 | static const char * | |
319 | get_at_spec(unsigned symbol_index) | |
320 | { | |
321 | static char at_buf[20]; | |
322 | if (symbol_index == 0) | |
323 | strcpy (at_buf, "$$"); | |
324 | else | |
325 | snprintf (at_buf, sizeof at_buf, "$%u", symbol_index); | |
326 | return at_buf; | |
327 | } | |
328 | ||
329 | static void | |
330 | show_sub_message (warnings warning, | |
331 | const char* cp, bool explicit_bracketing, | |
332 | int midrule_rhs_index, char dollar_or_at, | |
333 | unsigned indent, const variant *var) | |
334 | { | |
335 | const char *at_spec = get_at_spec (var->symbol_index); | |
336 | ||
337 | if (var->err == 0) | |
338 | complain_indent (&var->loc, warning, &indent, | |
339 | _("refers to: %c%s at %s"), dollar_or_at, | |
340 | var->id, at_spec); | |
341 | else | |
342 | { | |
343 | static struct obstack msg_buf; | |
344 | const char *tail = explicit_bracketing ? "" : cp + strlen (var->id); | |
345 | const char *id = var->hidden_by ? var->hidden_by->id : var->id; | |
346 | location id_loc = var->hidden_by ? var->hidden_by->loc : var->loc; | |
347 | ||
348 | /* Create the explanation message. */ | |
349 | obstack_init (&msg_buf); | |
350 | ||
351 | obstack_printf (&msg_buf, _("possibly meant: %c"), dollar_or_at); | |
352 | if (contains_dot_or_dash (id)) | |
353 | obstack_printf (&msg_buf, "[%s]", id); | |
354 | else | |
355 | obstack_sgrow (&msg_buf, id); | |
356 | obstack_sgrow (&msg_buf, tail); | |
357 | ||
358 | if (var->err & VARIANT_HIDDEN) | |
359 | { | |
360 | obstack_printf (&msg_buf, _(", hiding %c"), dollar_or_at); | |
361 | if (contains_dot_or_dash (var->id)) | |
362 | obstack_printf (&msg_buf, "[%s]", var->id); | |
363 | else | |
364 | obstack_sgrow (&msg_buf, var->id); | |
365 | obstack_sgrow (&msg_buf, tail); | |
366 | } | |
367 | ||
368 | obstack_printf (&msg_buf, _(" at %s"), at_spec); | |
369 | ||
370 | if (var->err & VARIANT_NOT_VISIBLE_FROM_MIDRULE) | |
371 | obstack_printf (&msg_buf, | |
372 | _(", cannot be accessed from mid-rule action at $%d"), | |
373 | midrule_rhs_index); | |
374 | ||
375 | complain_indent (&id_loc, warning, &indent, "%s", | |
376 | obstack_finish0 (&msg_buf)); | |
377 | obstack_free (&msg_buf, 0); | |
378 | } | |
379 | } | |
380 | ||
381 | static void | |
382 | show_sub_messages (warnings warning, | |
383 | const char* cp, bool explicit_bracketing, | |
384 | int midrule_rhs_index, char dollar_or_at, | |
385 | unsigned indent) | |
386 | { | |
387 | unsigned i; | |
388 | ||
389 | for (i = 0; i < variant_count; ++i) | |
390 | show_sub_message (warning | silent, | |
391 | cp, explicit_bracketing, | |
392 | midrule_rhs_index, dollar_or_at, | |
393 | indent, &variant_table[i]); | |
394 | } | |
395 | ||
396 | /* Returned from "parse_ref" when the reference | |
397 | is inappropriate. */ | |
398 | #define INVALID_REF (INT_MIN) | |
399 | ||
400 | /* Returned from "parse_ref" when the reference | |
401 | points to LHS ($$) of the current rule or midrule. */ | |
402 | #define LHS_REF (INT_MIN + 1) | |
403 | ||
404 | /* Parse named or positional reference. In case of positional | |
405 | references, can return negative values for $-n "deep" stack | |
406 | accesses. */ | |
407 | static long int | |
408 | parse_ref (char *cp, symbol_list *rule, int rule_length, | |
409 | int midrule_rhs_index, char *text, location text_loc, | |
410 | char dollar_or_at) | |
411 | { | |
412 | symbol_list *l; | |
413 | char *cp_end; | |
414 | bool explicit_bracketing; | |
415 | unsigned i; | |
416 | unsigned valid_variants = 0; | |
417 | unsigned valid_variant_index = 0; | |
418 | ||
419 | if ('$' == *cp) | |
420 | return LHS_REF; | |
421 | ||
422 | if (c_isdigit (*cp) || (*cp == '-' && c_isdigit (* (cp + 1)))) | |
423 | { | |
424 | long int num = strtol (cp, &cp, 10); | |
425 | if (1 - INT_MAX + rule_length <= num && num <= rule_length) | |
426 | return num; | |
427 | else | |
428 | { | |
429 | complain (&text_loc, complaint, _("integer out of range: %s"), | |
430 | quote (text)); | |
431 | return INVALID_REF; | |
432 | } | |
433 | } | |
434 | ||
435 | if ('[' == *cp) | |
436 | { | |
437 | /* Ignore the brackets. */ | |
438 | char *p; | |
439 | for (p = ++cp; *p != ']'; ++p) | |
440 | continue; | |
441 | cp_end = p; | |
442 | ||
443 | explicit_bracketing = true; | |
444 | } | |
445 | else | |
446 | { | |
447 | /* Take all characters of the name. */ | |
448 | char* p; | |
449 | for (p = cp; *p; ++p) | |
450 | if (is_dot_or_dash (*p)) | |
451 | { | |
452 | ref_tail_fields = p; | |
453 | break; | |
454 | } | |
455 | for (p = cp; *p; ++p) | |
456 | continue; | |
457 | cp_end = p; | |
458 | ||
459 | explicit_bracketing = false; | |
460 | } | |
461 | ||
462 | /* Add all relevant variants. */ | |
463 | { | |
464 | unsigned symbol_index; | |
465 | variant_count = 0; | |
466 | for (symbol_index = 0, l = rule; !symbol_list_null (l); | |
467 | ++symbol_index, l = l->next) | |
468 | { | |
469 | variant *var; | |
470 | if (l->content_type != SYMLIST_SYMBOL) | |
471 | continue; | |
472 | ||
473 | var = variant_add (l->content.sym->tag, l->sym_loc, | |
474 | symbol_index, cp, cp_end, explicit_bracketing); | |
475 | if (var && l->named_ref) | |
476 | var->hidden_by = l->named_ref; | |
477 | ||
478 | if (l->named_ref) | |
479 | variant_add (l->named_ref->id, l->named_ref->loc, | |
480 | symbol_index, cp, cp_end, explicit_bracketing); | |
481 | } | |
482 | } | |
483 | ||
484 | /* Check errors. */ | |
485 | for (i = 0; i < variant_count; ++i) | |
486 | { | |
487 | variant *var = &variant_table[i]; | |
488 | unsigned symbol_index = var->symbol_index; | |
489 | ||
490 | /* Check visibility from mid-rule actions. */ | |
491 | if (midrule_rhs_index != 0 | |
492 | && (symbol_index == 0 || midrule_rhs_index < symbol_index)) | |
493 | var->err |= VARIANT_NOT_VISIBLE_FROM_MIDRULE; | |
494 | ||
495 | /* Check correct bracketing. */ | |
496 | if (!explicit_bracketing && contains_dot_or_dash (var->id)) | |
497 | var->err |= VARIANT_BAD_BRACKETING; | |
498 | ||
499 | /* Check using of hidden symbols. */ | |
500 | if (var->hidden_by) | |
501 | var->err |= VARIANT_HIDDEN; | |
502 | ||
503 | if (!var->err) | |
504 | { | |
505 | valid_variant_index = i; | |
506 | ++valid_variants; | |
507 | } | |
508 | } | |
509 | ||
510 | switch (valid_variants) | |
511 | { | |
512 | case 0: | |
513 | { | |
514 | unsigned len = (explicit_bracketing || !ref_tail_fields) ? | |
515 | cp_end - cp : ref_tail_fields - cp; | |
516 | unsigned indent = 0; | |
517 | ||
518 | complain_indent (&text_loc, complaint, &indent, | |
519 | _("invalid reference: %s"), quote (text)); | |
520 | indent += SUB_INDENT; | |
521 | if (len == 0) | |
522 | { | |
523 | location sym_loc = text_loc; | |
524 | sym_loc.start.column += 1; | |
525 | sym_loc.end = sym_loc.start; | |
526 | complain_indent (&sym_loc, complaint, &indent, | |
527 | _("syntax error after '%c', expecting integer, " | |
528 | "letter, '_', '[', or '$'"), | |
529 | dollar_or_at); | |
530 | } | |
531 | else if (midrule_rhs_index) | |
532 | complain_indent (&rule->location, complaint, &indent, | |
533 | _("symbol not found in production before $%d: " | |
534 | "%.*s"), | |
535 | midrule_rhs_index, len, cp); | |
536 | else | |
537 | complain_indent (&rule->location, complaint, &indent, | |
538 | _("symbol not found in production: %.*s"), | |
539 | len, cp); | |
540 | ||
541 | if (variant_count > 0) | |
542 | show_sub_messages (complaint, | |
543 | cp, explicit_bracketing, midrule_rhs_index, | |
544 | dollar_or_at, indent); | |
545 | return INVALID_REF; | |
546 | } | |
547 | case 1: | |
548 | { | |
549 | unsigned indent = 0; | |
550 | if (variant_count > 1) | |
551 | { | |
552 | complain_indent (&text_loc, Wother, &indent, | |
553 | _("misleading reference: %s"), quote (text)); | |
554 | show_sub_messages (Wother, | |
555 | cp, explicit_bracketing, midrule_rhs_index, | |
556 | dollar_or_at, indent + SUB_INDENT); | |
557 | } | |
558 | { | |
559 | unsigned symbol_index = | |
560 | variant_table[valid_variant_index].symbol_index; | |
561 | return (symbol_index == midrule_rhs_index) ? LHS_REF : symbol_index; | |
562 | } | |
563 | } | |
564 | case 2: | |
565 | default: | |
566 | { | |
567 | unsigned indent = 0; | |
568 | complain_indent (&text_loc, complaint, &indent, | |
569 | _("ambiguous reference: %s"), quote (text)); | |
570 | show_sub_messages (complaint, | |
571 | cp, explicit_bracketing, midrule_rhs_index, | |
572 | dollar_or_at, indent + SUB_INDENT); | |
573 | return INVALID_REF; | |
574 | } | |
575 | } | |
576 | ||
577 | /* Not reachable. */ | |
578 | return INVALID_REF; | |
579 | } | |
580 | ||
581 | /* Keeps track of the maximum number of semantic values to the left of | |
582 | a handle (those referenced by $0, $-1, etc.) are required by the | |
583 | semantic actions of this grammar. */ | |
584 | int max_left_semantic_context = 0; | |
585 | ||
586 | ||
587 | /* If CP points to a typename (i.e., <.*?>), set TYPE_NAME to its | |
588 | beginning (i.e., after the opening "<", and return the pointer | |
589 | immediately after it. */ | |
590 | ||
591 | static | |
592 | char * | |
593 | fetch_type_name (char *cp, char const **type_name, | |
594 | location dollar_loc) | |
595 | { | |
596 | if (*cp == '<') | |
597 | { | |
598 | *type_name = ++cp; | |
599 | /* Series of non-'>' or "->". */ | |
600 | while (*cp != '>' || cp[-1] == '-') | |
601 | ++cp; | |
602 | ||
603 | /* The '>' symbol will be later replaced by '\0'. Original | |
604 | 'text' is needed for error messages. */ | |
605 | ++cp; | |
606 | if (untyped_var_seen) | |
607 | complain (&dollar_loc, complaint, | |
608 | _("explicit type given in untyped grammar")); | |
609 | tag_seen = true; | |
610 | } | |
611 | return cp; | |
612 | } | |
613 | ||
614 | /*------------------------------------------------------------------. | |
615 | | TEXT is pointing to a wannabee semantic value (i.e., a '$'). | | |
616 | | | | |
617 | | Possible inputs: $[<TYPENAME>]($|integer) | | |
618 | | | | |
619 | | Output to OBSTACK_FOR_STRING a reference to this semantic value. | | |
620 | `------------------------------------------------------------------*/ | |
621 | ||
622 | static void | |
623 | handle_action_dollar (symbol_list *rule, char *text, location dollar_loc) | |
624 | { | |
625 | char const *type_name = NULL; | |
626 | char *cp = text + 1; | |
627 | symbol_list *effective_rule; | |
628 | int effective_rule_length; | |
629 | int n; | |
630 | ||
631 | if (rule->midrule_parent_rule) | |
632 | { | |
633 | effective_rule = rule->midrule_parent_rule; | |
634 | effective_rule_length = rule->midrule_parent_rhs_index - 1; | |
635 | } | |
636 | else | |
637 | { | |
638 | effective_rule = rule; | |
639 | effective_rule_length = symbol_list_length (rule->next); | |
640 | } | |
641 | ||
642 | /* Get the type name if explicit. */ | |
643 | cp = fetch_type_name (cp, &type_name, dollar_loc); | |
644 | ||
645 | n = parse_ref (cp, effective_rule, effective_rule_length, | |
646 | rule->midrule_parent_rhs_index, text, dollar_loc, '$'); | |
647 | ||
648 | /* End type_name. */ | |
649 | if (type_name) | |
650 | cp[-1] = '\0'; | |
651 | ||
652 | switch (n) | |
653 | { | |
654 | case INVALID_REF: | |
655 | break; | |
656 | ||
657 | case LHS_REF: | |
658 | if (!type_name) | |
659 | type_name = symbol_list_n_type_name_get (rule, dollar_loc, 0); | |
660 | ||
661 | if (!type_name) | |
662 | { | |
663 | if (union_seen | tag_seen) | |
664 | { | |
665 | if (rule->midrule_parent_rule) | |
666 | complain (&dollar_loc, complaint, | |
667 | _("$$ for the midrule at $%d of %s" | |
668 | " has no declared type"), | |
669 | rule->midrule_parent_rhs_index, | |
670 | quote (effective_rule->content.sym->tag)); | |
671 | else | |
672 | complain (&dollar_loc, complaint, | |
673 | _("$$ of %s has no declared type"), | |
674 | quote (rule->content.sym->tag)); | |
675 | } | |
676 | else | |
677 | untyped_var_seen = true; | |
678 | } | |
679 | ||
680 | obstack_sgrow (&obstack_for_string, "]b4_lhs_value("); | |
681 | obstack_quote (&obstack_for_string, type_name); | |
682 | obstack_sgrow (&obstack_for_string, ")["); | |
683 | rule->action_props.is_value_used = true; | |
684 | break; | |
685 | ||
686 | default: | |
687 | if (max_left_semantic_context < 1 - n) | |
688 | max_left_semantic_context = 1 - n; | |
689 | if (!type_name && 0 < n) | |
690 | type_name = | |
691 | symbol_list_n_type_name_get (effective_rule, dollar_loc, n); | |
692 | if (!type_name) | |
693 | { | |
694 | if (union_seen | tag_seen) | |
695 | complain (&dollar_loc, complaint, | |
696 | _("$%s of %s has no declared type"), cp, | |
697 | quote (effective_rule->content.sym->tag)); | |
698 | else | |
699 | untyped_var_seen = true; | |
700 | } | |
701 | ||
702 | obstack_printf (&obstack_for_string, | |
703 | "]b4_rhs_value(%d, %d, ", effective_rule_length, n); | |
704 | obstack_quote (&obstack_for_string, type_name); | |
705 | obstack_sgrow (&obstack_for_string, ")["); | |
706 | if (n > 0) | |
707 | symbol_list_n_get (effective_rule, n)->action_props.is_value_used = | |
708 | true; | |
709 | break; | |
710 | } | |
711 | } | |
712 | ||
713 | ||
714 | /*------------------------------------------------------. | |
715 | | TEXT is a location token (i.e., a '@...'). Output to | | |
716 | | OBSTACK_FOR_STRING a reference to this location. | | |
717 | `------------------------------------------------------*/ | |
718 | ||
719 | static void | |
720 | handle_action_at (symbol_list *rule, char *text, location at_loc) | |
721 | { | |
722 | char *cp = text + 1; | |
723 | symbol_list *effective_rule; | |
724 | int effective_rule_length; | |
725 | int n; | |
726 | ||
727 | if (rule->midrule_parent_rule) | |
728 | { | |
729 | effective_rule = rule->midrule_parent_rule; | |
730 | effective_rule_length = rule->midrule_parent_rhs_index - 1; | |
731 | } | |
732 | else | |
733 | { | |
734 | effective_rule = rule; | |
735 | effective_rule_length = symbol_list_length (rule->next); | |
736 | } | |
737 | ||
738 | muscle_percent_define_ensure("locations", at_loc, true); | |
739 | ||
740 | n = parse_ref (cp, effective_rule, effective_rule_length, | |
741 | rule->midrule_parent_rhs_index, text, at_loc, '@'); | |
742 | switch (n) | |
743 | { | |
744 | case INVALID_REF: | |
745 | break; | |
746 | ||
747 | case LHS_REF: | |
748 | obstack_sgrow (&obstack_for_string, "]b4_lhs_location["); | |
749 | break; | |
750 | ||
751 | default: | |
752 | obstack_printf (&obstack_for_string, "]b4_rhs_location(%d, %d)[", | |
753 | effective_rule_length, n); | |
754 | break; | |
755 | } | |
756 | } | |
757 | ||
758 | ||
759 | /*-------------------------. | |
760 | | Initialize the scanner. | | |
761 | `-------------------------*/ | |
762 | ||
763 | /* Translate the dollars and ats in \a self, in the context \a sc_context | |
764 | (SC_RULE_ACTION, SC_SYMBOL_ACTION, INITIAL). */ | |
765 | ||
766 | static char const * | |
767 | translate_action (code_props *self, int sc_context) | |
768 | { | |
769 | char *res; | |
770 | static bool initialized = false; | |
771 | if (!initialized) | |
772 | { | |
773 | obstack_init (&obstack_for_string); | |
774 | yy_flex_debug = 0; | |
775 | initialized = true; | |
776 | } | |
777 | ||
778 | loc->start = loc->end = self->location.start; | |
779 | yy_switch_to_buffer (yy_scan_string (self->code)); | |
780 | res = code_lex (self, sc_context); | |
781 | yy_delete_buffer (YY_CURRENT_BUFFER); | |
782 | ||
783 | return res; | |
784 | } | |
785 | ||
786 | /*------------------------------------------------------------------------. | |
787 | | Implementation of the public interface as documented in "scan-code.h". | | |
788 | `------------------------------------------------------------------------*/ | |
789 | ||
790 | void | |
791 | code_props_none_init (code_props *self) | |
792 | { | |
793 | *self = code_props_none; | |
794 | } | |
795 | ||
796 | code_props code_props_none = CODE_PROPS_NONE_INIT; | |
797 | ||
798 | void | |
799 | code_props_plain_init (code_props *self, char const *code, | |
800 | location code_loc) | |
801 | { | |
802 | code_props_none_init (self); | |
803 | self->kind = CODE_PROPS_PLAIN; | |
804 | self->code = code; | |
805 | self->location = code_loc; | |
806 | } | |
807 | ||
808 | void | |
809 | code_props_symbol_action_init (code_props *self, char const *code, | |
810 | location code_loc) | |
811 | { | |
812 | code_props_none_init (self); | |
813 | self->kind = CODE_PROPS_SYMBOL_ACTION; | |
814 | self->code = code; | |
815 | self->location = code_loc; | |
816 | } | |
817 | ||
818 | void | |
819 | code_props_rule_action_init (code_props *self, char const *code, | |
820 | location code_loc, symbol_list *rule, | |
821 | named_ref *name, bool is_predicate) | |
822 | { | |
823 | code_props_none_init (self); | |
824 | self->kind = CODE_PROPS_RULE_ACTION; | |
825 | self->code = code; | |
826 | self->location = code_loc; | |
827 | self->rule = rule; | |
828 | self->named_ref = name; | |
829 | self->is_predicate = is_predicate; | |
830 | } | |
831 | ||
832 | void | |
833 | code_props_translate_code (code_props *self) | |
834 | { | |
835 | switch (self->kind) | |
836 | { | |
837 | case CODE_PROPS_NONE: | |
838 | break; | |
839 | case CODE_PROPS_PLAIN: | |
840 | self->code = translate_action (self, INITIAL); | |
841 | break; | |
842 | case CODE_PROPS_SYMBOL_ACTION: | |
843 | self->code = translate_action (self, SC_SYMBOL_ACTION); | |
844 | break; | |
845 | case CODE_PROPS_RULE_ACTION: | |
846 | self->code = translate_action (self, SC_RULE_ACTION); | |
847 | break; | |
848 | } | |
849 | } | |
850 | ||
851 | void | |
852 | code_scanner_last_string_free (void) | |
853 | { | |
854 | STRING_FREE; | |
855 | } | |
856 | ||
857 | void | |
858 | code_scanner_free (void) | |
859 | { | |
860 | obstack_free (&obstack_for_string, 0); | |
861 | variant_table_free (); | |
862 | ||
863 | /* Reclaim Flex's buffers. */ | |
864 | yylex_destroy (); | |
865 | } |