Add names for fallback tokens.

- Fixed: When falling back to the IDENTIFIER token, the identifier it
  would get was undefined, because it never got initialized.
This commit is contained in:
Randy Heit 2013-08-02 21:54:50 -05:00
parent 5cc8a9c2a7
commit 11b588de4a
3 changed files with 146 additions and 118 deletions

View file

@ -575,3 +575,19 @@ xx(DecoCallLineSpecial)
xx(DecoNameToClass)
xx(DecoFindMultiNameState)
xx(DecoFindSingleNameState)
// fallback names for ZScript parser
xx(default)
xx(sbyte)
xx(byte)
xx(short)
xx(ushort)
xx(int)
xx(uint)
xx(bool)
xx(float)
xx(double)
xx(string)
xx(vector)
xx(map)
xx(array)

View file

@ -435,7 +435,7 @@ vector_size(X) ::= LT INTCONST(A) GT.
/* Type names can also be used as identifiers in contexts where type names
* are not normally allowed. */
%fallback IDENTIFIER
SBYTE BYTE SHORT USHORT INT UINT BOOL FLOAT DOUBLE STRING VECTOR NAME.
SBYTE BYTE SHORT USHORT INT UINT BOOL FLOAT DOUBLE STRING VECTOR NAME MAP ARRAY VOID.
/* Aggregate types */
%type aggregate_type {ZCC_Type *}

View file

@ -13,12 +13,22 @@ static FString ZCCTokenName(int terminal);
#include "zcc-parse.h"
#include "zcc-parse.c"
static TMap<SWORD, SWORD> TokenMap;
struct TokenMapEntry
{
SWORD TokenType;
WORD TokenName;
TokenMapEntry(SWORD a, WORD b)
: TokenType(a), TokenName(b)
{ }
};
static TMap<SWORD, TokenMapEntry> TokenMap;
static SWORD BackTokenMap[YYERRORSYMBOL]; // YYERRORSYMBOL immediately follows the terminals described by the grammar
#define TOKENDEF2(sc, zcc, name) { TokenMapEntry tme(zcc, name); TokenMap.Insert(sc, tme); } BackTokenMap[zcc] = sc
#define TOKENDEF(sc, zcc) TOKENDEF2(sc, zcc, NAME_None)
static void InitTokenMap()
{
#define TOKENDEF(sc, zcc) TokenMap.Insert(sc, zcc); BackTokenMap[zcc] = sc
TOKENDEF ('=', ZCC_EQ);
TOKENDEF (TK_MulEq, ZCC_MULEQ);
TOKENDEF (TK_DivEq, ZCC_DIVEQ);
@ -81,20 +91,20 @@ static void InitTokenMap()
TOKENDEF ('}', ZCC_RBRACE);
TOKENDEF (TK_Struct, ZCC_STRUCT);
TOKENDEF (TK_Enum, ZCC_ENUM);
TOKENDEF(TK_SByte, ZCC_SBYTE);
TOKENDEF(TK_Byte, ZCC_BYTE);
TOKENDEF(TK_Short, ZCC_SHORT);
TOKENDEF(TK_UShort, ZCC_USHORT);
TOKENDEF(TK_Int, ZCC_INT);
TOKENDEF(TK_UInt, ZCC_UINT);
TOKENDEF(TK_Bool, ZCC_BOOL);
TOKENDEF(TK_Float, ZCC_FLOAT);
TOKENDEF(TK_Double, ZCC_DOUBLE);
TOKENDEF(TK_String, ZCC_STRING);
TOKENDEF(TK_Vector, ZCC_VECTOR);
TOKENDEF(TK_Name, ZCC_NAME);
TOKENDEF(TK_Map, ZCC_MAP);
TOKENDEF(TK_Array, ZCC_ARRAY);
TOKENDEF2(TK_SByte, ZCC_SBYTE, NAME_sbyte);
TOKENDEF2(TK_Byte, ZCC_BYTE, NAME_byte);
TOKENDEF2(TK_Short, ZCC_SHORT, NAME_short);
TOKENDEF2(TK_UShort, ZCC_USHORT, NAME_ushort);
TOKENDEF2(TK_Int, ZCC_INT, NAME_int);
TOKENDEF2(TK_UInt, ZCC_UINT, NAME_uint);
TOKENDEF2(TK_Bool, ZCC_BOOL, NAME_bool);
TOKENDEF2(TK_Float, ZCC_FLOAT, NAME_float);
TOKENDEF2(TK_Double, ZCC_DOUBLE, NAME_double);
TOKENDEF2(TK_String, ZCC_STRING, NAME_string);
TOKENDEF2(TK_Vector, ZCC_VECTOR, NAME_vector);
TOKENDEF2(TK_Name, ZCC_NAME, NAME_Name);
TOKENDEF2(TK_Map, ZCC_MAP, NAME_map);
TOKENDEF2(TK_Array, ZCC_ARRAY, NAME_array);
TOKENDEF (TK_Void, ZCC_VOID);
TOKENDEF ('[', ZCC_LBRACKET);
TOKENDEF (']', ZCC_RBRACKET);
@ -118,7 +128,7 @@ static void InitTokenMap()
TOKENDEF (TK_Else, ZCC_ELSE);
TOKENDEF (TK_Switch, ZCC_SWITCH);
TOKENDEF (TK_Case, ZCC_CASE);
TOKENDEF(TK_Default, ZCC_DEFAULT);
TOKENDEF2(TK_Default, ZCC_DEFAULT, NAME_default);
TOKENDEF (TK_Const, ZCC_CONST);
TOKENDEF (TK_Stop, ZCC_STOP);
TOKENDEF (TK_Wait, ZCC_WAIT);
@ -132,8 +142,9 @@ static void InitTokenMap()
TOKENDEF (TK_IntConst, ZCC_INTCONST);
TOKENDEF (TK_FloatConst, ZCC_FLOATCONST);
TOKENDEF (TK_NonWhitespace, ZCC_NWS);
#undef TOKENDEF
}
#undef TOKENDEF
#undef TOKENDEF2
static void DoParse(const char *filename)
{
@ -202,10 +213,11 @@ static void DoParse(const char *filename)
}
else
{
SWORD *zcctoken = TokenMap.CheckKey(sc.TokenType);
TokenMapEntry *zcctoken = TokenMap.CheckKey(sc.TokenType);
if (zcctoken != NULL)
{
tokentype = *zcctoken;
tokentype = zcctoken->TokenType;
value.Int = zcctoken->TokenName;
}
else
{