]> git.saurik.com Git - bison.git/blob - src/scan-gram.l
60813bb620d352ee0e2dea522b085a92d19cffaa
[bison.git] / src / scan-gram.l
1 /* Bison Grammar Scanner -*- C -*-
2
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free
4 Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 %option debug nodefault nounput noyywrap never-interactive
22 %option prefix="gram_" outfile="lex.yy.c"
23
24 %{
25 /* Work around a bug in flex 2.5.31. See Debian bug 333231
26 <http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=333231>. */
27 #undef gram_wrap
28 #define gram_wrap() 1
29
30 #define FLEX_PREFIX(Id) gram_ ## Id
31 #include "flex-scanner.h"
32
33 #include "complain.h"
34 #include "files.h"
35 #include "gram.h"
36 #include "quotearg.h"
37 #include "reader.h"
38 #include "uniqstr.h"
39
40 #include <mbswidth.h>
41 #include <quote.h>
42
43 #include "scan-gram.h"
44
45 #define YY_DECL GRAM_LEX_DECL
46
47 #define YY_USER_INIT \
48 code_start = scanner_cursor = loc->start; \
49
50 /* Location of scanner cursor. */
51 static boundary scanner_cursor;
52
53 #define YY_USER_ACTION location_compute (loc, &scanner_cursor, yytext, yyleng);
54
55 static size_t no_cr_read (FILE *, char *, size_t);
56 #define YY_INPUT(buf, result, size) ((result) = no_cr_read (yyin, buf, size))
57
58 #define ROLLBACK_CURRENT_TOKEN \
59 do { \
60 scanner_cursor.column -= mbsnwidth (yytext, yyleng, 0); \
61 yyless (0); \
62 } while (0)
63
64 /* A string representing the most recently saved token. */
65 static char *last_string;
66
67 /* Bracketed identifier. */
68 static uniqstr bracketed_id_str = 0;
69 static location bracketed_id_loc;
70 static boundary bracketed_id_start;
71 static int bracketed_id_context_state = 0;
72
73 void
74 gram_scanner_last_string_free (void)
75 {
76 STRING_FREE;
77 }
78
79 static void handle_syncline (char *, location);
80 static unsigned long int scan_integer (char const *p, int base, location loc);
81 static int convert_ucn_to_byte (char const *hex_text);
82 static void unexpected_eof (boundary, char const *);
83 static void unexpected_newline (boundary, char const *);
84
85 %}
86 /* A C-like comment in directives/rules. */
87 %x SC_YACC_COMMENT
88 /* Strings and characters in directives/rules. */
89 %x SC_ESCAPED_STRING SC_ESCAPED_CHARACTER
90 /* A identifier was just read in directives/rules. Special state
91 to capture the sequence `identifier :'. */
92 %x SC_AFTER_IDENTIFIER
93
94 /* Three types of user code:
95 - prologue (code between `%{' `%}' in the first section, before %%);
96 - actions, printers, union, etc, (between braced in the middle section);
97 - epilogue (everything after the second %%). */
98 %x SC_PROLOGUE SC_BRACED_CODE SC_EPILOGUE
99 /* C and C++ comments in code. */
100 %x SC_COMMENT SC_LINE_COMMENT
101 /* Strings and characters in code. */
102 %x SC_STRING SC_CHARACTER
103 /* Bracketed identifiers support. */
104 %x SC_BRACKETED_ID SC_RETURN_BRACKETED_ID
105
106 letter [-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_]
107 id {letter}({letter}|[0-9])*
108 directive %{id}
109 int [0-9]+
110
111 /* POSIX says that a tag must be both an id and a C union member, but
112 historically almost any character is allowed in a tag. We disallow
113 NUL and newline, as this simplifies our implementation. */
114 tag [^\0\n>]+
115
116 /* Zero or more instances of backslash-newline. Following GCC, allow
117 white space between the backslash and the newline. */
118 splice (\\[ \f\t\v]*\n)*
119
120 %%
121 %{
122 /* Nesting level of the current code in braces. */
123 int braces_level IF_LINT (= 0);
124
125 /* Parent context state, when applicable. */
126 int context_state IF_LINT (= 0);
127
128 /* Location of most recent identifier, when applicable. */
129 location id_loc IF_LINT (= empty_location);
130
131 /* Where containing code started, when applicable. Its initial
132 value is relevant only when yylex is invoked in the SC_EPILOGUE
133 start condition. */
134 boundary code_start = scanner_cursor;
135
136 /* Where containing comment or string or character literal started,
137 when applicable. */
138 boundary token_start IF_LINT (= scanner_cursor);
139 %}
140
141
142 /*-----------------------.
143 | Scanning white space. |
144 `-----------------------*/
145
146 <INITIAL,SC_AFTER_IDENTIFIER,SC_BRACKETED_ID,SC_RETURN_BRACKETED_ID>
147 {
148 /* Comments and white space. */
149 "," warn_at (*loc, _("stray `,' treated as white space"));
150 [ \f\n\t\v] |
151 "//".* ;
152 "/*" {
153 token_start = loc->start;
154 context_state = YY_START;
155 BEGIN SC_YACC_COMMENT;
156 }
157
158 /* #line directives are not documented, and may be withdrawn or
159 modified in future versions of Bison. */
160 ^"#line "{int}" \"".*"\"\n" {
161 handle_syncline (yytext + sizeof "#line " - 1, *loc);
162 }
163 }
164
165
166 /*----------------------------.
167 | Scanning Bison directives. |
168 `----------------------------*/
169
170 /* For directives that are also command line options, the regex must be
171 "%..."
172 after "[-_]"s are removed, and the directive must match the --long
173 option name, with a single string argument. Otherwise, add exceptions
174 to ../build-aux/cross-options.pl. */
175
176 <INITIAL>
177 {
178 "%binary" return PERCENT_NONASSOC;
179 "%code" return PERCENT_CODE;
180 "%debug" return PERCENT_DEBUG;
181 "%default"[-_]"prec" return PERCENT_DEFAULT_PREC;
182 "%define" return PERCENT_DEFINE;
183 "%defines" return PERCENT_DEFINES;
184 "%destructor" return PERCENT_DESTRUCTOR;
185 "%dprec" return PERCENT_DPREC;
186 "%error"[-_]"verbose" return PERCENT_ERROR_VERBOSE;
187 "%expect" return PERCENT_EXPECT;
188 "%expect"[-_]"rr" return PERCENT_EXPECT_RR;
189 "%file-prefix" return PERCENT_FILE_PREFIX;
190 "%fixed"[-_]"output"[-_]"files" return PERCENT_YACC;
191 "%initial-action" return PERCENT_INITIAL_ACTION;
192 "%glr-parser" return PERCENT_GLR_PARSER;
193 "%language" return PERCENT_LANGUAGE;
194 "%left" return PERCENT_LEFT;
195 "%lex-param" return PERCENT_LEX_PARAM;
196 "%locations" return PERCENT_LOCATIONS;
197 "%merge" return PERCENT_MERGE;
198 "%name"[-_]"prefix" return PERCENT_NAME_PREFIX;
199 "%no"[-_]"default"[-_]"prec" return PERCENT_NO_DEFAULT_PREC;
200 "%no"[-_]"lines" return PERCENT_NO_LINES;
201 "%nonassoc" return PERCENT_NONASSOC;
202 "%nondeterministic-parser" return PERCENT_NONDETERMINISTIC_PARSER;
203 "%nterm" return PERCENT_NTERM;
204 "%output" return PERCENT_OUTPUT;
205 "%parse-param" return PERCENT_PARSE_PARAM;
206 "%prec" return PERCENT_PREC;
207 "%printer" return PERCENT_PRINTER;
208 "%pure"[-_]"parser" return PERCENT_PURE_PARSER;
209 "%require" return PERCENT_REQUIRE;
210 "%right" return PERCENT_RIGHT;
211 "%skeleton" return PERCENT_SKELETON;
212 "%start" return PERCENT_START;
213 "%term" return PERCENT_TOKEN;
214 "%token" return PERCENT_TOKEN;
215 "%token"[-_]"table" return PERCENT_TOKEN_TABLE;
216 "%type" return PERCENT_TYPE;
217 "%union" return PERCENT_UNION;
218 "%verbose" return PERCENT_VERBOSE;
219 "%yacc" return PERCENT_YACC;
220
221 {directive} {
222 complain_at (*loc, _("invalid directive: %s"), quote (yytext));
223 }
224
225 "=" return EQUAL;
226 "|" return PIPE;
227 ";" return SEMICOLON;
228 "<*>" return TYPE_TAG_ANY;
229 "<>" return TYPE_TAG_NONE;
230
231 {id} {
232 val->uniqstr = uniqstr_new (yytext);
233 id_loc = *loc;
234 bracketed_id_str = NULL;
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 /* Identifiers may not start with a digit. Yet, don't silently
248 accept "1FOO" as "1 FOO". */
249 {int}{id} {
250 complain_at (*loc, _("invalid identifier: %s"), quote (yytext));
251 }
252
253 /* Characters. We don't check there is only one. */
254 "'" STRING_GROW; token_start = loc->start; BEGIN SC_ESCAPED_CHARACTER;
255
256 /* Strings. */
257 "\"" token_start = loc->start; BEGIN SC_ESCAPED_STRING;
258
259 /* Prologue. */
260 "%{" code_start = loc->start; BEGIN SC_PROLOGUE;
261
262 /* Code in between braces. */
263 "{" {
264 STRING_GROW;
265 braces_level = 0;
266 code_start = loc->start;
267 BEGIN SC_BRACED_CODE;
268 }
269
270 /* A type. */
271 "<"{tag}">" {
272 obstack_grow (&obstack_for_string, yytext + 1, yyleng - 2);
273 STRING_FINISH;
274 val->uniqstr = uniqstr_new (last_string);
275 STRING_FREE;
276 return TYPE;
277 }
278
279 "%%" {
280 static int percent_percent_count;
281 if (++percent_percent_count == 2)
282 BEGIN SC_EPILOGUE;
283 return PERCENT_PERCENT;
284 }
285
286 "[" {
287 bracketed_id_str = NULL;
288 bracketed_id_start = loc->start;
289 bracketed_id_context_state = YY_START;
290 BEGIN SC_BRACKETED_ID;
291 }
292
293 . {
294 complain_at (*loc, _("invalid character: %s"), quote (yytext));
295 }
296
297 <<EOF>> {
298 loc->start = loc->end = scanner_cursor;
299 yyterminate ();
300 }
301 }
302
303
304 /*-----------------------------------------------------------------.
305 | Scanning after an identifier, checking whether a colon is next. |
306 `-----------------------------------------------------------------*/
307
308 <SC_AFTER_IDENTIFIER>
309 {
310 "[" {
311 if (bracketed_id_str)
312 {
313 ROLLBACK_CURRENT_TOKEN;
314 BEGIN SC_RETURN_BRACKETED_ID;
315 *loc = id_loc;
316 return ID;
317 }
318 else
319 {
320 bracketed_id_start = loc->start;
321 bracketed_id_context_state = YY_START;
322 BEGIN SC_BRACKETED_ID;
323 }
324 }
325 ":" {
326 BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
327 *loc = id_loc;
328 return ID_COLON;
329 }
330 . {
331 ROLLBACK_CURRENT_TOKEN;
332 BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
333 *loc = id_loc;
334 return ID;
335 }
336 <<EOF>> {
337 BEGIN (bracketed_id_str ? SC_RETURN_BRACKETED_ID : INITIAL);
338 *loc = id_loc;
339 return ID;
340 }
341 }
342
343 /*--------------------------------.
344 | Scanning bracketed identifiers. |
345 `--------------------------------*/
346
347 <SC_BRACKETED_ID>
348 {
349 {id} {
350 if (bracketed_id_str)
351 {
352 complain_at (*loc, _("unexpected identifier in bracketed name: %s"),
353 quote (yytext));
354 }
355 else
356 {
357 bracketed_id_str = uniqstr_new (yytext);
358 bracketed_id_loc = *loc;
359 }
360 }
361 "]" {
362 BEGIN bracketed_id_context_state;
363 if (bracketed_id_str)
364 {
365 if (INITIAL == bracketed_id_context_state)
366 {
367 val->uniqstr = bracketed_id_str;
368 bracketed_id_str = 0;
369 *loc = bracketed_id_loc;
370 return BRACKETED_ID;
371 }
372 }
373 else
374 complain_at (*loc, _("an identifier expected"));
375 }
376 . {
377 complain_at (*loc, _("invalid character in bracketed name: %s"),
378 quote (yytext));
379 }
380 <<EOF>> {
381 BEGIN bracketed_id_context_state;
382 unexpected_eof (bracketed_id_start, "]");
383 }
384 }
385
386 <SC_RETURN_BRACKETED_ID>
387 {
388 . {
389 ROLLBACK_CURRENT_TOKEN;
390 val->uniqstr = bracketed_id_str;
391 bracketed_id_str = 0;
392 *loc = bracketed_id_loc;
393 BEGIN INITIAL;
394 return BRACKETED_ID;
395 }
396 }
397
398
399 /*---------------------------------------------------------------.
400 | Scanning a Yacc comment. The initial `/ *' is already eaten. |
401 `---------------------------------------------------------------*/
402
403 <SC_YACC_COMMENT>
404 {
405 "*/" BEGIN context_state;
406 .|\n ;
407 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
408 }
409
410
411 /*------------------------------------------------------------.
412 | Scanning a C comment. The initial `/ *' is already eaten. |
413 `------------------------------------------------------------*/
414
415 <SC_COMMENT>
416 {
417 "*"{splice}"/" STRING_GROW; BEGIN context_state;
418 <<EOF>> unexpected_eof (token_start, "*/"); BEGIN context_state;
419 }
420
421
422 /*--------------------------------------------------------------.
423 | Scanning a line comment. The initial `//' is already eaten. |
424 `--------------------------------------------------------------*/
425
426 <SC_LINE_COMMENT>
427 {
428 "\n" STRING_GROW; BEGIN context_state;
429 {splice} STRING_GROW;
430 <<EOF>> BEGIN context_state;
431 }
432
433
434 /*------------------------------------------------.
435 | Scanning a Bison string, including its escapes. |
436 | The initial quote is already eaten. |
437 `------------------------------------------------*/
438
439 <SC_ESCAPED_STRING>
440 {
441 "\""|"\n" {
442 if (yytext[0] == '\n')
443 unexpected_newline (token_start, "\"");
444 STRING_FINISH;
445 loc->start = token_start;
446 val->chars = last_string;
447 BEGIN INITIAL;
448 return STRING;
449 }
450 <<EOF>> {
451 unexpected_eof (token_start, "\"");
452 STRING_FINISH;
453 loc->start = token_start;
454 val->chars = last_string;
455 BEGIN INITIAL;
456 return STRING;
457 }
458 }
459
460 /*----------------------------------------------------------.
461 | Scanning a Bison character literal, decoding its escapes. |
462 | The initial quote is already eaten. |
463 `----------------------------------------------------------*/
464
465 <SC_ESCAPED_CHARACTER>
466 {
467 "'"|"\n" {
468 if (yytext[0] == '\n')
469 unexpected_newline (token_start, "'");
470 STRING_GROW;
471 STRING_FINISH;
472 loc->start = token_start;
473 val->character = last_string[1];
474 STRING_FREE;
475 BEGIN INITIAL;
476 return CHAR;
477 }
478 <<EOF>> {
479 unexpected_eof (token_start, "'");
480 STRING_FINISH;
481 loc->start = token_start;
482 if (strlen (last_string) > 1)
483 val->character = last_string[1];
484 else
485 val->character = last_string[0];
486 STRING_FREE;
487 BEGIN INITIAL;
488 return CHAR;
489 }
490 }
491
492 <SC_ESCAPED_CHARACTER,SC_ESCAPED_STRING>
493 {
494 \0 complain_at (*loc, _("invalid null character"));
495 }
496
497
498 /*----------------------------.
499 | Decode escaped characters. |
500 `----------------------------*/
501
502 <SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>
503 {
504 \\[0-7]{1,3} {
505 unsigned long int c = strtoul (yytext + 1, NULL, 8);
506 if (UCHAR_MAX < c)
507 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
508 else if (! c)
509 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
510 else
511 obstack_1grow (&obstack_for_string, c);
512 }
513
514 \\x[0-9abcdefABCDEF]+ {
515 verify (UCHAR_MAX < ULONG_MAX);
516 unsigned long int c = strtoul (yytext + 2, NULL, 16);
517 if (UCHAR_MAX < c)
518 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
519 else if (! c)
520 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
521 else
522 obstack_1grow (&obstack_for_string, c);
523 }
524
525 \\a obstack_1grow (&obstack_for_string, '\a');
526 \\b obstack_1grow (&obstack_for_string, '\b');
527 \\f obstack_1grow (&obstack_for_string, '\f');
528 \\n obstack_1grow (&obstack_for_string, '\n');
529 \\r obstack_1grow (&obstack_for_string, '\r');
530 \\t obstack_1grow (&obstack_for_string, '\t');
531 \\v obstack_1grow (&obstack_for_string, '\v');
532
533 /* \\[\"\'?\\] would be shorter, but it confuses xgettext. */
534 \\("\""|"'"|"?"|"\\") obstack_1grow (&obstack_for_string, yytext[1]);
535
536 \\(u|U[0-9abcdefABCDEF]{4})[0-9abcdefABCDEF]{4} {
537 int c = convert_ucn_to_byte (yytext);
538 if (c < 0)
539 complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
540 else if (! c)
541 complain_at (*loc, _("invalid null character: %s"), quote (yytext));
542 else
543 obstack_1grow (&obstack_for_string, c);
544 }
545 \\(.|\n) {
546 complain_at (*loc, _("unrecognized escape sequence: %s"), quote (yytext));
547 STRING_GROW;
548 }
549 }
550
551 /*--------------------------------------------.
552 | Scanning user-code characters and strings. |
553 `--------------------------------------------*/
554
555 <SC_CHARACTER,SC_STRING>
556 {
557 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
558 }
559
560 <SC_CHARACTER>
561 {
562 "'" STRING_GROW; BEGIN context_state;
563 \n unexpected_newline (token_start, "'"); BEGIN context_state;
564 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
565 }
566
567 <SC_STRING>
568 {
569 "\"" STRING_GROW; BEGIN context_state;
570 \n unexpected_newline (token_start, "\""); BEGIN context_state;
571 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
572 }
573
574
575 /*---------------------------------------------------.
576 | Strings, comments etc. can be found in user code. |
577 `---------------------------------------------------*/
578
579 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
580 {
581 "'" {
582 STRING_GROW;
583 context_state = YY_START;
584 token_start = loc->start;
585 BEGIN SC_CHARACTER;
586 }
587 "\"" {
588 STRING_GROW;
589 context_state = YY_START;
590 token_start = loc->start;
591 BEGIN SC_STRING;
592 }
593 "/"{splice}"*" {
594 STRING_GROW;
595 context_state = YY_START;
596 token_start = loc->start;
597 BEGIN SC_COMMENT;
598 }
599 "/"{splice}"/" {
600 STRING_GROW;
601 context_state = YY_START;
602 BEGIN SC_LINE_COMMENT;
603 }
604 }
605
606
607
608 /*-----------------------------------------------------------.
609 | Scanning some code in braces (actions). The initial "{" is |
610 | already eaten. |
611 `-----------------------------------------------------------*/
612
613 <SC_BRACED_CODE>
614 {
615 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
616 "%"{splice}">" STRING_GROW; braces_level--;
617 "}" {
618 obstack_1grow (&obstack_for_string, '}');
619
620 --braces_level;
621 if (braces_level < 0)
622 {
623 STRING_FINISH;
624 loc->start = code_start;
625 val->code = last_string;
626 BEGIN INITIAL;
627 return BRACED_CODE;
628 }
629 }
630
631 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
632 (as `<' `<%'). */
633 "<"{splice}"<" STRING_GROW;
634
635 <<EOF>> {
636 unexpected_eof (code_start, "}");
637 STRING_FINISH;
638 loc->start = code_start;
639 val->code = last_string;
640 BEGIN INITIAL;
641 return BRACED_CODE;
642 }
643 }
644
645
646 /*--------------------------------------------------------------.
647 | Scanning some prologue: from "%{" (already scanned) to "%}". |
648 `--------------------------------------------------------------*/
649
650 <SC_PROLOGUE>
651 {
652 "%}" {
653 STRING_FINISH;
654 loc->start = code_start;
655 val->chars = last_string;
656 BEGIN INITIAL;
657 return PROLOGUE;
658 }
659
660 <<EOF>> {
661 unexpected_eof (code_start, "%}");
662 STRING_FINISH;
663 loc->start = code_start;
664 val->chars = last_string;
665 BEGIN INITIAL;
666 return PROLOGUE;
667 }
668 }
669
670
671 /*---------------------------------------------------------------.
672 | Scanning the epilogue (everything after the second "%%", which |
673 | has already been eaten). |
674 `---------------------------------------------------------------*/
675
676 <SC_EPILOGUE>
677 {
678 <<EOF>> {
679 STRING_FINISH;
680 loc->start = code_start;
681 val->chars = last_string;
682 BEGIN INITIAL;
683 return EPILOGUE;
684 }
685 }
686
687
688 /*-----------------------------------------------------.
689 | By default, grow the string obstack with the input. |
690 `-----------------------------------------------------*/
691
692 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
693 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
694
695 %%
696
697 /* Read bytes from FP into buffer BUF of size SIZE. Return the
698 number of bytes read. Remove '\r' from input, treating \r\n
699 and isolated \r as \n. */
700
701 static size_t
702 no_cr_read (FILE *fp, char *buf, size_t size)
703 {
704 size_t bytes_read = fread (buf, 1, size, fp);
705 if (bytes_read)
706 {
707 char *w = memchr (buf, '\r', bytes_read);
708 if (w)
709 {
710 char const *r = ++w;
711 char const *lim = buf + bytes_read;
712
713 for (;;)
714 {
715 /* Found an '\r'. Treat it like '\n', but ignore any
716 '\n' that immediately follows. */
717 w[-1] = '\n';
718 if (r == lim)
719 {
720 int ch = getc (fp);
721 if (ch != '\n' && ungetc (ch, fp) != ch)
722 break;
723 }
724 else if (*r == '\n')
725 r++;
726
727 /* Copy until the next '\r'. */
728 do
729 {
730 if (r == lim)
731 return w - buf;
732 }
733 while ((*w++ = *r++) != '\r');
734 }
735
736 return w - buf;
737 }
738 }
739
740 return bytes_read;
741 }
742
743
744
745 /*------------------------------------------------------.
746 | Scan NUMBER for a base-BASE integer at location LOC. |
747 `------------------------------------------------------*/
748
749 static unsigned long int
750 scan_integer (char const *number, int base, location loc)
751 {
752 verify (INT_MAX < ULONG_MAX);
753 unsigned long int num = strtoul (number, NULL, base);
754
755 if (INT_MAX < num)
756 {
757 complain_at (loc, _("integer out of range: %s"), quote (number));
758 num = INT_MAX;
759 }
760
761 return num;
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 verify (UCHAR_MAX <= INT_MAX);
775 unsigned long int code = strtoul (ucn + 2, NULL, 16);
776
777 /* FIXME: Currently we assume Unicode-compatible unibyte characters
778 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
779 non-ASCII hosts we support only the portable C character set.
780 These limitations should be removed once we add support for
781 multibyte characters. */
782
783 if (UCHAR_MAX < code)
784 return -1;
785
786 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
787 {
788 /* A non-ASCII host. Use CODE to index into a table of the C
789 basic execution character set, which is guaranteed to exist on
790 all Standard C platforms. This table also includes '$', '@',
791 and '`', which are not in the basic execution character set but
792 which are unibyte characters on all the platforms that we know
793 about. */
794 static signed char const table[] =
795 {
796 '\0', -1, -1, -1, -1, -1, -1, '\a',
797 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
798 -1, -1, -1, -1, -1, -1, -1, -1,
799 -1, -1, -1, -1, -1, -1, -1, -1,
800 ' ', '!', '"', '#', '$', '%', '&', '\'',
801 '(', ')', '*', '+', ',', '-', '.', '/',
802 '0', '1', '2', '3', '4', '5', '6', '7',
803 '8', '9', ':', ';', '<', '=', '>', '?',
804 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
805 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
806 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
807 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
808 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
809 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
810 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
811 'x', 'y', 'z', '{', '|', '}', '~'
812 };
813
814 code = code < sizeof table ? table[code] : -1;
815 }
816 #endif
817
818 return code;
819 }
820
821
822 /*----------------------------------------------------------------.
823 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
824 `----------------------------------------------------------------*/
825
826 static void
827 handle_syncline (char *args, location loc)
828 {
829 char *after_num;
830 unsigned long int lineno = strtoul (args, &after_num, 10);
831 char *file = strchr (after_num, '"') + 1;
832 *strchr (file, '"') = '\0';
833 if (INT_MAX <= lineno)
834 {
835 warn_at (loc, _("line number overflow"));
836 lineno = INT_MAX;
837 }
838 current_file = uniqstr_new (file);
839 boundary_set (&scanner_cursor, current_file, lineno, 1);
840 }
841
842
843 /*----------------------------------------------------------------.
844 | For a token or comment starting at START, report message MSGID, |
845 | which should say that an end marker was found before |
846 | the expected TOKEN_END. |
847 `----------------------------------------------------------------*/
848
849 static void
850 unexpected_end (boundary start, char const *msgid, char const *token_end)
851 {
852 location loc;
853 loc.start = start;
854 loc.end = scanner_cursor;
855 complain_at (loc, _(msgid), token_end);
856 }
857
858
859 /*------------------------------------------------------------------------.
860 | Report an unexpected EOF in a token or comment starting at START. |
861 | An end of file was encountered and the expected TOKEN_END was missing. |
862 `------------------------------------------------------------------------*/
863
864 static void
865 unexpected_eof (boundary start, char const *token_end)
866 {
867 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
868 }
869
870
871 /*----------------------------------------.
872 | Likewise, but for unexpected newlines. |
873 `----------------------------------------*/
874
875 static void
876 unexpected_newline (boundary start, char const *token_end)
877 {
878 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
879 }
880
881
882 /*-------------------------.
883 | Initialize the scanner. |
884 `-------------------------*/
885
886 void
887 gram_scanner_initialize (void)
888 {
889 obstack_init (&obstack_for_string);
890 }
891
892
893 /*-----------------------------------------------.
894 | Free all the memory allocated to the scanner. |
895 `-----------------------------------------------*/
896
897 void
898 gram_scanner_free (void)
899 {
900 obstack_free (&obstack_for_string, 0);
901 /* Reclaim Flex's buffers. */
902 yylex_destroy ();
903 }