1 //**************************************************************************
2 //**
3 //**	##   ##    ##    ##   ##   ####     ####   ###     ###
4 //**	##   ##  ##  ##  ##   ##  ##  ##   ##  ##  ####   ####
5 //**	 ## ##  ##    ##  ## ##  ##    ## ##    ## ## ## ## ##
6 //**	 ## ##  ########  ## ##  ##    ## ##    ## ##  ###  ##
7 //**	  ###   ##    ##   ###    ##  ##   ##  ##  ##       ##
8 //**	   #    ##    ##    #      ####     ####   ##       ##
9 //**
10 //**	$Id: vc_lexer.h 3765 2008-09-09 21:34:40Z dj_jl $
11 //**
12 //**	Copyright (C) 1999-2006 Jānis Legzdiņš
13 //**
14 //**	This program is free software; you can redistribute it and/or
15 //**  modify it under the terms of the GNU General Public License
16 //**  as published by the Free Software Foundation; either version 2
17 //**  of the License, or (at your option) any later version.
18 //**
19 //**	This program is distributed in the hope that it will be useful,
20 //**  but WITHOUT ANY WARRANTY; without even the implied warranty of
21 //**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22 //**  GNU General Public License for more details.
23 //**
24 //**************************************************************************
25 
26 //==========================================================================
27 //
28 //	EToken
29 //
30 //	Token types.
31 //
32 //==========================================================================
33 
34 enum EToken
35 {
36 	TK_NoToken,
37 	TK_EOF,				//	Reached end of file
38 	TK_Identifier, 		//	Identifier, value: tk_String
39 	TK_NameLiteral,		//	Name constant, value: tk_Name
40 	TK_StringLiteral,	//	String, value: tk_String
41 	TK_IntLiteral,		//	Integer number, value: tk_Number
42 	TK_FloatLiteral,	//	Floating number, value: tk_Float
43 
44 	//	Keywords
45 	TK_Abstract,
46 	TK_Array,
47 	TK_Bool,
48 	TK_Break,
49 	TK_Byte,
50 	TK_Case,
51 	TK_Class,
52 	TK_Const,
53 	TK_Continue,
54 	TK_Decorate,
55 	TK_Default,
56 	TK_DefaultProperties,
57 	TK_Delegate,
58 	TK_Do,
59 	TK_Else,
60 	TK_Enum,
61 	TK_False,
62 	TK_Final,
63 	TK_Float,
64 	TK_For,
65 	TK_Foreach,
66 	TK_Game,
67 	TK_Get,
68 	TK_If,
69 	TK_Import,
70 	TK_Int,
71 	TK_Iterator,
72 	TK_Name,
73 	TK_Native,
74 	TK_None,
75 	TK_Null,
76 	TK_Optional,
77 	TK_Out,
78 	TK_Private,
79 	TK_ReadOnly,
80 	TK_Reliable,
81 	TK_Replication,
82 	TK_Return,
83 	TK_Self,
84 	TK_Set,
85 	TK_Spawner,
86 	TK_State,
87 	TK_States,
88 	TK_Static,
89 	TK_String,
90 	TK_Struct,
91 	TK_Switch,
92 	TK_Transient,
93 	TK_True,
94 	TK_Unreliable,
95 	TK_Vector,
96 	TK_Void,
97 	TK_While,
98 	TK_MobjInfo,
99 	TK_ScriptId,
100 
101 	//	Punctuation
102 	TK_VarArgs,
103 	TK_LShiftAssign,
104 	TK_RShiftAssign,
105 	TK_AddAssign,
106 	TK_MinusAssign,
107 	TK_MultiplyAssign,
108 	TK_DivideAssign,
109 	TK_ModAssign,
110 	TK_AndAssign,
111 	TK_OrAssign,
112 	TK_XOrAssign,
113 	TK_Equals,
114 	TK_NotEquals,
115 	TK_LessEquals,
116 	TK_GreaterEquals,
117 	TK_AndLog,
118 	TK_OrLog,
119 	TK_LShift,
120 	TK_RShift,
121 	TK_Inc,
122 	TK_Dec,
123 	TK_Arrow,
124 	TK_DColon,
125 	TK_Less,
126 	TK_Greater,
127 	TK_Quest,
128 	TK_And,
129 	TK_Or,
130 	TK_XOr,
131 	TK_Tilde,
132 	TK_Not,
133 	TK_Plus,
134 	TK_Minus,
135 	TK_Asterisk,
136 	TK_Slash,
137 	TK_Percent,
138 	TK_LParen,
139 	TK_RParen,
140 	TK_Dot,
141 	TK_Comma,
142 	TK_Semicolon,
143 	TK_Colon,
144 	TK_Assign,
145 	TK_LBracket,
146 	TK_RBracket,
147 	TK_LBrace,
148 	TK_RBrace,
149 };
150 
151 //==========================================================================
152 //
153 //	VLexer
154 //
155 //	Lexer class.
156 //
157 //==========================================================================
158 
159 class VLexer
160 {
161 private:
162 	enum { MAX_QUOTED_LENGTH = 256 };
163 	enum { MAX_IDENTIFIER_LENGTH = 64 };
164 	enum { EOF_CHARACTER = 127 };
165 	enum { NON_HEX_DIGIT = 255 };
166 
167 	enum
168 	{
169 		CHR_EOF,
170 		CHR_Letter,
171 		CHR_Number,
172 		CHR_Quote,
173 		CHR_SingleQuote,
174 		CHR_Special
175 	};
176 
177 	enum
178 	{
179 		IF_False,		//	Skipping the content
180 		IF_True,		//	Parsing the content
181 		IF_ElseFalse,	//	Else case, skipping content
182 		IF_ElseTrue,	//	Else case, parsing content
183 		IF_Skip,		//	Conditon inside curently skipped code
184 		IF_ElseSkip,	//	Else case inside curently skipped code
185 	};
186 
187 	struct VSourceFile
188 	{
189 		VSourceFile*	Next;	//	Nesting stack
190 		VStr			FileName;
191 		VStr			Path;
192 		char*			FileStart;
193 		char*			FilePtr;
194 		char*			FileEnd;
195 		char			Chr;
196 		TLocation		Loc;
197 		int 			SourceIdx;
198 		int 			Line;
199 		bool			IncLineNumber;
200 		bool			NewLine;
201 		TArray<int>		IfStates;
202 		bool			Skipping;
203 	};
204 
205 	char			ASCIIToChrCode[256];
206 	vuint8			ASCIIToHexDigit[256];
207 	char			TokenStringBuffer[MAX_QUOTED_LENGTH];
208 	bool			SourceOpen;
209 	char			Chr;
210 	TArray<VStr>	Defines;
211 	TArray<VStr>	IncludePath;
212 	VSourceFile*	Src;
213 
214 	void NextChr();
215 	void SkipWhitespaceAndComments();
216 	void ProcessPreprocessor();
217 	void ProcessDefine();
218 	void ProcessIf(bool);
219 	void ProcessElse();
220 	void ProcessEndIf();
221 	void ProcessInclude();
222 	void PushSource(TLocation&, const VStr&);
223 	void PopSource();
224 	void ProcessNumberToken();
225 	void ProcessChar();
226 	void ProcessQuoteToken();
227 	void ProcessSingleQuoteToken();
228 	void ProcessLetterToken(bool);
229 	void ProcessSpecialToken();
230 	void ProcessFileName();
231 
232 public:
233 	EToken				Token;
234 	TLocation			Location;
235 	vint32				Number;
236 	float				Float;
237 	char*				String;
238 	VName				Name;
239 	bool				NewLine;
240 
241 	static const char*	TokenNames[];
242 
243 	VLexer();
244 	~VLexer();
245 	void AddDefine(const VStr&);
246 	void AddIncludePath(const VStr&);
247 	void OpenSource(const VStr&);
248 
249 	void NextToken();
250 	bool Check(EToken);
251 	void Expect(EToken);
252 	void Expect(EToken, ECompileError);
253 };
254