Mon, 03 Mar 2014 17:02:38 +0200
- reserved 'constexpr' as a keyword because I know I will need it someday
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", | |
117
cc1e14d55047
- reserved 'constexpr' as a keyword because I know I will need it someday
Teemu Piippo <crimsondusk64@gmail.com>
parents:
116
diff
changeset
|
88 | "constexpr", |
88 | 89 | "default", |
90 | "do", | |
91 | "else", | |
92 | "event", | |
93 | "eventdef", | |
94 | "for", | |
95 | "funcdef", | |
96 | "if", | |
97 | "int", | |
98 | "mainloop", | |
99 | "onenter", | |
100 | "onexit", | |
101 | "state", | |
102 | "switch", | |
103 | "str", | |
116
56ff19947607
- added using statement for specifying the target zandronum version. will be used later
Teemu Piippo <crimsondusk64@gmail.com>
parents:
115
diff
changeset
|
104 | "using", |
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 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
115 | static_assert (countof (gTokenStrings) == (int)gLastNamedToken + 1, |
88 | 116 | "Count of gTokenStrings is not the same as the amount of named token identifiers."); |
117 | ||
118 | // ============================================================================= | |
119 | // | |
120 | LexerScanner::LexerScanner (FILE* fp) : | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
121 | m_line (1) |
88 | 122 | { |
123 | long fsize, bytes; | |
124 | ||
125 | fseek (fp, 0l, SEEK_END); | |
126 | fsize = ftell (fp); | |
127 | rewind (fp); | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
128 | m_data = new char[fsize]; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
129 | m_position = m_lineBreakPosition = &m_data[0]; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
130 | bytes = fread (m_data, 1, fsize, fp); |
88 | 131 | assert (bytes >= fsize); |
132 | } | |
133 | ||
134 | // ============================================================================= | |
135 | // | |
136 | LexerScanner::~LexerScanner() | |
137 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
138 | delete m_data; |
88 | 139 | } |
140 | ||
141 | // ============================================================================= | |
142 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
143 | bool LexerScanner::checkString (const char* c, int flags) |
88 | 144 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
145 | bool r = strncmp (m_position, c, strlen (c)) == 0; |
88 | 146 | |
147 | // There is to be a non-symbol character after words | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
148 | if (r && (flags & FCheckWord) && isSymbolChar (m_position[strlen (c)], true)) |
88 | 149 | r = false; |
150 | ||
151 | // Advance the cursor unless we want to just peek | |
152 | if (r && !(flags & FCheckPeek)) | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
153 | m_position += strlen (c); |
88 | 154 | |
155 | return r; | |
156 | } | |
157 | ||
158 | // ============================================================================= | |
159 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
160 | bool LexerScanner::getNextToken() |
88 | 161 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
162 | m_tokenText = ""; |
88 | 163 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
164 | while (isspace (*m_position)) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
165 | skip(); |
88 | 166 | |
167 | // Check for comments | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
168 | if (strncmp (m_position, "//", 2) == 0) |
88 | 169 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
170 | m_position += 2; |
88 | 171 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
172 | while (*m_position != '\n') |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
173 | skip(); |
88 | 174 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
175 | return getNextToken(); |
88 | 176 | } |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
177 | elif (strncmp (m_position, "/*", 2) == 0) |
88 | 178 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
179 | skip (2); // skip the start symbols |
88 | 180 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
181 | while (strncmp (m_position, "*/", 2) != 0) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
182 | skip(); |
88 | 183 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
184 | skip (2); // skip the end symbols |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
185 | return getNextToken(); |
88 | 186 | } |
187 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
188 | if (*m_position == '\0') |
88 | 189 | return false; |
190 | ||
191 | // Check tokens | |
192 | for (int i = 0; i < countof (gTokenStrings); ++i) | |
193 | { | |
194 | int flags = 0; | |
195 | ||
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
196 | if (i >= gFirstNamedToken) |
88 | 197 | flags |= FCheckWord; |
198 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
199 | if (checkString (gTokenStrings[i], flags)) |
88 | 200 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
201 | m_tokenText = gTokenStrings[i]; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
202 | m_tokenType = (ETokenType) i; |
88 | 203 | return true; |
204 | } | |
205 | } | |
206 | ||
207 | // Check and parse string | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
208 | if (*m_position == '\"') |
88 | 209 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
210 | m_position++; |
88 | 211 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
212 | while (*m_position != '\"') |
88 | 213 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
214 | if (!*m_position) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
215 | error ("unterminated string"); |
88 | 216 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
217 | if (checkString ("\\n")) |
88 | 218 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
219 | m_tokenText += '\n'; |
88 | 220 | continue; |
221 | } | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
222 | elif (checkString ("\\t")) |
88 | 223 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
224 | m_tokenText += '\t'; |
88 | 225 | continue; |
226 | } | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
227 | elif (checkString ("\\\"")) |
88 | 228 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
229 | m_tokenText += '"'; |
88 | 230 | continue; |
231 | } | |
232 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
233 | m_tokenText += *m_position++; |
88 | 234 | } |
235 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
236 | m_tokenType =TK_String; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
237 | skip(); // skip the final quote |
88 | 238 | return true; |
239 | } | |
240 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
241 | if (isdigit (*m_position)) |
88 | 242 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
243 | while (isdigit (*m_position)) |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
244 | m_tokenText += *m_position++; |
88 | 245 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
246 | m_tokenType =TK_Number; |
88 | 247 | return true; |
248 | } | |
249 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
250 | if (isSymbolChar (*m_position, false)) |
88 | 251 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
252 | m_tokenType =TK_Symbol; |
88 | 253 | |
254 | do | |
255 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
256 | if (!isSymbolChar (*m_position, true)) |
88 | 257 | break; |
258 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
259 | m_tokenText += *m_position++; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
260 | } while (*m_position != '\0'); |
88 | 261 | |
262 | return true; | |
263 | } | |
264 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
265 | error ("unknown character \"%1\"", *m_position); |
88 | 266 | return false; |
267 | } | |
268 | ||
269 | // ============================================================================= | |
270 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
271 | void LexerScanner::skip() |
88 | 272 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
273 | if (*m_position == '\n') |
88 | 274 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
275 | m_line++; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
276 | m_lineBreakPosition = m_position; |
88 | 277 | } |
278 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
279 | m_position++; |
88 | 280 | } |
281 | ||
282 | // ============================================================================= | |
283 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
284 | void LexerScanner::skip (int chars) |
88 | 285 | { |
286 | for (int i = 0; i < chars; ++i) | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
287 | skip(); |
88 | 288 | } |
289 | ||
290 | // ============================================================================= | |
291 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
292 | String LexerScanner::getTokenString (ETokenType a) |
88 | 293 | { |
110
7a7a53f1d51b
- extended refactor to EToken (now TokenType)
Teemu Piippo <crimsondusk64@gmail.com>
parents:
108
diff
changeset
|
294 | assert ((int) a <= gLastNamedToken); |
88 | 295 | return gTokenStrings[a]; |
296 | } | |
297 | ||
298 | // ============================================================================= | |
299 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
300 | String LexerScanner::readLine() |
88 | 301 | { |
302 | String line; | |
303 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
304 | while (*m_position != '\n') |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
112
diff
changeset
|
305 | line += *(m_position++); |
88 | 306 | |
307 | return line; | |
108
6409ece8297c
- refactored enums, macros split from Main.h to Macros.h
Teemu Piippo <crimsondusk64@gmail.com>
parents:
107
diff
changeset
|
308 | } |