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