Sun, 09 Feb 2014 14:56:58 +0200
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
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 <cstdio> | |
30 | #include <cstdlib> | |
31 | #include <cassert> | |
32 | #include <cstring> | |
33 | #include <string> | |
34 | #include "LexerScanner.h" | |
35 | #include "Lexer.h" | |
36 | ||
37 | static const String gTokenStrings[] = | |
38 | { | |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
39 | "<<=", |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
40 | ">>=", |
88 | 41 | "==", |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
42 | "!=", |
88 | 43 | "[]", |
44 | "+=", | |
45 | "-=", | |
46 | "*=", | |
47 | "/=", | |
48 | "%=", | |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
49 | "<<", |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
50 | ">>", |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
51 | ">=", |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
52 | "<=", |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
53 | "&&", |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
54 | "||", |
98
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
55 | "++", |
ea02b78a737a
- loop structures now work again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
91
diff
changeset
|
56 | "--", |
88 | 57 | "'", |
58 | "$", | |
59 | "(", | |
60 | ")", | |
61 | "[", | |
62 | "]", | |
63 | "{", | |
64 | "}", | |
65 | "=", | |
66 | "+", | |
67 | "-", | |
68 | "*", | |
69 | "/", | |
70 | "%", | |
71 | ",", | |
72 | "<", | |
73 | ">", | |
74 | ".", | |
75 | ":", | |
76 | ";", | |
77 | "#", | |
78 | "!", | |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
79 | "&", |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
80 | "|", |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
81 | "^", |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
82 | "?", |
88 | 83 | "->", |
84 | "bool", | |
85 | "break", | |
86 | "case", | |
87 | "continue", | |
88 | "const", | |
89 | "default", | |
90 | "do", | |
91 | "else", | |
92 | "event", | |
93 | "eventdef", | |
94 | "for", | |
95 | "funcdef", | |
96 | "goto", | |
97 | "if", | |
98 | "int", | |
99 | "mainloop", | |
100 | "onenter", | |
101 | "onexit", | |
102 | "state", | |
103 | "switch", | |
104 | "str", | |
101
9ffae10ef76f
- variables: merged const and mutable variables into one system, added constexpr variable support. still no locals
Teemu Piippo <crimsondusk64@gmail.com>
parents:
98
diff
changeset
|
105 | "var", |
88 | 106 | "void", |
107 | "while", | |
91
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
108 | "true", |
427eb377d53e
- committed work so far done on expressions
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
109 | "false", |
88 | 110 | "enum", |
111 | "func", | |
112 | "return", | |
113 | }; | |
114 | ||
115 | static_assert (countof (gTokenStrings) == (int) tkLastNamedToken + 1, | |
116 | "Count of gTokenStrings is not the same as the amount of named token identifiers."); | |
117 | ||
118 | // ============================================================================= | |
119 | // | |
120 | LexerScanner::LexerScanner (FILE* fp) : | |
121 | mLine (1) | |
122 | { | |
123 | long fsize, bytes; | |
124 | ||
125 | fseek (fp, 0l, SEEK_END); | |
126 | fsize = ftell (fp); | |
127 | rewind (fp); | |
128 | mData = new char[fsize]; | |
129 | mPosition = mLineBreakPosition = &mData[0]; | |
130 | bytes = fread (mData, 1, fsize, fp); | |
131 | assert (bytes >= fsize); | |
132 | } | |
133 | ||
134 | // ============================================================================= | |
135 | // | |
136 | LexerScanner::~LexerScanner() | |
137 | { | |
138 | delete mData; | |
139 | } | |
140 | ||
141 | // ============================================================================= | |
142 | // | |
143 | bool LexerScanner::CheckString (const char* c, int flags) | |
144 | { | |
145 | bool r = strncmp (mPosition, c, strlen (c)) == 0; | |
146 | ||
147 | // There is to be a non-symbol character after words | |
148 | if (r && (flags & FCheckWord) && IsSymbolChar (mPosition[strlen (c)], true)) | |
149 | r = false; | |
150 | ||
151 | // Advance the cursor unless we want to just peek | |
152 | if (r && !(flags & FCheckPeek)) | |
153 | mPosition += strlen (c); | |
154 | ||
155 | return r; | |
156 | } | |
157 | ||
158 | // ============================================================================= | |
159 | // | |
160 | bool LexerScanner::GetNextToken() | |
161 | { | |
162 | mTokenText = ""; | |
163 | ||
164 | while (isspace (*mPosition)) | |
165 | Skip(); | |
166 | ||
167 | // Check for comments | |
168 | if (strncmp (mPosition, "//", 2) == 0) | |
169 | { | |
170 | mPosition += 2; | |
171 | ||
172 | while (*mPosition != '\n') | |
173 | Skip(); | |
174 | ||
175 | return GetNextToken(); | |
176 | } | |
177 | elif (strncmp (mPosition, "/*", 2) == 0) | |
178 | { | |
179 | Skip (2); // skip the start symbols | |
180 | ||
181 | while (strncmp (mPosition, "*/", 2) != 0) | |
182 | Skip(); | |
183 | ||
184 | Skip (2); // skip the end symbols | |
185 | return GetNextToken(); | |
186 | } | |
187 | ||
188 | if (*mPosition == '\0') | |
189 | return false; | |
190 | ||
191 | // Check tokens | |
192 | for (int i = 0; i < countof (gTokenStrings); ++i) | |
193 | { | |
194 | int flags = 0; | |
195 | ||
196 | if (i >= tkFirstNamedToken) | |
197 | flags |= FCheckWord; | |
198 | ||
199 | if (CheckString (gTokenStrings[i], flags)) | |
200 | { | |
201 | mTokenText = gTokenStrings[i]; | |
202 | mTokenType = (EToken) i; | |
203 | return true; | |
204 | } | |
205 | } | |
206 | ||
207 | // Check and parse string | |
208 | if (*mPosition == '\"') | |
209 | { | |
210 | mPosition++; | |
211 | ||
212 | while (*mPosition != '\"') | |
213 | { | |
214 | if (!*mPosition) | |
215 | Error ("unterminated string"); | |
216 | ||
217 | if (CheckString ("\\n")) | |
218 | { | |
219 | mTokenText += '\n'; | |
220 | continue; | |
221 | } | |
222 | elif (CheckString ("\\t")) | |
223 | { | |
224 | mTokenText += '\t'; | |
225 | continue; | |
226 | } | |
227 | elif (CheckString ("\\\"")) | |
228 | { | |
229 | mTokenText += '"'; | |
230 | continue; | |
231 | } | |
232 | ||
233 | mTokenText += *mPosition++; | |
234 | } | |
235 | ||
236 | mTokenType = tkString; | |
237 | Skip(); // skip the final quote | |
238 | return true; | |
239 | } | |
240 | ||
241 | if (isdigit (*mPosition)) | |
242 | { | |
243 | while (isdigit (*mPosition)) | |
244 | mTokenText += *mPosition++; | |
245 | ||
246 | mTokenType = tkNumber; | |
247 | return true; | |
248 | } | |
249 | ||
250 | if (IsSymbolChar (*mPosition, false)) | |
251 | { | |
252 | mTokenType = tkSymbol; | |
253 | ||
254 | do | |
255 | { | |
256 | if (!IsSymbolChar (*mPosition, true)) | |
257 | break; | |
258 | ||
259 | mTokenText += *mPosition++; | |
260 | } while (*mPosition != '\0'); | |
261 | ||
262 | return true; | |
263 | } | |
264 | ||
265 | Error ("unknown character \"%1\"", *mPosition); | |
266 | return false; | |
267 | } | |
268 | ||
269 | // ============================================================================= | |
270 | // | |
271 | void LexerScanner::Skip() | |
272 | { | |
273 | if (*mPosition == '\n') | |
274 | { | |
275 | mLine++; | |
276 | mLineBreakPosition = mPosition; | |
277 | } | |
278 | ||
279 | mPosition++; | |
280 | } | |
281 | ||
282 | // ============================================================================= | |
283 | // | |
284 | void LexerScanner::Skip (int chars) | |
285 | { | |
286 | for (int i = 0; i < chars; ++i) | |
287 | Skip(); | |
288 | } | |
289 | ||
290 | // ============================================================================= | |
291 | // | |
292 | String LexerScanner::GetTokenString (EToken a) | |
293 | { | |
294 | assert ((int) a <= tkLastNamedToken); | |
295 | return gTokenStrings[a]; | |
296 | } | |
297 | ||
298 | // ============================================================================= | |
299 | // | |
300 | String LexerScanner::ReadLine() | |
301 | { | |
302 | String line; | |
303 | ||
304 | while (*mPosition != '\n') | |
305 | line += *(mPosition++); | |
306 | ||
307 | return line; | |
308 | } |