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