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