2001-05-22 09:32:24 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* scanner.c */
|
|
|
|
/* */
|
|
|
|
/* Source file line info structure */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2009-09-18 14:28:52 +00:00
|
|
|
/* (C) 1998-2009, Ullrich von Bassewitz */
|
|
|
|
/* Roemerstrasse 52 */
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
/* EMail: uz@cc65.org */
|
2001-05-22 09:32:24 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
/* */
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
/* */
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
/* distribution. */
|
|
|
|
/* */
|
|
|
|
/*****************************************************************************/
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
2004-06-06 15:59:44 +00:00
|
|
|
#include <math.h>
|
2000-09-14 19:26:13 +00:00
|
|
|
|
2000-08-23 06:45:55 +00:00
|
|
|
/* common */
|
2001-01-05 19:24:47 +00:00
|
|
|
#include "chartype.h"
|
2008-08-11 17:51:00 +00:00
|
|
|
#include "fp.h"
|
2000-08-23 06:45:55 +00:00
|
|
|
#include "tgttrans.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-08-23 06:45:55 +00:00
|
|
|
/* cc65 */
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "datatype.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "function.h"
|
|
|
|
#include "global.h"
|
2002-09-29 21:09:47 +00:00
|
|
|
#include "hexval.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "ident.h"
|
2000-06-14 21:01:37 +00:00
|
|
|
#include "input.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "litpool.h"
|
|
|
|
#include "preproc.h"
|
2004-06-21 22:22:11 +00:00
|
|
|
#include "scanner.h"
|
|
|
|
#include "standard.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "symtab.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2004-06-21 22:30:07 +00:00
|
|
|
/* data */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Token CurTok; /* The current token */
|
|
|
|
Token NextTok; /* The next token */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Token types */
|
2004-06-21 22:22:11 +00:00
|
|
|
enum {
|
|
|
|
TT_C89 = 0x01 << STD_C89, /* Token valid in C89 */
|
|
|
|
TT_C99 = 0x01 << STD_C99, /* Token valid in C99 */
|
|
|
|
TT_CC65 = 0x01 << STD_CC65 /* Token valid in cc65 */
|
|
|
|
};
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Token table */
|
2000-06-21 21:02:44 +00:00
|
|
|
static const struct Keyword {
|
2000-05-28 13:40:48 +00:00
|
|
|
char* Key; /* Keyword name */
|
|
|
|
unsigned char Tok; /* The token */
|
2004-06-21 22:22:11 +00:00
|
|
|
unsigned char Std; /* Token supported in which standards? */
|
2000-05-28 13:40:48 +00:00
|
|
|
} Keywords [] = {
|
2004-06-21 22:30:07 +00:00
|
|
|
{ "_Pragma", TOK_PRAGMA, TT_C89 | TT_C99 | TT_CC65 }, /* !! */
|
2004-06-21 22:22:11 +00:00
|
|
|
{ "__AX__", TOK_AX, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "__A__", TOK_A, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "__EAX__", TOK_EAX, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "__X__", TOK_X, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "__Y__", TOK_Y, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "__asm__", TOK_ASM, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "__attribute__", TOK_ATTRIBUTE, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "__far__", TOK_FAR, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "__fastcall__", TOK_FASTCALL, TT_C89 | TT_C99 | TT_CC65 },
|
2006-02-17 20:27:51 +00:00
|
|
|
{ "__inline__", TOK_INLINE, TT_C89 | TT_C99 | TT_CC65 },
|
2004-06-21 22:22:11 +00:00
|
|
|
{ "__near__", TOK_NEAR, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "asm", TOK_ASM, TT_CC65 },
|
|
|
|
{ "auto", TOK_AUTO, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "break", TOK_BREAK, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "case", TOK_CASE, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "char", TOK_CHAR, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "const", TOK_CONST, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "continue", TOK_CONTINUE, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "default", TOK_DEFAULT, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "do", TOK_DO, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "double", TOK_DOUBLE, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "else", TOK_ELSE, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "enum", TOK_ENUM, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "extern", TOK_EXTERN, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "far", TOK_FAR, TT_CC65 },
|
|
|
|
{ "fastcall", TOK_FASTCALL, TT_CC65 },
|
|
|
|
{ "float", TOK_FLOAT, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "for", TOK_FOR, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "goto", TOK_GOTO, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "if", TOK_IF, TT_C89 | TT_C99 | TT_CC65 },
|
2006-02-17 20:27:51 +00:00
|
|
|
{ "inline", TOK_INLINE, TT_C99 | TT_CC65 },
|
2004-06-21 22:22:11 +00:00
|
|
|
{ "int", TOK_INT, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "long", TOK_LONG, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "near", TOK_NEAR, TT_CC65 },
|
|
|
|
{ "register", TOK_REGISTER, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "restrict", TOK_RESTRICT, TT_C99 | TT_CC65 },
|
|
|
|
{ "return", TOK_RETURN, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "short", TOK_SHORT, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "signed", TOK_SIGNED, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "sizeof", TOK_SIZEOF, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "static", TOK_STATIC, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "struct", TOK_STRUCT, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "switch", TOK_SWITCH, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "typedef", TOK_TYPEDEF, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "union", TOK_UNION, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "unsigned", TOK_UNSIGNED, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "void", TOK_VOID, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "volatile", TOK_VOLATILE, TT_C89 | TT_C99 | TT_CC65 },
|
|
|
|
{ "while", TOK_WHILE, TT_C89 | TT_C99 | TT_CC65 },
|
2003-08-02 09:55:54 +00:00
|
|
|
};
|
2000-05-28 13:40:48 +00:00
|
|
|
#define KEY_COUNT (sizeof (Keywords) / sizeof (Keywords [0]))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Stuff for determining the type of an integer constant */
|
|
|
|
#define IT_INT 0x01
|
|
|
|
#define IT_UINT 0x02
|
|
|
|
#define IT_LONG 0x04
|
|
|
|
#define IT_ULONG 0x08
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2004-06-21 22:22:11 +00:00
|
|
|
/* code */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int CmpKey (const void* Key, const void* Elem)
|
|
|
|
/* Compare function for bsearch */
|
|
|
|
{
|
|
|
|
return strcmp ((const char*) Key, ((const struct Keyword*) Elem)->Key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-06-21 22:22:11 +00:00
|
|
|
static token_t FindKey (const char* Key)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Find a keyword and return the token. Return IDENT if the token is not a
|
|
|
|
* keyword.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
struct Keyword* K;
|
|
|
|
K = bsearch (Key, Keywords, KEY_COUNT, sizeof (Keywords [0]), CmpKey);
|
2004-06-21 22:22:11 +00:00
|
|
|
if (K && (K->Std & (0x01 << IS_Get (&Standard))) != 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
return K->Tok;
|
|
|
|
} else {
|
2000-06-12 18:31:40 +00:00
|
|
|
return TOK_IDENT;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-08-14 22:18:26 +00:00
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static int SkipWhite (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Skip white space in the input stream, reading and preprocessing new lines
|
|
|
|
* if necessary. Return 0 if end of file is reached, return 1 otherwise.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
while (1) {
|
2004-09-16 12:42:41 +00:00
|
|
|
while (CurC == '\0') {
|
2000-06-14 08:18:19 +00:00
|
|
|
if (NextLine () == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
Preprocess ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2001-06-24 16:08:35 +00:00
|
|
|
if (IsSpace (CurC)) {
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 15:53:11 +00:00
|
|
|
int TokIsFuncSpec (const Token* T)
|
|
|
|
/* Return true if the token is a function specifier */
|
|
|
|
{
|
|
|
|
return (T->Tok == TOK_INLINE) || (T->Tok == TOK_FASTCALL) ||
|
|
|
|
(T->Tok == TOK_NEAR) || (T->Tok == TOK_FAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-09-16 12:42:41 +00:00
|
|
|
void SymName (char* S)
|
|
|
|
/* Read a symbol from the input stream. The first character must have been
|
|
|
|
* checked before calling this function. The buffer is expected to be at
|
|
|
|
* least of size MAX_IDENTLEN+1.
|
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2004-09-16 12:42:41 +00:00
|
|
|
unsigned Len = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
do {
|
2004-09-16 12:42:41 +00:00
|
|
|
if (Len < MAX_IDENTLEN) {
|
|
|
|
++Len;
|
|
|
|
*S++ = CurC;
|
|
|
|
}
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2001-01-05 19:24:47 +00:00
|
|
|
} while (IsIdent (CurC) || IsDigit (CurC));
|
2004-09-16 12:42:41 +00:00
|
|
|
*S = '\0';
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-09-16 12:42:41 +00:00
|
|
|
int IsSym (char* S)
|
|
|
|
/* If a symbol follows, read it and return 1, otherwise return 0 */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
if (IsIdent (CurC)) {
|
2004-09-16 12:42:41 +00:00
|
|
|
SymName (S);
|
2000-05-28 13:40:48 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-22 09:32:24 +00:00
|
|
|
static void UnknownChar (char C)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Error message for unknown character */
|
|
|
|
{
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Invalid input character with code %02X", C & 0xFF);
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar (); /* Skip */
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void SetTok (int tok)
|
2001-05-22 09:32:24 +00:00
|
|
|
/* Set NextTok.Tok and bump line ptr */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = tok;
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
static int ParseChar (void)
|
2004-09-16 12:42:41 +00:00
|
|
|
/* Parse a character. Converts escape chars into character codes. */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
int C;
|
2004-10-09 14:32:27 +00:00
|
|
|
int HadError;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Check for escape chars */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (CurC == '\\') {
|
|
|
|
NextChar ();
|
|
|
|
switch (CurC) {
|
2004-03-02 19:42:19 +00:00
|
|
|
case '?':
|
|
|
|
C = '\?';
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
C = '\a';
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
case 'b':
|
2000-06-21 21:02:44 +00:00
|
|
|
C = '\b';
|
2003-08-29 09:16:05 +00:00
|
|
|
break;
|
2000-06-21 21:02:44 +00:00
|
|
|
case 'f':
|
2004-06-21 22:22:11 +00:00
|
|
|
C = '\f';
|
2003-08-29 09:16:05 +00:00
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
case 'r':
|
2003-08-29 09:16:05 +00:00
|
|
|
C = '\r';
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
case 'n':
|
2003-08-29 09:16:05 +00:00
|
|
|
C = '\n';
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
case 't':
|
2003-08-29 09:16:05 +00:00
|
|
|
C = '\t';
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
C = '\v';
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
case '\"':
|
2003-08-29 09:16:05 +00:00
|
|
|
C = '\"';
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
case '\'':
|
2003-08-29 09:16:05 +00:00
|
|
|
C = '\'';
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
case '\\':
|
2003-08-29 09:16:05 +00:00
|
|
|
C = '\\';
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
case 'x':
|
|
|
|
case 'X':
|
2003-08-29 09:16:05 +00:00
|
|
|
/* Hex character constant */
|
2004-10-09 14:32:27 +00:00
|
|
|
if (!IsXDigit (NextC)) {
|
2004-09-16 12:42:41 +00:00
|
|
|
Error ("\\x used with no following hex digits");
|
|
|
|
C = ' ';
|
2004-10-09 14:32:27 +00:00
|
|
|
} else {
|
|
|
|
HadError = 0;
|
|
|
|
C = 0;
|
|
|
|
while (IsXDigit (NextC)) {
|
|
|
|
if ((C << 4) >= 256) {
|
|
|
|
if (!HadError) {
|
|
|
|
Error ("Hex character constant out of range");
|
|
|
|
HadError = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
C = (C << 4) | HexVal (NextC);
|
|
|
|
}
|
|
|
|
NextChar ();
|
2004-09-16 12:42:41 +00:00
|
|
|
}
|
|
|
|
}
|
2003-08-29 09:16:05 +00:00
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
case '0':
|
|
|
|
case '1':
|
2003-06-27 08:10:08 +00:00
|
|
|
case '2':
|
|
|
|
case '3':
|
2003-08-29 09:16:05 +00:00
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
case '6':
|
|
|
|
case '7':
|
2004-10-09 14:32:27 +00:00
|
|
|
/* Octal constant */
|
|
|
|
HadError = 0;
|
|
|
|
C = HexVal (CurC);
|
|
|
|
while (IsODigit (NextC)) {
|
|
|
|
if ((C << 3) >= 256) {
|
|
|
|
if (!HadError) {
|
|
|
|
Error ("Octal character constant out of range");
|
|
|
|
HadError = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
C = (C << 3) | HexVal (NextC);
|
|
|
|
}
|
|
|
|
NextChar ();
|
|
|
|
}
|
|
|
|
break;
|
2000-06-21 21:02:44 +00:00
|
|
|
default:
|
2004-06-21 22:22:11 +00:00
|
|
|
Error ("Illegal character constant");
|
2004-10-09 14:32:27 +00:00
|
|
|
C = ' ';
|
2003-08-29 09:16:05 +00:00
|
|
|
/* Try to do error recovery, otherwise the compiler will spit
|
|
|
|
* out thousands of errors in this place and abort.
|
|
|
|
*/
|
|
|
|
if (CurC != '\'' && CurC != '\0') {
|
|
|
|
while (NextC != '\'' && NextC != '\"' && NextC != '\0') {
|
|
|
|
NextChar ();
|
|
|
|
}
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
2000-06-21 21:02:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
C = CurC;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2000-06-21 21:02:44 +00:00
|
|
|
/* Skip the character read */
|
|
|
|
NextChar ();
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Do correct sign extension */
|
2000-06-21 21:02:44 +00:00
|
|
|
return SignExtendChar (C);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void CharConst (void)
|
|
|
|
/* Parse a character constant. */
|
|
|
|
{
|
2000-06-21 21:02:44 +00:00
|
|
|
int C;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Skip the quote */
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Get character */
|
2000-06-21 21:02:44 +00:00
|
|
|
C = ParseChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Check for closing quote */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (CurC != '\'') {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("`\'' expected");
|
2000-06-21 21:02:44 +00:00
|
|
|
} else {
|
|
|
|
/* Skip the quote */
|
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup values and attributes */
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_CCONST;
|
2000-08-23 06:45:55 +00:00
|
|
|
|
|
|
|
/* Translate into target charset */
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.IVal = SignExtendChar (TgtTranslateChar (C));
|
2000-08-23 06:45:55 +00:00
|
|
|
|
|
|
|
/* Character constants have type int */
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Type = type_int;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void StringConst (void)
|
|
|
|
/* Parse a quoted string */
|
2009-12-04 14:12:25 +00:00
|
|
|
{
|
|
|
|
/* String buffer */
|
|
|
|
StrBuf S = AUTO_STRBUF_INITIALIZER;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-09-18 14:28:52 +00:00
|
|
|
/* Concatenate strings. If at least one of the concenated strings is a wide
|
|
|
|
* character literal, the whole string is a wide char literal, otherwise
|
|
|
|
* it's a normal string literal.
|
|
|
|
*/
|
|
|
|
while (1) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-09-18 14:28:52 +00:00
|
|
|
/* Check if this is a normal or a wide char string */
|
|
|
|
if (CurC == 'L' && NextC == '\"') {
|
|
|
|
/* Wide character literal */
|
|
|
|
NextTok.Tok = TOK_WCSCONST;
|
|
|
|
NextChar ();
|
|
|
|
NextChar ();
|
|
|
|
} else if (CurC == '\"') {
|
|
|
|
/* Skip the quote char */
|
|
|
|
NextChar ();
|
|
|
|
} else {
|
|
|
|
/* No string */
|
|
|
|
break;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-09-18 14:28:52 +00:00
|
|
|
/* Read until end of string */
|
|
|
|
while (CurC != '\"') {
|
|
|
|
if (CurC == '\0') {
|
|
|
|
Error ("Unexpected newline");
|
|
|
|
break;
|
|
|
|
}
|
2009-12-04 14:12:25 +00:00
|
|
|
SB_AppendChar (&S, ParseChar ());
|
2009-09-18 14:28:52 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2009-09-18 14:28:52 +00:00
|
|
|
/* Skip closing quote char if there was one */
|
2004-06-06 15:59:44 +00:00
|
|
|
NextChar ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Skip white space, read new input */
|
2000-06-21 21:02:44 +00:00
|
|
|
SkipWhite ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the string */
|
2009-12-04 14:12:25 +00:00
|
|
|
SB_AppendChar (&S, '\0');
|
|
|
|
|
|
|
|
/* Add the whole string to the literal pool */
|
|
|
|
NextTok.IVal = AddLiteralStr (&S);
|
|
|
|
NextTok.Tok = TOK_SCONST;
|
|
|
|
|
|
|
|
/* Free the buffer */
|
|
|
|
SB_Done (&S);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-06-06 15:59:44 +00:00
|
|
|
static void NumericConst (void)
|
|
|
|
/* Parse a numeric constant */
|
|
|
|
{
|
|
|
|
unsigned Base; /* Temporary number base */
|
|
|
|
unsigned Prefix; /* Base according to prefix */
|
2008-03-31 20:54:45 +00:00
|
|
|
StrBuf S = STATIC_STRBUF_INITIALIZER;
|
2004-06-06 15:59:44 +00:00
|
|
|
int IsFloat;
|
|
|
|
char C;
|
|
|
|
unsigned DigitVal;
|
|
|
|
unsigned long IVal; /* Value */
|
|
|
|
|
|
|
|
/* Check for a leading hex or octal prefix and determine the possible
|
|
|
|
* integer types.
|
|
|
|
*/
|
|
|
|
if (CurC == '0') {
|
|
|
|
/* Gobble 0 and examine next char */
|
|
|
|
NextChar ();
|
|
|
|
if (toupper (CurC) == 'X') {
|
|
|
|
Base = Prefix = 16;
|
|
|
|
NextChar (); /* gobble "x" */
|
|
|
|
} else {
|
|
|
|
Base = 10; /* Assume 10 for now - see below */
|
|
|
|
Prefix = 8; /* Actual prefix says octal */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Base = Prefix = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Because floating point numbers don't have octal prefixes (a number
|
|
|
|
* with a leading zero is decimal), we first have to read the number
|
|
|
|
* before converting it, so we can determine if it's a float or an
|
|
|
|
* integer.
|
|
|
|
*/
|
|
|
|
while (IsXDigit (CurC) && HexVal (CurC) < Base) {
|
|
|
|
SB_AppendChar (&S, CurC);
|
|
|
|
NextChar ();
|
|
|
|
}
|
|
|
|
SB_Terminate (&S);
|
|
|
|
|
|
|
|
/* The following character tells us if we have an integer or floating
|
2004-06-21 22:22:11 +00:00
|
|
|
* point constant. Note: Hexadecimal floating point constants aren't
|
|
|
|
* supported in C89.
|
2004-06-06 15:59:44 +00:00
|
|
|
*/
|
|
|
|
IsFloat = (CurC == '.' ||
|
|
|
|
(Base == 10 && toupper (CurC) == 'E') ||
|
2004-06-21 22:22:11 +00:00
|
|
|
(Base == 16 && toupper (CurC) == 'P' && IS_Get (&Standard) >= STD_C99));
|
2004-06-06 15:59:44 +00:00
|
|
|
|
|
|
|
/* If we don't have a floating point type, an octal prefix results in an
|
|
|
|
* octal base.
|
|
|
|
*/
|
|
|
|
if (!IsFloat && Prefix == 8) {
|
|
|
|
Base = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Since we do now know the correct base, convert the remembered input
|
|
|
|
* into a number.
|
|
|
|
*/
|
|
|
|
SB_Reset (&S);
|
|
|
|
IVal = 0;
|
|
|
|
while ((C = SB_Get (&S)) != '\0') {
|
|
|
|
DigitVal = HexVal (C);
|
|
|
|
if (DigitVal >= Base) {
|
|
|
|
Error ("Numeric constant contains digits beyond the radix");
|
|
|
|
}
|
|
|
|
IVal = (IVal * Base) + DigitVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't need the string buffer any longer */
|
2008-03-31 20:54:45 +00:00
|
|
|
SB_Done (&S);
|
2004-06-06 15:59:44 +00:00
|
|
|
|
|
|
|
/* Distinguish between integer and floating point constants */
|
|
|
|
if (!IsFloat) {
|
|
|
|
|
|
|
|
unsigned Types;
|
|
|
|
int HaveSuffix;
|
|
|
|
|
|
|
|
/* Check for a suffix and determine the possible types */
|
|
|
|
HaveSuffix = 1;
|
|
|
|
if (toupper (CurC) == 'U') {
|
|
|
|
/* Unsigned type */
|
|
|
|
NextChar ();
|
|
|
|
if (toupper (CurC) != 'L') {
|
|
|
|
Types = IT_UINT | IT_ULONG;
|
|
|
|
} else {
|
|
|
|
NextChar ();
|
|
|
|
Types = IT_ULONG;
|
|
|
|
}
|
|
|
|
} else if (toupper (CurC) == 'L') {
|
|
|
|
/* Long type */
|
|
|
|
NextChar ();
|
|
|
|
if (toupper (CurC) != 'U') {
|
|
|
|
Types = IT_LONG | IT_ULONG;
|
|
|
|
} else {
|
|
|
|
NextChar ();
|
|
|
|
Types = IT_ULONG;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
HaveSuffix = 0;
|
|
|
|
if (Prefix == 10) {
|
|
|
|
/* Decimal constants are of any type but uint */
|
|
|
|
Types = IT_INT | IT_LONG | IT_ULONG;
|
|
|
|
} else {
|
|
|
|
/* Octal or hex constants are of any type */
|
|
|
|
Types = IT_INT | IT_UINT | IT_LONG | IT_ULONG;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check the range to determine the type */
|
|
|
|
if (IVal > 0x7FFF) {
|
|
|
|
/* Out of range for int */
|
|
|
|
Types &= ~IT_INT;
|
|
|
|
/* If the value is in the range 0x8000..0xFFFF, unsigned int is not
|
|
|
|
* allowed, and we don't have a type specifying suffix, emit a
|
|
|
|
* warning, because the constant is of type long.
|
|
|
|
*/
|
|
|
|
if (IVal <= 0xFFFF && (Types & IT_UINT) == 0 && !HaveSuffix) {
|
|
|
|
Warning ("Constant is long");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (IVal > 0xFFFF) {
|
|
|
|
/* Out of range for unsigned int */
|
|
|
|
Types &= ~IT_UINT;
|
|
|
|
}
|
|
|
|
if (IVal > 0x7FFFFFFF) {
|
|
|
|
/* Out of range for long int */
|
|
|
|
Types &= ~IT_LONG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now set the type string to the smallest type in types */
|
|
|
|
if (Types & IT_INT) {
|
|
|
|
NextTok.Type = type_int;
|
|
|
|
} else if (Types & IT_UINT) {
|
|
|
|
NextTok.Type = type_uint;
|
|
|
|
} else if (Types & IT_LONG) {
|
|
|
|
NextTok.Type = type_long;
|
|
|
|
} else {
|
|
|
|
NextTok.Type = type_ulong;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the value and the token */
|
|
|
|
NextTok.IVal = IVal;
|
|
|
|
NextTok.Tok = TOK_ICONST;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Float constant */
|
2008-08-11 17:51:00 +00:00
|
|
|
Double FVal = FP_D_FromInt (IVal); /* Convert to double */
|
2004-06-06 15:59:44 +00:00
|
|
|
|
|
|
|
/* Check for a fractional part and read it */
|
|
|
|
if (CurC == '.') {
|
|
|
|
|
2008-08-11 17:51:00 +00:00
|
|
|
Double Scale;
|
2004-06-06 15:59:44 +00:00
|
|
|
|
|
|
|
/* Skip the dot */
|
|
|
|
NextChar ();
|
|
|
|
|
2008-08-11 17:51:00 +00:00
|
|
|
/* Read fractional digits */
|
|
|
|
Scale = FP_D_Make (1.0);
|
2004-06-06 15:59:44 +00:00
|
|
|
while (IsXDigit (CurC) && (DigitVal = HexVal (CurC)) < Base) {
|
2008-08-11 17:51:00 +00:00
|
|
|
/* Get the value of this digit */
|
|
|
|
Double FracVal = FP_D_Div (FP_D_FromInt (DigitVal * Base), Scale);
|
|
|
|
/* Add it to the float value */
|
|
|
|
FVal = FP_D_Add (FVal, FracVal);
|
|
|
|
/* Scale base */
|
|
|
|
Scale = FP_D_Mul (Scale, FP_D_FromInt (DigitVal));
|
|
|
|
/* Skip the digit */
|
2004-06-06 15:59:44 +00:00
|
|
|
NextChar ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for an exponent and read it */
|
|
|
|
if ((Base == 16 && toupper (CurC) == 'F') ||
|
|
|
|
(Base == 10 && toupper (CurC) == 'E')) {
|
|
|
|
|
|
|
|
int Sign;
|
|
|
|
unsigned Digits;
|
|
|
|
unsigned Exp;
|
|
|
|
|
|
|
|
/* Skip the exponent notifier */
|
|
|
|
NextChar ();
|
|
|
|
|
|
|
|
/* Read an optional sign */
|
|
|
|
Sign = 1;
|
|
|
|
if (CurC == '-') {
|
|
|
|
Sign = -1;
|
|
|
|
NextChar ();
|
2008-03-31 21:17:27 +00:00
|
|
|
} else if (CurC == '+') {
|
|
|
|
NextChar ();
|
2004-06-06 15:59:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read exponent digits. Since we support only 32 bit floats
|
|
|
|
* with a maximum exponent of +-/127, we read the exponent
|
|
|
|
* part as integer with up to 3 digits and drop the remainder.
|
|
|
|
* This avoids an overflow of Exp. The exponent is always
|
|
|
|
* decimal, even for hex float consts.
|
|
|
|
*/
|
|
|
|
Digits = 0;
|
|
|
|
Exp = 0;
|
|
|
|
while (IsDigit (CurC)) {
|
|
|
|
if (++Digits <= 3) {
|
|
|
|
Exp = Exp * 10 + HexVal (CurC);
|
|
|
|
}
|
|
|
|
NextChar ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for errors: We must have exponent digits, and not more
|
|
|
|
* than three.
|
|
|
|
*/
|
|
|
|
if (Digits == 0) {
|
|
|
|
Error ("Floating constant exponent has no digits");
|
|
|
|
} else if (Digits > 3) {
|
|
|
|
Warning ("Floating constant exponent is too large");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Scale the exponent and adjust the value accordingly */
|
|
|
|
if (Exp) {
|
2008-08-11 17:51:00 +00:00
|
|
|
FVal = FP_D_Mul (FVal, FP_D_Make (pow (10, Exp)));
|
2004-06-06 15:59:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for a suffix and determine the type of the constant */
|
|
|
|
if (toupper (CurC) == 'F') {
|
|
|
|
NextChar ();
|
|
|
|
NextTok.Type = type_float;
|
|
|
|
} else {
|
|
|
|
NextTok.Type = type_double;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the value and the token */
|
|
|
|
NextTok.FVal = FVal;
|
|
|
|
NextTok.Tok = TOK_FCONST;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
void NextToken (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Get next token from input stream */
|
2003-05-07 21:06:42 +00:00
|
|
|
{
|
2000-05-28 13:40:48 +00:00
|
|
|
ident token;
|
|
|
|
|
2001-06-24 16:08:35 +00:00
|
|
|
/* We have to skip white space here before shifting tokens, since the
|
2001-05-22 09:32:24 +00:00
|
|
|
* tokens and the current line info is invalid at startup and will get
|
|
|
|
* initialized by reading the first time from the file. Remember if
|
|
|
|
* we were at end of input and handle that later.
|
|
|
|
*/
|
|
|
|
int GotEOF = (SkipWhite() == 0);
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Current token is the lookahead token */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.LI) {
|
|
|
|
ReleaseLineInfo (CurTok.LI);
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
CurTok = NextTok;
|
|
|
|
|
2003-08-19 20:30:55 +00:00
|
|
|
/* When reading the first time from the file, the line info in NextTok,
|
|
|
|
* which was copied to CurTok is invalid. Since the information from
|
|
|
|
* the token is used for error messages, we must make it valid.
|
|
|
|
*/
|
|
|
|
if (CurTok.LI == 0) {
|
|
|
|
CurTok.LI = UseLineInfo (GetCurLineInfo ());
|
|
|
|
}
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Remember the starting position of the next token */
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.LI = UseLineInfo (GetCurLineInfo ());
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2001-05-22 09:32:24 +00:00
|
|
|
/* Now handle end of input. */
|
2001-06-24 16:08:35 +00:00
|
|
|
if (GotEOF) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* End of file reached */
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_CEOF;
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Determine the next token from the lookahead */
|
2004-06-06 18:36:08 +00:00
|
|
|
if (IsDigit (CurC) || (CurC == '.' && IsDigit (NextC))) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* A number */
|
2004-06-06 15:59:44 +00:00
|
|
|
NumericConst ();
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-18 14:28:52 +00:00
|
|
|
/* Check for wide character literals */
|
|
|
|
if (CurC == 'L' && NextC == '\"') {
|
|
|
|
StringConst ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for keywords and identifiers */
|
2000-06-21 21:02:44 +00:00
|
|
|
if (IsSym (token)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Check for a keyword */
|
2001-05-22 09:32:24 +00:00
|
|
|
if ((NextTok.Tok = FindKey (token)) != TOK_IDENT) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Reserved word found */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* No reserved word, check for special symbols */
|
2004-06-21 22:22:11 +00:00
|
|
|
if (token[0] == '_' && token[1] == '_') {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Special symbols */
|
2004-06-21 22:22:11 +00:00
|
|
|
if (strcmp (token+2, "FILE__") == 0) {
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.IVal = AddLiteral (GetCurrentFile());
|
|
|
|
NextTok.Tok = TOK_SCONST;
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
2004-06-21 22:22:11 +00:00
|
|
|
} else if (strcmp (token+2, "LINE__") == 0) {
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_ICONST;
|
|
|
|
NextTok.IVal = GetCurrentLine();
|
|
|
|
NextTok.Type = type_int;
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
2004-06-21 22:22:11 +00:00
|
|
|
} else if (strcmp (token+2, "func__") == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* __func__ is only defined in functions */
|
|
|
|
if (CurrentFunc) {
|
2002-06-12 12:12:04 +00:00
|
|
|
NextTok.IVal = AddLiteral (F_GetFuncName (CurrentFunc));
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_SCONST;
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* No reserved word but identifier */
|
|
|
|
strcpy (NextTok.Ident, token);
|
2000-06-12 18:31:40 +00:00
|
|
|
NextTok.Tok = TOK_IDENT;
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Monstrous switch statement ahead... */
|
2000-06-21 21:02:44 +00:00
|
|
|
switch (CurC) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
case '!':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
if (CurC == '=') {
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_NE);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_BOOL_NOT;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\"':
|
|
|
|
StringConst ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '%':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
if (CurC == '=') {
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_MOD_ASSIGN);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_MOD;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '&':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
switch (CurC) {
|
2000-05-28 13:40:48 +00:00
|
|
|
case '&':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_BOOL_AND);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
case '=':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_AND_ASSIGN);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_AND;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\'':
|
|
|
|
CharConst ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '(':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_LPAREN);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ')':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_RPAREN);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '*':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
if (CurC == '=') {
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_MUL_ASSIGN);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_STAR;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '+':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
switch (CurC) {
|
2000-05-28 13:40:48 +00:00
|
|
|
case '+':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_INC);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
case '=':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_PLUS_ASSIGN);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_PLUS;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ',':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_COMMA);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '-':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
switch (CurC) {
|
2000-05-28 13:40:48 +00:00
|
|
|
case '-':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_DEC);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
case '=':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_MINUS_ASSIGN);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
case '>':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_PTR_REF);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_MINUS;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2004-06-06 18:36:08 +00:00
|
|
|
case '.':
|
|
|
|
NextChar ();
|
|
|
|
if (CurC == '.') {
|
2004-06-06 15:59:44 +00:00
|
|
|
NextChar ();
|
|
|
|
if (CurC == '.') {
|
2004-06-06 18:36:08 +00:00
|
|
|
SetTok (TOK_ELLIPSIS);
|
2004-06-06 15:59:44 +00:00
|
|
|
} else {
|
2004-06-06 18:36:08 +00:00
|
|
|
UnknownChar (CurC);
|
2004-06-06 15:59:44 +00:00
|
|
|
}
|
2004-06-06 18:36:08 +00:00
|
|
|
} else {
|
|
|
|
NextTok.Tok = TOK_DOT;
|
2004-06-06 15:59:44 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '/':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
if (CurC == '=') {
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_DIV_ASSIGN);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_DIV;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ':':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_COLON);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ';':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_SEMI);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '<':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
switch (CurC) {
|
2000-05-28 13:40:48 +00:00
|
|
|
case '=':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_LE);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
case '<':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
if (CurC == '=') {
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_SHL_ASSIGN);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_SHL;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_LT;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '=':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
if (CurC == '=') {
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_EQ);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_ASSIGN;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '>':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
switch (CurC) {
|
2000-05-28 13:40:48 +00:00
|
|
|
case '=':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_GE);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
case '>':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
if (CurC == '=') {
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_SHR_ASSIGN);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_SHR;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_GT;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '?':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_QUEST);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '[':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_LBRACK);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ']':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_RBRACK);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '^':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
if (CurC == '=') {
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_XOR_ASSIGN);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_XOR;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '{':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_LCURLY);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '|':
|
2000-06-21 21:02:44 +00:00
|
|
|
NextChar ();
|
|
|
|
switch (CurC) {
|
2000-05-28 13:40:48 +00:00
|
|
|
case '|':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_BOOL_OR);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
case '=':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_OR_ASSIGN);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
default:
|
2001-05-22 09:32:24 +00:00
|
|
|
NextTok.Tok = TOK_OR;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '}':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_RCURLY);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '~':
|
2000-06-12 18:31:40 +00:00
|
|
|
SetTok (TOK_COMP);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2001-05-22 09:32:24 +00:00
|
|
|
UnknownChar (CurC);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-09-09 20:49:20 +00:00
|
|
|
void SkipTokens (const token_t* TokenList, unsigned TokenCount)
|
|
|
|
/* Skip tokens until we reach TOK_CEOF or a token in the given token list.
|
|
|
|
* This routine is used for error recovery.
|
|
|
|
*/
|
2002-06-12 12:12:04 +00:00
|
|
|
{
|
2001-09-09 20:49:20 +00:00
|
|
|
while (CurTok.Tok != TOK_CEOF) {
|
|
|
|
|
|
|
|
/* Check if the current token is in the token list */
|
|
|
|
unsigned I;
|
|
|
|
for (I = 0; I < TokenCount; ++I) {
|
|
|
|
if (CurTok.Tok == TokenList[I]) {
|
|
|
|
/* Found a token in the list */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not in the list: Skip it */
|
|
|
|
NextToken ();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
int Consume (token_t Token, const char* ErrorMsg)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Eat token if it is the next in the input stream, otherwise print an error
|
2002-09-29 21:09:47 +00:00
|
|
|
* message. Returns true if the token was found and false otherwise.
|
2000-05-28 13:40:48 +00:00
|
|
|
*/
|
|
|
|
{
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == Token) {
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2002-09-29 21:09:47 +00:00
|
|
|
return 1;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2009-09-28 15:59:18 +00:00
|
|
|
Error ("%s", ErrorMsg);
|
2002-09-29 21:09:47 +00:00
|
|
|
return 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
int ConsumeColon (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Check for a colon and skip it. */
|
|
|
|
{
|
2002-09-29 21:09:47 +00:00
|
|
|
return Consume (TOK_COLON, "`:' expected");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
int ConsumeSemi (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Check for a semicolon and skip it. */
|
|
|
|
{
|
|
|
|
/* Try do be smart about typos... */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_SEMI) {
|
2002-09-29 21:09:47 +00:00
|
|
|
NextToken ();
|
|
|
|
return 1;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("`;' expected");
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_COLON || CurTok.Tok == TOK_COMMA) {
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2002-09-29 21:09:47 +00:00
|
|
|
return 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
int ConsumeComma (void)
|
2000-09-14 19:26:13 +00:00
|
|
|
/* Check for a comma and skip it. */
|
|
|
|
{
|
|
|
|
/* Try do be smart about typos... */
|
|
|
|
if (CurTok.Tok == TOK_COMMA) {
|
2002-09-29 21:09:47 +00:00
|
|
|
NextToken ();
|
|
|
|
return 1;
|
2000-09-14 19:26:13 +00:00
|
|
|
} else {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("`,' expected");
|
2000-09-14 19:26:13 +00:00
|
|
|
if (CurTok.Tok == TOK_SEMI) {
|
|
|
|
NextToken ();
|
|
|
|
}
|
2002-09-29 21:09:47 +00:00
|
|
|
return 0;
|
2000-09-14 19:26:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
int ConsumeLParen (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Check for a left parenthesis and skip it */
|
|
|
|
{
|
2002-09-29 21:09:47 +00:00
|
|
|
return Consume (TOK_LPAREN, "`(' expected");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
int ConsumeRParen (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Check for a right parenthesis and skip it */
|
|
|
|
{
|
2002-09-29 21:09:47 +00:00
|
|
|
return Consume (TOK_RPAREN, "`)' expected");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
int ConsumeLBrack (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Check for a left bracket and skip it */
|
|
|
|
{
|
2002-09-29 21:09:47 +00:00
|
|
|
return Consume (TOK_LBRACK, "`[' expected");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
int ConsumeRBrack (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Check for a right bracket and skip it */
|
|
|
|
{
|
2002-09-29 21:09:47 +00:00
|
|
|
return Consume (TOK_RBRACK, "`]' expected");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
int ConsumeLCurly (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Check for a left curly brace and skip it */
|
|
|
|
{
|
2002-09-29 21:09:47 +00:00
|
|
|
return Consume (TOK_LCURLY, "`{' expected");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-09-29 21:09:47 +00:00
|
|
|
int ConsumeRCurly (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Check for a right curly brace and skip it */
|
|
|
|
{
|
2002-09-29 21:09:47 +00:00
|
|
|
return Consume (TOK_RCURLY, "`}' expected");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|