Mon, 03 Mar 2014 17:02:38 +0200
- reserved 'constexpr' as a keyword because I know I will need it someday
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 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
52 | void Lexer::processFile (String fileName) |
88 | 53 | { |
54 | gFileNameStack << fileName; | |
55 | FILE* fp = fopen (fileName, "r"); | |
56 | ||
57 | if (fp == null) | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
58 | error ("couldn't open %1 for reading: %2", fileName, strerror (errno)); |
88 | 59 | |
60 | LexerScanner sc (fp); | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
61 | checkFileHeader (sc); |
88 | 62 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
63 | while (sc.getNextToken()) |
88 | 64 | { |
65 | // Preprocessor commands: | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
66 | if (sc.getTokenType() ==TK_Hash) |
88 | 67 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
68 | mustGetFromScanner (sc,TK_Symbol); |
88 | 69 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
70 | if (sc.getTokenText() == "include") |
88 | 71 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
72 | mustGetFromScanner (sc,TK_String); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
73 | String fileName = sc.getTokenText(); |
88 | 74 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
75 | if (gFileNameStack.contains (fileName)) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
76 | error ("attempted to #include %1 recursively", sc.getTokenText()); |
88 | 77 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
78 | processFile (fileName); |
88 | 79 | } |
80 | else | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
81 | error ("unknown preprocessor directive \"#%1\"", sc.getTokenText()); |
88 | 82 | } |
83 | else | |
84 | { | |
112
def56932f938
- changed the PROPERTY macro to a simper version and brought some refactoring with it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
110
diff
changeset
|
85 | TokenInfo tok; |
88 | 86 | tok.file = fileName; |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
87 | tok.line = sc.getLine(); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
88 | tok.column = sc.getColumn(); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
89 | tok.type = sc.getTokenType(); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
90 | tok.text = sc.getTokenText(); |
88 | 91 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
92 | // devf ("Token #%1: %2:%3:%4: %5 (%6)\n", mTokens.size(), |
110
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 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
96 | m_tokens << tok; |
88 | 97 | } |
98 | } | |
99 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
100 | m_tokenPosition = m_tokens.begin() - 1; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
101 | gFileNameStack.removeOne (fileName); |
88 | 102 | } |
103 | ||
104 | // ============================================================================ | |
105 | // | |
106 | static bool IsValidHeader (String header) | |
107 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
108 | if (header.endsWith ("\n")) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
109 | header.removeFromEnd (1); |
88 | 110 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
111 | StringList tokens = header.split (" "); |
88 | 112 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
113 | if (tokens.size() != 2 || tokens[0] != "#!botc" || tokens[1].isEmpty()) |
88 | 114 | return false; |
115 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
116 | StringList nums = tokens[1].split ("."); |
88 | 117 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
118 | if (nums.size() == 2) |
88 | 119 | nums << "0"; |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
120 | elif (nums.size() != 3) |
88 | 121 | return false; |
122 | ||
123 | bool okA, okB, okC; | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
124 | long major = nums[0].toLong (&okA); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
125 | long minor = nums[1].toLong (&okB); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
126 | long patch = nums[2].toLong (&okC); |
88 | 127 | |
128 | if (!okA || !okB || !okC) | |
129 | return false; | |
130 | ||
131 | if (VERSION_NUMBER < MAKE_VERSION_NUMBER (major, minor, patch)) | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
132 | error ("The script file requires " APPNAME " v%1, this is v%2", |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
133 | makeVersionString (major, minor, patch), versionString (false)); |
88 | 134 | |
135 | return true; | |
136 | } | |
137 | ||
138 | // ============================================================================ | |
139 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
140 | void Lexer::checkFileHeader (LexerScanner& sc) |
88 | 141 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
142 | if (!IsValidHeader (sc.readLine())) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
143 | error ("Not a valid botscript file! File must start with '#!botc <version>'"); |
88 | 144 | } |
145 | ||
146 | // ============================================================================= | |
147 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
148 | bool Lexer::next (ETokenType req) |
88 | 149 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
150 | Iterator pos = m_tokenPosition; |
88 | 151 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
152 | if (m_tokens.isEmpty()) |
88 | 153 | return false; |
154 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
155 | m_tokenPosition++; |
88 | 156 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
157 | if (isAtEnd() || (req !=TK_Any && tokenType() != req)) |
88 | 158 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
159 | m_tokenPosition = pos; |
88 | 160 | return false; |
161 | } | |
162 | ||
163 | return true; | |
164 | } | |
165 | ||
166 | // ============================================================================= | |
167 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
168 | void Lexer::mustGetNext (ETokenType tok) |
88 | 169 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
170 | if (!next()) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
171 | error ("unexpected EOF"); |
88 | 172 | |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
173 | if (tok !=TK_Any) |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
174 | tokenMustBe (tok); |
88 | 175 | } |
176 | ||
177 | // ============================================================================= | |
178 | // eugh.. | |
179 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
180 | void Lexer::mustGetFromScanner (LexerScanner& sc, ETokenType tt) |
88 | 181 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
182 | if (sc.getNextToken() == false) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
183 | error ("unexpected EOF"); |
88 | 184 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
185 | if (tt != TK_Any && sc.getTokenType() != tt) |
88 | 186 | { |
187 | // TODO | |
112
def56932f938
- changed the PROPERTY macro to a simper version and brought some refactoring with it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
110
diff
changeset
|
188 | TokenInfo tok; |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
189 | tok.type = sc.getTokenType(); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
190 | tok.text = sc.getTokenText(); |
88 | 191 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
192 | error ("at %1:%2: expected %3, got %4", |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
193 | gFileNameStack.last(), |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
194 | sc.getLine(), |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
195 | describeTokenType (tt), |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
196 | describeToken (&tok)); |
88 | 197 | } |
198 | } | |
199 | ||
200 | // ============================================================================= | |
201 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
202 | void Lexer::mustGetAnyOf (const List<ETokenType>& toks) |
88 | 203 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
204 | if (!next()) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
205 | error ("unexpected EOF"); |
88 | 206 | |
112
def56932f938
- changed the PROPERTY macro to a simper version and brought some refactoring with it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
110
diff
changeset
|
207 | for (ETokenType tok : toks) |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
208 | if (tokenType() == tok) |
88 | 209 | return; |
210 | ||
211 | String toknames; | |
212 | ||
112
def56932f938
- changed the PROPERTY macro to a simper version and brought some refactoring with it
Teemu Piippo <crimsondusk64@gmail.com>
parents:
110
diff
changeset
|
213 | for (const ETokenType& tokType : toks) |
88 | 214 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
215 | if (&tokType == &toks.last()) |
88 | 216 | toknames += " or "; |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
217 | elif (toknames.isEmpty() == false) |
88 | 218 | toknames += ", "; |
219 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
220 | toknames += describeTokenType (tokType); |
88 | 221 | } |
222 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
223 | error ("expected %1, got %2", toknames, describeToken (token())); |
88 | 224 | } |
225 | ||
226 | // ============================================================================= | |
227 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
228 | int Lexer::getOneSymbol (const StringList& syms) |
88 | 229 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
230 | if (!next()) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
231 | error ("unexpected EOF"); |
88 | 232 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
233 | if (tokenType() ==TK_Symbol) |
88 | 234 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
235 | for (int i = 0; i < syms.size(); ++i) |
88 | 236 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
237 | if (syms[i] == token()->text) |
88 | 238 | return i; |
239 | } | |
240 | } | |
241 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
242 | error ("expected one of %1, got %2", syms, describeToken (token())); |
88 | 243 | return -1; |
244 | } | |
245 | ||
246 | // ============================================================================= | |
247 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
248 | void Lexer::tokenMustBe (ETokenType tok) |
88 | 249 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
250 | if (tokenType() != tok) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
251 | error ("expected %1, got %2", describeTokenType (tok), |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
252 | describeToken (token())); |
88 | 253 | } |
254 | ||
255 | // ============================================================================= | |
256 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
257 | String Lexer::describeTokenPrivate (ETokenType tokType, Lexer::TokenInfo* tok) |
88 | 258 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
259 | if (tokType <gLastNamedToken) |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
260 | return "\"" + LexerScanner::getTokenString (tokType) + "\""; |
88 | 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 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
276 | bool Lexer::peekNext (Lexer::TokenInfo* tk) |
88 | 277 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
278 | Iterator pos = m_tokenPosition; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
279 | bool r = next(); |
88 | 280 | |
281 | if (r && tk != null) | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
282 | *tk = *m_tokenPosition; |
88 | 283 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
284 | m_tokenPosition = pos; |
88 | 285 | return r; |
286 | } | |
287 | ||
288 | // ============================================================================= | |
289 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
290 | bool Lexer::peekNextType (ETokenType req) |
99
44c0c7f31ae8
- changed the syntax of funcdef to something sane
Teemu Piippo <crimsondusk64@gmail.com>
parents:
89
diff
changeset
|
291 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
292 | Iterator pos = m_tokenPosition; |
99
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 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
295 | if (next() && tokenType() == req) |
99
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 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
298 | m_tokenPosition = pos; |
99
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 | // |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
304 | Lexer* Lexer::getCurrentLexer() |
88 | 305 | { |
306 | return gMainLexer; | |
307 | } | |
308 | ||
309 | // ============================================================================= | |
310 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
311 | String Lexer::peekNextString (int a) |
88 | 312 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
313 | if (m_tokenPosition + a >= m_tokens.end()) |
88 | 314 | return ""; |
315 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
316 | Iterator oldpos = m_tokenPosition; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
317 | m_tokenPosition += a; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
318 | String result = token()->text; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
319 | m_tokenPosition = oldpos; |
88 | 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 | // |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
325 | String Lexer::describeCurrentPosition() |
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
|
326 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
327 | return token()->file + ":" + token()->line; |
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
|
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 | // |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
332 | String Lexer::describeTokenPosition() |
89
029a330a9bef
- blargh. buffers weren't initialized properly
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
333 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
334 | return format ("%1 / %2", m_tokenPosition - m_tokens.begin(), m_tokens.size()); |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
335 | } |
116
56ff19947607
- added using statement for specifying the target zandronum version. will be used later
Teemu Piippo <crimsondusk64@gmail.com>
parents:
115
diff
changeset
|
336 | |
56ff19947607
- added using statement for specifying the target zandronum version. will be used later
Teemu Piippo <crimsondusk64@gmail.com>
parents:
115
diff
changeset
|
337 | // ============================================================================= |
56ff19947607
- added using statement for specifying the target zandronum version. will be used later
Teemu Piippo <crimsondusk64@gmail.com>
parents:
115
diff
changeset
|
338 | // |
56ff19947607
- added using statement for specifying the target zandronum version. will be used later
Teemu Piippo <crimsondusk64@gmail.com>
parents:
115
diff
changeset
|
339 | void Lexer::mustGetSymbol (const String& a) |
56ff19947607
- added using statement for specifying the target zandronum version. will be used later
Teemu Piippo <crimsondusk64@gmail.com>
parents:
115
diff
changeset
|
340 | { |
56ff19947607
- added using statement for specifying the target zandronum version. will be used later
Teemu Piippo <crimsondusk64@gmail.com>
parents:
115
diff
changeset
|
341 | mustGetNext (TK_Any); |
56ff19947607
- added using statement for specifying the target zandronum version. will be used later
Teemu Piippo <crimsondusk64@gmail.com>
parents:
115
diff
changeset
|
342 | if (token()->text != a) |
56ff19947607
- added using statement for specifying the target zandronum version. will be used later
Teemu Piippo <crimsondusk64@gmail.com>
parents:
115
diff
changeset
|
343 | error ("expected \"%1\", got \"%2\"", a, token()->text); |
56ff19947607
- added using statement for specifying the target zandronum version. will be used later
Teemu Piippo <crimsondusk64@gmail.com>
parents:
115
diff
changeset
|
344 | } |