/src/ogre/OgreMain/src/OgreScriptLexer.cpp
Line | Count | Source |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #include "OgreStableHeaders.h" |
29 | | #include "OgreScriptLexer.h" |
30 | | |
31 | | namespace Ogre { |
32 | | ScriptTokenList ScriptLexer::tokenize(const String &str, const String& source) |
33 | 0 | { |
34 | 0 | String error; |
35 | 0 | ScriptTokenList ret = _tokenize(str, source.c_str(), error); |
36 | |
|
37 | 0 | if (!error.empty()) |
38 | 0 | LogManager::getSingleton().logError("ScriptLexer - " + error); |
39 | |
|
40 | 0 | return ret; |
41 | 0 | } |
42 | | |
43 | | ScriptTokenList ScriptLexer::_tokenize(const String &str, const char* source, String& error) |
44 | 0 | { |
45 | | // State enums |
46 | 0 | enum{ READY = 0, COMMENT, MULTICOMMENT, WORD, QUOTE, VAR, POSSIBLECOMMENT }; |
47 | | |
48 | | // Set up some constant characters of interest |
49 | 0 | const wchar_t varopener = '$', quote = '\"', slash = '/', backslash = '\\', openbrace = '{', closebrace = '}', colon = ':', star = '*', cr = '\r', lf = '\n'; |
50 | 0 | char c = 0, lastc = 0; |
51 | |
|
52 | 0 | String lexeme; |
53 | 0 | uint32 line = 1, state = READY, lastQuote = 0, firstOpenBrace = 0, braceLayer = 0; |
54 | 0 | ScriptTokenList tokens; |
55 | | |
56 | | // Iterate over the input |
57 | 0 | for(char i : str) |
58 | 0 | { |
59 | 0 | lastc = c; |
60 | 0 | c = i; |
61 | |
|
62 | 0 | if(c == quote) |
63 | 0 | lastQuote = line; |
64 | | |
65 | 0 | if(state == READY || state == WORD || state == VAR) |
66 | 0 | { |
67 | 0 | if(c == openbrace) |
68 | 0 | { |
69 | 0 | if(braceLayer == 0) |
70 | 0 | firstOpenBrace = line; |
71 | | |
72 | 0 | braceLayer ++; |
73 | 0 | } |
74 | 0 | else if(c == closebrace) |
75 | 0 | { |
76 | 0 | if (braceLayer == 0) |
77 | 0 | { |
78 | 0 | error = StringUtil::format( |
79 | 0 | "no matching open bracket '{' found for close bracket '}' at %s:%d", source, |
80 | 0 | line); |
81 | 0 | return tokens; |
82 | 0 | } |
83 | | |
84 | 0 | braceLayer --; |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | | |
89 | 0 | switch(state) |
90 | 0 | { |
91 | 0 | case READY: |
92 | 0 | if(c == slash && lastc == slash) |
93 | 0 | { |
94 | | // Comment start, clear out the lexeme |
95 | 0 | lexeme = ""; |
96 | 0 | state = COMMENT; |
97 | 0 | } |
98 | 0 | else if(c == star && lastc == slash) |
99 | 0 | { |
100 | 0 | lexeme = ""; |
101 | 0 | state = MULTICOMMENT; |
102 | 0 | } |
103 | 0 | else if(c == quote) |
104 | 0 | { |
105 | | // Clear out the lexeme ready to be filled with quotes! |
106 | 0 | lexeme = c; |
107 | 0 | state = QUOTE; |
108 | 0 | } |
109 | 0 | else if(c == varopener) |
110 | 0 | { |
111 | | // Set up to read in a variable |
112 | 0 | lexeme = c; |
113 | 0 | state = VAR; |
114 | 0 | } |
115 | 0 | else if(isNewline(c)) |
116 | 0 | { |
117 | 0 | lexeme = c; |
118 | 0 | setToken(lexeme, line, tokens); |
119 | 0 | } |
120 | 0 | else if(!isWhitespace(c)) |
121 | 0 | { |
122 | 0 | lexeme = c; |
123 | 0 | if(c == slash) |
124 | 0 | state = POSSIBLECOMMENT; |
125 | 0 | else |
126 | 0 | state = WORD; |
127 | 0 | } |
128 | 0 | break; |
129 | 0 | case COMMENT: |
130 | 0 | if(isNewline(c)) |
131 | 0 | { |
132 | 0 | lexeme = c; |
133 | 0 | setToken(lexeme, line, tokens); |
134 | 0 | state = READY; |
135 | 0 | } |
136 | 0 | break; |
137 | 0 | case MULTICOMMENT: |
138 | 0 | if(c == slash && lastc == star) |
139 | 0 | state = READY; |
140 | 0 | break; |
141 | 0 | case POSSIBLECOMMENT: |
142 | 0 | if(c == slash && lastc == slash) |
143 | 0 | { |
144 | 0 | lexeme = ""; |
145 | 0 | state = COMMENT; |
146 | 0 | break; |
147 | 0 | } |
148 | 0 | else if(c == star && lastc == slash) |
149 | 0 | { |
150 | 0 | lexeme = ""; |
151 | 0 | state = MULTICOMMENT; |
152 | 0 | break; |
153 | 0 | } |
154 | 0 | else |
155 | 0 | { |
156 | 0 | state = WORD; |
157 | 0 | OGRE_FALLTHROUGH; |
158 | 0 | } |
159 | 0 | case WORD: |
160 | 0 | if(isNewline(c)) |
161 | 0 | { |
162 | 0 | setToken(lexeme, line, tokens); |
163 | 0 | lexeme = c; |
164 | 0 | setToken(lexeme, line, tokens); |
165 | 0 | state = READY; |
166 | 0 | } |
167 | 0 | else if(isWhitespace(c)) |
168 | 0 | { |
169 | 0 | setToken(lexeme, line, tokens); |
170 | 0 | state = READY; |
171 | 0 | } |
172 | 0 | else if(c == openbrace || c == closebrace || c == colon) |
173 | 0 | { |
174 | 0 | setToken(lexeme, line, tokens); |
175 | 0 | lexeme = c; |
176 | 0 | setToken(lexeme, line, tokens); |
177 | 0 | state = READY; |
178 | 0 | } |
179 | 0 | else |
180 | 0 | { |
181 | 0 | lexeme += c; |
182 | 0 | } |
183 | 0 | break; |
184 | 0 | case QUOTE: |
185 | 0 | if(c != backslash) |
186 | 0 | { |
187 | | // Allow embedded quotes with escaping |
188 | 0 | if(c == quote && lastc == backslash) |
189 | 0 | { |
190 | 0 | lexeme += c; |
191 | 0 | } |
192 | 0 | else if(c == quote) |
193 | 0 | { |
194 | 0 | lexeme += c; |
195 | 0 | setToken(lexeme, line, tokens); |
196 | 0 | state = READY; |
197 | 0 | } |
198 | 0 | else |
199 | 0 | { |
200 | | // Backtrack here and allow a backslash normally within the quote |
201 | 0 | if(lastc == backslash) |
202 | 0 | lexeme = lexeme + "\\" + c; |
203 | 0 | else |
204 | 0 | lexeme += c; |
205 | 0 | } |
206 | 0 | } |
207 | 0 | break; |
208 | 0 | case VAR: |
209 | 0 | if(isNewline(c)) |
210 | 0 | { |
211 | 0 | setToken(lexeme, line, tokens); |
212 | 0 | lexeme = c; |
213 | 0 | setToken(lexeme, line, tokens); |
214 | 0 | state = READY; |
215 | 0 | } |
216 | 0 | else if(isWhitespace(c)) |
217 | 0 | { |
218 | 0 | setToken(lexeme, line, tokens); |
219 | 0 | state = READY; |
220 | 0 | } |
221 | 0 | else if(c == openbrace || c == closebrace || c == colon) |
222 | 0 | { |
223 | 0 | setToken(lexeme, line, tokens); |
224 | 0 | lexeme = c; |
225 | 0 | setToken(lexeme, line, tokens); |
226 | 0 | state = READY; |
227 | 0 | } |
228 | 0 | else |
229 | 0 | { |
230 | 0 | lexeme += c; |
231 | 0 | } |
232 | 0 | break; |
233 | 0 | } |
234 | | |
235 | | // Separate check for newlines just to track line numbers |
236 | 0 | if(c == cr || (c == lf && lastc != cr)) |
237 | 0 | line++; |
238 | 0 | } |
239 | | |
240 | | // Check for valid exit states |
241 | 0 | if(state == WORD || state == VAR) |
242 | 0 | { |
243 | 0 | if(!lexeme.empty()) |
244 | 0 | setToken(lexeme, line, tokens); |
245 | 0 | } |
246 | 0 | else |
247 | 0 | { |
248 | 0 | if(state == QUOTE) |
249 | 0 | { |
250 | 0 | error = StringUtil::format("no matching \" found for \" at %s:%d", source, lastQuote); |
251 | 0 | return tokens; |
252 | 0 | } |
253 | 0 | } |
254 | | |
255 | | // Check that all opened brackets have been closed |
256 | 0 | if (braceLayer == 1) |
257 | 0 | { |
258 | 0 | error = StringUtil::format("no matching closing bracket '}' for open bracket '{' at %s:%d", |
259 | 0 | source, firstOpenBrace); |
260 | 0 | } |
261 | 0 | else if (braceLayer > 1) |
262 | 0 | { |
263 | 0 | error = StringUtil::format( |
264 | 0 | "too many open brackets (%d) '{' without matching closing bracket '}' in %s", braceLayer, |
265 | 0 | source); |
266 | 0 | } |
267 | | |
268 | 0 | return tokens; |
269 | 0 | } |
270 | | |
271 | | void ScriptLexer::setToken(const Ogre::String &lexeme, Ogre::uint32 line, ScriptTokenList& tokens) |
272 | 0 | { |
273 | 0 | const char openBracket = '{', closeBracket = '}', colon = ':', |
274 | 0 | quote = '\"', var = '$'; |
275 | |
|
276 | 0 | ScriptToken token; |
277 | 0 | token.line = line; |
278 | 0 | bool ignore = false; |
279 | | |
280 | | // Check the user token map first |
281 | 0 | if(lexeme.size() == 1 && isNewline(lexeme[0])) |
282 | 0 | { |
283 | 0 | token.type = TID_NEWLINE; |
284 | 0 | if(!tokens.empty() && tokens.back().type == TID_NEWLINE) |
285 | 0 | ignore = true; |
286 | 0 | } |
287 | 0 | else if(lexeme.size() == 1 && lexeme[0] == openBracket) |
288 | 0 | token.type = TID_LBRACKET; |
289 | 0 | else if(lexeme.size() == 1 && lexeme[0] == closeBracket) |
290 | 0 | token.type = TID_RBRACKET; |
291 | 0 | else if(lexeme.size() == 1 && lexeme[0] == colon) |
292 | 0 | token.type = TID_COLON; |
293 | 0 | else |
294 | 0 | { |
295 | 0 | token.lexeme = lexeme; |
296 | | |
297 | | // This is either a non-zero length phrase or quoted phrase |
298 | 0 | if(lexeme.size() >= 2 && lexeme[0] == quote && lexeme[lexeme.size() - 1] == quote) |
299 | 0 | { |
300 | 0 | token.type = TID_QUOTE; |
301 | 0 | } |
302 | 0 | else if(lexeme.size() > 1 && lexeme[0] == var) |
303 | 0 | token.type = TID_VARIABLE; |
304 | 0 | else |
305 | 0 | { |
306 | 0 | token.type = TID_WORD; |
307 | 0 | } |
308 | 0 | } |
309 | |
|
310 | 0 | if(!ignore) |
311 | 0 | tokens.push_back(token); |
312 | 0 | } |
313 | | |
314 | | bool ScriptLexer::isWhitespace(Ogre::String::value_type c) |
315 | 0 | { |
316 | 0 | return c == ' ' || c == '\r' || c == '\t'; |
317 | 0 | } |
318 | | |
319 | | bool ScriptLexer::isNewline(Ogre::String::value_type c) |
320 | 0 | { |
321 | 0 | return c == '\n' || c == '\r'; |
322 | 0 | } |
323 | | } |