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