ack/lang/cem/cpp.ansi/LLlex.c

666 lines
14 KiB
C
Raw Normal View History

1989-10-23 10:35:56 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1994-06-24 14:02:31 +00:00
/* $Id$ */
1989-10-23 10:35:56 +00:00
/* L E X I C A L A N A L Y Z E R */
#include "parameters.h"
1989-10-23 10:35:56 +00:00
#include <string.h>
#include <alloc.h>
#include "input.h"
#include "arith.h"
#include "macro.h"
#include "idf.h"
#include "LLlex.h"
#include "Lpars.h"
#include "replace.h"
#include "class.h"
#include "error.h"
#include "bits.h"
1989-10-23 10:35:56 +00:00
#define BUFSIZ 1024
1989-10-23 10:35:56 +00:00
struct token dot;
int ReplaceMacros = 1; /* replacing macros */
int AccDefined = 0; /* accept "defined(...)" */
int UnknownIdIsZero = 0; /* interpret unknown id as integer 0 */
int Unstacked = 0; /* an unstack is done */
int AccFileSpecifier = 0; /* return filespecifier <...> */
int LexSave = 0; /* last character read by GetChar */
extern int InputLevel; /* # of current macro expansions */
1989-10-23 10:35:56 +00:00
#define FLG_ESEEN 0x01 /* possibly a floating point number */
#define FLG_DOTSEEN 0x02 /* certainly a floating point number */
1989-10-23 10:35:56 +00:00
/* Private forward definitions */
static arith char_constant(char*);
static char* string_token(char *, int);
static int quoted(register int);
static int val_in_base(register int, int);
static int trigraph(void);
int LLlex(void)
1989-10-23 10:35:56 +00:00
{
return (DOT != EOF) ? GetToken(&dot) : EOF;
}
int GetToken(register struct token* ptok)
1989-10-23 10:35:56 +00:00
{
/* GetToken() is the actual token recognizer. It calls the
control line interpreter if it encounters a "\n{w}*#"
combination. Macro replacement is also performed if it is
needed.
1989-10-23 10:35:56 +00:00
*/
char buf[BUFSIZ];
register int ch, nch;
again: /* rescan the input after an error or replacement */
1989-10-23 10:35:56 +00:00
ch = GetChar();
1990-10-02 17:19:17 +00:00
/* rescan, the following character has been read */
1989-10-23 10:35:56 +00:00
if ((ch & 0200) && ch != EOI) /* stop on non-ascii character */
fatal("non-ascii '\\%03o' read", ch & 0377);
/* keep track of the place of the token in the file */
switch (class(ch))
{ /* detect character class */
case STNL: /* newline, vertical space or formfeed */
LineNumber++;
return ptok->tk_symb = EOF;
case STSKIP: /* just skip the skip characters */
goto again;
case STGARB: /* garbage character */
garbage:
if (040 < ch && ch < 0177)
error("garbage char %c", ch);
else
error("garbage char \\%03o", ch);
goto again;
case STSIMP: /* a simple character, no part of compound token*/
1989-10-23 10:35:56 +00:00
return ptok->tk_symb = ch;
case STCOMP: /* maybe the start of a compound token */
nch = GetChar(); /* character lookahead */
switch (ch)
{
case '!':
if (nch == '=')
return ptok->tk_symb = NOTEQUAL;
UnGetChar();
return ptok->tk_symb = ch;
case '&':
if (nch == '&')
return ptok->tk_symb = AND;
else if (nch == '=')
return ptok->tk_symb = ANDAB;
UnGetChar();
return ptok->tk_symb = ch;
case '+':
if (nch == '+')
return ptok->tk_symb = PLUSPLUS;
else if (nch == '=')
return ptok->tk_symb = PLUSAB;
UnGetChar();
return ptok->tk_symb = ch;
case '-':
if (nch == '-')
return ptok->tk_symb = MINMIN;
else if (nch == '>')
return ptok->tk_symb = ARROW;
else if (nch == '=')
return ptok->tk_symb = MINAB;
UnGetChar();
return ptok->tk_symb = ch;
case '<':
if (AccFileSpecifier)
{
UnGetChar(); /* pushback nch */
ptok->tk_str = string_token("file specifier", '>');
return ptok->tk_symb = FILESPECIFIER;
}
else if (nch == '<')
{
if ((nch = GetChar()) == '=')
return ptok->tk_symb = LEFTAB;
UnGetChar();
return ptok->tk_symb = LEFT;
}
else if (nch == '=')
return ptok->tk_symb = LESSEQ;
UnGetChar();
return ptok->tk_symb = ch;
case '=':
if (nch == '=')
return ptok->tk_symb = EQUAL;
UnGetChar();
return ptok->tk_symb = ch;
case '>':
if (nch == '=')
return ptok->tk_symb = GREATEREQ;
else if (nch == '>')
{
if ((nch = GetChar()) == '=')
return ptok->tk_symb = RIGHTAB;
UnGetChar();
return ptok->tk_symb = RIGHT;
}
UnGetChar();
return ptok->tk_symb = ch;
case '|':
if (nch == '|')
return ptok->tk_symb = OR;
else if (nch == '=')
return ptok->tk_symb = ORAB;
UnGetChar();
return ptok->tk_symb = ch;
case '%':
if (nch == '=')
return ptok->tk_symb = MODAB;
UnGetChar();
return ptok->tk_symb = ch;
case '*':
if (nch == '=')
return ptok->tk_symb = TIMESAB;
UnGetChar();
return ptok->tk_symb = ch;
case '^':
if (nch == '=')
return ptok->tk_symb = XORAB;
UnGetChar();
return ptok->tk_symb = ch;
case '/':
if (!InputLevel)
{
if (nch == '*')
{
skipcomment();
goto again;
}
else if (nch == '/')
{
skiplinecomment();
goto again;
}
}
else if (nch == '=')
return ptok->tk_symb = DIVAB;
UnGetChar();
return ptok->tk_symb = ch;
default:
crash("bad class for char 0%o", ch);
/* NOTREACHED */
1989-10-23 10:35:56 +00:00
}
case STCHAR: /* character constant */
ptok->tk_val = char_constant("character");
return ptok->tk_symb = INTEGER;
case STSTR: /* string */
ptok->tk_str = string_token("string", '"');
return ptok->tk_symb = STRING;
case STELL: /* wide character constant/string prefix */
nch = GetChar();
if (nch == '"')
{
ptok->tk_str = string_token("wide character string", '"');
return ptok->tk_symb = STRING;
}
else if (nch == '\'')
{
ptok->tk_val = char_constant("wide character");
return ptok->tk_symb = INTEGER;
1989-10-23 10:35:56 +00:00
}
UnGetChar();
/* fallthrough */
case STIDF:
{
extern int idfsize; /* ??? */
register char* tg = &buf[0];
register char* maxpos = &buf[idfsize];
int NoExpandNext = 0;
#define tstmac(bx) \
if (!(bits[ch] & bx)) \
goto nomac
#define cpy *tg++ = ch
#define load \
(ch = GetChar()); \
if (!in_idf(ch)) \
goto endidf
if (Unstacked)
EnableMacros(); /* unstack macro's when allowed. */
if (ch == NOEXPM)
{
NoExpandNext = 1;
ch = GetChar();
}
1989-10-23 10:35:56 +00:00
#ifdef DOBITS
cpy;
tstmac(bit0);
load;
cpy;
tstmac(bit1);
load;
cpy;
tstmac(bit2);
load;
cpy;
tstmac(bit3);
load;
cpy;
tstmac(bit4);
load;
cpy;
tstmac(bit5);
load;
cpy;
tstmac(bit6);
load;
cpy;
tstmac(bit7);
load;
1989-10-23 10:35:56 +00:00
#endif
for (;;)
{
if (tg < maxpos)
{
cpy;
}
load;
1989-10-23 10:35:56 +00:00
}
endidf:
/*if (ch != EOI) UnGetChar();*/
UnGetChar();
*tg++ = '\0'; /* mark the end of the identifier */
if (ReplaceMacros)
{
register struct idf* idef = findidf(buf);
if (idef && idef->id_macro && !NoExpandNext)
{
if (replace(idef))
goto again;
}
1989-10-23 10:35:56 +00:00
}
nomac: /* buf can already be null-terminated. soit */
1989-10-23 10:35:56 +00:00
ch = GetChar();
while (in_idf(ch))
{
if (tg < maxpos)
*tg++ = ch;
1989-10-23 10:35:56 +00:00
ch = GetChar();
}
UnGetChar();
*tg++ = '\0'; /* mark the end of the identifier */
1989-10-23 10:35:56 +00:00
NoExpandNext = 0;
if (UnknownIdIsZero)
{
ptok->tk_val = (arith)0;
return ptok->tk_symb = INTEGER;
}
ptok->tk_str = Malloc((unsigned)(tg - buf));
strcpy(ptok->tk_str, buf);
return IDENTIFIER;
1989-10-23 10:35:56 +00:00
}
case STNUM: /* a numeric constant */
{ /* it may only be an integer constant */
register int base = 10, vch;
register arith val = 0;
int ovfl = 0;
arith ubound = max_arith / (base / 2);
/* Since the preprocessor only knows integers and has
* nothing to do with ellipsis we just return when the
* pp-number starts with a '.'
*/
if (ch == '.')
{
return ptok->tk_symb = ch;
}
if (ch == '0')
{
ch = GetChar();
if (ch == 'x' || ch == 'X')
{
base = 16;
ch = GetChar();
}
else
{
base = 8;
}
}
while ((vch = val_in_base(ch, base)) >= 0)
{
if (val < 0 || val > ubound)
ovfl++;
val *= base;
if (val < 0 && val + vch >= 0)
ovfl++;
val += vch;
ch = GetChar();
}
ptok->tk_unsigned = 0;
if (ch == 'u' || ch == 'U')
{
ptok->tk_unsigned = 1;
ch = GetChar();
if (ch == 'l' || ch == 'L')
{
ch = GetChar();
}
}
else if (ch == 'l' || ch == 'L')
{
ch = GetChar();
if (ch == 'u' || ch == 'U')
{
ptok->tk_unsigned = 1;
ch = GetChar();
}
}
if (ovfl)
{
warning("overflow in constant");
ptok->tk_unsigned = 1;
}
else if (val < 0)
{
/* give warning??? */
ptok->tk_unsigned = 1;
}
UnGetChar();
ptok->tk_val = val;
return ptok->tk_symb = INTEGER;
}
case STEOI: /* end of text on source file */
return ptok->tk_symb = EOF;
case STMSPEC:
if (!InputLevel)
goto garbage;
if (ch == TOKSEP)
goto again;
1989-10-23 10:35:56 +00:00
/* fallthrough shouldn't happen */
default: /* this cannot happen */
crash("bad class for char 0%o", ch);
1989-10-23 10:35:56 +00:00
}
/*NOTREACHED*/
}
void skipcomment(void)
1989-10-23 10:35:56 +00:00
{
/* The last character read has been the '*' of '/_*'. The
characters, except NL and EOI, between '/_*' and the first
occurring '*_/' are not interpreted.
NL only affects the LineNumber. EOI is not legal.
Important note: it is not possible to stop skipping comment
beyond the end-of-file of an included file.
EOI is returned by LoadChar only on encountering EOF of the
top-level file...
1989-10-23 10:35:56 +00:00
*/
register int c;
NoUnstack++;
c = GetChar();
do
{
while (c != '*')
{
if (class(c) == STNL)
{
1989-10-23 10:35:56 +00:00
++LineNumber;
}
else if (c == EOI)
{
1989-10-23 10:35:56 +00:00
NoUnstack--;
return;
}
c = GetChar();
} /* last Character seen was '*' */
c = GetChar();
} while (c != '/');
NoUnstack--;
}
void skiplinecomment(void)
{
/* The last character read has been the '/' of '//'. We read
and discard all characters up to but not including the next
NL. */
for (;;) {
int c = GetChar();
if ((class(c) == STNL) || (c == EOI))
{
UnGetChar();
break;
}
}
}
static arith char_constant(char* nm)
1989-10-23 10:35:56 +00:00
{
register arith val = 0;
register int ch;
int size = 0;
ch = GetChar();
if (ch == '\'')
error("%s constant too short", nm);
else
while (ch != '\'')
{
if (ch == '\n')
{
error("newline in %s constant", nm);
LineNumber++;
break;
}
if (ch == '\\')
ch = quoted(GetChar());
if (ch >= 128)
ch -= 256;
if (size < sizeof(arith))
val |= ch << (8 * size);
size++;
ch = GetChar();
1989-10-23 10:35:56 +00:00
}
if (size > sizeof(arith))
error("%s constant too long", nm);
1990-02-01 13:09:40 +00:00
else if (size > 1)
strict("%s constant includes more than one character", nm);
1989-10-23 10:35:56 +00:00
return val;
}
static char* string_token(char *nm, int stop_char)
1989-10-23 10:35:56 +00:00
{
register int ch;
register int str_size;
register char* str = Malloc((unsigned)(str_size = ISTRSIZE));
1989-10-23 10:35:56 +00:00
register int pos = 0;
1989-10-23 10:35:56 +00:00
ch = GetChar();
while (ch != stop_char)
{
if (ch == '\n')
{
1989-10-23 10:35:56 +00:00
error("newline in %s", nm);
LineNumber++;
break;
}
if (ch == EOI)
{
1989-10-23 10:35:56 +00:00
error("end-of-file inside %s", nm);
break;
}
if (ch == '\\' && !AccFileSpecifier)
ch = quoted(GetChar());
str[pos++] = ch;
if (pos == str_size)
1990-10-02 17:19:17 +00:00
str = Realloc(str, (unsigned)(str_size <<= 1));
1989-10-23 10:35:56 +00:00
ch = GetChar();
}
str[pos++] = '\0'; /* for filenames etc. */
1990-10-02 17:19:17 +00:00
str = Realloc(str, (unsigned)pos);
1989-10-23 10:35:56 +00:00
return str;
}
static int quoted(register int ch)
{
1989-10-23 10:35:56 +00:00
/* quoted() replaces an escaped character sequence by the
character meant.
1989-10-23 10:35:56 +00:00
*/
/* first char after backslash already in ch */
if (!is_oct(ch))
{ /* a quoted char */
switch (ch)
1989-10-23 10:35:56 +00:00
{
case 'n':
ch = '\n';
break;
case 't':
ch = '\t';
break;
case 'b':
ch = '\b';
break;
case 'r':
ch = '\r';
break;
case 'f':
ch = '\f';
break;
case 'a': /* alert */
ch = '\007';
break;
case 'v': /* vertical tab */
ch = '\013';
break;
case 'x': /* quoted hex */
{
register int hex = 0;
register int vch;
for (;;)
{
ch = GetChar();
if (vch = val_in_base(ch, 16), vch == -1)
break;
hex = hex * 16 + vch;
}
UnGetChar();
ch = hex;
1989-10-23 10:35:56 +00:00
}
}
}
else
{ /* a quoted octal */
1989-10-23 10:35:56 +00:00
register int oct = 0, cnt = 0;
do
{
oct = oct * 8 + (ch - '0');
1989-10-23 10:35:56 +00:00
ch = GetChar();
} while (is_oct(ch) && ++cnt < 3);
UnGetChar();
ch = oct;
}
return ch & 0377;
1989-10-23 10:35:56 +00:00
}
static int val_in_base(register int ch, int base)
1989-10-23 10:35:56 +00:00
{
switch (base)
{
case 8:
return (is_dig(ch) && ch < '9') ? ch - '0' : -1;
case 10:
return is_dig(ch) ? ch - '0' : -1;
case 16:
return is_dig(ch) ? ch - '0' : is_hex(ch) ? (ch - 'a' + 10) & 017 : -1;
default:
fatal("(val_in_base) illegal base value %d", base);
/* NOTREACHED */
1989-10-23 10:35:56 +00:00
}
}
int GetChar(void)
1989-10-23 10:35:56 +00:00
{
/* The routines GetChar and trigraph parses the trigraph
sequences and removes occurences of \\\n.
1989-10-23 10:35:56 +00:00
*/
register int ch;
again:
LoadChar(ch);
/* possible trigraph sequence */
if (ch == '?')
ch = trigraph();
/* \\\n are removed from the input stream */
if (ch == '\\')
{
1989-10-23 10:35:56 +00:00
LoadChar(ch);
if (ch == '\n')
{
1989-10-23 10:35:56 +00:00
++LineNumber;
goto again;
}
PushBack();
ch = '\\';
}
return (LexSave = ch);
1989-10-23 10:35:56 +00:00
}
static int trigraph(void)
1989-10-23 10:35:56 +00:00
{
register int ch;
LoadChar(ch);
if (ch == '?')
{
1989-10-23 10:35:56 +00:00
LoadChar(ch);
switch (ch)
{ /* its a trigraph */
case '=':
ch = '#';
return (ch);
case '(':
ch = '[';
return (ch);
case '/':
ch = '\\';
return (ch);
case ')':
ch = ']';
return (ch);
case '\'':
ch = '^';
return (ch);
case '<':
ch = '{';
return (ch);
case '!':
ch = '|';
return (ch);
case '>':
ch = '}';
return (ch);
case '-':
ch = '~';
return (ch);
1989-10-23 10:35:56 +00:00
}
PushBack();
}
PushBack();
return ('?');
1989-10-23 10:35:56 +00:00
}