]> git.saurik.com Git - bison.git/blame_incremental - src/scan-gram.l
(CHARACTER): Remove unused token. All uses removed.
[bison.git] / src / scan-gram.l
... / ...
CommitLineData
1/* Bison Grammar Scanner -*- C -*-
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA
20*/
21
22%option debug nodefault noyywrap nounput never-interactive stack
23%option prefix="gram_" outfile="lex.yy.c"
24
25%{
26#include "system.h"
27#include "mbswidth.h"
28#include "complain.h"
29#include "quote.h"
30#include "getargs.h"
31#include "gram.h"
32#include "reader.h"
33
34/* Each time we match a string, move the end cursor to its end. */
35#define YY_USER_INIT \
36do { \
37 LOCATION_RESET (*yylloc); \
38 yylloc->file = infile; \
39 /* This is only to avoid GCC warnings. */ \
40 if (yycontrol) {;}; \
41} while (0)
42
43#define YY_USER_ACTION extend_location (yylloc, yytext, yyleng);
44#define YY_STEP LOCATION_STEP (*yylloc)
45
46#define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size))
47
48
49/* Read bytes from FP into buffer BUF of size SIZE. Return the
50 number of bytes read. Remove '\r' from input, treating \r\n
51 and isolated \r as \n. */
52
53static size_t
54no_cr_read (FILE *fp, char *buf, size_t size)
55{
56 size_t s = fread (buf, 1, size, fp);
57 if (s)
58 {
59 char *w = memchr (buf, '\r', s);
60 if (w)
61 {
62 char const *r = ++w;
63 char const *lim = buf + s;
64
65 for (;;)
66 {
67 /* Found an '\r'. Treat it like '\n', but ignore any
68 '\n' that immediately follows. */
69 w[-1] = '\n';
70 if (r == lim)
71 {
72 int ch = getc (fp);
73 if (ch != '\n' && ungetc (ch, fp) != ch)
74 break;
75 }
76 else if (*r == '\n')
77 r++;
78
79 /* Copy until the next '\r'. */
80 do
81 {
82 if (r == lim)
83 return w - buf;
84 }
85 while ((*w++ = *r++) != '\r');
86 }
87
88 return w - buf;
89 }
90 }
91
92 return s;
93}
94
95
96/* Extend *LOC to account for token TOKEN of size SIZE. */
97
98static void
99extend_location (location_t *loc, char const *token, int size)
100{
101 int line = loc->last_line;
102 int column = loc->last_column;
103 char const *p0 = token;
104 char const *p = token;
105 char const *lim = token + size;
106
107 for (p = token; p < lim; p++)
108 switch (*p)
109 {
110 case '\r':
111 /* \r shouldn't survive no_cr_read. */
112 abort ();
113
114 case '\n':
115 line++;
116 column = 1;
117 p0 = p + 1;
118 break;
119
120 case '\t':
121 column += mbsnwidth (p0, p - p0, 0);
122 column += 8 - ((column - 1) & 7);
123 p0 = p + 1;
124 break;
125 }
126
127 loc->last_line = line;
128 loc->last_column = column + mbsnwidth (p0, p - p0, 0);
129}
130
131
132/* Report an unexpected end of file at LOC. A token or comment began
133 with TOKEN_START, but an end of file was encountered and the
134 expected TOKEN_END was missing. */
135
136static void
137unexpected_end_of_file (location_t loc,
138 char const *token_start, char const *token_end)
139{
140 complain_at (loc, _("unexpected end of file in `%s ... %s'"),
141 token_start, token_end);
142}
143
144
145
146/* STRING_OBSTACK -- Used to store all the characters that we need to
147 keep (to construct ID, STRINGS etc.). Use the following macros to
148 use it.
149
150 Use YY_OBS_GROW to append what has just been matched, and
151 YY_OBS_FINISH to end the string (it puts the ending 0).
152 YY_OBS_FINISH also stores this string in LAST_STRING, which can be
153 used, and which is used by YY_OBS_FREE to free the last string. */
154
155static struct obstack string_obstack;
156char *last_string;
157
158#define YY_OBS_GROW \
159 obstack_grow (&string_obstack, yytext, yyleng)
160
161#define YY_OBS_FINISH \
162 do { \
163 obstack_1grow (&string_obstack, '\0'); \
164 last_string = obstack_finish (&string_obstack); \
165 } while (0)
166
167#define YY_OBS_FREE \
168 do { \
169 obstack_free (&string_obstack, last_string); \
170 } while (0)
171
172void
173scanner_last_string_free (void)
174{
175 YY_OBS_FREE;
176}
177
178
179static int percent_percent_count = 0;
180
181/* Within well-formed rules, RULE_LENGTH is the number of values in
182 the current rule so far, which says where to find `$0' with respect
183 to the top of the stack. It is not the same as the rule->length in
184 the case of mid rule actions.
185
186 Outside of well-formed rules, RULE_LENGTH has an undefined value. */
187static int rule_length;
188
189static void handle_dollar (braced_code_t code_kind,
190 char *cp, location_t location);
191static void handle_at (braced_code_t code_kind,
192 char *cp, location_t location);
193static void handle_syncline (char *args, location_t *location);
194static int convert_ucn_to_byte (char const *hex_text);
195
196%}
197%x SC_COMMENT SC_LINE_COMMENT SC_YACC_COMMENT
198%x SC_STRING SC_CHARACTER
199%x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
200%x SC_BRACED_CODE SC_PROLOGUE SC_EPILOGUE
201
202letter [.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
203id {letter}({letter}|[0-9])*
204directive %{letter}({letter}|[0-9]|-)*
205int [0-9]+
206
207/* POSIX says that a tag must be both an id and a C union member, but
208 historically almost any character is allowed in a tag. We disallow
209 NUL and newline, as this simplifies our implementation. */
210tag [^\0\n>]+
211
212/* Zero or more instances of backslash-newline. Following GCC, allow
213 white space between the backslash and the newline. */
214splice (\\[ \f\t\v]*\n)*
215
216%%
217%{
218 int braces_level IF_LINT (= 0);
219
220 /* At each yylex invocation, mark the current position as the
221 start of the next token. */
222 YY_STEP;
223%}
224
225
226 /*----------------------------.
227 | Scanning Bison directives. |
228 `----------------------------*/
229<INITIAL>
230{
231 "%binary" return PERCENT_NONASSOC;
232 "%debug" return PERCENT_DEBUG;
233 "%define" return PERCENT_DEFINE;
234 "%defines" return PERCENT_DEFINES;
235 "%destructor" return PERCENT_DESTRUCTOR;
236 "%dprec" return PERCENT_DPREC;
237 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
238 "%expect" return PERCENT_EXPECT;
239 "%file-prefix" return PERCENT_FILE_PREFIX;
240 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
241 "%glr-parser" return PERCENT_GLR_PARSER;
242 "%left" return PERCENT_LEFT;
243 "%locations" return PERCENT_LOCATIONS;
244 "%merge" return PERCENT_MERGE;
245 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
246 "%no"[-_]"lines" return PERCENT_NO_LINES;
247 "%nonassoc" return PERCENT_NONASSOC;
248 "%nterm" return PERCENT_NTERM;
249 "%output" return PERCENT_OUTPUT;
250 "%parse-param" return PERCENT_PARSE_PARAM;
251 "%prec" rule_length--; return PERCENT_PREC;
252 "%printer" return PERCENT_PRINTER;
253 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
254 "%right" return PERCENT_RIGHT;
255 "%lex-param" return PERCENT_LEX_PARAM;
256 "%skeleton" return PERCENT_SKELETON;
257 "%start" return PERCENT_START;
258 "%term" return PERCENT_TOKEN;
259 "%token" return PERCENT_TOKEN;
260 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
261 "%type" return PERCENT_TYPE;
262 "%union" return PERCENT_UNION;
263 "%verbose" return PERCENT_VERBOSE;
264 "%yacc" return PERCENT_YACC;
265
266 {directive} {
267 complain_at (*yylloc, _("invalid directive: %s"), quote (yytext));
268 YY_STEP;
269 }
270
271 ^"#line "{int}" \""[^\"]*"\"\n" handle_syncline (yytext + strlen ("#line "), yylloc); YY_STEP;
272
273 "=" return EQUAL;
274 ":" rule_length = 0; return COLON;
275 "|" rule_length = 0; return PIPE;
276 "," return COMMA;
277 ";" return SEMICOLON;
278
279 [ \f\n\t\v]+ YY_STEP;
280
281 {id} {
282 yylval->symbol = symbol_get (yytext, *yylloc);
283 rule_length++;
284 return ID;
285 }
286
287 {int} {
288 unsigned long num;
289 errno = 0;
290 num = strtoul (yytext, 0, 10);
291 if (INT_MAX < num || errno)
292 {
293 complain_at (*yylloc, _("integer out of range: %s"), quote (yytext));
294 num = INT_MAX;
295 }
296 yylval->integer = num;
297 return INT;
298 }
299
300 /* Characters. We don't check there is only one. */
301 "'" YY_OBS_GROW; yy_push_state (SC_ESCAPED_CHARACTER);
302
303 /* Strings. */
304 "\"" YY_OBS_GROW; yy_push_state (SC_ESCAPED_STRING);
305
306 /* Comments. */
307 "/*" BEGIN SC_YACC_COMMENT;
308 "//".* YY_STEP;
309
310 /* Prologue. */
311 "%{" yy_push_state (SC_PROLOGUE);
312
313 /* Code in between braces. */
314 "{" YY_OBS_GROW; braces_level = 0; yy_push_state (SC_BRACED_CODE);
315
316 /* A type. */
317 "<"{tag}">" {
318 obstack_grow (&string_obstack, yytext + 1, yyleng - 2);
319 YY_OBS_FINISH;
320 yylval->string = last_string;
321 return TYPE;
322 }
323
324
325 "%%" {
326 if (++percent_percent_count == 2)
327 yy_push_state (SC_EPILOGUE);
328 return PERCENT_PERCENT;
329 }
330
331 . {
332 complain_at (*yylloc, _("invalid character: %s"), quote (yytext));
333 YY_STEP;
334 }
335}
336
337
338 /*-------------------------------------------------------------------.
339 | Whatever the start condition (but those which correspond to |
340 | entities `swallowed' by Bison: SC_YACC_COMMENT, SC_ESCAPED_STRING, |
341 | and SC_ESCAPED_CHARACTER), no M4 character must escape as is. |
342 `-------------------------------------------------------------------*/
343
344<SC_COMMENT,SC_LINE_COMMENT,SC_STRING,SC_CHARACTER,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
345{
346 \[ obstack_sgrow (&string_obstack, "@<:@");
347 \] obstack_sgrow (&string_obstack, "@:>@");
348}
349
350
351 /*---------------------------------------------------------------.
352 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
353 `---------------------------------------------------------------*/
354
355<SC_YACC_COMMENT>
356{
357 "*/" {
358 YY_STEP;
359 BEGIN INITIAL;
360 }
361
362 [^*]+|"*" ;
363
364 <<EOF>> {
365 unexpected_end_of_file (*yylloc, "/*", "*/");
366 BEGIN INITIAL;
367 }
368}
369
370
371 /*------------------------------------------------------------.
372 | Scanning a C comment. The initial `/ *' is already eaten. |
373 `------------------------------------------------------------*/
374
375<SC_COMMENT>
376{
377 "*"{splice}"/" YY_OBS_GROW; yy_pop_state ();
378 [^*\[\]]+|"*" YY_OBS_GROW;
379
380 <<EOF>> {
381 unexpected_end_of_file (*yylloc, "/*", "*/");
382 yy_pop_state ();
383 }
384}
385
386
387 /*--------------------------------------------------------------.
388 | Scanning a line comment. The initial `//' is already eaten. |
389 `--------------------------------------------------------------*/
390
391<SC_LINE_COMMENT>
392{
393 "\n" YY_OBS_GROW; yy_pop_state ();
394 ([^\n\[\]]|{splice})+ YY_OBS_GROW;
395 <<EOF>> yy_pop_state ();
396}
397
398
399 /*----------------------------------------------------------------.
400 | Scanning a C string, including its escapes. The initial `"' is |
401 | already eaten. |
402 `----------------------------------------------------------------*/
403
404<SC_ESCAPED_STRING>
405{
406 "\"" {
407 assert (yy_top_state () == INITIAL);
408 YY_OBS_GROW;
409 YY_OBS_FINISH;
410 yylval->string = last_string;
411 yy_pop_state ();
412 rule_length++;
413 return STRING;
414 }
415
416 [^\"\\]+ YY_OBS_GROW;
417
418 <<EOF>> {
419 unexpected_end_of_file (*yylloc, "\"", "\"");
420 assert (yy_top_state () == INITIAL);
421 YY_OBS_FINISH;
422 yylval->string = last_string;
423 yy_pop_state ();
424 return STRING;
425 }
426}
427
428 /*---------------------------------------------------------------.
429 | Scanning a C character, decoding its escapes. The initial "'" |
430 | is already eaten. |
431 `---------------------------------------------------------------*/
432
433<SC_ESCAPED_CHARACTER>
434{
435 "'" {
436 YY_OBS_GROW;
437 assert (yy_top_state () == INITIAL);
438 {
439 YY_OBS_FINISH;
440 yylval->symbol = symbol_get (last_string, *yylloc);
441 symbol_class_set (yylval->symbol, token_sym, *yylloc);
442 symbol_user_token_number_set (yylval->symbol,
443 (unsigned char) last_string[1], *yylloc);
444 YY_OBS_FREE;
445 yy_pop_state ();
446 rule_length++;
447 return ID;
448 }
449 }
450
451 [^\'\\]+ YY_OBS_GROW;
452
453 <<EOF>> {
454 unexpected_end_of_file (*yylloc, "'", "'");
455 assert (yy_top_state () == INITIAL);
456 YY_OBS_FINISH;
457 yylval->string = last_string;
458 yy_pop_state ();
459 return CHARACTER;
460 }
461}
462
463
464 /*----------------------------.
465 | Decode escaped characters. |
466 `----------------------------*/
467
468<SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
469{
470 \\[0-7]{1,3} {
471 unsigned long c = strtoul (yytext + 1, 0, 8);
472 if (UCHAR_MAX < c)
473 {
474 complain_at (*yylloc, _("invalid escape sequence: %s"),
475 quote (yytext));
476 YY_STEP;
477 }
478 else
479 obstack_1grow (&string_obstack, c);
480 }
481
482 \\x[0-9a-fA-F]+ {
483 unsigned long c;
484 errno = 0;
485 c = strtoul (yytext + 2, 0, 16);
486 if (UCHAR_MAX < c || errno)
487 {
488 complain_at (*yylloc, _("invalid escape sequence: %s"),
489 quote (yytext));
490 YY_STEP;
491 }
492 else
493 obstack_1grow (&string_obstack, c);
494 }
495
496 \\a obstack_1grow (&string_obstack, '\a');
497 \\b obstack_1grow (&string_obstack, '\b');
498 \\f obstack_1grow (&string_obstack, '\f');
499 \\n obstack_1grow (&string_obstack, '\n');
500 \\r obstack_1grow (&string_obstack, '\r');
501 \\t obstack_1grow (&string_obstack, '\t');
502 \\v obstack_1grow (&string_obstack, '\v');
503 \\[\"\'?\\] obstack_1grow (&string_obstack, yytext[1]);
504 \\(u|U[0-9a-fA-F]{4})[0-9a-fA-F]{4} {
505 int c = convert_ucn_to_byte (yytext);
506 if (c < 0)
507 {
508 complain_at (*yylloc, _("invalid escape sequence: %s"),
509 quote (yytext));
510 YY_STEP;
511 }
512 else
513 obstack_1grow (&string_obstack, c);
514 }
515 \\(.|\n) {
516 complain_at (*yylloc, _("unrecognized escape sequence: %s"),
517 quote (yytext));
518 YY_OBS_GROW;
519 }
520 /* FLex wants this rule, in case of a `\<<EOF>>'. */
521 \\ YY_OBS_GROW;
522}
523
524
525 /*----------------------------------------------------------.
526 | Scanning a C character without decoding its escapes. The |
527 | initial "'" is already eaten. |
528 `----------------------------------------------------------*/
529
530<SC_CHARACTER>
531{
532 "'" {
533 YY_OBS_GROW;
534 assert (yy_top_state () != INITIAL);
535 yy_pop_state ();
536 }
537
538 [^'\[\]\\]+ YY_OBS_GROW;
539 \\{splice}[^\[\]] YY_OBS_GROW;
540 {splice} YY_OBS_GROW;
541 /* Needed for `\<<EOF>>', `\\<<newline>>[', and `\\<<newline>>]'. */
542 \\ YY_OBS_GROW;
543
544 <<EOF>> {
545 unexpected_end_of_file (*yylloc, "'", "'");
546 assert (yy_top_state () != INITIAL);
547 yy_pop_state ();
548 }
549}
550
551
552 /*----------------------------------------------------------------.
553 | Scanning a C string, without decoding its escapes. The initial |
554 | `"' is already eaten. |
555 `----------------------------------------------------------------*/
556
557<SC_STRING>
558{
559 "\"" {
560 assert (yy_top_state () != INITIAL);
561 YY_OBS_GROW;
562 yy_pop_state ();
563 }
564
565 [^\"\[\]\\]+ YY_OBS_GROW;
566 \\{splice}[^\[\]] YY_OBS_GROW;
567 {splice} YY_OBS_GROW;
568 /* Needed for `\<<EOF>>', `\\<<newline>>[', and `\\<<newline>>]'. */
569 \\ YY_OBS_GROW;
570
571 <<EOF>> {
572 unexpected_end_of_file (*yylloc, "\"", "\"");
573 assert (yy_top_state () != INITIAL);
574 yy_pop_state ();
575 }
576}
577
578
579 /*---------------------------------------------------.
580 | Strings, comments etc. can be found in user code. |
581 `---------------------------------------------------*/
582
583<SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
584{
585 /* Characters. We don't check there is only one. */
586 "'" YY_OBS_GROW; yy_push_state (SC_CHARACTER);
587
588 /* Strings. */
589 "\"" YY_OBS_GROW; yy_push_state (SC_STRING);
590
591 /* Comments. */
592 "/"{splice}"*" YY_OBS_GROW; yy_push_state (SC_COMMENT);
593 "/"{splice}"/" YY_OBS_GROW; yy_push_state (SC_LINE_COMMENT);
594
595 /* Not comments. */
596 "/" YY_OBS_GROW;
597}
598
599
600 /*---------------------------------------------------------------.
601 | Scanning some code in braces (%union and actions). The initial |
602 | "{" is already eaten. |
603 `---------------------------------------------------------------*/
604
605<SC_BRACED_CODE>
606{
607 "{"|"<"{splice}"%" YY_OBS_GROW; braces_level++;
608 "%"{splice}">" YY_OBS_GROW; braces_level--;
609 "}" {
610 YY_OBS_GROW;
611 braces_level--;
612 if (braces_level < 0)
613 {
614 yy_pop_state ();
615 YY_OBS_FINISH;
616 yylval->string = last_string;
617 rule_length++;
618 return BRACED_CODE;
619 }
620 }
621
622 "$"("<"{tag}">")?(-?[0-9]+|"$") { handle_dollar (current_braced_code,
623 yytext, *yylloc); }
624 "@"(-?[0-9]+|"$") { handle_at (current_braced_code,
625 yytext, *yylloc); }
626
627 /* `"<"{splice}"<"' tokenizes `<<%' correctly (as `<<' `%') rather
628 than incorrrectly (as `<' `<%'). */
629 [^\"$%\'/<@\[\]\{\}]+|[$%/<@]|"<"{splice}"<" YY_OBS_GROW;
630
631 <<EOF>> {
632 unexpected_end_of_file (*yylloc, "{", "}");
633 yy_pop_state ();
634 YY_OBS_FINISH;
635 yylval->string = last_string;
636 return BRACED_CODE;
637 }
638
639}
640
641
642 /*--------------------------------------------------------------.
643 | Scanning some prologue: from "%{" (already scanned) to "%}". |
644 `--------------------------------------------------------------*/
645
646<SC_PROLOGUE>
647{
648 "%}" {
649 yy_pop_state ();
650 YY_OBS_FINISH;
651 yylval->string = last_string;
652 return PROLOGUE;
653 }
654
655 [^%\[\]/\'\"]+ YY_OBS_GROW;
656 "%" YY_OBS_GROW;
657
658 <<EOF>> {
659 unexpected_end_of_file (*yylloc, "%{", "%}");
660 yy_pop_state ();
661 YY_OBS_FINISH;
662 yylval->string = last_string;
663 return PROLOGUE;
664 }
665}
666
667
668 /*---------------------------------------------------------------.
669 | Scanning the epilogue (everything after the second "%%", which |
670 | has already been eaten). |
671 `---------------------------------------------------------------*/
672
673<SC_EPILOGUE>
674{
675 [^\[\]]+ YY_OBS_GROW;
676
677 <<EOF>> {
678 yy_pop_state ();
679 YY_OBS_FINISH;
680 yylval->string = last_string;
681 return EPILOGUE;
682 }
683}
684
685
686%%
687
688/*------------------------------------------------------------------.
689| TEXT is pointing to a wannabee semantic value (i.e., a `$'). |
690| |
691| Possible inputs: $[<TYPENAME>]($|integer) |
692| |
693| Output to the STRING_OBSTACK a reference to this semantic value. |
694`------------------------------------------------------------------*/
695
696static inline void
697handle_action_dollar (char *text, location_t location)
698{
699 const char *type_name = NULL;
700 char *cp = text + 1;
701
702 /* Get the type name if explicit. */
703 if (*cp == '<')
704 {
705 type_name = ++cp;
706 while (*cp != '>')
707 ++cp;
708 *cp = '\0';
709 ++cp;
710 }
711
712 if (*cp == '$')
713 {
714 if (!type_name)
715 type_name = symbol_list_n_type_name_get (current_rule, location, 0);
716 if (!type_name && typed)
717 complain_at (location, _("$$ of `%s' has no declared type"),
718 current_rule->sym->tag);
719 if (!type_name)
720 type_name = "";
721 obstack_fgrow1 (&string_obstack,
722 "]b4_lhs_value([%s])[", type_name);
723 }
724 else
725 {
726 long num;
727 errno = 0;
728 num = strtol (cp, 0, 10);
729
730 if (INT_MIN <= num && num <= rule_length && ! errno)
731 {
732 int n = num;
733 if (!type_name && n > 0)
734 type_name = symbol_list_n_type_name_get (current_rule, location,
735 n);
736 if (!type_name && typed)
737 complain_at (location, _("$%d of `%s' has no declared type"),
738 n, current_rule->sym->tag);
739 if (!type_name)
740 type_name = "";
741 obstack_fgrow3 (&string_obstack,
742 "]b4_rhs_value([%d], [%d], [%s])[",
743 rule_length, n, type_name);
744 }
745 else
746 complain_at (location, _("integer out of range: %s"), quote (text));
747 }
748}
749
750
751/*---------------------------------------------------------------.
752| TEXT is expected to be $$ in some code associated to a symbol: |
753| destructor or printer. |
754`---------------------------------------------------------------*/
755
756static inline void
757handle_symbol_code_dollar (char *text, location_t location)
758{
759 char *cp = text + 1;
760 if (*cp == '$')
761 obstack_sgrow (&string_obstack, "]b4_dollar_dollar[");
762 else
763 complain_at (location, _("invalid value: %s"), quote (text));
764}
765
766
767/*-----------------------------------------------------------------.
768| Dispatch onto handle_action_dollar, or handle_destructor_dollar, |
769| depending upon CODE_KIND. |
770`-----------------------------------------------------------------*/
771
772static void
773handle_dollar (braced_code_t braced_code_kind,
774 char *text, location_t location)
775{
776 switch (braced_code_kind)
777 {
778 case action_braced_code:
779 handle_action_dollar (text, location);
780 break;
781
782 case destructor_braced_code:
783 case printer_braced_code:
784 handle_symbol_code_dollar (text, location);
785 break;
786 }
787}
788
789
790/*------------------------------------------------------.
791| TEXT is a location token (i.e., a `@...'). Output to |
792| STRING_OBSTACK a reference to this location. |
793`------------------------------------------------------*/
794
795static inline void
796handle_action_at (char *text, location_t location)
797{
798 char *cp = text + 1;
799 locations_flag = 1;
800
801 if (*cp == '$')
802 {
803 obstack_sgrow (&string_obstack, "]b4_lhs_location[");
804 }
805 else
806 {
807 long num;
808 errno = 0;
809 num = strtol (cp, 0, 10);
810
811 if (INT_MIN <= num && num <= rule_length && ! errno)
812 {
813 int n = num;
814 obstack_fgrow2 (&string_obstack, "]b4_rhs_location([%d], [%d])[",
815 rule_length, n);
816 }
817 else
818 complain_at (location, _("integer out of range: %s"), quote (text));
819 }
820}
821
822
823/*---------------------------------------------------------------.
824| TEXT is expected to be @$ in some code associated to a symbol: |
825| destructor or printer. |
826`---------------------------------------------------------------*/
827
828static inline void
829handle_symbol_code_at (char *text, location_t location)
830{
831 char *cp = text + 1;
832 if (*cp == '$')
833 obstack_sgrow (&string_obstack, "]b4_at_dollar[");
834 else
835 complain_at (location, _("invalid value: %s"), quote (text));
836}
837
838
839/*-------------------------------------------------------------------.
840| Dispatch onto handle_action_at, or handle_destructor_at, depending |
841| upon CODE_KIND. |
842`-------------------------------------------------------------------*/
843
844static void
845handle_at (braced_code_t braced_code_kind,
846 char *text, location_t location)
847{
848 switch (braced_code_kind)
849 {
850 case action_braced_code:
851 handle_action_at (text, location);
852 break;
853
854 case destructor_braced_code:
855 case printer_braced_code:
856 handle_symbol_code_at (text, location);
857 break;
858 }
859}
860
861
862/*------------------------------------------------------------------.
863| Convert universal character name UCN to a single-byte character, |
864| and return that character. Return -1 if UCN does not correspond |
865| to a single-byte character. |
866`------------------------------------------------------------------*/
867
868static int
869convert_ucn_to_byte (char const *ucn)
870{
871 unsigned long code = strtoul (ucn + 2, 0, 16);
872
873 /* FIXME: Currently we assume Unicode-compatible unibyte characters
874 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
875 non-ASCII hosts we support only the portable C character set.
876 These limitations should be removed once we add support for
877 multibyte characters. */
878
879 if (UCHAR_MAX < code)
880 return -1;
881
882#if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
883 {
884 /* A non-ASCII host. Use CODE to index into a table of the C
885 basic execution character set, which is guaranteed to exist on
886 all Standard C platforms. This table also includes '$', '@',
887 and '`', which are not in the basic execution character set but
888 which are unibyte characters on all the platforms that we know
889 about. */
890 static signed char const table[] =
891 {
892 '\0', -1, -1, -1, -1, -1, -1, '\a',
893 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
894 -1, -1, -1, -1, -1, -1, -1, -1,
895 -1, -1, -1, -1, -1, -1, -1, -1,
896 ' ', '!', '"', '#', '$', '%', '&', '\'',
897 '(', ')', '*', '+', ',', '-', '.', '/',
898 '0', '1', '2', '3', '4', '5', '6', '7',
899 '8', '9', ':', ';', '<', '=', '>', '?',
900 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
901 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
902 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
903 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
904 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
905 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
906 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
907 'x', 'y', 'z', '{', '|', '}', '~'
908 };
909
910 code = code < sizeof table ? table[code] : -1;
911 }
912#endif
913
914 return code;
915}
916
917
918/*----------------------------------------------------------------.
919| Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
920`----------------------------------------------------------------*/
921
922static void
923handle_syncline (char *args, location_t *location)
924{
925 int lineno = strtol (args, &args, 10);
926 const char *file = NULL;
927 file = strchr (args, '"') + 1;
928 *strchr (file, '"') = 0;
929 /* FIXME: Leaking... Can't free, as some locations are still
930 pointing to the old file name. */
931 infile = xstrdup (file);
932 location->file = infile;
933 location->last_line = lineno;
934}
935
936/*-------------------------.
937| Initialize the scanner. |
938`-------------------------*/
939
940void
941scanner_initialize (void)
942{
943 obstack_init (&string_obstack);
944}
945
946
947/*-----------------------------------------------.
948| Free all the memory allocated to the scanner. |
949`-----------------------------------------------*/
950
951void
952scanner_free (void)
953{
954 obstack_free (&string_obstack, 0);
955 /* Reclaim Flex's buffers. */
956 yy_delete_buffer (YY_CURRENT_BUFFER);
957}