]>
git.saurik.com Git - bison.git/blob - src/lex.c
62afbee53b15a3f0cfef548c3652872a4736d4c1
1 /* Token-reader for Bison's input parser,
2 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
32 /* Buffer for storing the current token. */
33 static struct obstack token_obstack
;
34 const char *token_buffer
= NULL
;
36 symbol_t
*symval
= NULL
;
39 /* A token to be reread, see unlex and lex. */
40 static token_t unlexed
= tok_undef
;
41 static symbol_t
*unlexed_symval
= NULL
;
42 static const char *unlexed_token_buffer
= NULL
;
47 obstack_init (&token_obstack
);
55 obstack_free (&token_obstack
, NULL
);
60 skip_white_space (void)
74 /* FIXME: Should probably be merged with copy_comment. */
76 if (c
!= '*' && c
!= '/')
78 complain (_("unexpected `/' found and ignored"));
81 cplus_comment
= (c
== '/');
88 if (!cplus_comment
&& c
== '*')
107 fatal (_("unterminated comment"));
130 /*-----------------------------------------------------.
131 | Do a getc, but give error message if EOF encountered |
132 `-----------------------------------------------------*/
139 fatal (_("unexpected end of file"));
144 /*---------------------------------------------------------------.
145 | Read one literal character from FINPUT, process \-escapes, and |
146 | return the character. |
147 `---------------------------------------------------------------*/
158 complain (_("unescaped newline in constant"));
189 else if (c
<= '7' && c
>= '0')
192 while (c
<= '7' && c
>= '0')
194 res
= (res
* 8) + (c
- '0');
195 if (res
>= 256 || res
< 0)
197 complain (_("octal value outside range 0...255: `\\%o'"),
212 if (c
>= '0' && c
<= '9')
213 res
*= 16, res
+= c
- '0';
214 else if (c
>= 'a' && c
<= 'f')
215 res
*= 16, res
+= c
- 'a' + 10;
216 else if (c
>= 'A' && c
<= 'F')
217 res
*= 16, res
+= c
- 'A' + 10;
220 if (res
>= 256 || res
< 0)
222 complain (_("hexadecimal value above 255: `\\x%x'"), res
);
232 char badchar
[] = "c";
234 complain (_("unknown escape sequence: `\\' followed by `%s'"),
245 unlex (token_t token
)
248 unlexed_token_buffer
= token_buffer
;
249 unlexed_symval
= symval
;
252 /*-----------------------------------------------------------------.
253 | We just read `<' from FIN. Store in TOKEN_BUFFER, the type name |
254 | specified between the `<...>'. |
255 `-----------------------------------------------------------------*/
258 read_type_name (FILE *fin
)
265 fatal (_("unterminated type name at end of file"));
268 complain (_("unterminated type name"));
273 obstack_1grow (&token_obstack
, c
);
276 obstack_1grow (&token_obstack
, '\0');
277 token_buffer
= obstack_finish (&token_obstack
);
286 /* Just to make sure. */
289 if (unlexed
!= tok_undef
)
291 token_t res
= unlexed
;
292 symval
= unlexed_symval
;
293 token_buffer
= unlexed_token_buffer
;
298 c
= skip_white_space ();
303 token_buffer
= "EOF";
306 case 'A': case 'B': case 'C': case 'D': case 'E':
307 case 'F': case 'G': case 'H': case 'I': case 'J':
308 case 'K': case 'L': case 'M': case 'N': case 'O':
309 case 'P': case 'Q': case 'R': case 'S': case 'T':
310 case 'U': case 'V': case 'W': case 'X': case 'Y':
312 case 'a': case 'b': case 'c': case 'd': case 'e':
313 case 'f': case 'g': case 'h': case 'i': case 'j':
314 case 'k': case 'l': case 'm': case 'n': case 'o':
315 case 'p': case 'q': case 'r': case 's': case 't':
316 case 'u': case 'v': case 'w': case 'x': case 'y':
320 while (isalnum (c
) || c
== '_' || c
== '.')
322 obstack_1grow (&token_obstack
, c
);
325 obstack_1grow (&token_obstack
, '\0');
326 token_buffer
= obstack_finish (&token_obstack
);
328 symval
= getsym (token_buffer
);
329 return tok_identifier
;
331 case '0': case '1': case '2': case '3': case '4':
332 case '5': case '6': case '7': case '8': case '9':
338 obstack_1grow (&token_obstack
, c
);
339 numval
= numval
* 10 + c
- '0';
342 obstack_1grow (&token_obstack
, '\0');
343 token_buffer
= obstack_finish (&token_obstack
);
349 /* parse the literal token and compute character code in code */
352 int code
= literalchar ();
354 obstack_1grow (&token_obstack
, '\'');
355 obstack_1grow (&token_obstack
, code
);
360 complain (_("use \"...\" for multi-character literal tokens"));
361 while (literalchar () != '\'')
364 obstack_1grow (&token_obstack
, '\'');
365 obstack_1grow (&token_obstack
, '\0');
366 token_buffer
= obstack_finish (&token_obstack
);
367 symval
= getsym (token_buffer
);
368 symbol_class_set (symval
, token_sym
);
369 symbol_user_token_number_set (symval
, code
);
370 return tok_identifier
;
374 /* parse the literal string token and treat as an identifier */
379 obstack_1grow (&token_obstack
, '\"');
380 /* Read up to and including ". */
383 code
= literalchar ();
384 obstack_1grow (&token_obstack
, code
);
386 while (code
!= '\"');
387 obstack_1grow (&token_obstack
, '\0');
388 token_buffer
= obstack_finish (&token_obstack
);
390 symval
= getsym (token_buffer
);
391 symbol_class_set (symval
, token_sym
);
392 return tok_identifier
;
405 return tok_semicolon
;
413 return tok_left_curly
;
416 obstack_1grow (&token_obstack
, c
);
420 obstack_1grow (&token_obstack
, c
);
424 while (c
== ' ' || c
== '\n' || c
== '\t');
425 obstack_1grow (&token_obstack
, '\0');
426 token_buffer
= obstack_finish (&token_obstack
);
430 return tok_left_curly
;
439 read_type_name (finput
);
443 return parse_percent_token ();
446 obstack_1grow (&token_obstack
, c
);
447 obstack_1grow (&token_obstack
, '\0');
448 token_buffer
= obstack_finish (&token_obstack
);
453 /* This function is a strcmp, which doesn't differentiate `-' and `_'
457 option_strcmp (const char *left
, const char *right
)
459 const unsigned char *l
, *r
;
464 l
= (const unsigned char *)left
;
465 r
= (const unsigned char *)right
;
466 while (((c
= *l
- *r
++) == 0 && *l
!= '\0')
467 || ((*l
== '-' || *l
== '_') && (*r
== '_' || *r
== '-')))
472 /* Parse a token which starts with %.
473 Assumes the % has already been read and discarded. */
476 parse_percent_token (void)
478 const struct option_table_s
*tx
= NULL
;
479 const char *arg
= NULL
;
480 /* Where the ARG was found in token_buffer. */
481 size_t arg_offset
= 0;
483 int c
= getc (finput
);
484 obstack_1grow (&token_obstack
, '%');
485 obstack_1grow (&token_obstack
, c
);
489 obstack_1grow (&token_obstack
, '\0');
490 token_buffer
= obstack_finish (&token_obstack
);
495 return tok_two_percents
;
498 return tok_percent_left_curly
;
500 /* The following guys are here for backward compatibility with
501 very ancient Yacc versions. The paper of Johnson mentions
502 them (as ancient :). */
523 while (c
= getc (finput
), isalpha (c
) || c
== '_' || c
== '-')
527 obstack_1grow (&token_obstack
, c
);
530 /* %DIRECTIVE="ARG". Separate into
531 TOKEN_BUFFER = `%DIRECTIVE\0ARG\0'.
532 This is a bit hackish, but once we move to a Bison parser,
533 things will be cleaned up. */
536 /* End of the directive. We skip the `='. */
537 obstack_1grow (&token_obstack
, '\0');
538 /* Fetch the ARG if present. */
543 arg_offset
= obstack_object_size (&token_obstack
);
544 /* Read up to and including `"'. Do not append the closing
545 `"' in the output: it's not part of the ARG. */
546 while ((code
= literalchar ()) != '"')
547 obstack_1grow (&token_obstack
, code
);
549 /* else: should be an error. */
554 obstack_1grow (&token_obstack
, '\0');
555 token_buffer
= obstack_finish (&token_obstack
);
557 arg
= token_buffer
+ arg_offset
;
559 /* table lookup % directive */
560 for (tx
= option_table
; tx
->name
; tx
++)
561 if ((tx
->access
== opt_percent
|| tx
->access
== opt_both
)
562 && option_strcmp (token_buffer
+ 1, tx
->name
) == 0)
565 if (arg
&& tx
->ret_val
!= tok_stropt
)
566 fatal (_("`%s' supports no argument: %s"), token_buffer
, quote (arg
));
575 char **flag
= (char **) tx
->flag
;
576 /* Keep only the first assignment: command line options have
577 already been processed, and we want them to have
578 precedence. Side effect: if this %-option is used
579 several times, only the first is honored. Bah. */
581 *flag
= xstrdup (arg
);
584 fatal (_("`%s' requires an argument"), token_buffer
);
590 *((int *) (tx
->flag
)) = 1;
595 fatal (_("`%s' is no longer supported"), token_buffer
);