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