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