Wed, 12 Feb 2014 06:50:13 +0200
- extended refactor to EToken (now TokenType)
88 | 1 | /* |
2 | Copyright 2012-2014 Santeri Piippo | |
3 | All rights reserved. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions | |
7 | are met: | |
8 | ||
9 | 1. Redistributions of source code must retain the above copyright | |
10 | notice, this list of conditions and the following disclaimer. | |
11 | 2. Redistributions in binary form must reproduce the above copyright | |
12 | notice, this list of conditions and the following disclaimer in the | |
13 | documentation and/or other materials provided with the distribution. | |
14 | 3. The name of the author may not be used to endorse or promote products | |
15 | derived from this software without specific prior written permission. | |
16 | ||
17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
18 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
20 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
22 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
26 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #include <cstring> | |
30 | #include "Lexer.h" | |
31 | ||
32 | static StringList gFileNameStack; | |
33 | static Lexer* gMainLexer = null; | |
34 | ||
35 | // ============================================================================= | |
36 | // | |
37 | Lexer::Lexer() | |
38 | { | |
39 | assert (gMainLexer == null); | |
40 | gMainLexer = this; | |
41 | } | |
42 | ||
43 | // ============================================================================= | |
44 | // | |
45 | Lexer::~Lexer() | |
46 | { | |
47 | gMainLexer = null; | |
48 | } | |
49 | ||
50 | // ============================================================================= | |
51 | // | |
52 | void Lexer::ProcessFile (String fileName) | |
53 | { | |
54 | gFileNameStack << fileName; | |
55 | FILE* fp = fopen (fileName, "r"); | |
56 | ||
57 | if (fp == null) | |
58 | Error ("couldn't open %1 for reading: %2", fileName, strerror (errno)); | |
59 | ||
60 | LexerScanner sc (fp); | |
61 | CheckFileHeader (sc); | |
62 | ||
63 | while (sc.GetNextToken()) | |
64 | { | |
65 | // Preprocessor commands: | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
66 | if (sc.GetTokenType() ==TK_Hash) |
88 | 67 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
68 | MustGetFromScanner (sc,TK_Symbol); |
88 | 69 | |
70 | if (sc.GetTokenText() == "include") | |
71 | { | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
72 | MustGetFromScanner (sc,TK_String); |
88 | 73 | String fileName = sc.GetTokenText(); |
74 | ||
75 | if (gFileNameStack.Contains (fileName)) | |
76 | Error ("attempted to #include %1 recursively", sc.GetTokenText()); | |
77 | ||
78 | ProcessFile (fileName); | |
79 | } | |
80 | else | |
81 | Error ("unknown preprocessor directive \"#%1\"", sc.GetTokenText()); | |
82 | } | |
83 | else | |
84 | { | |
85 | Token tok; | |
86 | tok.file = fileName; | |
87 | tok.line = sc.GetLine(); | |
88 | tok.column = sc.GetColumn(); | |
89 | tok.type = sc.GetTokenType(); | |
90 | tok.text = sc.GetTokenText(); | |
91 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
92 | // devf ("Token #%1: %2:%3:%4: %5 (%6)\n", mTokens.Size(), |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
93 | // tok.file, tok.line, tok.column, DescribeToken (&tok), |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
94 | // GetTokenTypeString (tok.type)); |
88 | 95 | |
96 | mTokens << tok; | |
97 | } | |
98 | } | |
99 | ||
100 | mTokenPosition = mTokens.begin() - 1; | |
101 | gFileNameStack.Remove (fileName); | |
102 | } | |
103 | ||
104 | // ============================================================================ | |
105 | // | |
106 | static bool IsValidHeader (String header) | |
107 | { | |
108 | if (header.EndsWith ("\n")) | |
109 | header.RemoveFromEnd (1); | |
110 | ||
111 | StringList tokens = header.Split (" "); | |
112 | ||
113 | if (tokens.Size() != 2 || tokens[0] != "#!botc" || tokens[1].IsEmpty()) | |
114 | return false; | |
115 | ||
116 | StringList nums = tokens[1].Split ("."); | |
117 | ||
118 | if (nums.Size() == 2) | |
119 | nums << "0"; | |
120 | elif (nums.Size() != 3) | |
121 | return false; | |
122 | ||
123 | bool okA, okB, okC; | |
124 | long major = nums[0].ToLong (&okA); | |
125 | long minor = nums[1].ToLong (&okB); | |
126 | long patch = nums[2].ToLong (&okC); | |
127 | ||
128 | if (!okA || !okB || !okC) | |
129 | return false; | |
130 | ||
131 | if (VERSION_NUMBER < MAKE_VERSION_NUMBER (major, minor, patch)) | |
132 | Error ("The script file requires " APPNAME " v%1, this is v%2", | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
133 | MakeVersionString (major, minor, patch), GetVersionString (false)); |
88 | 134 | |
135 | return true; | |
136 | } | |
137 | ||
138 | // ============================================================================ | |
139 | // | |
140 | void Lexer::CheckFileHeader (LexerScanner& sc) | |
141 | { | |
142 | if (!IsValidHeader (sc.ReadLine())) | |
143 | Error ("Not a valid botscript file! File must start with '#!botc <version>'"); | |
144 | } | |
145 | ||
146 | // ============================================================================= | |
147 | // | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
148 | bool Lexer::GetNext (TokenType req) |
88 | 149 | { |
150 | Iterator pos = mTokenPosition; | |
151 | ||
152 | if (mTokens.IsEmpty()) | |
153 | return false; | |
154 | ||
155 | mTokenPosition++; | |
156 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
157 | if (IsAtEnd() || (req !=TK_Any && GetTokenType() != req)) |
88 | 158 | { |
159 | mTokenPosition = pos; | |
160 | return false; | |
161 | } | |
162 | ||
163 | return true; | |
164 | } | |
165 | ||
166 | // ============================================================================= | |
167 | // | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
168 | void Lexer::MustGetNext (TokenType tok) |
88 | 169 | { |
170 | if (!GetNext()) | |
171 | Error ("unexpected EOF"); | |
172 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
173 | if (tok !=TK_Any) |
103
48472c0678cc
- removed tkAny as the default value of Lexer::MustGetNext to prevent problems like the one last commit fixed
Teemu Piippo <crimsondusk64@gmail.com>
parents:
99
diff
changeset
|
174 | TokenMustBe (tok); |
88 | 175 | } |
176 | ||
177 | // ============================================================================= | |
178 | // eugh.. | |
179 | // | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
180 | void Lexer::MustGetFromScanner (LexerScanner& sc, TokenType tt) |
88 | 181 | { |
182 | if (!sc.GetNextToken()) | |
183 | Error ("unexpected EOF"); | |
184 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
185 | if (tt !=TK_Any && sc.GetTokenType() != tt) |
88 | 186 | { |
187 | // TODO | |
188 | Token tok; | |
189 | tok.type = sc.GetTokenType(); | |
190 | tok.text = sc.GetTokenText(); | |
191 | ||
192 | Error ("at %1:%2: expected %3, got %4", | |
193 | gFileNameStack.Last(), | |
194 | sc.GetLine(), | |
195 | DescribeTokenType (tt), | |
196 | DescribeToken (&tok)); | |
197 | } | |
198 | } | |
199 | ||
200 | // ============================================================================= | |
201 | // | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
202 | void Lexer::MustGetAnyOf (const List<TokenType>& toks) |
88 | 203 | { |
204 | if (!GetNext()) | |
205 | Error ("unexpected EOF"); | |
206 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
207 | for (TokenType tok : toks) |
88 | 208 | if (GetTokenType() == tok) |
209 | return; | |
210 | ||
211 | String toknames; | |
212 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
213 | for (const TokenType& tokType : toks) |
88 | 214 | { |
215 | if (&tokType == &toks.Last()) | |
216 | toknames += " or "; | |
217 | elif (toknames.IsEmpty() == false) | |
218 | toknames += ", "; | |
219 | ||
220 | toknames += DescribeTokenType (tokType); | |
221 | } | |
222 | ||
223 | Error ("expected %1, got %2", toknames, DescribeToken (GetToken())); | |
224 | } | |
225 | ||
226 | // ============================================================================= | |
227 | // | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
228 | int Lexer::GetOneSymbol (const StringList& syms) |
88 | 229 | { |
230 | if (!GetNext()) | |
231 | Error ("unexpected EOF"); | |
232 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
233 | if (GetTokenType() ==TK_Symbol) |
88 | 234 | { |
235 | for (int i = 0; i < syms.Size(); ++i) | |
236 | { | |
237 | if (syms[i] == GetToken()->text) | |
238 | return i; | |
239 | } | |
240 | } | |
241 | ||
242 | Error ("expected one of %1, got %2", syms, DescribeToken (GetToken())); | |
243 | return -1; | |
244 | } | |
245 | ||
246 | // ============================================================================= | |
247 | // | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
248 | void Lexer::TokenMustBe (TokenType tok) |
88 | 249 | { |
250 | if (GetTokenType() != tok) | |
251 | Error ("expected %1, got %2", DescribeTokenType (tok), | |
252 | DescribeToken (GetToken())); | |
253 | } | |
254 | ||
255 | // ============================================================================= | |
256 | // | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
257 | String Lexer::DescribeTokenPrivate (TokenType tokType, Lexer::Token* tok) |
88 | 258 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
259 | if (tokType <gLastNamedToken) |
88 | 260 | return "\"" + LexerScanner::GetTokenString (tokType) + "\""; |
261 | ||
262 | switch (tokType) | |
263 | { | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
264 | case TK_Symbol: return tok ? tok->text : "a symbol"; |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
265 | case TK_Number: return tok ? tok->text : "a number"; |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
266 | case TK_String: return tok ? ("\"" + tok->text + "\"") : "a string"; |
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
267 | case TK_Any: return tok ? tok->text : "any token"; |
88 | 268 | default: break; |
269 | } | |
270 | ||
271 | return ""; | |
272 | } | |
273 | ||
274 | // ============================================================================= | |
275 | // | |
276 | bool Lexer::PeekNext (Lexer::Token* tk) | |
277 | { | |
278 | Iterator pos = mTokenPosition; | |
279 | bool r = GetNext(); | |
280 | ||
281 | if (r && tk != null) | |
282 | *tk = *mTokenPosition; | |
283 | ||
284 | mTokenPosition = pos; | |
285 | return r; | |
286 | } | |
287 | ||
288 | // ============================================================================= | |
289 | // | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
290 | bool Lexer::PeekNextType (TokenType req) |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
291 | { |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
292 | Iterator pos = mTokenPosition; |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
293 | bool result = false; |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
294 | |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
295 | if (GetNext() && GetTokenType() == req) |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
296 | result = true; |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
297 | |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
298 | mTokenPosition = pos; |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
299 | return result; |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
300 | } |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
301 | |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
302 | // ============================================================================= |
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
303 | // |
88 | 304 | Lexer* Lexer::GetCurrentLexer() |
305 | { | |
306 | return gMainLexer; | |
307 | } | |
308 | ||
309 | // ============================================================================= | |
310 | // | |
311 | String Lexer::PeekNextString (int a) | |
312 | { | |
313 | if (mTokenPosition + a >= mTokens.end()) | |
314 | return ""; | |
315 | ||
316 | Iterator oldpos = mTokenPosition; | |
317 | mTokenPosition += a; | |
318 | String result = GetToken()->text; | |
319 | mTokenPosition = oldpos; | |
320 | return result; | |
321 | } | |
89
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
322 | |
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
323 | // ============================================================================= |
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
324 | // |
105
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
103
diff
changeset
|
325 | String Lexer::DescribeCurrentPosition() |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
103
diff
changeset
|
326 | { |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
103
diff
changeset
|
327 | return GetToken()->file + ":" + GetToken()->line; |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
103
diff
changeset
|
328 | } |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
103
diff
changeset
|
329 | |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
103
diff
changeset
|
330 | // ============================================================================= |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
103
diff
changeset
|
331 | // |
6dbac3305614
- highly reworked variable support, variable declarations now are declared with 'var', uses are prefixed with '$', merged constant handling into variables
Teemu Piippo <crimsondusk64@gmail.com>
parents:
103
diff
changeset
|
332 | String Lexer::DescribeTokenPosition() |
89
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
333 | { |
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
334 | return Format ("%1 / %2", mTokenPosition - mTokens.begin(), mTokens.Size()); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
335 | } |