]>
git.saurik.com Git - bison.git/blob - src/lex.c
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. */
24 #include "getopt.h" /* for optarg */
32 /* Buffer for storing the current token. */
33 static struct obstack token_obstack
;
34 const char *token_buffer
= NULL
;
39 /* A token to be reread, see unlex and lex. */
40 static token_t unlexed
= tok_undef
;
41 static bucket
*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. |
146 | Append the normalized string version of the char to OUT. Assign |
147 | the character code to *PCODE. Return 1 unless the character is an |
148 | unescaped `term' or \n report error for \n. |
149 `------------------------------------------------------------------*/
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. */
157 literalchar (struct obstack
*out
, int *pcode
, char term
)
168 complain (_("unescaped newline in constant"));
202 else if (c
<= '7' && c
>= '0')
205 while (c
<= '7' && c
>= '0')
207 code
= (code
* 8) + (c
- '0');
208 if (code
>= 256 || code
< 0)
210 complain (_("octal value outside range 0...255: `\\%o'"),
225 if (c
>= '0' && c
<= '9')
226 code
*= 16, code
+= c
- '0';
227 else if (c
>= 'a' && c
<= 'f')
228 code
*= 16, code
+= c
- 'a' + 10;
229 else if (c
>= 'A' && c
<= 'F')
230 code
*= 16, code
+= c
- 'A' + 10;
233 if (code
>= 256 || code
< 0)
235 complain (_("hexadecimal value above 255: `\\x%x'"), code
);
245 char badchar
[] = "c";
247 complain (_("unknown escape sequence: `\\' followed by `%s'"),
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. */
258 if (code
== term
&& wasquote
)
260 else if (code
== '\\')
265 else if (code
== '\'')
270 else if (code
== '\"')
275 else if (code
>= 040 && code
< 0177)
277 else if (code
== '\t')
282 else if (code
== '\n')
287 else if (code
== '\r')
292 else if (code
== '\v')
297 else if (code
== '\b')
302 else if (code
== '\f')
310 *cp
++ = code
/ 0100 + '0';
311 *cp
++ = ((code
/ 010) & 07) + '0';
312 *cp
++ = (code
& 07) + '0';
317 obstack_sgrow (out
, buf
);
324 unlex (token_t token
)
327 unlexed_token_buffer
= token_buffer
;
328 unlexed_symval
= symval
;
331 /*-----------------------------------------------------------------.
332 | We just read `<' from FIN. Store in TOKEN_BUFFER, the type name |
333 | specified between the `<...>'. |
334 `-----------------------------------------------------------------*/
337 read_type_name (FILE *fin
)
344 fatal (_("unterminated type name at end of file"));
347 complain (_("unterminated type name"));
352 obstack_1grow (&token_obstack
, c
);
355 obstack_1grow (&token_obstack
, '\0');
356 token_buffer
= obstack_finish (&token_obstack
);
365 /* Just to make sure. */
368 if (unlexed
!= tok_undef
)
370 token_t res
= unlexed
;
371 symval
= unlexed_symval
;
372 token_buffer
= unlexed_token_buffer
;
377 c
= skip_white_space ();
382 token_buffer
= "EOF";
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':
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':
399 while (isalnum (c
) || c
== '_' || c
== '.')
401 obstack_1grow (&token_obstack
, c
);
404 obstack_1grow (&token_obstack
, '\0');
405 token_buffer
= obstack_finish (&token_obstack
);
407 symval
= getsym (token_buffer
);
408 return tok_identifier
;
410 case '0': case '1': case '2': case '3': case '4':
411 case '5': case '6': case '7': case '8': case '9':
417 obstack_1grow (&token_obstack
, c
);
418 numval
= numval
* 10 + c
- '0';
421 obstack_1grow (&token_obstack
, '\0');
422 token_buffer
= obstack_finish (&token_obstack
);
428 /* parse the literal token and compute character code in code */
433 obstack_1grow (&token_obstack
, '\'');
434 literalchar (&token_obstack
, &code
, '\'');
439 complain (_("use \"...\" for multi-character literal tokens"));
441 if (!literalchar (0, &discode
, '\''))
444 obstack_1grow (&token_obstack
, '\'');
445 obstack_1grow (&token_obstack
, '\0');
446 token_buffer
= obstack_finish (&token_obstack
);
447 symval
= getsym (token_buffer
);
448 symval
->class = token_sym
;
449 if (symval
->user_token_number
== SUNDEF
)
450 symval
->user_token_number
= code
;
451 return tok_identifier
;
455 /* parse the literal string token and treat as an identifier */
458 int code
; /* ignored here */
460 obstack_1grow (&token_obstack
, '\"');
461 /* Read up to and including ". */
462 while (literalchar (&token_obstack
, &code
, '\"'))
464 obstack_1grow (&token_obstack
, '\0');
465 token_buffer
= obstack_finish (&token_obstack
);
467 symval
= getsym (token_buffer
);
468 symval
->class = token_sym
;
470 return tok_identifier
;
483 return tok_semicolon
;
491 return tok_left_curly
;
494 obstack_1grow (&token_obstack
, c
);
498 obstack_1grow (&token_obstack
, c
);
502 while (c
== ' ' || c
== '\n' || c
== '\t');
503 obstack_1grow (&token_obstack
, '\0');
504 token_buffer
= obstack_finish (&token_obstack
);
508 return tok_left_curly
;
517 read_type_name (finput
);
521 return parse_percent_token ();
524 obstack_1grow (&token_obstack
, c
);
525 obstack_1grow (&token_obstack
, '\0');
526 token_buffer
= obstack_finish (&token_obstack
);
531 /* This function is a strcmp, which doesn't differentiate `-' and `_'
535 option_strcmp (const char *left
, const char *right
)
537 const unsigned char *l
, *r
;
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
== '-')))
550 /* Parse a token which starts with %.
551 Assumes the % has already been read and discarded. */
554 parse_percent_token (void)
556 const struct option_table_struct
*tx
;
558 int c
= getc (finput
);
563 return tok_two_percents
;
566 return tok_percent_left_curly
;
587 obstack_1grow (&token_obstack
, '%');
588 while (isalpha (c
) || c
== '_' || c
== '-')
592 obstack_1grow (&token_obstack
, c
);
597 obstack_1grow (&token_obstack
, '\0');
598 token_buffer
= obstack_finish (&token_obstack
);
600 /* table lookup % directive */
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)
608 *((int *) (tx
->set_flag
)) = 1;
615 *((char **) (tx
->set_flag
)) = optarg
;
620 fatal (_("`%s' is no longer supported"), token_buffer
);
624 /* Other cases do not apply here. */