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