X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/5ce94c2964afb6e66070f6dbfc8b8de8ab1d77d0..ceed8467b7db3615d3c9bfa9f3e5525d54c6a161:/src/lex.c diff --git a/src/lex.c b/src/lex.c index ad06ea6a..b236ea43 100644 --- a/src/lex.c +++ b/src/lex.c @@ -1,21 +1,22 @@ /* Token-reader for Bison's input parser, - Copyright (C) 1984, 1986, 1989, 1992 Free Software Foundation, Inc. + Copyright (C) 1984, 1986, 1989, 1992, 2000 Free Software Foundation, Inc. -This file is part of Bison, the GNU Compiler Compiler. + This file is part of Bison, the GNU Compiler Compiler. -Bison is free software; you can redistribute it and/or modify -it under the terms of the GNU General Public License as published by -the Free Software Foundation; either version 2, or (at your option) -any later version. + Bison is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. -Bison is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU General Public License for more details. + Bison is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. -You should have received a copy of the GNU General Public License -along with Bison; see the file COPYING. If not, write to -the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ + You should have received a copy of the GNU General Public License + along with Bison; see the file COPYING. If not, write to + the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ /* @@ -25,44 +26,37 @@ the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ and the name is looked up in the symbol table using symtab.c; symval is set to a pointer to the entry found. */ -#include -#include #include "system.h" +#include "getargs.h" #include "files.h" #include "getopt.h" /* for optarg */ #include "symtab.h" #include "lex.h" -#include "new.h" - -/* flags set by % directives */ -extern int definesflag; /* for -d */ -extern int toknumflag; /* for -k */ -extern int noparserflag; /* for -n */ -extern int fixed_outfiles; /* for -y */ -extern int nolinesflag; /* for -l */ -extern int rawtoknumflag; /* for -r */ -extern int verboseflag; /* for -v */ -extern int debugflag; /* for -t */ -extern char *spec_name_prefix; /* for -p */ -extern char *spec_file_prefix; /* for -b */ +#include "alloc.h" +#include "complain.h" + /*spec_outfile is declared in files.h, for -o */ -extern int lineno; extern int translations; -int parse_percent_token(); +extern void init_lex PARAMS((void)); +extern char *grow_token_buffer PARAMS((char *)); +extern int skip_white_space PARAMS((void)); +extern void unlex PARAMS((int)); +extern int lex PARAMS((void)); +extern int parse_percent_token PARAMS((void)); + +static int safegetc PARAMS((FILE *)); +static int literalchar PARAMS((char **, int *, char)); /* functions from main.c */ -extern char *printable_version(); -extern void fatal(); -extern void warni(); -extern void warn(); +extern char *printable_version PARAMS((int)); /* Buffer for storing the current token. */ char *token_buffer; /* Allocated size of token_buffer, not including space for terminator. */ -static int maxtoken; +int maxtoken; bucket *symval; int numval; @@ -72,7 +66,7 @@ static bucket *unlexed_symval; /* by the next call to lex */ void -init_lex() +init_lex (void) { maxtoken = 100; token_buffer = NEW2 (maxtoken + 1, char); @@ -80,9 +74,8 @@ init_lex() } -static char * -grow_token_buffer (p) - char *p; +char * +grow_token_buffer (char *p) { int offset = p - token_buffer; maxtoken *= 2; @@ -92,7 +85,7 @@ grow_token_buffer (p) int -skip_white_space() +skip_white_space (void) { register int c; register int inside; @@ -109,7 +102,7 @@ skip_white_space() c = getc(finput); if (c != '*' && c != '/') { - warn(_("unexpected `/' found and ignored")); + complain (_("unexpected `/' found and ignored")); break; } cplus_comment = (c == '/'); @@ -138,7 +131,7 @@ skip_white_space() c = getc(finput); } else if (c == EOF) - fatal(_("unterminated comment")); + fatal (_("unterminated comment")); else c = getc(finput); } @@ -155,19 +148,18 @@ skip_white_space() break; default: - return (c); + return c; } } } /* do a getc, but give error message if EOF encountered */ -int -safegetc(f) - FILE *f; +static int +safegetc (FILE *f) { register int c = getc(f); if (c == EOF) - fatal(_("Unexpected end of file")); + fatal (_("unexpected end of file")); return c; } @@ -177,11 +169,8 @@ safegetc(f) return 1 unless the character is an unescaped `term' or \n report error for \n */ -int -literalchar(pp, pcode, term) - char **pp; - int *pcode; - char term; +static int +literalchar (char **pp, int *pcode, char term) { register int c; register char *p; @@ -191,7 +180,7 @@ literalchar(pp, pcode, term) c = safegetc(finput); if (c == '\n') { - warn(_("unescaped newline in constant")); + complain (_("unescaped newline in constant")); ungetc(c, finput); code = '?'; wasquote = 1; @@ -211,7 +200,7 @@ literalchar(pp, pcode, term) else if (c == 'r') code = '\r'; else if (c == 'f') code = '\f'; else if (c == 'b') code = '\b'; - else if (c == 'v') code = 013; + else if (c == 'v') code = '\013'; else if (c == '\\') code = '\\'; else if (c == '\'') code = '\''; else if (c == '\"') code = '\"'; @@ -223,7 +212,8 @@ literalchar(pp, pcode, term) code = (code * 8) + (c - '0'); if (code >= 256 || code < 0) { - warni(_("octal value outside range 0...255: `\\%o'"), code); + complain (_("octal value outside range 0...255: `\\%o'"), + code); code &= 0xFF; break; } @@ -247,7 +237,8 @@ literalchar(pp, pcode, term) break; if (code >= 256 || code<0) { - warni(_("hexadecimal value above 255: `\\x%x'"), code); + complain (_("hexadecimal value above 255: `\\x%x'"), + code); code &= 0xFF; break; } @@ -257,8 +248,8 @@ literalchar(pp, pcode, term) } else { - warni (_("unknown escape sequence: `\\' followed by `%s'"), - printable_version(c)); + complain (_("unknown escape sequence: `\\' followed by `%s'"), + printable_version(c)); code = '?'; } } /* has \ */ @@ -268,7 +259,9 @@ literalchar(pp, pcode, term) so that `\012' and `\n' can be interchangeable. */ p = *pp; - if (code == '\\') {*p++ = '\\'; *p++ = '\\';} + if (code == term && wasquote) + *p++ = code; + else if (code == '\\') {*p++ = '\\'; *p++ = '\\';} else if (code == '\'') {*p++ = '\\'; *p++ = '\'';} else if (code == '\"') {*p++ = '\\'; *p++ = '\"';} else if (code >= 040 && code < 0177) @@ -293,8 +286,7 @@ literalchar(pp, pcode, term) void -unlex(token) - int token; +unlex (int token) { unlexed = token; unlexed_symval = symval; @@ -302,7 +294,7 @@ unlex(token) int -lex() +lex (void) { register int c; char *p; @@ -312,7 +304,7 @@ lex() symval = unlexed_symval; c = unlexed; unlexed = -1; - return (c); + return c; } c = skip_white_space(); @@ -323,7 +315,7 @@ lex() { case EOF: strcpy(token_buffer, "EOF"); - return (ENDFILE); + return ENDFILE; case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J': @@ -351,7 +343,7 @@ lex() *p = 0; ungetc(c, finput); symval = getsym(token_buffer); - return (IDENTIFIER); + return IDENTIFIER; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': @@ -370,7 +362,7 @@ lex() } *p = 0; ungetc(c, finput); - return (NUMBER); + return NUMBER; } case '\'': @@ -389,7 +381,7 @@ lex() c = getc(finput); if (c != '\'') { - warn(_("use \"...\" for multi-character literal tokens")); + complain (_("use \"...\" for multi-character literal tokens")); while (1) { dp = discard; @@ -403,7 +395,7 @@ lex() symval->class = STOKEN; if (! symval->user_token_number) symval->user_token_number = code; - return (IDENTIFIER); + return IDENTIFIER; } case '\"': @@ -425,23 +417,23 @@ lex() symval = getsym(token_buffer); symval->class = STOKEN; - return (IDENTIFIER); + return IDENTIFIER; } case ',': - return (COMMA); + return COMMA; case ':': - return (COLON); + return COLON; case ';': - return (SEMICOLON); + return SEMICOLON; case '|': - return (BAR); + return BAR; case '{': - return (LEFT_CURLY); + return LEFT_CURLY; case '=': do @@ -454,12 +446,12 @@ lex() if (c == '{') { strcpy(token_buffer, "={"); - return(LEFT_CURLY); + return LEFT_CURLY; } else { ungetc(c, finput); - return(ILLEGAL); + return ILLEGAL; } case '<': @@ -468,10 +460,10 @@ lex() while (c != '>') { if (c == EOF) - fatal(_("unterminated type name at end of file")); + fatal (_("unterminated type name at end of file")); if (c == '\n') { - warn(_("unterminated type name")); + complain (_("unterminated type name")); ungetc(c, finput); break; } @@ -483,14 +475,14 @@ lex() c = getc(finput); } *p = 0; - return (TYPENAME); + return TYPENAME; case '%': - return (parse_percent_token()); + return parse_percent_token(); default: - return (ILLEGAL); + return ILLEGAL; } } @@ -499,7 +491,7 @@ lex() set. A retval action returns the code. */ struct percent_table_struct { - char *name; + const char *name; void *setflag; int retval; } percent_table[] = @@ -542,8 +534,8 @@ struct percent_table_struct { /* These would be acceptable, but they do not affect processing */ {"verbose", &verboseflag, NOOP}, /* -v */ {"debug", &debugflag, NOOP}, /* -t */ - /* {"help", , NOOP}, /* -h */ - /* {"version", , NOOP}, /* -V */ + /* {"help", , NOOP},*/ /* -h */ + /* {"version", , NOOP},*/ /* -V */ #endif {NULL, NULL, ILLEGAL} @@ -553,7 +545,7 @@ struct percent_table_struct { Assumes the % has already been read and discarded. */ int -parse_percent_token () +parse_percent_token (void) { register int c; register char *p; @@ -568,28 +560,28 @@ parse_percent_token () switch (c) { case '%': - return (TWO_PERCENTS); + return TWO_PERCENTS; case '{': - return (PERCENT_LEFT_CURLY); + return PERCENT_LEFT_CURLY; case '<': - return (LEFT); + return LEFT; case '>': - return (RIGHT); + return RIGHT; case '2': - return (NONASSOC); + return NONASSOC; case '0': - return (TOKEN); + return TOKEN; case '=': - return (PREC); + return PREC; } if (!isalpha(c)) - return (ILLEGAL); + return ILLEGAL; p = token_buffer; *p++ = '%';