]>
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 */
33 /* Buffer for storing the current token. */
34 struct obstack token_obstack
;
35 const char *token_buffer
= NULL
;
40 static int unlexed
; /* these two describe a token to be reread */
41 static bucket
*unlexed_symval
; /* by the next call to lex */
47 obstack_init (&token_obstack
);
53 skip_white_space (void)
67 /* FIXME: Should probably be merged with copy_comment. */
69 if (c
!= '*' && c
!= '/')
71 complain (_("unexpected `/' found and ignored"));
74 cplus_comment
= (c
== '/');
81 if (!cplus_comment
&& c
== '*')
100 fatal (_("unterminated comment"));
123 /*-----------------------------------------------------.
124 | Do a getc, but give error message if EOF encountered |
125 `-----------------------------------------------------*/
132 fatal (_("unexpected end of file"));
137 /*------------------------------------------------------------------.
138 | Read one literal character from finput. Process \ escapes. |
139 | Append the normalized string version of the char to OUT. Assign |
140 | the character code to *PCODE. Return 1 unless the character is an |
141 | unescaped `term' or \n report error for \n. |
142 `------------------------------------------------------------------*/
144 /* FIXME: We could directly work in the obstack, but that would make
145 it more difficult to move to quotearg some day. So for the time
146 being, I prefer have literalchar behave like quotearg, and change
147 my mind later if I was wrong. */
150 literalchar (struct obstack
*out
, int *pcode
, char term
)
161 complain (_("unescaped newline in constant"));
195 else if (c
<= '7' && c
>= '0')
198 while (c
<= '7' && c
>= '0')
200 code
= (code
* 8) + (c
- '0');
201 if (code
>= 256 || code
< 0)
203 complain (_("octal value outside range 0...255: `\\%o'"),
218 if (c
>= '0' && c
<= '9')
219 code
*= 16, code
+= c
- '0';
220 else if (c
>= 'a' && c
<= 'f')
221 code
*= 16, code
+= c
- 'a' + 10;
222 else if (c
>= 'A' && c
<= 'F')
223 code
*= 16, code
+= c
- 'A' + 10;
226 if (code
>= 256 || code
< 0)
228 complain (_("hexadecimal value above 255: `\\x%x'"), code
);
238 char badchar
[] = "c";
240 complain (_("unknown escape sequence: `\\' followed by `%s'"),
246 /* now fill BUF with the canonical name for this character as a
247 literal token. Do not use what the user typed, so that `\012'
248 and `\n' can be interchangeable. */
251 if (code
== term
&& wasquote
)
253 else if (code
== '\\')
258 else if (code
== '\'')
263 else if (code
== '\"')
268 else if (code
>= 040 && code
< 0177)
270 else if (code
== '\t')
275 else if (code
== '\n')
280 else if (code
== '\r')
285 else if (code
== '\v')
290 else if (code
== '\b')
295 else if (code
== '\f')
303 *cp
++ = code
/ 0100 + '0';
304 *cp
++ = ((code
/ 010) & 07) + '0';
305 *cp
++ = (code
& 07) + '0';
310 obstack_sgrow (out
, buf
);
320 unlexed_symval
= symval
;
323 /*-----------------------------------------------------------------.
324 | We just read `<' from FIN. Store in TOKEN_BUFFER, the type name |
325 | specified between the `<...>'. |
326 `-----------------------------------------------------------------*/
329 read_type_name (FILE *fin
)
336 fatal (_("unterminated type name at end of file"));
339 complain (_("unterminated type name"));
344 obstack_1grow (&token_obstack
, c
);
347 obstack_1grow (&token_obstack
, '\0');
348 token_buffer
= obstack_finish (&token_obstack
);
357 /* Just to make sure. */
362 symval
= unlexed_symval
;
368 c
= skip_white_space ();
373 token_buffer
= "EOF";
376 case 'A': case 'B': case 'C': case 'D': case 'E':
377 case 'F': case 'G': case 'H': case 'I': case 'J':
378 case 'K': case 'L': case 'M': case 'N': case 'O':
379 case 'P': case 'Q': case 'R': case 'S': case 'T':
380 case 'U': case 'V': case 'W': case 'X': case 'Y':
382 case 'a': case 'b': case 'c': case 'd': case 'e':
383 case 'f': case 'g': case 'h': case 'i': case 'j':
384 case 'k': case 'l': case 'm': case 'n': case 'o':
385 case 'p': case 'q': case 'r': case 's': case 't':
386 case 'u': case 'v': case 'w': case 'x': case 'y':
390 while (isalnum (c
) || c
== '_' || c
== '.')
392 obstack_1grow (&token_obstack
, c
);
395 obstack_1grow (&token_obstack
, '\0');
396 token_buffer
= obstack_finish (&token_obstack
);
398 symval
= getsym (token_buffer
);
399 return tok_identifier
;
401 case '0': case '1': case '2': case '3': case '4':
402 case '5': case '6': case '7': case '8': case '9':
408 obstack_1grow (&token_obstack
, c
);
409 numval
= numval
* 10 + c
- '0';
412 obstack_1grow (&token_obstack
, '\0');
413 token_buffer
= obstack_finish (&token_obstack
);
419 /* parse the literal token and compute character code in code */
425 obstack_1grow (&token_obstack
, '\'');
426 literalchar (&token_obstack
, &code
, '\'');
431 complain (_("use \"...\" for multi-character literal tokens"));
433 if (!literalchar (0, &discode
, '\''))
436 obstack_1grow (&token_obstack
, '\'');
437 obstack_1grow (&token_obstack
, '\0');
438 token_buffer
= obstack_finish (&token_obstack
);
439 symval
= getsym (token_buffer
);
440 symval
->class = token_sym
;
441 if (!symval
->user_token_number
)
442 symval
->user_token_number
= code
;
443 return tok_identifier
;
447 /* parse the literal string token and treat as an identifier */
451 int code
; /* ignored here */
453 obstack_1grow (&token_obstack
, '\"');
454 /* Read up to and including ". */
455 while (literalchar (&token_obstack
, &code
, '\"'))
457 obstack_1grow (&token_obstack
, '\0');
458 token_buffer
= obstack_finish (&token_obstack
);
460 symval
= getsym (token_buffer
);
461 symval
->class = token_sym
;
463 return tok_identifier
;
473 return tok_semicolon
;
479 return tok_left_curly
;
488 while (c
== ' ' || c
== '\n' || c
== '\t');
493 return tok_left_curly
;
502 read_type_name (finput
);
506 return parse_percent_token ();
513 /* This function is a strcmp, which doesn't differentiate `-' and `_'
517 option_strcmp (const char *left
, const char *right
)
519 const unsigned char *l
, *r
;
522 assert(left
!= NULL
&& right
!= NULL
);
523 l
= (const unsigned char *)left
;
524 r
= (const unsigned char *)right
;
525 while (((c
= *l
- *r
++) == 0 && *l
!= '\0')
526 || ((*l
== '-' || *l
== '_') && (*r
== '_' || *r
== '-')))
531 /* Parse a token which starts with %.
532 Assumes the % has already been read and discarded. */
535 parse_percent_token (void)
538 const struct option_table_struct
*tx
;
545 return tok_two_percents
;
548 return tok_percent_left_curly
;
569 obstack_1grow (&token_obstack
, '%');
570 while (isalpha (c
) || c
== '_' || c
== '-')
574 obstack_1grow (&token_obstack
, c
);
579 obstack_1grow (&token_obstack
, '\0');
580 token_buffer
= obstack_finish (&token_obstack
);
582 /* table lookup % directive */
583 for (tx
= option_table
; tx
->name
; tx
++)
584 if ((tx
->access
== opt_percent
|| tx
->access
== opt_both
)
585 && option_strcmp (token_buffer
+ 1, tx
->name
) == 0)
590 *((int *) (tx
->set_flag
)) = 1;
597 *((char **) (tx
->set_flag
)) = optarg
;
602 fatal (_("`%s' is no longer supported"), token_buffer
);