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