]>
git.saurik.com Git - bison.git/blob - src/lex.c
4eb8f313215a19461b293af4d7e9429e940c9408
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 struct obstack token_obstack
;
34 const char *token_buffer
= NULL
;
39 /* these two describe a token to be reread */
40 static token_t unlexed
= tok_undef
;
41 /* by the next call to lex */
42 static bucket
*unlexed_symval
= NULL
;
48 obstack_init (&token_obstack
);
56 obstack_free (&token_obstack
, NULL
);
61 skip_white_space (void)
75 /* FIXME: Should probably be merged with copy_comment. */
77 if (c
!= '*' && c
!= '/')
79 complain (_("unexpected `/' found and ignored"));
82 cplus_comment
= (c
== '/');
89 if (!cplus_comment
&& c
== '*')
108 fatal (_("unterminated comment"));
131 /*-----------------------------------------------------.
132 | Do a getc, but give error message if EOF encountered |
133 `-----------------------------------------------------*/
140 fatal (_("unexpected end of file"));
145 /*------------------------------------------------------------------.
146 | Read one literal character from finput. Process \ escapes. |
147 | Append the normalized string version of the char to OUT. Assign |
148 | the character code to *PCODE. Return 1 unless the character is an |
149 | unescaped `term' or \n report error for \n. |
150 `------------------------------------------------------------------*/
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. */
158 literalchar (struct obstack
*out
, int *pcode
, char term
)
169 complain (_("unescaped newline in constant"));
203 else if (c
<= '7' && c
>= '0')
206 while (c
<= '7' && c
>= '0')
208 code
= (code
* 8) + (c
- '0');
209 if (code
>= 256 || code
< 0)
211 complain (_("octal value outside range 0...255: `\\%o'"),
226 if (c
>= '0' && c
<= '9')
227 code
*= 16, code
+= c
- '0';
228 else if (c
>= 'a' && c
<= 'f')
229 code
*= 16, code
+= c
- 'a' + 10;
230 else if (c
>= 'A' && c
<= 'F')
231 code
*= 16, code
+= c
- 'A' + 10;
234 if (code
>= 256 || code
< 0)
236 complain (_("hexadecimal value above 255: `\\x%x'"), code
);
246 char badchar
[] = "c";
248 complain (_("unknown escape sequence: `\\' followed by `%s'"),
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. */
259 if (code
== term
&& wasquote
)
261 else if (code
== '\\')
266 else if (code
== '\'')
271 else if (code
== '\"')
276 else if (code
>= 040 && code
< 0177)
278 else if (code
== '\t')
283 else if (code
== '\n')
288 else if (code
== '\r')
293 else if (code
== '\v')
298 else if (code
== '\b')
303 else if (code
== '\f')
311 *cp
++ = code
/ 0100 + '0';
312 *cp
++ = ((code
/ 010) & 07) + '0';
313 *cp
++ = (code
& 07) + '0';
318 obstack_sgrow (out
, buf
);
325 unlex (token_t token
)
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
;
376 c
= skip_white_space ();
381 token_buffer
= "EOF";
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':
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':
398 while (isalnum (c
) || c
== '_' || c
== '.')
400 obstack_1grow (&token_obstack
, c
);
403 obstack_1grow (&token_obstack
, '\0');
404 token_buffer
= obstack_finish (&token_obstack
);
406 symval
= getsym (token_buffer
);
407 return tok_identifier
;
409 case '0': case '1': case '2': case '3': case '4':
410 case '5': case '6': case '7': case '8': case '9':
416 obstack_1grow (&token_obstack
, c
);
417 numval
= numval
* 10 + c
- '0';
420 obstack_1grow (&token_obstack
, '\0');
421 token_buffer
= obstack_finish (&token_obstack
);
427 /* parse the literal token and compute character code in code */
432 obstack_1grow (&token_obstack
, '\'');
433 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
)
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 /* This function is a strcmp, which doesn't differentiate `-' and `_'
534 option_strcmp (const char *left
, const char *right
)
536 const unsigned char *l
, *r
;
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
== '-')))
549 /* Parse a token which starts with %.
550 Assumes the % has already been read and discarded. */
553 parse_percent_token (void)
555 const struct option_table_struct
*tx
;
557 int c
= getc (finput
);
562 return tok_two_percents
;
565 return tok_percent_left_curly
;
586 obstack_1grow (&token_obstack
, '%');
587 while (isalpha (c
) || c
== '_' || c
== '-')
591 obstack_1grow (&token_obstack
, c
);
596 obstack_1grow (&token_obstack
, '\0');
597 token_buffer
= obstack_finish (&token_obstack
);
599 /* table lookup % directive */
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)
607 *((int *) (tx
->set_flag
)) = 1;
614 *((char **) (tx
->set_flag
)) = optarg
;
619 fatal (_("`%s' is no longer supported"), token_buffer
);
623 /* Other cases do not apply here. */