1 /* Token-reader for Bison's input parser,
2 Copyright 1984, 1986, 1989, 1992, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
30 /* Buffer for storing the current token. */
31 struct obstack token_obstack
;
32 const char *token_buffer
= NULL
;
34 bucket
*symval
= NULL
;
37 /* A token to be reread, see unlex and lex. */
38 static token_t unlexed
= tok_undef
;
39 static bucket
*unlexed_symval
= NULL
;
40 static const char *unlexed_token_buffer
= NULL
;
45 obstack_init (&token_obstack
);
53 obstack_free (&token_obstack
, NULL
);
58 skip_white_space (void)
72 /* FIXME: Should probably be merged with copy_comment. */
74 if (c
!= '*' && c
!= '/')
76 complain (_("unexpected `/' found and ignored"));
79 cplus_comment
= (c
== '/');
86 if (!cplus_comment
&& c
== '*')
105 fatal (_("unterminated comment"));
128 /*-----------------------------------------------------.
129 | Do a getc, but give error message if EOF encountered |
130 `-----------------------------------------------------*/
137 fatal (_("unexpected end of file"));
142 /*------------------------------------------------------------------.
143 | Read one literal character from finput. Process \ escapes. |
144 | Append the normalized string version of the char to OUT. Assign |
145 | the character code to *PCODE. Return 1 unless the character is an |
146 | unescaped `term' or \n report error for \n. |
147 `------------------------------------------------------------------*/
149 /* FIXME: We could directly work in the obstack, but that would make
150 it more difficult to move to quotearg some day. So for the time
151 being, I prefer have literalchar behave like quotearg, and change
152 my mind later if I was wrong. */
155 literalchar (struct obstack
*out
, int *pcode
, char term
)
166 complain (_("unescaped newline in constant"));
200 else if (c
<= '7' && c
>= '0')
203 while (c
<= '7' && c
>= '0')
205 code
= (code
* 8) + (c
- '0');
206 if (code
>= 256 || code
< 0)
208 complain (_("octal value outside range 0...255: `\\%o'"),
223 if (c
>= '0' && c
<= '9')
224 code
*= 16, code
+= c
- '0';
225 else if (c
>= 'a' && c
<= 'f')
226 code
*= 16, code
+= c
- 'a' + 10;
227 else if (c
>= 'A' && c
<= 'F')
228 code
*= 16, code
+= c
- 'A' + 10;
231 if (code
>= 256 || code
< 0)
233 complain (_("hexadecimal value above 255: `\\x%x'"), code
);
243 char badchar
[] = "c";
245 complain (_("unknown escape sequence: `\\' followed by `%s'"),
251 /* now fill BUF with the canonical name for this character as a
252 literal token. Do not use what the user typed, so that `\012'
253 and `\n' can be interchangeable. */
256 if (code
== term
&& wasquote
)
258 else if (code
== '\\')
263 else if (code
== '\'')
268 else if (code
== '\"')
273 else if (code
>= 040 && code
< 0177)
275 else if (code
== '\t')
280 else if (code
== '\n')
285 else if (code
== '\r')
290 else if (code
== '\v')
295 else if (code
== '\b')
300 else if (code
== '\f')
308 *cp
++ = code
/ 0100 + '0';
309 *cp
++ = ((code
/ 010) & 07) + '0';
310 *cp
++ = (code
& 07) + '0';
315 obstack_sgrow (out
, buf
);
322 unlex (token_t token
)
325 unlexed_token_buffer
= token_buffer
;
326 unlexed_symval
= symval
;
329 /*-----------------------------------------------------------------.
330 | We just read `<' from FIN. Store in TOKEN_BUFFER, the type name |
331 | specified between the `<...>'. |
332 `-----------------------------------------------------------------*/
335 read_type_name (FILE *fin
)
342 fatal (_("unterminated type name at end of file"));
345 complain (_("unterminated type name"));
350 obstack_1grow (&token_obstack
, c
);
353 obstack_1grow (&token_obstack
, '\0');
354 token_buffer
= obstack_finish (&token_obstack
);
363 /* Just to make sure. */
366 if (unlexed
!= tok_undef
)
368 token_t res
= unlexed
;
369 symval
= unlexed_symval
;
370 token_buffer
= unlexed_token_buffer
;
375 c
= skip_white_space ();
380 token_buffer
= "EOF";
383 case 'A': case 'B': case 'C': case 'D': case 'E':
384 case 'F': case 'G': case 'H': case 'I': case 'J':
385 case 'K': case 'L': case 'M': case 'N': case 'O':
386 case 'P': case 'Q': case 'R': case 'S': case 'T':
387 case 'U': case 'V': case 'W': case 'X': case 'Y':
389 case 'a': case 'b': case 'c': case 'd': case 'e':
390 case 'f': case 'g': case 'h': case 'i': case 'j':
391 case 'k': case 'l': case 'm': case 'n': case 'o':
392 case 'p': case 'q': case 'r': case 's': case 't':
393 case 'u': case 'v': case 'w': case 'x': case 'y':
397 while (isalnum (c
) || c
== '_' || c
== '.')
399 obstack_1grow (&token_obstack
, c
);
402 obstack_1grow (&token_obstack
, '\0');
403 token_buffer
= obstack_finish (&token_obstack
);
405 symval
= getsym (token_buffer
);
406 return tok_identifier
;
408 case '0': case '1': case '2': case '3': case '4':
409 case '5': case '6': case '7': case '8': case '9':
415 obstack_1grow (&token_obstack
, c
);
416 numval
= numval
* 10 + c
- '0';
419 obstack_1grow (&token_obstack
, '\0');
420 token_buffer
= obstack_finish (&token_obstack
);
426 /* parse the literal token and compute character code in code */
431 obstack_1grow (&token_obstack
, '\'');
432 literalchar (&token_obstack
, &code
, '\'');
438 complain (_("use \"...\" for multi-character literal tokens"));
440 if (!literalchar (0, &discode
, '\''))
443 obstack_1grow (&token_obstack
, '\'');
444 obstack_1grow (&token_obstack
, '\0');
445 token_buffer
= obstack_finish (&token_obstack
);
446 symval
= getsym (token_buffer
);
447 symval
->class = token_sym
;
448 if (symval
->user_token_number
== SUNDEF
)
449 symval
->user_token_number
= code
;
450 return tok_identifier
;
454 /* parse the literal string token and treat as an identifier */
457 int code
; /* ignored here */
459 obstack_1grow (&token_obstack
, '\"');
460 /* Read up to and including ". */
461 while (literalchar (&token_obstack
, &code
, '\"'))
463 obstack_1grow (&token_obstack
, '\0');
464 token_buffer
= obstack_finish (&token_obstack
);
466 symval
= getsym (token_buffer
);
467 symval
->class = token_sym
;
469 return tok_identifier
;
482 return tok_semicolon
;
490 return tok_left_curly
;
493 obstack_1grow (&token_obstack
, c
);
497 obstack_1grow (&token_obstack
, c
);
501 while (c
== ' ' || c
== '\n' || c
== '\t');
502 obstack_1grow (&token_obstack
, '\0');
503 token_buffer
= obstack_finish (&token_obstack
);
507 return tok_left_curly
;
516 read_type_name (finput
);
520 return parse_percent_token ();
523 obstack_1grow (&token_obstack
, c
);
524 obstack_1grow (&token_obstack
, '\0');
525 token_buffer
= obstack_finish (&token_obstack
);
530 /* the following table dictates the action taken for the various %
531 directives. A set_flag value causes the named flag to be set. A
532 retval action returns the code. */
533 struct percent_table_struct
540 struct percent_table_struct percent_table
[] =
542 { "token", NULL
, tok_token
},
543 { "term", NULL
, tok_token
},
544 { "nterm", NULL
, tok_nterm
},
545 { "type", NULL
, tok_type
},
546 { "guard", NULL
, tok_guard
},
547 { "union", NULL
, tok_union
},
548 { "expect", NULL
, tok_expect
},
549 { "thong", NULL
, tok_thong
},
550 { "start", NULL
, tok_start
},
551 { "left", NULL
, tok_left
},
552 { "right", NULL
, tok_right
},
553 { "nonassoc", NULL
, tok_nonassoc
},
554 { "binary", NULL
, tok_nonassoc
},
555 { "prec", NULL
, tok_prec
},
556 { "locations", &locations_flag
, tok_intopt
}, /* -l */
557 { "no-lines", &no_lines_flag
, tok_intopt
}, /* -l */
558 { "raw", NULL
, tok_obsolete
}, /* -r */
559 { "token-table", &token_table_flag
, tok_intopt
}, /* -k */
560 { "yacc", &yacc_flag
, tok_intopt
}, /* -y */
561 { "fixed-output-files",&yacc_flag
, tok_intopt
}, /* -y */
562 { "defines", &defines_flag
, tok_intopt
}, /* -d */
563 { "no-parser", &no_parser_flag
, tok_intopt
}, /* -n */
564 { "graph", &graph_flag
, tok_intopt
}, /* -g */
566 /* FIXME: semantic parsers which will output an `include' of an
567 output file: be sure that the name included is indeed the name of
569 { "output", &spec_outfile
, tok_stropt
}, /* -o */
570 { "file-prefix", &spec_file_prefix
, tok_stropt
}, /* -b */
571 { "name-prefix", &spec_name_prefix
, tok_stropt
}, /* -p */
573 { "verbose", &verbose_flag
, tok_intopt
}, /* -v */
574 { "debug", &debug_flag
, tok_intopt
}, /* -t */
575 { "semantic-parser", &semantic_parser
, tok_intopt
},
576 { "pure-parser", &pure_parser
, tok_intopt
},
578 { NULL
, NULL
, tok_illegal
}
581 /* Parse a token which starts with %.
582 Assumes the % has already been read and discarded. */
585 parse_percent_token (void)
587 struct percent_table_struct
*tx
= NULL
;
588 const char *arg
= NULL
;
589 /* Where the ARG was found in token_buffer. */
590 size_t arg_offset
= 0;
592 int c
= getc (finput
);
597 return tok_two_percents
;
600 return tok_percent_left_curly
;
602 /* FIXME: Who the heck are those 5 guys!?! `%<' = `%left'!!!
603 Let's ask for there removal. */
623 obstack_1grow (&token_obstack
, '%');
624 while (isalpha (c
) || c
== '_' || c
== '-')
628 obstack_1grow (&token_obstack
, c
);
632 /* %DIRECTIVE="ARG". Separate into
633 TOKEN_BUFFER = `%DIRECTIVE\0ARG\0'.
634 This is a bit hackish, but once we move to a Bison parser,
635 things will be cleaned up. */
638 /* End of the directive. We skip the `='. */
639 obstack_1grow (&token_obstack
, '\0');
640 /* Fetch the ARG if present. */
645 arg_offset
= obstack_object_size (&token_obstack
);
646 /* Read up to and including `"'. Do not append the closing
647 `"' in the output: it's not part of the ARG. */
648 while (literalchar (NULL
, &code
, '"'))
649 obstack_1grow (&token_obstack
, code
);
651 /* else: should be an error. */
656 obstack_1grow (&token_obstack
, '\0');
657 token_buffer
= obstack_finish (&token_obstack
);
659 arg
= token_buffer
+ arg_offset
;
661 /* table lookup % directive */
662 for (tx
= percent_table
; tx
->name
; tx
++)
663 if (strcmp (token_buffer
+ 1, tx
->name
) == 0)
666 if (arg
&& tx
->retval
!= tok_stropt
)
667 fatal (_("`%s' supports no argument: %s"), token_buffer
, quote (arg
));
672 assert (tx
->set_flag
);
675 /* Keep only the first assignment: command line options have
676 already been processed, and we want them to have
677 precedence. Side effect: if this %-option is used
678 several times, only the first is honored. Bah. */
679 if (!*((char **) (tx
->set_flag
)))
680 *((char **) (tx
->set_flag
)) = xstrdup (arg
);
683 fatal (_("`%s' requires an argument"), token_buffer
);
688 assert (tx
->set_flag
);
689 *((int *) (tx
->set_flag
)) = 1;
694 fatal (_("`%s' is no longer supported"), token_buffer
);