Mon, 03 Mar 2014 01:04:16 +0200
- reformatting... again
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) : | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
119 | m_line (1) |
88 | 120 | { |
121 | long fsize, bytes; | |
122 | ||
123 | fseek (fp, 0l, SEEK_END); | |
124 | fsize = ftell (fp); | |
125 | rewind (fp); | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
126 | m_data = new char[fsize]; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
127 | m_position = m_lineBreakPosition = &m_data[0]; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
128 | bytes = fread (m_data, 1, fsize, fp); |
88 | 129 | assert (bytes >= fsize); |
130 | } | |
131 | ||
132 | // ============================================================================= | |
133 | // | |
134 | LexerScanner::~LexerScanner() | |
135 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
136 | delete m_data; |
88 | 137 | } |
138 | ||
139 | // ============================================================================= | |
140 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
141 | bool LexerScanner::checkString (const char* c, int flags) |
88 | 142 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
143 | bool r = strncmp (m_position, c, strlen (c)) == 0; |
88 | 144 | |
145 | // There is to be a non-symbol character after words | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
146 | if (r && (flags & FCheckWord) && isSymbolChar (m_position[strlen (c)], true)) |
88 | 147 | r = false; |
148 | ||
149 | // Advance the cursor unless we want to just peek | |
150 | if (r && !(flags & FCheckPeek)) | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
151 | m_position += strlen (c); |
88 | 152 | |
153 | return r; | |
154 | } | |
155 | ||
156 | // ============================================================================= | |
157 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
158 | bool LexerScanner::getNextToken() |
88 | 159 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
160 | m_tokenText = ""; |
88 | 161 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
162 | while (isspace (*m_position)) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
163 | skip(); |
88 | 164 | |
165 | // Check for comments | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
166 | if (strncmp (m_position, "//", 2) == 0) |
88 | 167 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
168 | m_position += 2; |
88 | 169 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
170 | while (*m_position != '\n') |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
171 | skip(); |
88 | 172 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
173 | return getNextToken(); |
88 | 174 | } |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
175 | elif (strncmp (m_position, "/*", 2) == 0) |
88 | 176 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
177 | skip (2); // skip the start symbols |
88 | 178 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
179 | while (strncmp (m_position, "*/", 2) != 0) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
180 | skip(); |
88 | 181 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
182 | skip (2); // skip the end symbols |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
183 | return getNextToken(); |
88 | 184 | } |
185 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
186 | if (*m_position == '\0') |
88 | 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 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
197 | if (checkString (gTokenStrings[i], flags)) |
88 | 198 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
199 | m_tokenText = gTokenStrings[i]; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
200 | m_tokenType = (ETokenType) i; |
88 | 201 | return true; |
202 | } | |
203 | } | |
204 | ||
205 | // Check and parse string | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
206 | if (*m_position == '\"') |
88 | 207 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
208 | m_position++; |
88 | 209 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
210 | while (*m_position != '\"') |
88 | 211 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
212 | if (!*m_position) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
213 | error ("unterminated string"); |
88 | 214 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
215 | if (checkString ("\\n")) |
88 | 216 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
217 | m_tokenText += '\n'; |
88 | 218 | continue; |
219 | } | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
220 | elif (checkString ("\\t")) |
88 | 221 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
222 | m_tokenText += '\t'; |
88 | 223 | continue; |
224 | } | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
225 | elif (checkString ("\\\"")) |
88 | 226 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
227 | m_tokenText += '"'; |
88 | 228 | continue; |
229 | } | |
230 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
231 | m_tokenText += *m_position++; |
88 | 232 | } |
233 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
234 | m_tokenType =TK_String; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
235 | skip(); // skip the final quote |
88 | 236 | return true; |
237 | } | |
238 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
239 | if (isdigit (*m_position)) |
88 | 240 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
241 | while (isdigit (*m_position)) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
242 | m_tokenText += *m_position++; |
88 | 243 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
244 | m_tokenType =TK_Number; |
88 | 245 | return true; |
246 | } | |
247 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
248 | if (isSymbolChar (*m_position, false)) |
88 | 249 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
250 | m_tokenType =TK_Symbol; |
88 | 251 | |
252 | do | |
253 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
254 | if (!isSymbolChar (*m_position, true)) |
88 | 255 | break; |
256 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
257 | m_tokenText += *m_position++; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
258 | } while (*m_position != '\0'); |
88 | 259 | |
260 | return true; | |
261 | } | |
262 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
263 | error ("unknown character \"%1\"", *m_position); |
88 | 264 | return false; |
265 | } | |
266 | ||
267 | // ============================================================================= | |
268 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
269 | void LexerScanner::skip() |
88 | 270 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
271 | if (*m_position == '\n') |
88 | 272 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
273 | m_line++; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
274 | m_lineBreakPosition = m_position; |
88 | 275 | } |
276 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
277 | m_position++; |
88 | 278 | } |
279 | ||
280 | // ============================================================================= | |
281 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
282 | void LexerScanner::skip (int chars) |
88 | 283 | { |
284 | for (int i = 0; i < chars; ++i) | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
285 | skip(); |
88 | 286 | } |
287 | ||
288 | // ============================================================================= | |
289 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
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 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
298 | String LexerScanner::readLine() |
88 | 299 | { |
300 | String line; | |
301 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
302 | while (*m_position != '\n') |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
303 | line += *(m_position++); |
88 | 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 | } |