]> git.saurik.com Git - bison.git/blob - src/scan-gram.l
tests: don't abuse AT_BISON_CHECK.
[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 /* Quote only if escaping won't make the character visible. */
564 if (isspace ((unsigned char) *p) && isprint ((unsigned char) *p))
565 p = quote (p);
566 else
567 p = quotearg_style_mem (escape_quoting_style, p, 1);
568 complain_at (*loc, _("invalid character after \\-escape: %s"), p);
569 }
570 }
571
572 /*--------------------------------------------.
573 | Scanning user-code characters and strings. |
574 `--------------------------------------------*/
575
576 <SC_CHARACTER,SC_STRING>
577 {
578 {splice}|\\{splice}[^\n\[\]] STRING_GROW;
579 }
580
581 <SC_CHARACTER>
582 {
583 "'" STRING_GROW; BEGIN context_state;
584 \n unexpected_newline (token_start, "'"); BEGIN context_state;
585 <<EOF>> unexpected_eof (token_start, "'"); BEGIN context_state;
586 }
587
588 <SC_STRING>
589 {
590 "\"" STRING_GROW; BEGIN context_state;
591 \n unexpected_newline (token_start, "\""); BEGIN context_state;
592 <<EOF>> unexpected_eof (token_start, "\""); BEGIN context_state;
593 }
594
595
596 /*---------------------------------------------------.
597 | Strings, comments etc. can be found in user code. |
598 `---------------------------------------------------*/
599
600 <SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>
601 {
602 "'" {
603 STRING_GROW;
604 context_state = YY_START;
605 token_start = loc->start;
606 BEGIN SC_CHARACTER;
607 }
608 "\"" {
609 STRING_GROW;
610 context_state = YY_START;
611 token_start = loc->start;
612 BEGIN SC_STRING;
613 }
614 "/"{splice}"*" {
615 STRING_GROW;
616 context_state = YY_START;
617 token_start = loc->start;
618 BEGIN SC_COMMENT;
619 }
620 "/"{splice}"/" {
621 STRING_GROW;
622 context_state = YY_START;
623 BEGIN SC_LINE_COMMENT;
624 }
625 }
626
627
628
629 /*-----------------------------------------------------------.
630 | Scanning some code in braces (actions). The initial "{" is |
631 | already eaten. |
632 `-----------------------------------------------------------*/
633
634 <SC_BRACED_CODE>
635 {
636 "{"|"<"{splice}"%" STRING_GROW; braces_level++;
637 "%"{splice}">" STRING_GROW; braces_level--;
638 "}" {
639 obstack_1grow (&obstack_for_string, '}');
640
641 --braces_level;
642 if (braces_level < 0)
643 {
644 STRING_FINISH;
645 loc->start = code_start;
646 val->code = last_string;
647 BEGIN INITIAL;
648 return BRACED_CODE;
649 }
650 }
651
652 /* Tokenize `<<%' correctly (as `<<' `%') rather than incorrrectly
653 (as `<' `<%'). */
654 "<"{splice}"<" STRING_GROW;
655
656 <<EOF>> {
657 unexpected_eof (code_start, "}");
658 STRING_FINISH;
659 loc->start = code_start;
660 val->code = last_string;
661 BEGIN INITIAL;
662 return BRACED_CODE;
663 }
664 }
665
666
667 /*--------------------------------------------------------------.
668 | Scanning some prologue: from "%{" (already scanned) to "%}". |
669 `--------------------------------------------------------------*/
670
671 <SC_PROLOGUE>
672 {
673 "%}" {
674 STRING_FINISH;
675 loc->start = code_start;
676 val->chars = last_string;
677 BEGIN INITIAL;
678 return PROLOGUE;
679 }
680
681 <<EOF>> {
682 unexpected_eof (code_start, "%}");
683 STRING_FINISH;
684 loc->start = code_start;
685 val->chars = last_string;
686 BEGIN INITIAL;
687 return PROLOGUE;
688 }
689 }
690
691
692 /*---------------------------------------------------------------.
693 | Scanning the epilogue (everything after the second "%%", which |
694 | has already been eaten). |
695 `---------------------------------------------------------------*/
696
697 <SC_EPILOGUE>
698 {
699 <<EOF>> {
700 STRING_FINISH;
701 loc->start = code_start;
702 val->chars = last_string;
703 BEGIN INITIAL;
704 return EPILOGUE;
705 }
706 }
707
708
709 /*-----------------------------------------------------.
710 | By default, grow the string obstack with the input. |
711 `-----------------------------------------------------*/
712
713 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE,SC_STRING,SC_CHARACTER,SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>. |
714 <SC_COMMENT,SC_LINE_COMMENT,SC_BRACED_CODE,SC_PROLOGUE,SC_EPILOGUE>\n STRING_GROW;
715
716 %%
717
718 /* Read bytes from FP into buffer BUF of size SIZE. Return the
719 number of bytes read. Remove '\r' from input, treating \r\n
720 and isolated \r as \n. */
721
722 static size_t
723 no_cr_read (FILE *fp, char *buf, size_t size)
724 {
725 size_t bytes_read = fread (buf, 1, size, fp);
726 if (bytes_read)
727 {
728 char *w = memchr (buf, '\r', bytes_read);
729 if (w)
730 {
731 char const *r = ++w;
732 char const *lim = buf + bytes_read;
733
734 for (;;)
735 {
736 /* Found an '\r'. Treat it like '\n', but ignore any
737 '\n' that immediately follows. */
738 w[-1] = '\n';
739 if (r == lim)
740 {
741 int ch = getc (fp);
742 if (ch != '\n' && ungetc (ch, fp) != ch)
743 break;
744 }
745 else if (*r == '\n')
746 r++;
747
748 /* Copy until the next '\r'. */
749 do
750 {
751 if (r == lim)
752 return w - buf;
753 }
754 while ((*w++ = *r++) != '\r');
755 }
756
757 return w - buf;
758 }
759 }
760
761 return bytes_read;
762 }
763
764
765
766 /*------------------------------------------------------.
767 | Scan NUMBER for a base-BASE integer at location LOC. |
768 `------------------------------------------------------*/
769
770 static unsigned long int
771 scan_integer (char const *number, int base, location loc)
772 {
773 verify (INT_MAX < ULONG_MAX);
774 unsigned long int num = strtoul (number, NULL, base);
775
776 if (INT_MAX < num)
777 {
778 complain_at (loc, _("integer out of range: %s"), quote (number));
779 num = INT_MAX;
780 }
781
782 return num;
783 }
784
785
786 /*------------------------------------------------------------------.
787 | Convert universal character name UCN to a single-byte character, |
788 | and return that character. Return -1 if UCN does not correspond |
789 | to a single-byte character. |
790 `------------------------------------------------------------------*/
791
792 static int
793 convert_ucn_to_byte (char const *ucn)
794 {
795 verify (UCHAR_MAX <= INT_MAX);
796 unsigned long int code = strtoul (ucn + 2, NULL, 16);
797
798 /* FIXME: Currently we assume Unicode-compatible unibyte characters
799 on ASCII hosts (i.e., Latin-1 on hosts with 8-bit bytes). On
800 non-ASCII hosts we support only the portable C character set.
801 These limitations should be removed once we add support for
802 multibyte characters. */
803
804 if (UCHAR_MAX < code)
805 return -1;
806
807 #if ! ('$' == 0x24 && '@' == 0x40 && '`' == 0x60 && '~' == 0x7e)
808 {
809 /* A non-ASCII host. Use CODE to index into a table of the C
810 basic execution character set, which is guaranteed to exist on
811 all Standard C platforms. This table also includes '$', '@',
812 and '`', which are not in the basic execution character set but
813 which are unibyte characters on all the platforms that we know
814 about. */
815 static signed char const table[] =
816 {
817 '\0', -1, -1, -1, -1, -1, -1, '\a',
818 '\b', '\t', '\n', '\v', '\f', '\r', -1, -1,
819 -1, -1, -1, -1, -1, -1, -1, -1,
820 -1, -1, -1, -1, -1, -1, -1, -1,
821 ' ', '!', '"', '#', '$', '%', '&', '\'',
822 '(', ')', '*', '+', ',', '-', '.', '/',
823 '0', '1', '2', '3', '4', '5', '6', '7',
824 '8', '9', ':', ';', '<', '=', '>', '?',
825 '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
826 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
827 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
828 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
829 '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
830 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
831 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
832 'x', 'y', 'z', '{', '|', '}', '~'
833 };
834
835 code = code < sizeof table ? table[code] : -1;
836 }
837 #endif
838
839 return code;
840 }
841
842
843 /*----------------------------------------------------------------.
844 | Handle `#line INT "FILE"'. ARGS has already skipped `#line '. |
845 `----------------------------------------------------------------*/
846
847 static void
848 handle_syncline (char *args, location loc)
849 {
850 char *after_num;
851 unsigned long int lineno = strtoul (args, &after_num, 10);
852 char *file = strchr (after_num, '"') + 1;
853 *strchr (file, '"') = '\0';
854 if (INT_MAX <= lineno)
855 {
856 warn_at (loc, _("line number overflow"));
857 lineno = INT_MAX;
858 }
859 current_file = uniqstr_new (file);
860 boundary_set (&scanner_cursor, current_file, lineno, 1);
861 }
862
863
864 /*----------------------------------------------------------------.
865 | For a token or comment starting at START, report message MSGID, |
866 | which should say that an end marker was found before |
867 | the expected TOKEN_END. |
868 `----------------------------------------------------------------*/
869
870 static void
871 unexpected_end (boundary start, char const *msgid, char const *token_end)
872 {
873 location loc;
874 loc.start = start;
875 loc.end = scanner_cursor;
876 complain_at (loc, _(msgid), token_end);
877 }
878
879
880 /*------------------------------------------------------------------------.
881 | Report an unexpected EOF in a token or comment starting at START. |
882 | An end of file was encountered and the expected TOKEN_END was missing. |
883 `------------------------------------------------------------------------*/
884
885 static void
886 unexpected_eof (boundary start, char const *token_end)
887 {
888 unexpected_end (start, N_("missing `%s' at end of file"), token_end);
889 }
890
891
892 /*----------------------------------------.
893 | Likewise, but for unexpected newlines. |
894 `----------------------------------------*/
895
896 static void
897 unexpected_newline (boundary start, char const *token_end)
898 {
899 unexpected_end (start, N_("missing `%s' at end of line"), token_end);
900 }
901
902
903 /*-------------------------.
904 | Initialize the scanner. |
905 `-------------------------*/
906
907 void
908 gram_scanner_initialize (void)
909 {
910 obstack_init (&obstack_for_string);
911 }
912
913
914 /*-----------------------------------------------.
915 | Free all the memory allocated to the scanner. |
916 `-----------------------------------------------*/
917
918 void
919 gram_scanner_free (void)
920 {
921 obstack_free (&obstack_for_string, 0);
922 /* Reclaim Flex's buffers. */
923 yylex_destroy ();
924 }