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