]> git.saurik.com Git - bison.git/blame - src/lex.c
* tests/regression.at (AT_TEST_CPP_GUARD_H): New.
[bison.git] / src / lex.c
CommitLineData
40675e7c 1/* Token-reader for Bison's input parser,
62ab6972 2 Copyright 1984, 1986, 1989, 1992, 2000, 2001 Free Software Foundation, Inc.
40675e7c 3
a0f6b076 4 This file is part of Bison, the GNU Compiler Compiler.
40675e7c 5
a0f6b076
AD
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
40675e7c 10
a0f6b076
AD
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
40675e7c 15
a0f6b076
AD
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
40675e7c 20
40675e7c 21#include "system.h"
ceed8467 22#include "getargs.h"
40675e7c 23#include "files.h"
a44c2277 24#include "getopt.h" /* for optarg */
40675e7c 25#include "symtab.h"
82b6d266 26#include "options.h"
40675e7c 27#include "lex.h"
a0f6b076 28#include "complain.h"
b2ca4022 29#include "gram.h"
ff4a34be 30#include "quote.h"
40675e7c
DM
31
32/* Buffer for storing the current token. */
0846f581 33static struct obstack token_obstack;
b0ce6046 34const char *token_buffer = NULL;
40675e7c
DM
35
36bucket *symval;
37int numval;
38
561f9a30 39/* A token to be reread, see unlex and lex. */
342b8b6e 40static token_t unlexed = tok_undef;
342b8b6e 41static bucket *unlexed_symval = NULL;
561f9a30 42static const char *unlexed_token_buffer = NULL;
40675e7c
DM
43
44void
342b8b6e 45lex_init (void)
40675e7c 46{
f17bcd1f 47 obstack_init (&token_obstack);
342b8b6e
AD
48 unlexed = tok_undef;
49}
50
51
52void
53lex_free (void)
54{
55 obstack_free (&token_obstack, NULL);
40675e7c
DM
56}
57
58
40675e7c 59int
d2729d44 60skip_white_space (void)
40675e7c 61{
abadc117
AD
62 int c;
63 int inside;
40675e7c 64
abadc117 65 c = getc (finput);
40675e7c
DM
66
67 for (;;)
68 {
69 int cplus_comment;
70
71 switch (c)
72 {
73 case '/':
79282c5a 74 /* FIXME: Should probably be merged with copy_comment. */
abadc117 75 c = getc (finput);
a083fbbf 76 if (c != '*' && c != '/')
a44c2277 77 {
a0f6b076 78 complain (_("unexpected `/' found and ignored"));
a44c2277
RS
79 break;
80 }
40675e7c
DM
81 cplus_comment = (c == '/');
82
abadc117 83 c = getc (finput);
40675e7c
DM
84
85 inside = 1;
86 while (inside)
87 {
88 if (!cplus_comment && c == '*')
89 {
90 while (c == '*')
abadc117 91 c = getc (finput);
40675e7c
DM
92
93 if (c == '/')
94 {
95 inside = 0;
abadc117 96 c = getc (finput);
40675e7c
DM
97 }
98 }
99 else if (c == '\n')
100 {
101 lineno++;
102 if (cplus_comment)
103 inside = 0;
abadc117 104 c = getc (finput);
40675e7c
DM
105 }
106 else if (c == EOF)
a0f6b076 107 fatal (_("unterminated comment"));
40675e7c 108 else
abadc117 109 c = getc (finput);
40675e7c
DM
110 }
111
112 break;
113
114 case '\n':
115 lineno++;
116
117 case ' ':
118 case '\t':
119 case '\f':
abadc117 120 c = getc (finput);
40675e7c
DM
121 break;
122
123 default:
36281465 124 return c;
40675e7c
DM
125 }
126 }
127}
128
79282c5a
AD
129
130/*-----------------------------------------------------.
131| Do a getc, but give error message if EOF encountered |
132`-----------------------------------------------------*/
133
4a120d45 134static int
abadc117 135xgetc (FILE *f)
a44c2277 136{
abadc117 137 int c = getc (f);
a44c2277 138 if (c == EOF)
a0f6b076 139 fatal (_("unexpected end of file"));
a44c2277
RS
140 return c;
141}
142
abadc117
AD
143
144/*------------------------------------------------------------------.
145| Read one literal character from finput. Process \ escapes. |
f17bcd1f 146| Append the normalized string version of the char to OUT. Assign |
abadc117 147| the character code to *PCODE. Return 1 unless the character is an |
f17bcd1f 148| unescaped `term' or \n report error for \n. |
abadc117
AD
149`------------------------------------------------------------------*/
150
f17bcd1f
AD
151/* FIXME: We could directly work in the obstack, but that would make
152 it more difficult to move to quotearg some day. So for the time
153 being, I prefer have literalchar behave like quotearg, and change
154 my mind later if I was wrong. */
155
4a120d45 156static int
f17bcd1f 157literalchar (struct obstack *out, int *pcode, char term)
a44c2277 158{
abadc117 159 int c;
f17bcd1f
AD
160 char buf[4096];
161 char *cp;
abadc117 162 int code;
a44c2277
RS
163 int wasquote = 0;
164
abadc117 165 c = xgetc (finput);
a083fbbf 166 if (c == '\n')
a44c2277 167 {
a0f6b076 168 complain (_("unescaped newline in constant"));
abadc117 169 ungetc (c, finput);
a44c2277
RS
170 code = '?';
171 wasquote = 1;
172 }
173 else if (c != '\\')
174 {
175 code = c;
a083fbbf 176 if (c == term)
a44c2277
RS
177 wasquote = 1;
178 }
179 else
180 {
abadc117
AD
181 c = xgetc (finput);
182 if (c == 't')
183 code = '\t';
184 else if (c == 'n')
185 code = '\n';
186 else if (c == 'a')
187 code = '\007';
188 else if (c == 'r')
189 code = '\r';
190 else if (c == 'f')
191 code = '\f';
192 else if (c == 'b')
193 code = '\b';
194 else if (c == 'v')
195 code = '\013';
196 else if (c == '\\')
197 code = '\\';
198 else if (c == '\'')
199 code = '\'';
200 else if (c == '\"')
201 code = '\"';
a44c2277
RS
202 else if (c <= '7' && c >= '0')
203 {
204 code = 0;
205 while (c <= '7' && c >= '0')
206 {
207 code = (code * 8) + (c - '0');
208 if (code >= 256 || code < 0)
209 {
a0f6b076
AD
210 complain (_("octal value outside range 0...255: `\\%o'"),
211 code);
a44c2277
RS
212 code &= 0xFF;
213 break;
214 }
abadc117 215 c = xgetc (finput);
a44c2277 216 }
abadc117 217 ungetc (c, finput);
a44c2277
RS
218 }
219 else if (c == 'x')
220 {
abadc117 221 c = xgetc (finput);
a44c2277
RS
222 code = 0;
223 while (1)
224 {
225 if (c >= '0' && c <= '9')
abadc117 226 code *= 16, code += c - '0';
a44c2277 227 else if (c >= 'a' && c <= 'f')
abadc117 228 code *= 16, code += c - 'a' + 10;
a44c2277 229 else if (c >= 'A' && c <= 'F')
abadc117 230 code *= 16, code += c - 'A' + 10;
a083fbbf 231 else
a44c2277 232 break;
abadc117 233 if (code >= 256 || code < 0)
a44c2277 234 {
abadc117 235 complain (_("hexadecimal value above 255: `\\x%x'"), code);
a44c2277
RS
236 code &= 0xFF;
237 break;
238 }
abadc117 239 c = xgetc (finput);
a44c2277 240 }
abadc117 241 ungetc (c, finput);
a44c2277
RS
242 }
243 else
244 {
b0ce6046
AD
245 char badchar [] = "c";
246 badchar[0] = c;
a0f6b076 247 complain (_("unknown escape sequence: `\\' followed by `%s'"),
b0ce6046 248 quote (badchar));
a44c2277
RS
249 code = '?';
250 }
abadc117 251 } /* has \ */
a44c2277 252
f17bcd1f
AD
253 /* now fill BUF with the canonical name for this character as a
254 literal token. Do not use what the user typed, so that `\012'
255 and `\n' can be interchangeable. */
a44c2277 256
f17bcd1f 257 cp = buf;
e5335b74 258 if (code == term && wasquote)
f17bcd1f 259 *cp++ = code;
abadc117
AD
260 else if (code == '\\')
261 {
f17bcd1f
AD
262 *cp++ = '\\';
263 *cp++ = '\\';
abadc117
AD
264 }
265 else if (code == '\'')
266 {
f17bcd1f
AD
267 *cp++ = '\\';
268 *cp++ = '\'';
abadc117
AD
269 }
270 else if (code == '\"')
271 {
f17bcd1f
AD
272 *cp++ = '\\';
273 *cp++ = '\"';
abadc117 274 }
5ce94c29 275 else if (code >= 040 && code < 0177)
f17bcd1f 276 *cp++ = code;
abadc117
AD
277 else if (code == '\t')
278 {
f17bcd1f
AD
279 *cp++ = '\\';
280 *cp++ = 't';
abadc117
AD
281 }
282 else if (code == '\n')
283 {
f17bcd1f
AD
284 *cp++ = '\\';
285 *cp++ = 'n';
abadc117
AD
286 }
287 else if (code == '\r')
288 {
f17bcd1f
AD
289 *cp++ = '\\';
290 *cp++ = 'r';
abadc117
AD
291 }
292 else if (code == '\v')
293 {
f17bcd1f
AD
294 *cp++ = '\\';
295 *cp++ = 'v';
abadc117
AD
296 }
297 else if (code == '\b')
298 {
f17bcd1f
AD
299 *cp++ = '\\';
300 *cp++ = 'b';
abadc117
AD
301 }
302 else if (code == '\f')
303 {
f17bcd1f
AD
304 *cp++ = '\\';
305 *cp++ = 'f';
abadc117 306 }
a44c2277
RS
307 else
308 {
f17bcd1f
AD
309 *cp++ = '\\';
310 *cp++ = code / 0100 + '0';
311 *cp++ = ((code / 010) & 07) + '0';
312 *cp++ = (code & 07) + '0';
a44c2277 313 }
f17bcd1f
AD
314 *cp = '\0';
315
316 if (out)
317 obstack_sgrow (out, buf);
a44c2277 318 *pcode = code;
abadc117 319 return !wasquote;
a44c2277
RS
320}
321
40675e7c
DM
322
323void
342b8b6e 324unlex (token_t token)
40675e7c
DM
325{
326 unlexed = token;
561f9a30 327 unlexed_token_buffer = token_buffer;
40675e7c
DM
328 unlexed_symval = symval;
329}
330
f282676b
AD
331/*-----------------------------------------------------------------.
332| We just read `<' from FIN. Store in TOKEN_BUFFER, the type name |
333| specified between the `<...>'. |
334`-----------------------------------------------------------------*/
335
336void
337read_type_name (FILE *fin)
338{
f282676b
AD
339 int c = getc (fin);
340
341 while (c != '>')
342 {
343 if (c == EOF)
344 fatal (_("unterminated type name at end of file"));
345 if (c == '\n')
346 {
347 complain (_("unterminated type name"));
348 ungetc (c, fin);
349 break;
350 }
351
f17bcd1f 352 obstack_1grow (&token_obstack, c);
f282676b
AD
353 c = getc (fin);
354 }
f17bcd1f
AD
355 obstack_1grow (&token_obstack, '\0');
356 token_buffer = obstack_finish (&token_obstack);
f282676b
AD
357}
358
40675e7c 359
511e79b3 360token_t
d2729d44 361lex (void)
40675e7c 362{
abadc117 363 int c;
f17bcd1f
AD
364
365 /* Just to make sure. */
366 token_buffer = NULL;
40675e7c 367
342b8b6e 368 if (unlexed != tok_undef)
40675e7c 369 {
342b8b6e 370 token_t res = unlexed;
40675e7c 371 symval = unlexed_symval;
561f9a30 372 token_buffer = unlexed_token_buffer;
342b8b6e
AD
373 unlexed = tok_undef;
374 return res;
40675e7c
DM
375 }
376
abadc117 377 c = skip_white_space ();
40675e7c
DM
378
379 switch (c)
380 {
381 case EOF:
f17bcd1f 382 token_buffer = "EOF";
511e79b3 383 return tok_eof;
40675e7c 384
abadc117
AD
385 case 'A': case 'B': case 'C': case 'D': case 'E':
386 case 'F': case 'G': case 'H': case 'I': case 'J':
387 case 'K': case 'L': case 'M': case 'N': case 'O':
388 case 'P': case 'Q': case 'R': case 'S': case 'T':
389 case 'U': case 'V': case 'W': case 'X': case 'Y':
40675e7c 390 case 'Z':
abadc117
AD
391 case 'a': case 'b': case 'c': case 'd': case 'e':
392 case 'f': case 'g': case 'h': case 'i': case 'j':
393 case 'k': case 'l': case 'm': case 'n': case 'o':
394 case 'p': case 'q': case 'r': case 's': case 't':
395 case 'u': case 'v': case 'w': case 'x': case 'y':
40675e7c 396 case 'z':
abadc117
AD
397 case '.': case '_':
398
abadc117 399 while (isalnum (c) || c == '_' || c == '.')
40675e7c 400 {
f17bcd1f 401 obstack_1grow (&token_obstack, c);
abadc117 402 c = getc (finput);
40675e7c 403 }
f17bcd1f
AD
404 obstack_1grow (&token_obstack, '\0');
405 token_buffer = obstack_finish (&token_obstack);
abadc117
AD
406 ungetc (c, finput);
407 symval = getsym (token_buffer);
511e79b3 408 return tok_identifier;
40675e7c 409
abadc117
AD
410 case '0': case '1': case '2': case '3': case '4':
411 case '5': case '6': case '7': case '8': case '9':
40675e7c
DM
412 {
413 numval = 0;
414
abadc117 415 while (isdigit (c))
40675e7c 416 {
f17bcd1f 417 obstack_1grow (&token_obstack, c);
abadc117
AD
418 numval = numval * 10 + c - '0';
419 c = getc (finput);
40675e7c 420 }
f17bcd1f
AD
421 obstack_1grow (&token_obstack, '\0');
422 token_buffer = obstack_finish (&token_obstack);
abadc117 423 ungetc (c, finput);
511e79b3 424 return tok_number;
40675e7c
DM
425 }
426
427 case '\'':
40675e7c
DM
428 /* parse the literal token and compute character code in code */
429
40675e7c 430 {
a44c2277 431 int code, discode;
5ce94c29 432
f17bcd1f
AD
433 obstack_1grow (&token_obstack, '\'');
434 literalchar (&token_obstack, &code, '\'');
40675e7c 435
abadc117 436 c = getc (finput);
a44c2277 437 if (c != '\'')
40675e7c 438 {
a0f6b076 439 complain (_("use \"...\" for multi-character literal tokens"));
5ce94c29 440 while (1)
f17bcd1f
AD
441 if (!literalchar (0, &discode, '\''))
442 break;
40675e7c 443 }
f17bcd1f
AD
444 obstack_1grow (&token_obstack, '\'');
445 obstack_1grow (&token_obstack, '\0');
446 token_buffer = obstack_finish (&token_obstack);
abadc117 447 symval = getsym (token_buffer);
d7020c20 448 symval->class = token_sym;
abadc117 449 if (!symval->user_token_number)
a44c2277 450 symval->user_token_number = code;
511e79b3 451 return tok_identifier;
a44c2277 452 }
40675e7c 453
a44c2277 454 case '\"':
a44c2277
RS
455 /* parse the literal string token and treat as an identifier */
456
a44c2277 457 {
abadc117 458 int code; /* ignored here */
f17bcd1f
AD
459
460 obstack_1grow (&token_obstack, '\"');
79282c5a 461 /* Read up to and including ". */
f17bcd1f
AD
462 while (literalchar (&token_obstack, &code, '\"'))
463 /* nothing */;
464 obstack_1grow (&token_obstack, '\0');
465 token_buffer = obstack_finish (&token_obstack);
a44c2277 466
abadc117 467 symval = getsym (token_buffer);
d7020c20 468 symval->class = token_sym;
a44c2277 469
511e79b3 470 return tok_identifier;
40675e7c
DM
471 }
472
473 case ',':
342b8b6e 474 token_buffer = ",";
511e79b3 475 return tok_comma;
40675e7c
DM
476
477 case ':':
342b8b6e 478 token_buffer = ":";
511e79b3 479 return tok_colon;
40675e7c
DM
480
481 case ';':
342b8b6e 482 token_buffer = ";";
511e79b3 483 return tok_semicolon;
40675e7c
DM
484
485 case '|':
342b8b6e 486 token_buffer = "|";
511e79b3 487 return tok_bar;
40675e7c
DM
488
489 case '{':
342b8b6e 490 token_buffer = "{";
511e79b3 491 return tok_left_curly;
40675e7c
DM
492
493 case '=':
342b8b6e 494 obstack_1grow (&token_obstack, c);
40675e7c
DM
495 do
496 {
abadc117 497 c = getc (finput);
342b8b6e 498 obstack_1grow (&token_obstack, c);
abadc117
AD
499 if (c == '\n')
500 lineno++;
40675e7c 501 }
abadc117 502 while (c == ' ' || c == '\n' || c == '\t');
342b8b6e
AD
503 obstack_1grow (&token_obstack, '\0');
504 token_buffer = obstack_finish (&token_obstack);
40675e7c
DM
505
506 if (c == '{')
a44c2277 507 {
511e79b3 508 return tok_left_curly;
a44c2277 509 }
40675e7c
DM
510 else
511 {
abadc117 512 ungetc (c, finput);
511e79b3 513 return tok_illegal;
40675e7c
DM
514 }
515
516 case '<':
f282676b 517 read_type_name (finput);
511e79b3 518 return tok_typename;
a083fbbf 519
40675e7c 520 case '%':
abadc117 521 return parse_percent_token ();
40675e7c
DM
522
523 default:
342b8b6e
AD
524 obstack_1grow (&token_obstack, c);
525 obstack_1grow (&token_obstack, '\0');
526 token_buffer = obstack_finish (&token_obstack);
511e79b3 527 return tok_illegal;
40675e7c
DM
528 }
529}
530
82b6d266
PB
531/* This function is a strcmp, which doesn't differentiate `-' and `_'
532 chars. */
6deb4447 533
82b6d266
PB
534static int
535option_strcmp (const char *left, const char *right)
abadc117 536{
342b8b6e
AD
537 const unsigned char *l, *r;
538 int c;
539
540 assert (left);
541 assert (right);
542 l = (const unsigned char *)left;
543 r = (const unsigned char *)right;
544 while (((c = *l - *r++) == 0 && *l != '\0')
545 || ((*l == '-' || *l == '_') && (*r == '_' || *r == '-')))
546 l++;
547 return c;
82b6d266 548}
a44c2277
RS
549
550/* Parse a token which starts with %.
551 Assumes the % has already been read and discarded. */
40675e7c 552
342b8b6e 553token_t
d2729d44 554parse_percent_token (void)
40675e7c 555{
82b6d266 556 const struct option_table_struct *tx;
40675e7c 557
342b8b6e 558 int c = getc (finput);
40675e7c
DM
559
560 switch (c)
561 {
562 case '%':
511e79b3 563 return tok_two_percents;
40675e7c
DM
564
565 case '{':
511e79b3 566 return tok_percent_left_curly;
40675e7c
DM
567
568 case '<':
511e79b3 569 return tok_left;
40675e7c
DM
570
571 case '>':
511e79b3 572 return tok_right;
40675e7c
DM
573
574 case '2':
511e79b3 575 return tok_nonassoc;
40675e7c
DM
576
577 case '0':
511e79b3 578 return tok_token;
40675e7c
DM
579
580 case '=':
511e79b3 581 return tok_prec;
40675e7c 582 }
f17bcd1f 583
abadc117 584 if (!isalpha (c))
511e79b3 585 return tok_illegal;
40675e7c 586
f17bcd1f 587 obstack_1grow (&token_obstack, '%');
abadc117 588 while (isalpha (c) || c == '_' || c == '-')
40675e7c 589 {
6bc35ae5
MA
590 if (c == '_')
591 c = '-';
f17bcd1f 592 obstack_1grow (&token_obstack, c);
abadc117 593 c = getc (finput);
40675e7c
DM
594 }
595
abadc117 596 ungetc (c, finput);
f17bcd1f
AD
597 obstack_1grow (&token_obstack, '\0');
598 token_buffer = obstack_finish (&token_obstack);
40675e7c 599
a44c2277 600 /* table lookup % directive */
82b6d266
PB
601 for (tx = option_table; tx->name; tx++)
602 if ((tx->access == opt_percent || tx->access == opt_both)
603 && option_strcmp (token_buffer + 1, tx->name) == 0)
a44c2277 604 break;
6deb4447 605
62ab6972 606 if (tx->set_flag)
a44c2277 607 {
62ab6972 608 *((int *) (tx->set_flag)) = 1;
511e79b3 609 return tok_noop;
a44c2277 610 }
62ab6972 611
82b6d266 612 switch (tx->ret_val)
a44c2277 613 {
62ab6972
AD
614 case tok_setopt:
615 *((char **) (tx->set_flag)) = optarg;
511e79b3 616 return tok_noop;
62ab6972
AD
617 break;
618
619 case tok_obsolete:
620 fatal (_("`%s' is no longer supported"), token_buffer);
621 break;
342b8b6e
AD
622
623 default:
624 /* Other cases do not apply here. */
625 break;
a44c2277 626 }
62ab6972 627
82b6d266 628 return tx->ret_val;
40675e7c 629}