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