1 | /** $Id: VDScript.cpp 44964 2013-03-08 21:06:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * VBox HDD container test utility - scripting engine.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2013 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 | /** @page pg_vd_script VDScript - Simple scripting language for VD I/O testing.
|
---|
20 | *
|
---|
21 | * This component implements a very simple scripting language to make testing the VD
|
---|
22 | * library more flexible and testcases faster to implement without the need to recompile
|
---|
23 | * everything after it changed.
|
---|
24 | * The language is a small subset of the C language. It doesn't support unions, structs,
|
---|
25 | * global variables, typedefed types or pointers (yet). It also adds a boolean and a string type.
|
---|
26 | * Strings are immutable and only to print messages from the script.
|
---|
27 | * There are also not the default types like int or unsigned because theire ranges are architecture
|
---|
28 | * dependent. Instead VDScript uses uint8_t, int8_t, ... as primitive types.
|
---|
29 | *
|
---|
30 | * Why inventing a completely new language?
|
---|
31 | *
|
---|
32 | * Well it is not a completely new language to start with, it is a subset of C and the
|
---|
33 | * language can be extended later on to reach the full C language later on.
|
---|
34 | * Second, there is no static typed scripting language I like which could be implemented
|
---|
35 | * and finally because I can ;)
|
---|
36 | * The code implementing the scripting engine is designed to be easily incorporated into other
|
---|
37 | * code. Could be used as a scripting language for the VBox debugger for example or in the scm
|
---|
38 | * tool to automatically rewrite C code using the AST VDSCript generates...
|
---|
39 | *
|
---|
40 | * The syntax of VDSCript is derived from the C syntax. The syntax of C in BNF was taken
|
---|
41 | * from: http://www.csci.csusb.edu/dick/samples/c.syntax.html
|
---|
42 | * and: http://slps.github.com/zoo/c/iso-9899-tc3.html
|
---|
43 | * and: http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
|
---|
44 | */
|
---|
45 | #define LOGGROUP LOGGROUP_DEFAULT
|
---|
46 | #include <iprt/string.h>
|
---|
47 | #include <iprt/list.h>
|
---|
48 | #include <iprt/mem.h>
|
---|
49 | #include <iprt/ctype.h>
|
---|
50 | #include <iprt/stream.h>
|
---|
51 |
|
---|
52 | #include <VBox/log.h>
|
---|
53 |
|
---|
54 | #include "VDScriptAst.h"
|
---|
55 | #include "VDScriptInternal.h"
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * VD script token class.
|
---|
59 | */
|
---|
60 | typedef enum VDTOKENCLASS
|
---|
61 | {
|
---|
62 | /** Invalid. */
|
---|
63 | VDTOKENCLASS_INVALID = 0,
|
---|
64 | /** Identifier class. */
|
---|
65 | VDTOKENCLASS_IDENTIFIER,
|
---|
66 | /** Numerical constant. */
|
---|
67 | VDTOKENCLASS_NUMCONST,
|
---|
68 | /** String constant. */
|
---|
69 | VDTOKENCLASS_STRINGCONST,
|
---|
70 | /** Operators */
|
---|
71 | VDTOKENCLASS_OPERATORS,
|
---|
72 | /** Reserved keyword */
|
---|
73 | VDTOKENCLASS_KEYWORD,
|
---|
74 | /** Punctuator */
|
---|
75 | VDTOKENCLASS_PUNCTUATOR,
|
---|
76 | /** End of stream */
|
---|
77 | VDTOKENCLASS_EOS,
|
---|
78 | /** 32bit hack. */
|
---|
79 | VDTOKENCLASS_32BIT_HACK = 0x7fffffff
|
---|
80 | } VDTOKENCLASS;
|
---|
81 | /** Pointer to a token class. */
|
---|
82 | typedef VDTOKENCLASS *PVDTOKENCLASS;
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Keyword types.
|
---|
86 | */
|
---|
87 | typedef enum VDSCRIPTTOKENKEYWORD
|
---|
88 | {
|
---|
89 | VDSCRIPTTOKENKEYWORD_INVALID = 0,
|
---|
90 | VDSCRIPTTOKENKEYWORD_CONTINUE,
|
---|
91 | VDSCRIPTTOKENKEYWORD_DEFAULT,
|
---|
92 | VDSCRIPTTOKENKEYWORD_RETURN,
|
---|
93 | VDSCRIPTTOKENKEYWORD_SWITCH,
|
---|
94 | VDSCRIPTTOKENKEYWORD_WHILE,
|
---|
95 | VDSCRIPTTOKENKEYWORD_BREAK,
|
---|
96 | VDSCRIPTTOKENKEYWORD_FALSE,
|
---|
97 | VDSCRIPTTOKENKEYWORD_TRUE,
|
---|
98 | VDSCRIPTTOKENKEYWORD_ELSE,
|
---|
99 | VDSCRIPTTOKENKEYWORD_CASE,
|
---|
100 | VDSCRIPTTOKENKEYWORD_FOR,
|
---|
101 | VDSCRIPTTOKENKEYWORD_IF,
|
---|
102 | VDSCRIPTTOKENKEYWORD_DO,
|
---|
103 | VDSCRIPTTOKENKEYWORD_32BIT_HACK = 0x7fffffff
|
---|
104 | } VDSCRIPTTOKENKEYWORD;
|
---|
105 | /** Pointer to a keyword type. */
|
---|
106 | typedef VDSCRIPTTOKENKEYWORD *PVDSCRIPTTOKENKEYWORD;
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * VD script token.
|
---|
110 | */
|
---|
111 | typedef struct VDSCRIPTTOKEN
|
---|
112 | {
|
---|
113 | /** Token class. */
|
---|
114 | VDTOKENCLASS enmClass;
|
---|
115 | /** Token position in the source buffer. */
|
---|
116 | VDSRCPOS Pos;
|
---|
117 | /** Data based on the token class. */
|
---|
118 | union
|
---|
119 | {
|
---|
120 | /** Identifier. */
|
---|
121 | struct
|
---|
122 | {
|
---|
123 | /** Pointer to the start of the identifier. */
|
---|
124 | const char *pszIde;
|
---|
125 | /** Number of characters for the identifier excluding the null terminator. */
|
---|
126 | size_t cchIde;
|
---|
127 | } Ide;
|
---|
128 | /** Numerical constant. */
|
---|
129 | struct
|
---|
130 | {
|
---|
131 | uint64_t u64;
|
---|
132 | } NumConst;
|
---|
133 | /** String constant */
|
---|
134 | struct
|
---|
135 | {
|
---|
136 | /** Pointer to the start of the string constant. */
|
---|
137 | const char *pszString;
|
---|
138 | /** Number of characters of the string, including the null terminator. */
|
---|
139 | size_t cchString;
|
---|
140 | } StringConst;
|
---|
141 | /** Operator */
|
---|
142 | struct
|
---|
143 | {
|
---|
144 | /** The operator string. */
|
---|
145 | char aszOp[4]; /** Maximum of 3 for >>= + null terminator. */
|
---|
146 | } Operator;
|
---|
147 | /** Keyword. */
|
---|
148 | struct
|
---|
149 | {
|
---|
150 | /** The keyword type. */
|
---|
151 | VDSCRIPTTOKENKEYWORD enmKeyword;
|
---|
152 | } Keyword;
|
---|
153 | /** Punctuator. */
|
---|
154 | struct
|
---|
155 | {
|
---|
156 | /** The punctuator in question. */
|
---|
157 | char chPunctuator;
|
---|
158 | } Punctuator;
|
---|
159 | } Class;
|
---|
160 | } VDSCRIPTTOKEN;
|
---|
161 | /** Pointer to a script token. */
|
---|
162 | typedef VDSCRIPTTOKEN *PVDSCRIPTTOKEN;
|
---|
163 | /** Pointer to a const script token. */
|
---|
164 | typedef const VDSCRIPTTOKEN *PCVDSCRIPTTOKEN;
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Tokenizer state.
|
---|
168 | */
|
---|
169 | typedef struct VDTOKENIZER
|
---|
170 | {
|
---|
171 | /** Char buffer to read from. */
|
---|
172 | const char *pszInput;
|
---|
173 | /** Current position ininput buffer. */
|
---|
174 | VDSRCPOS Pos;
|
---|
175 | /** Token 1. */
|
---|
176 | VDSCRIPTTOKEN Token1;
|
---|
177 | /** Token 2. */
|
---|
178 | VDSCRIPTTOKEN Token2;
|
---|
179 | /** Pointer to the current active token. */
|
---|
180 | PVDSCRIPTTOKEN pTokenCurr;
|
---|
181 | /** The next token in the input stream (used for peeking). */
|
---|
182 | PVDSCRIPTTOKEN pTokenNext;
|
---|
183 | } VDTOKENIZER;
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Operators entry.
|
---|
187 | */
|
---|
188 | typedef struct VDSCRIPTOP
|
---|
189 | {
|
---|
190 | /** Operator string. */
|
---|
191 | const char *pszOp;
|
---|
192 | /** Size of the operator in characters without zero terminator. */
|
---|
193 | size_t cchOp;
|
---|
194 | } VDSCRIPTOP;
|
---|
195 | /** Pointer to a script operator. */
|
---|
196 | typedef VDSCRIPTOP *PVDSCRIPTOP;
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * Known operators array, sort from higest character count to lowest.
|
---|
200 | */
|
---|
201 | static VDSCRIPTOP g_aScriptOps[] =
|
---|
202 | {
|
---|
203 | {">>=", 3},
|
---|
204 | {"<<=", 3},
|
---|
205 | {"+=", 2},
|
---|
206 | {"-=", 2},
|
---|
207 | {"/=", 2},
|
---|
208 | {"%=", 2},
|
---|
209 | {"&=", 2},
|
---|
210 | {"|=", 2},
|
---|
211 | {"^=", 2},
|
---|
212 | {"&&", 2},
|
---|
213 | {"||", 2},
|
---|
214 | {"<<", 2},
|
---|
215 | {">>", 2},
|
---|
216 | {"++", 2},
|
---|
217 | {"--", 2},
|
---|
218 | {"==", 2},
|
---|
219 | {"!=", 2},
|
---|
220 | {">=", 2},
|
---|
221 | {"<=", 2},
|
---|
222 | {"=", 1},
|
---|
223 | {"+", 1},
|
---|
224 | {"-", 1},
|
---|
225 | {"*", 1},
|
---|
226 | {"/", 1},
|
---|
227 | {"%", 1},
|
---|
228 | {"|", 1},
|
---|
229 | {"&", 1},
|
---|
230 | {"^", 1},
|
---|
231 | {"<", 1},
|
---|
232 | {">", 1},
|
---|
233 | {"!", 1},
|
---|
234 | {"~", 1}
|
---|
235 | };
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * Known punctuators.
|
---|
239 | */
|
---|
240 | static VDSCRIPTOP g_aScriptPunctuators[] =
|
---|
241 | {
|
---|
242 | {"(", 1},
|
---|
243 | {")", 1},
|
---|
244 | {"{", 1},
|
---|
245 | {"}", 1},
|
---|
246 | {",", 1},
|
---|
247 | {";", 1},
|
---|
248 | };
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Keyword entry.
|
---|
252 | */
|
---|
253 | typedef struct VDSCRIPTKEYWORD
|
---|
254 | {
|
---|
255 | /** Keyword string. */
|
---|
256 | const char *pszKeyword;
|
---|
257 | /** Size of the string in characters without zero terminator. */
|
---|
258 | size_t cchKeyword;
|
---|
259 | /** Keyword type. */
|
---|
260 | VDSCRIPTTOKENKEYWORD enmKeyword;
|
---|
261 | } VDSCRIPTKEYWORD;
|
---|
262 | /** */
|
---|
263 | typedef VDSCRIPTKEYWORD *PVDSCRIPTKEYWORD;
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Known keywords.
|
---|
267 | */
|
---|
268 | static VDSCRIPTKEYWORD g_aKeywords[] =
|
---|
269 | {
|
---|
270 | {"continue", 8, VDSCRIPTTOKENKEYWORD_CONTINUE},
|
---|
271 | {"default", 7, VDSCRIPTTOKENKEYWORD_DEFAULT},
|
---|
272 | {"return", 6, VDSCRIPTTOKENKEYWORD_RETURN},
|
---|
273 | {"switch", 6, VDSCRIPTTOKENKEYWORD_SWITCH},
|
---|
274 | {"while", 5, VDSCRIPTTOKENKEYWORD_WHILE},
|
---|
275 | {"break", 5, VDSCRIPTTOKENKEYWORD_BREAK},
|
---|
276 | {"false", 5, VDSCRIPTTOKENKEYWORD_FALSE},
|
---|
277 | {"true", 4, VDSCRIPTTOKENKEYWORD_TRUE},
|
---|
278 | {"else", 4, VDSCRIPTTOKENKEYWORD_ELSE},
|
---|
279 | {"case", 4, VDSCRIPTTOKENKEYWORD_CASE},
|
---|
280 | {"for", 3, VDSCRIPTTOKENKEYWORD_FOR},
|
---|
281 | {"if", 2, VDSCRIPTTOKENKEYWORD_IF},
|
---|
282 | {"do", 2, VDSCRIPTTOKENKEYWORD_DO}
|
---|
283 | };
|
---|
284 |
|
---|
285 | static int vdScriptParseCompoundStatement(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTSTMT *ppAstNodeCompound);
|
---|
286 | static int vdScriptParseStatement(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTSTMT *ppAstNodeStmt);
|
---|
287 | static int vdScriptParseExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr);
|
---|
288 | static int vdScriptParseAssignmentExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr);
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Returns whether the tokenizer reached the end of the stream.
|
---|
292 | *
|
---|
293 | * @returns true if the tokenizer reached the end of stream marker
|
---|
294 | * false otherwise.
|
---|
295 | * @param pTokenizer The tokenizer state.
|
---|
296 | */
|
---|
297 | DECLINLINE(bool) vdScriptTokenizerIsEos(PVDTOKENIZER pTokenizer)
|
---|
298 | {
|
---|
299 | return *pTokenizer->pszInput == '\0';
|
---|
300 | }
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * Skip one character in the input stream.
|
---|
304 | *
|
---|
305 | * @returns nothing.
|
---|
306 | * @param pTokenizer The tokenizer state.
|
---|
307 | */
|
---|
308 | DECLINLINE(void) vdScriptTokenizerSkipCh(PVDTOKENIZER pTokenizer)
|
---|
309 | {
|
---|
310 | pTokenizer->pszInput++;
|
---|
311 | pTokenizer->Pos.iChStart++;
|
---|
312 | pTokenizer->Pos.iChEnd++;
|
---|
313 | }
|
---|
314 |
|
---|
315 | /**
|
---|
316 | * Returns the next char in the input buffer without advancing it.
|
---|
317 | *
|
---|
318 | * @returns Next character in the input buffer.
|
---|
319 | * @param pTokenizer The tokenizer state.
|
---|
320 | */
|
---|
321 | DECLINLINE(char) vdScriptTokenizerPeekCh(PVDTOKENIZER pTokenizer)
|
---|
322 | {
|
---|
323 | return vdScriptTokenizerIsEos(pTokenizer)
|
---|
324 | ? '\0'
|
---|
325 | : *(pTokenizer->pszInput + 1);
|
---|
326 | }
|
---|
327 |
|
---|
328 | /**
|
---|
329 | * Returns the next character in the input buffer advancing the internal
|
---|
330 | * position.
|
---|
331 | *
|
---|
332 | * @returns Next character in the stream.
|
---|
333 | * @param pTokenizer The tokenizer state.
|
---|
334 | */
|
---|
335 | DECLINLINE(char) vdScriptTokenizerGetCh(PVDTOKENIZER pTokenizer)
|
---|
336 | {
|
---|
337 | char ch;
|
---|
338 |
|
---|
339 | if (vdScriptTokenizerIsEos(pTokenizer))
|
---|
340 | ch = '\0';
|
---|
341 | else
|
---|
342 | ch = *pTokenizer->pszInput;
|
---|
343 |
|
---|
344 | return ch;
|
---|
345 | }
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * Sets a new line for the tokenizer.
|
---|
349 | *
|
---|
350 | * @returns nothing.
|
---|
351 | * @param pTokenizer The tokenizer state.
|
---|
352 | */
|
---|
353 | DECLINLINE(void) vdScriptTokenizerNewLine(PVDTOKENIZER pTokenizer, unsigned cSkip)
|
---|
354 | {
|
---|
355 | pTokenizer->pszInput += cSkip;
|
---|
356 | pTokenizer->Pos.iLine++;
|
---|
357 | pTokenizer->Pos.iChStart = 1;
|
---|
358 | pTokenizer->Pos.iChEnd = 1;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * Checks whether the current position in the input stream is a new line
|
---|
363 | * and skips it.
|
---|
364 | *
|
---|
365 | * @returns Flag whether there was a new line at the current position
|
---|
366 | * in the input buffer.
|
---|
367 | * @param pTokenizer The tokenizer state.
|
---|
368 | */
|
---|
369 | DECLINLINE(bool) vdScriptTokenizerIsSkipNewLine(PVDTOKENIZER pTokenizer)
|
---|
370 | {
|
---|
371 | bool fNewline = true;
|
---|
372 |
|
---|
373 | if ( vdScriptTokenizerGetCh(pTokenizer) == '\r'
|
---|
374 | && vdScriptTokenizerPeekCh(pTokenizer) == '\n')
|
---|
375 | vdScriptTokenizerNewLine(pTokenizer, 2);
|
---|
376 | else if (vdScriptTokenizerGetCh(pTokenizer) == '\n')
|
---|
377 | vdScriptTokenizerNewLine(pTokenizer, 1);
|
---|
378 | else
|
---|
379 | fNewline = false;
|
---|
380 |
|
---|
381 | return fNewline;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Skips a multi line comment.
|
---|
386 | *
|
---|
387 | * @returns nothing.
|
---|
388 | * @param pTokenizer The tokenizer state.
|
---|
389 | */
|
---|
390 | DECLINLINE(void) vdScriptTokenizerSkipComment(PVDTOKENIZER pTokenizer)
|
---|
391 | {
|
---|
392 | while ( !vdScriptTokenizerIsEos(pTokenizer)
|
---|
393 | && ( vdScriptTokenizerGetCh(pTokenizer) != '*'
|
---|
394 | || vdScriptTokenizerPeekCh(pTokenizer) != '/'))
|
---|
395 | {
|
---|
396 | if (!vdScriptTokenizerIsSkipNewLine(pTokenizer))
|
---|
397 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
398 | }
|
---|
399 |
|
---|
400 | if (!vdScriptTokenizerIsEos(pTokenizer))
|
---|
401 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
402 | if (!vdScriptTokenizerIsEos(pTokenizer))
|
---|
403 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
404 | }
|
---|
405 |
|
---|
406 | /**
|
---|
407 | * Skip all whitespace starting from the current input buffer position.
|
---|
408 | * Skips all present comments too.
|
---|
409 | *
|
---|
410 | * @returns nothing.
|
---|
411 | * @param pTokenizer The tokenizer state.
|
---|
412 | */
|
---|
413 | DECLINLINE(void) vdScriptTokenizerSkipWhitespace(PVDTOKENIZER pTokenizer)
|
---|
414 | {
|
---|
415 | while (!vdScriptTokenizerIsEos(pTokenizer))
|
---|
416 | {
|
---|
417 | while ( vdScriptTokenizerGetCh(pTokenizer) == ' '
|
---|
418 | || vdScriptTokenizerGetCh(pTokenizer) == '\t')
|
---|
419 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
420 |
|
---|
421 | if ( !vdScriptTokenizerIsEos(pTokenizer)
|
---|
422 | && !vdScriptTokenizerIsSkipNewLine(pTokenizer))
|
---|
423 | {
|
---|
424 | if ( vdScriptTokenizerGetCh(pTokenizer) == '/'
|
---|
425 | && vdScriptTokenizerPeekCh(pTokenizer) == '*')
|
---|
426 | {
|
---|
427 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
428 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
429 | vdScriptTokenizerSkipComment(pTokenizer);
|
---|
430 | }
|
---|
431 | else
|
---|
432 | break; /* Skipped everything, next is some real content. */
|
---|
433 | }
|
---|
434 | }
|
---|
435 | }
|
---|
436 |
|
---|
437 | /**
|
---|
438 | * Get an identifier token from the tokenizer.
|
---|
439 | *
|
---|
440 | * @returns nothing.
|
---|
441 | * @param pTokenizer The tokenizer state.
|
---|
442 | * @param pToken The uninitialized token.
|
---|
443 | */
|
---|
444 | static void vdScriptTokenizerGetIdeOrKeyword(PVDTOKENIZER pTokenizer, PVDSCRIPTTOKEN pToken)
|
---|
445 | {
|
---|
446 | char ch;
|
---|
447 | size_t cchIde = 0;
|
---|
448 | bool fIsKeyword = false;
|
---|
449 | const char *pszIde = pTokenizer->pszInput;
|
---|
450 |
|
---|
451 | pToken->Pos = pTokenizer->Pos;
|
---|
452 |
|
---|
453 | Assert(RT_C_IS_ALPHA(*pszIde) || *pszIde == '_' );
|
---|
454 |
|
---|
455 | do
|
---|
456 | {
|
---|
457 | cchIde++;
|
---|
458 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
459 | ch = vdScriptTokenizerGetCh(pTokenizer);
|
---|
460 | }
|
---|
461 | while (RT_C_IS_ALNUM(ch) || ch == '_');
|
---|
462 |
|
---|
463 | /* Check whether we got an identifier or an reserved keyword. */
|
---|
464 | for (unsigned i = 0; i < RT_ELEMENTS(g_aKeywords); i++)
|
---|
465 | {
|
---|
466 | if (!RTStrNCmp(g_aKeywords[i].pszKeyword, pszIde, g_aKeywords[i].cchKeyword))
|
---|
467 | {
|
---|
468 | fIsKeyword = true;
|
---|
469 | pToken->enmClass = VDTOKENCLASS_KEYWORD;
|
---|
470 | pToken->Class.Keyword.enmKeyword = g_aKeywords[i].enmKeyword;
|
---|
471 | break;
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | if (!fIsKeyword)
|
---|
476 | {
|
---|
477 | pToken->enmClass = VDTOKENCLASS_IDENTIFIER;
|
---|
478 | pToken->Class.Ide.pszIde = pszIde;
|
---|
479 | pToken->Class.Ide.cchIde = cchIde;
|
---|
480 | }
|
---|
481 | pToken->Pos.iChEnd += cchIde;
|
---|
482 | }
|
---|
483 |
|
---|
484 | /**
|
---|
485 | * Get a numerical constant from the tokenizer.
|
---|
486 | *
|
---|
487 | * @returns nothing.
|
---|
488 | * @param pTokenizer The tokenizer state.
|
---|
489 | * @param pToken The uninitialized token.
|
---|
490 | */
|
---|
491 | static void vdScriptTokenizerGetNumberConst(PVDTOKENIZER pTokenizer, PVDSCRIPTTOKEN pToken)
|
---|
492 | {
|
---|
493 | unsigned uBase = 10;
|
---|
494 | char *pszNext = NULL;
|
---|
495 |
|
---|
496 | Assert(RT_C_IS_DIGIT(vdScriptTokenizerGetCh(pTokenizer)));
|
---|
497 |
|
---|
498 | /* Let RTStrToUInt64Ex() do all the work, looks C compliant :). */
|
---|
499 | pToken->enmClass = VDTOKENCLASS_NUMCONST;
|
---|
500 | int rc = RTStrToUInt64Ex(pTokenizer->pszInput, &pszNext, 0, &pToken->Class.NumConst.u64);
|
---|
501 | Assert(RT_SUCCESS(rc) || rc == VWRN_TRAILING_CHARS || rc == VWRN_TRAILING_SPACES);
|
---|
502 | /** @todo: Handle number to big, throw a warning */
|
---|
503 |
|
---|
504 | unsigned cchNumber = pszNext - pTokenizer->pszInput;
|
---|
505 | for (unsigned i = 0; i < cchNumber; i++)
|
---|
506 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
507 |
|
---|
508 | /* Check for a supported suffix, supported are K|M|G. */
|
---|
509 | if (vdScriptTokenizerGetCh(pTokenizer) == 'K')
|
---|
510 | {
|
---|
511 | pToken->Class.NumConst.u64 *= _1K;
|
---|
512 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
513 | }
|
---|
514 | else if (vdScriptTokenizerGetCh(pTokenizer) == 'M')
|
---|
515 | {
|
---|
516 | pToken->Class.NumConst.u64 *= _1M;
|
---|
517 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
518 | }
|
---|
519 | else if (vdScriptTokenizerGetCh(pTokenizer) == 'G')
|
---|
520 | {
|
---|
521 | pToken->Class.NumConst.u64 *= _1G;
|
---|
522 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
523 | }
|
---|
524 | }
|
---|
525 |
|
---|
526 | /**
|
---|
527 | * Parses a string constant.
|
---|
528 | *
|
---|
529 | * @returns nothing.
|
---|
530 | * @param pTokenizer The tokenizer state.
|
---|
531 | * @param pToken The uninitialized token.
|
---|
532 | *
|
---|
533 | * @remarks: No escape sequences allowed at this time.
|
---|
534 | */
|
---|
535 | static void vdScriptTokenizerGetStringConst(PVDTOKENIZER pTokenizer, PVDSCRIPTTOKEN pToken)
|
---|
536 | {
|
---|
537 | size_t cchStr = 0;
|
---|
538 |
|
---|
539 | Assert(vdScriptTokenizerGetCh(pTokenizer) == '\"');
|
---|
540 | vdScriptTokenizerSkipCh(pTokenizer); /* Skip " */
|
---|
541 |
|
---|
542 | pToken->enmClass = VDTOKENCLASS_STRINGCONST;
|
---|
543 | pToken->Pos = pTokenizer->Pos;
|
---|
544 | pToken->Class.StringConst.pszString = pTokenizer->pszInput;
|
---|
545 |
|
---|
546 | while (vdScriptTokenizerGetCh(pTokenizer) != '\"')
|
---|
547 | {
|
---|
548 | cchStr++;
|
---|
549 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
550 | }
|
---|
551 |
|
---|
552 | vdScriptTokenizerSkipCh(pTokenizer); /* Skip closing " */
|
---|
553 |
|
---|
554 | pToken->Class.StringConst.cchString = cchStr;
|
---|
555 | pToken->Pos.iChEnd += cchStr;
|
---|
556 | }
|
---|
557 |
|
---|
558 | /**
|
---|
559 | * Get the end of stream token.
|
---|
560 | *
|
---|
561 | * @returns nothing.
|
---|
562 | * @param pTokenizer The tokenizer state.
|
---|
563 | * @param pToken The uninitialized token.
|
---|
564 | */
|
---|
565 | static void vdScriptTokenizerGetEos(PVDTOKENIZER pTokenizer, PVDSCRIPTTOKEN pToken)
|
---|
566 | {
|
---|
567 | Assert(vdScriptTokenizerGetCh(pTokenizer) == '\0');
|
---|
568 |
|
---|
569 | pToken->enmClass = VDTOKENCLASS_EOS;
|
---|
570 | pToken->Pos = pTokenizer->Pos;
|
---|
571 | }
|
---|
572 |
|
---|
573 | /**
|
---|
574 | * Get operator or punctuator token.
|
---|
575 | *
|
---|
576 | * @returns nothing.
|
---|
577 | * @param pTokenizer The tokenizer state.
|
---|
578 | * @param pToken The uninitialized token.
|
---|
579 | */
|
---|
580 | static void vdScriptTokenizerGetOperatorOrPunctuator(PVDTOKENIZER pTokenizer, PVDSCRIPTTOKEN pToken)
|
---|
581 | {
|
---|
582 | bool fOpFound = false;
|
---|
583 |
|
---|
584 | pToken->enmClass = VDTOKENCLASS_INVALID;
|
---|
585 | pToken->Pos = pTokenizer->Pos;
|
---|
586 |
|
---|
587 | /*
|
---|
588 | * Use table based approach here, not the fastest solution but enough for our purpose
|
---|
589 | * for now.
|
---|
590 | */
|
---|
591 | for (unsigned i = 0; i < RT_ELEMENTS(g_aScriptOps); i++)
|
---|
592 | {
|
---|
593 | if (!RTStrNCmp(g_aScriptOps[i].pszOp, pTokenizer->pszInput, g_aScriptOps[i].cchOp))
|
---|
594 | {
|
---|
595 | memset(pToken->Class.Operator.aszOp, 0, sizeof(pToken->Class.Operator.aszOp));
|
---|
596 |
|
---|
597 | int rc = RTStrCopy(pToken->Class.Operator.aszOp, sizeof(pToken->Class.Operator.aszOp), g_aScriptOps[i].pszOp);
|
---|
598 | AssertRC(rc);
|
---|
599 |
|
---|
600 | pToken->enmClass = VDTOKENCLASS_OPERATORS;
|
---|
601 | pToken->Pos.iChEnd += g_aScriptOps[i].cchOp;
|
---|
602 |
|
---|
603 | /** @todo: Make this prettier. */
|
---|
604 | for (unsigned j = 0; j < g_aScriptOps[i].cchOp; j++)
|
---|
605 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
606 | fOpFound = true;
|
---|
607 | break;
|
---|
608 | }
|
---|
609 | }
|
---|
610 |
|
---|
611 | if (!fOpFound)
|
---|
612 | {
|
---|
613 | for (unsigned i = 0; i < RT_ELEMENTS(g_aScriptPunctuators); i++)
|
---|
614 | {
|
---|
615 | if (!RTStrNCmp(g_aScriptPunctuators[i].pszOp, pTokenizer->pszInput, g_aScriptPunctuators[i].cchOp))
|
---|
616 | {
|
---|
617 | pToken->Pos.iChEnd += g_aScriptPunctuators[i].cchOp;
|
---|
618 | pToken->enmClass = VDTOKENCLASS_PUNCTUATOR;
|
---|
619 | pToken->Class.Punctuator.chPunctuator = *g_aScriptPunctuators[i].pszOp;
|
---|
620 |
|
---|
621 | vdScriptTokenizerSkipCh(pTokenizer);
|
---|
622 | fOpFound = true;
|
---|
623 | break;
|
---|
624 | }
|
---|
625 | }
|
---|
626 | }
|
---|
627 | }
|
---|
628 |
|
---|
629 | /**
|
---|
630 | * Read the next token from the tokenizer stream.
|
---|
631 | *
|
---|
632 | * @returns nothing.
|
---|
633 | * @param pTokenizer The tokenizer to read from.
|
---|
634 | * @param pToken Uninitialized token to fill the token data into.
|
---|
635 | */
|
---|
636 | static void vdScriptTokenizerReadNextToken(PVDTOKENIZER pTokenizer, PVDSCRIPTTOKEN pToken)
|
---|
637 | {
|
---|
638 | /* Skip all eventually existing whitespace, newlines and comments first. */
|
---|
639 | vdScriptTokenizerSkipWhitespace(pTokenizer);
|
---|
640 |
|
---|
641 | char ch = vdScriptTokenizerGetCh(pTokenizer);
|
---|
642 | if (RT_C_IS_ALPHA(ch) || ch == '_')
|
---|
643 | vdScriptTokenizerGetIdeOrKeyword(pTokenizer, pToken);
|
---|
644 | else if (RT_C_IS_DIGIT(ch))
|
---|
645 | vdScriptTokenizerGetNumberConst(pTokenizer, pToken);
|
---|
646 | else if (ch == '\"')
|
---|
647 | vdScriptTokenizerGetStringConst(pTokenizer, pToken);
|
---|
648 | else if (ch == '\0')
|
---|
649 | vdScriptTokenizerGetEos(pTokenizer, pToken);
|
---|
650 | else
|
---|
651 | vdScriptTokenizerGetOperatorOrPunctuator(pTokenizer, pToken);
|
---|
652 | }
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * Create a new tokenizer.
|
---|
656 | *
|
---|
657 | * @returns Pointer to the new tokenizer state on success.
|
---|
658 | * NULL if out of memory.
|
---|
659 | * @param pszInput The input to create the tokenizer for.
|
---|
660 | */
|
---|
661 | static PVDTOKENIZER vdScriptTokenizerCreate(const char *pszInput)
|
---|
662 | {
|
---|
663 | PVDTOKENIZER pTokenizer = (PVDTOKENIZER)RTMemAllocZ(sizeof(VDTOKENIZER));
|
---|
664 | if (pTokenizer)
|
---|
665 | {
|
---|
666 | pTokenizer->pszInput = pszInput;
|
---|
667 | pTokenizer->Pos.iLine = 1;
|
---|
668 | pTokenizer->Pos.iChStart = 1;
|
---|
669 | pTokenizer->Pos.iChEnd = 1;
|
---|
670 | pTokenizer->pTokenCurr = &pTokenizer->Token1;
|
---|
671 | pTokenizer->pTokenNext = &pTokenizer->Token2;
|
---|
672 | /* Fill the tokenizer with two first tokens. */
|
---|
673 | vdScriptTokenizerReadNextToken(pTokenizer, pTokenizer->pTokenCurr);
|
---|
674 | vdScriptTokenizerReadNextToken(pTokenizer, pTokenizer->pTokenNext);
|
---|
675 | }
|
---|
676 |
|
---|
677 | return pTokenizer;
|
---|
678 | }
|
---|
679 |
|
---|
680 | /**
|
---|
681 | * Destroys a given tokenizer state.
|
---|
682 | *
|
---|
683 | * @returns nothing.
|
---|
684 | * @param pTokenizer The tokenizer to destroy.
|
---|
685 | */
|
---|
686 | static void vdScriptTokenizerDestroy(PVDTOKENIZER pTokenizer)
|
---|
687 | {
|
---|
688 | RTMemFree(pTokenizer);
|
---|
689 | }
|
---|
690 |
|
---|
691 | /**
|
---|
692 | * Get the current token in the input stream.
|
---|
693 | *
|
---|
694 | * @returns Pointer to the next token in the stream.
|
---|
695 | * @param pTokenizer The tokenizer to destroy.
|
---|
696 | */
|
---|
697 | DECLINLINE(PCVDSCRIPTTOKEN) vdScriptTokenizerGetToken(PVDTOKENIZER pTokenizer)
|
---|
698 | {
|
---|
699 | return pTokenizer->pTokenCurr;
|
---|
700 | }
|
---|
701 |
|
---|
702 | /**
|
---|
703 | * Get the class of the current token.
|
---|
704 | *
|
---|
705 | * @returns Class of the current token.
|
---|
706 | * @param pTokenizer The tokenizer state.
|
---|
707 | */
|
---|
708 | DECLINLINE(VDTOKENCLASS) vdScriptTokenizerGetTokenClass(PVDTOKENIZER pTokenizer)
|
---|
709 | {
|
---|
710 | return pTokenizer->pTokenCurr->enmClass;
|
---|
711 | }
|
---|
712 |
|
---|
713 | /**
|
---|
714 | * Returns the token class of the next token in the stream.
|
---|
715 | *
|
---|
716 | * @returns Token class of the next token.
|
---|
717 | * @param pTokenizer The tokenizer state.
|
---|
718 | */
|
---|
719 | DECLINLINE(VDTOKENCLASS) vdScriptTokenizerPeekNextClass(PVDTOKENIZER pTokenizer)
|
---|
720 | {
|
---|
721 | return pTokenizer->pTokenNext->enmClass;
|
---|
722 | }
|
---|
723 |
|
---|
724 | /**
|
---|
725 | * Consume the current token advancing to the next in the stream.
|
---|
726 | *
|
---|
727 | * @returns nothing.
|
---|
728 | * @param pTokenizer The tokenizer state.
|
---|
729 | */
|
---|
730 | static void vdScriptTokenizerConsume(PVDTOKENIZER pTokenizer)
|
---|
731 | {
|
---|
732 | PVDSCRIPTTOKEN pTokenTmp = pTokenizer->pTokenCurr;
|
---|
733 |
|
---|
734 | /* Switch next token to current token and read in the next token. */
|
---|
735 | pTokenizer->pTokenCurr = pTokenizer->pTokenNext;
|
---|
736 | pTokenizer->pTokenNext = pTokenTmp;
|
---|
737 | vdScriptTokenizerReadNextToken(pTokenizer, pTokenizer->pTokenNext);
|
---|
738 | }
|
---|
739 |
|
---|
740 | /**
|
---|
741 | * Check whether the next token in the input stream is a punctuator and matches the given
|
---|
742 | * character.
|
---|
743 | *
|
---|
744 | * @returns true if the token matched.
|
---|
745 | * false otherwise.
|
---|
746 | * @param pTokenizer The tokenizer state.
|
---|
747 | * @param chCheck The punctuator to check against.
|
---|
748 | */
|
---|
749 | static bool vdScriptTokenizerIsPunctuatorEqual(PVDTOKENIZER pTokenizer, char chCheck)
|
---|
750 | {
|
---|
751 | PCVDSCRIPTTOKEN pToken = vdScriptTokenizerGetToken(pTokenizer);
|
---|
752 |
|
---|
753 | if ( pToken->enmClass == VDTOKENCLASS_PUNCTUATOR
|
---|
754 | && pToken->Class.Punctuator.chPunctuator == chCheck)
|
---|
755 | return true;
|
---|
756 |
|
---|
757 | return false;
|
---|
758 | }
|
---|
759 |
|
---|
760 | /**
|
---|
761 | * Check whether the next token in the input stream is a punctuator and matches the given
|
---|
762 | * character and skips it.
|
---|
763 | *
|
---|
764 | * @returns true if the token matched and was skipped.
|
---|
765 | * false otherwise.
|
---|
766 | * @param pTokenizer The tokenizer state.
|
---|
767 | * @param chCheck The punctuator to check against.
|
---|
768 | */
|
---|
769 | static bool vdScriptTokenizerSkipIfIsPunctuatorEqual(PVDTOKENIZER pTokenizer, char chCheck)
|
---|
770 | {
|
---|
771 | bool fEqual = vdScriptTokenizerIsPunctuatorEqual(pTokenizer, chCheck);
|
---|
772 | if (fEqual)
|
---|
773 | vdScriptTokenizerConsume(pTokenizer);
|
---|
774 |
|
---|
775 | return fEqual;
|
---|
776 | }
|
---|
777 |
|
---|
778 | /**
|
---|
779 | * Check whether the next token in the input stream is a keyword and matches the given
|
---|
780 | * keyword.
|
---|
781 | *
|
---|
782 | * @returns true if the token matched.
|
---|
783 | * false otherwise.
|
---|
784 | * @param pTokenizer The tokenizer state.
|
---|
785 | * @param enmKey The keyword to check against.
|
---|
786 | */
|
---|
787 | static bool vdScriptTokenizerIsKeywordEqual(PVDTOKENIZER pTokenizer, VDSCRIPTTOKENKEYWORD enmKeyword)
|
---|
788 | {
|
---|
789 | PCVDSCRIPTTOKEN pToken = vdScriptTokenizerGetToken(pTokenizer);
|
---|
790 |
|
---|
791 | if ( pToken->enmClass == VDTOKENCLASS_KEYWORD
|
---|
792 | && pToken->Class.Keyword.enmKeyword == enmKeyword)
|
---|
793 | return true;
|
---|
794 |
|
---|
795 | return false;
|
---|
796 | }
|
---|
797 |
|
---|
798 | /**
|
---|
799 | * Check whether the next token in the input stream is a keyword and matches the given
|
---|
800 | * keyword and skips it.
|
---|
801 | *
|
---|
802 | * @returns true if the token matched and was skipped.
|
---|
803 | * false otherwise.
|
---|
804 | * @param pTokenizer The tokenizer state.
|
---|
805 | * @param enmKey The keyword to check against.
|
---|
806 | */
|
---|
807 | static bool vdScriptTokenizerSkipIfIsKeywordEqual(PVDTOKENIZER pTokenizer, VDSCRIPTTOKENKEYWORD enmKeyword)
|
---|
808 | {
|
---|
809 | bool fEqual = vdScriptTokenizerIsKeywordEqual(pTokenizer, enmKeyword);
|
---|
810 | if (fEqual)
|
---|
811 | vdScriptTokenizerConsume(pTokenizer);
|
---|
812 |
|
---|
813 | return fEqual;
|
---|
814 | }
|
---|
815 |
|
---|
816 | /**
|
---|
817 | * Check whether the next token in the input stream is a keyword and matches the given
|
---|
818 | * keyword.
|
---|
819 | *
|
---|
820 | * @returns true if the token matched.
|
---|
821 | * false otherwise.
|
---|
822 | * @param pTokenizer The tokenizer state.
|
---|
823 | * @param pszOp The operation to check against.
|
---|
824 | */
|
---|
825 | static bool vdScriptTokenizerIsOperatorEqual(PVDTOKENIZER pTokenizer, const char *pszOp)
|
---|
826 | {
|
---|
827 | PCVDSCRIPTTOKEN pToken = vdScriptTokenizerGetToken(pTokenizer);
|
---|
828 |
|
---|
829 | if ( pToken->enmClass == VDTOKENCLASS_OPERATORS
|
---|
830 | && !RTStrCmp(pToken->Class.Operator.aszOp, pszOp))
|
---|
831 | return true;
|
---|
832 |
|
---|
833 | return false;
|
---|
834 | }
|
---|
835 |
|
---|
836 | /**
|
---|
837 | * Check whether the next token in the input stream is an operator and matches the given
|
---|
838 | * keyword and skips it.
|
---|
839 | *
|
---|
840 | * @returns true if the token matched and was skipped.
|
---|
841 | * false otherwise.
|
---|
842 | * @param pTokenizer The tokenizer state.
|
---|
843 | * @param pszOp The operation to check against.
|
---|
844 | */
|
---|
845 | static bool vdScriptTokenizerSkipIfIsOperatorEqual(PVDTOKENIZER pTokenizer, const char *pszOp)
|
---|
846 | {
|
---|
847 | bool fEqual = vdScriptTokenizerIsOperatorEqual(pTokenizer, pszOp);
|
---|
848 | if (fEqual)
|
---|
849 | vdScriptTokenizerConsume(pTokenizer);
|
---|
850 |
|
---|
851 | return fEqual;
|
---|
852 | }
|
---|
853 |
|
---|
854 | /**
|
---|
855 | * Record an error while parsing.
|
---|
856 | *
|
---|
857 | * @returns VBox status code passed.
|
---|
858 | */
|
---|
859 | static int vdScriptParserError(PVDSCRIPTCTXINT pThis, int rc, RT_SRC_POS_DECL, const char *pszFmt, ...)
|
---|
860 | {
|
---|
861 | NOREF(pThis);
|
---|
862 | NOREF(pszFmt);
|
---|
863 | RTPrintf(pszFmt);
|
---|
864 | return rc;
|
---|
865 | }
|
---|
866 |
|
---|
867 | /**
|
---|
868 | * Puts the next identifier AST node on the stack.
|
---|
869 | *
|
---|
870 | * @returns VBox status code.
|
---|
871 | * @param pThis The script context.
|
---|
872 | * @param ppAstNodeIde Where to store the identifier AST node on success.
|
---|
873 | */
|
---|
874 | static int vdScriptParseIde(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTIDE *ppAstNodeIde)
|
---|
875 | {
|
---|
876 | int rc = VINF_SUCCESS;
|
---|
877 | PCVDSCRIPTTOKEN pToken;
|
---|
878 |
|
---|
879 | LogFlowFunc(("pThis=%p ppAstNodeIde=%p\n", pThis, ppAstNodeIde));
|
---|
880 |
|
---|
881 | pToken = vdScriptTokenizerGetToken(pThis->pTokenizer);
|
---|
882 | if (pToken->enmClass != VDTOKENCLASS_IDENTIFIER)
|
---|
883 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected identifer got...\n");
|
---|
884 | else
|
---|
885 | {
|
---|
886 | /* Create new AST node and push onto stack. */
|
---|
887 | PVDSCRIPTASTIDE pAstNodeIde = vdScriptAstNodeIdeAlloc(pToken->Class.Ide.cchIde);
|
---|
888 | if (pAstNodeIde)
|
---|
889 | {
|
---|
890 | rc = RTStrCopyEx(pAstNodeIde->aszIde, pToken->Class.Ide.cchIde + 1, pToken->Class.Ide.pszIde, pToken->Class.Ide.cchIde);
|
---|
891 | AssertRC(rc);
|
---|
892 | pAstNodeIde->cchIde = pToken->Class.Ide.cchIde;
|
---|
893 |
|
---|
894 | *ppAstNodeIde = pAstNodeIde;
|
---|
895 | vdScriptTokenizerConsume(pThis->pTokenizer);
|
---|
896 | }
|
---|
897 | else
|
---|
898 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating identifier AST node\n");
|
---|
899 | }
|
---|
900 |
|
---|
901 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
902 | return rc;
|
---|
903 | }
|
---|
904 |
|
---|
905 | /**
|
---|
906 | * Parse a primary expression.
|
---|
907 | *
|
---|
908 | * @returns VBox status code.
|
---|
909 | * @param pThis The script context.
|
---|
910 | * @param ppAstNodeExpr Where to store the primary expression on success.
|
---|
911 | */
|
---|
912 | static int vdScriptParsePrimaryExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
913 | {
|
---|
914 | int rc = VINF_SUCCESS;
|
---|
915 |
|
---|
916 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
917 |
|
---|
918 | if (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, '('))
|
---|
919 | {
|
---|
920 | rc = vdScriptParseExpression(pThis, ppAstNodeExpr);
|
---|
921 | if (RT_SUCCESS(rc)
|
---|
922 | && !vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ')'))
|
---|
923 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \")\", got ...\n");
|
---|
924 | }
|
---|
925 | else
|
---|
926 | {
|
---|
927 | PVDSCRIPTASTEXPR pExpr = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
928 | if (pExpr)
|
---|
929 | {
|
---|
930 | if (vdScriptTokenizerGetTokenClass(pThis->pTokenizer) == VDTOKENCLASS_IDENTIFIER)
|
---|
931 | {
|
---|
932 | PVDSCRIPTASTIDE pIde = NULL;
|
---|
933 | rc = vdScriptParseIde(pThis, &pIde);
|
---|
934 | if (RT_SUCCESS(rc))
|
---|
935 | {
|
---|
936 | pExpr->enmType = VDSCRIPTEXPRTYPE_PRIMARY_IDENTIFIER;
|
---|
937 | pExpr->pIde = pIde;
|
---|
938 | }
|
---|
939 | }
|
---|
940 | else if (vdScriptTokenizerGetTokenClass(pThis->pTokenizer) == VDTOKENCLASS_NUMCONST)
|
---|
941 | {
|
---|
942 | PCVDSCRIPTTOKEN pToken = vdScriptTokenizerGetToken(pThis->pTokenizer);
|
---|
943 | pExpr->enmType = VDSCRIPTEXPRTYPE_PRIMARY_NUMCONST;
|
---|
944 | pExpr->u64 = pToken->Class.NumConst.u64;
|
---|
945 | vdScriptTokenizerConsume(pThis->pTokenizer);
|
---|
946 | }
|
---|
947 | else if (vdScriptTokenizerGetTokenClass(pThis->pTokenizer) == VDTOKENCLASS_STRINGCONST)
|
---|
948 | {
|
---|
949 | PCVDSCRIPTTOKEN pToken = vdScriptTokenizerGetToken(pThis->pTokenizer);
|
---|
950 | pExpr->enmType = VDSCRIPTEXPRTYPE_PRIMARY_STRINGCONST;
|
---|
951 | pExpr->pszStr = RTStrDupN(pToken->Class.StringConst.pszString, pToken->Class.StringConst.cchString);
|
---|
952 | vdScriptTokenizerConsume(pThis->pTokenizer);
|
---|
953 |
|
---|
954 | if (!pExpr->pszStr)
|
---|
955 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating string\n");
|
---|
956 | }
|
---|
957 | else if (vdScriptTokenizerGetTokenClass(pThis->pTokenizer) == VDTOKENCLASS_KEYWORD)
|
---|
958 | {
|
---|
959 | PCVDSCRIPTTOKEN pToken = vdScriptTokenizerGetToken(pThis->pTokenizer);
|
---|
960 | pExpr->enmType = VDSCRIPTEXPRTYPE_PRIMARY_BOOLEAN;
|
---|
961 |
|
---|
962 | if (pToken->Class.Keyword.enmKeyword == VDSCRIPTTOKENKEYWORD_TRUE)
|
---|
963 | pExpr->f = true;
|
---|
964 | else if (pToken->Class.Keyword.enmKeyword == VDSCRIPTTOKENKEYWORD_FALSE)
|
---|
965 | pExpr->f = false;
|
---|
966 | else
|
---|
967 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Unexpected keyword, expected true or false\n");
|
---|
968 | vdScriptTokenizerConsume(pThis->pTokenizer);
|
---|
969 | }
|
---|
970 | else
|
---|
971 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \"(\" | identifier | constant | string, got ...\n");
|
---|
972 |
|
---|
973 | if (RT_FAILURE(rc))
|
---|
974 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
975 | else
|
---|
976 | *ppAstNodeExpr = pExpr;
|
---|
977 | }
|
---|
978 | else
|
---|
979 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
980 | }
|
---|
981 |
|
---|
982 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
983 | return rc;
|
---|
984 | }
|
---|
985 |
|
---|
986 | /**
|
---|
987 | * Parse an argument list for a function call.
|
---|
988 | *
|
---|
989 | * @returns VBox status code.
|
---|
990 | * @param pThis The script context.
|
---|
991 | * @param pFnCall The function call AST node.
|
---|
992 | */
|
---|
993 | static int vdScriptParseFnCallArgumentList(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR pFnCall)
|
---|
994 | {
|
---|
995 | int rc = VINF_SUCCESS;
|
---|
996 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
997 |
|
---|
998 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
999 |
|
---|
1000 | rc = vdScriptParseAssignmentExpression(pThis, &pExpr);
|
---|
1001 | if (RT_SUCCESS(rc))
|
---|
1002 | {
|
---|
1003 | RTListAppend(&pFnCall->FnCall.ListArgs, &pExpr->Core.ListNode);
|
---|
1004 | while (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ','))
|
---|
1005 | {
|
---|
1006 | rc = vdScriptParseAssignmentExpression(pThis, &pExpr);
|
---|
1007 | if (RT_SUCCESS(rc))
|
---|
1008 | RTListAppend(&pFnCall->FnCall.ListArgs, &pExpr->Core.ListNode);
|
---|
1009 | else
|
---|
1010 | break;
|
---|
1011 | }
|
---|
1012 | if ( RT_SUCCESS(rc)
|
---|
1013 | && !vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ')'))
|
---|
1014 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \")\", got ...\n");
|
---|
1015 | }
|
---|
1016 |
|
---|
1017 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1018 | return rc;
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | /**
|
---|
1022 | * Parse a postfix expression.
|
---|
1023 | *
|
---|
1024 | * @returns VBox status code.
|
---|
1025 | * @param pThis The script context.
|
---|
1026 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1027 | *
|
---|
1028 | * @note Syntax:
|
---|
1029 | * postfix-expression:
|
---|
1030 | * primary-expression
|
---|
1031 | * postfix-expression ( argument-expression )
|
---|
1032 | * postfix-expression ++
|
---|
1033 | * postfix-expression --
|
---|
1034 | */
|
---|
1035 | static int vdScriptParsePostfixExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1036 | {
|
---|
1037 | int rc = VINF_SUCCESS;
|
---|
1038 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
1039 |
|
---|
1040 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1041 |
|
---|
1042 | rc = vdScriptParsePrimaryExpression(pThis, &pExpr);
|
---|
1043 | if (RT_SUCCESS(rc))
|
---|
1044 | {
|
---|
1045 | while (true)
|
---|
1046 | {
|
---|
1047 | PVDSCRIPTASTEXPR pExprNew = NULL;
|
---|
1048 |
|
---|
1049 | if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "++"))
|
---|
1050 | {
|
---|
1051 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1052 | if (pExprNew)
|
---|
1053 | {
|
---|
1054 | pExprNew->enmType = VDSCRIPTEXPRTYPE_POSTFIX_INCREMENT;
|
---|
1055 | pExprNew->pExpr = pExpr;
|
---|
1056 | pExpr = pExprNew;
|
---|
1057 | }
|
---|
1058 | else
|
---|
1059 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1060 | }
|
---|
1061 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "--"))
|
---|
1062 | {
|
---|
1063 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1064 | if (pExprNew)
|
---|
1065 | {
|
---|
1066 | pExprNew->enmType = VDSCRIPTEXPRTYPE_POSTFIX_DECREMENT;
|
---|
1067 | pExprNew->pExpr = pExpr;
|
---|
1068 | pExpr = pExprNew;
|
---|
1069 | }
|
---|
1070 | else
|
---|
1071 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1072 | }
|
---|
1073 | else if (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, '('))
|
---|
1074 | {
|
---|
1075 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1076 | if (pExprNew)
|
---|
1077 | {
|
---|
1078 | pExprNew->enmType = VDSCRIPTEXPRTYPE_POSTFIX_FNCALL;
|
---|
1079 | RTListInit(&pExprNew->FnCall.ListArgs);
|
---|
1080 | if (!vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ')'))
|
---|
1081 | rc = vdScriptParseFnCallArgumentList(pThis, pExprNew);
|
---|
1082 | pExprNew->FnCall.pFnIde = pExpr;
|
---|
1083 | pExpr = pExprNew;
|
---|
1084 | }
|
---|
1085 | else
|
---|
1086 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1087 | }
|
---|
1088 | else
|
---|
1089 | break;
|
---|
1090 |
|
---|
1091 | if (RT_FAILURE(rc))
|
---|
1092 | break;
|
---|
1093 | }
|
---|
1094 |
|
---|
1095 | if (RT_SUCCESS(rc))
|
---|
1096 | *ppAstNodeExpr = pExpr;
|
---|
1097 | else
|
---|
1098 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
1099 | }
|
---|
1100 |
|
---|
1101 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1102 | return rc;
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | /**
|
---|
1106 | * Parse an unary expression.
|
---|
1107 | *
|
---|
1108 | * @returns VBox status code.
|
---|
1109 | * @param pThis The script context.
|
---|
1110 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1111 | *
|
---|
1112 | * @note Syntax:
|
---|
1113 | * unary-expression:
|
---|
1114 | * postfix-expression
|
---|
1115 | * ++ unary-expression
|
---|
1116 | * -- unary-expression
|
---|
1117 | * + unary-expression
|
---|
1118 | * - unary-expression
|
---|
1119 | * ~ unary-expression
|
---|
1120 | * ! unary-expression
|
---|
1121 | */
|
---|
1122 | static int vdScriptParseUnaryExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1123 | {
|
---|
1124 | int rc = VINF_SUCCESS;
|
---|
1125 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
1126 | PVDSCRIPTASTEXPR pExprTop = NULL;
|
---|
1127 |
|
---|
1128 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1129 |
|
---|
1130 | while (true)
|
---|
1131 | {
|
---|
1132 | bool fQuit = false;
|
---|
1133 | PVDSCRIPTASTEXPR pExprNew = NULL;
|
---|
1134 |
|
---|
1135 | if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "++"))
|
---|
1136 | {
|
---|
1137 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1138 | if (pExprNew)
|
---|
1139 | pExprNew->enmType = VDSCRIPTEXPRTYPE_UNARY_INCREMENT;
|
---|
1140 | else
|
---|
1141 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1142 | }
|
---|
1143 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "--"))
|
---|
1144 | {
|
---|
1145 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1146 | if (pExprNew)
|
---|
1147 | pExprNew->enmType = VDSCRIPTEXPRTYPE_UNARY_DECREMENT;
|
---|
1148 | else
|
---|
1149 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1150 | }
|
---|
1151 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "+"))
|
---|
1152 | {
|
---|
1153 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1154 | if (pExprNew)
|
---|
1155 | pExprNew->enmType = VDSCRIPTEXPRTYPE_UNARY_POSSIGN;
|
---|
1156 | else
|
---|
1157 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1158 | }
|
---|
1159 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "-"))
|
---|
1160 | {
|
---|
1161 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1162 | if (pExprNew)
|
---|
1163 | pExprNew->enmType = VDSCRIPTEXPRTYPE_UNARY_NEGSIGN;
|
---|
1164 | else
|
---|
1165 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1166 | }
|
---|
1167 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "~"))
|
---|
1168 | {
|
---|
1169 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1170 | if (pExprNew)
|
---|
1171 | pExprNew->enmType = VDSCRIPTEXPRTYPE_UNARY_INVERT;
|
---|
1172 | else
|
---|
1173 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1174 | }
|
---|
1175 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "!"))
|
---|
1176 | {
|
---|
1177 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1178 | if (pExprNew)
|
---|
1179 | pExprNew->enmType = VDSCRIPTEXPRTYPE_UNARY_NEGATE;
|
---|
1180 | else
|
---|
1181 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1182 | }
|
---|
1183 | else
|
---|
1184 | {
|
---|
1185 | /* Must be a postfix expression. */
|
---|
1186 | rc = vdScriptParsePostfixExpression(pThis, &pExprNew);
|
---|
1187 | fQuit = true;
|
---|
1188 | }
|
---|
1189 |
|
---|
1190 | if (RT_SUCCESS(rc))
|
---|
1191 | {
|
---|
1192 | if (!pExprTop)
|
---|
1193 | {
|
---|
1194 | pExprTop = pExprNew;
|
---|
1195 | pExpr = pExprNew;
|
---|
1196 | }
|
---|
1197 | else
|
---|
1198 | {
|
---|
1199 | pExpr->pExpr = pExprNew;
|
---|
1200 | pExpr = pExprNew;
|
---|
1201 | }
|
---|
1202 | if (fQuit)
|
---|
1203 | break;
|
---|
1204 | }
|
---|
1205 | else
|
---|
1206 | break;
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 | if (RT_SUCCESS(rc))
|
---|
1210 | *ppAstNodeExpr = pExprTop;
|
---|
1211 | else if (pExprTop)
|
---|
1212 | vdScriptAstNodeFree(&pExprTop->Core);
|
---|
1213 |
|
---|
1214 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1215 | return rc;
|
---|
1216 | }
|
---|
1217 |
|
---|
1218 | /**
|
---|
1219 | * Parse a multiplicative expression.
|
---|
1220 | *
|
---|
1221 | * @returns VBox status code.
|
---|
1222 | * @param pThis The script context.
|
---|
1223 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1224 | *
|
---|
1225 | * @note Syntax:
|
---|
1226 | * multiplicative-expression:
|
---|
1227 | * unary-expression
|
---|
1228 | * multiplicative-expression * unary-expression
|
---|
1229 | * multiplicative-expression / unary-expression
|
---|
1230 | * multiplicative-expression % unary-expression
|
---|
1231 | */
|
---|
1232 | static int vdScriptParseMultiplicativeExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1233 | {
|
---|
1234 | int rc = VINF_SUCCESS;
|
---|
1235 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
1236 |
|
---|
1237 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1238 |
|
---|
1239 | rc = vdScriptParseUnaryExpression(pThis, &pExpr);
|
---|
1240 | if (RT_SUCCESS(rc))
|
---|
1241 | {
|
---|
1242 | PVDSCRIPTASTEXPR pExprNew = NULL;
|
---|
1243 | while (RT_SUCCESS(rc))
|
---|
1244 | {
|
---|
1245 | if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "*"))
|
---|
1246 | {
|
---|
1247 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1248 | if (pExprNew)
|
---|
1249 | pExprNew->enmType = VDSCRIPTEXPRTYPE_MULTIPLICATION;
|
---|
1250 | else
|
---|
1251 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1252 | }
|
---|
1253 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "/"))
|
---|
1254 | {
|
---|
1255 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1256 | if (pExprNew)
|
---|
1257 | pExprNew->enmType = VDSCRIPTEXPRTYPE_DIVISION;
|
---|
1258 | else
|
---|
1259 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1260 | }
|
---|
1261 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "%"))
|
---|
1262 | {
|
---|
1263 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1264 | if (pExprNew)
|
---|
1265 | pExprNew->enmType = VDSCRIPTEXPRTYPE_MODULUS;
|
---|
1266 | else
|
---|
1267 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1268 | }
|
---|
1269 | else
|
---|
1270 | break;
|
---|
1271 |
|
---|
1272 | pExprNew->BinaryOp.pLeftExpr = pExpr;
|
---|
1273 | pExpr = pExprNew;
|
---|
1274 | rc = vdScriptParseUnaryExpression(pThis, &pExprNew);
|
---|
1275 | if (RT_SUCCESS(rc))
|
---|
1276 | pExpr->BinaryOp.pRightExpr = pExprNew;
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | if (RT_SUCCESS(rc))
|
---|
1280 | *ppAstNodeExpr = pExpr;
|
---|
1281 | else
|
---|
1282 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1286 | return rc;
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | /**
|
---|
1290 | * Parse a additive expression.
|
---|
1291 | *
|
---|
1292 | * @returns VBox status code.
|
---|
1293 | * @param pThis The script context.
|
---|
1294 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1295 | *
|
---|
1296 | * @note Syntax:
|
---|
1297 | * additive-expression:
|
---|
1298 | * multiplicative-expression
|
---|
1299 | * additive-expression + multiplicative-expression
|
---|
1300 | * additive-expression - multiplicative-expression
|
---|
1301 | */
|
---|
1302 | static int vdScriptParseAdditiveExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1303 | {
|
---|
1304 | int rc = VINF_SUCCESS;
|
---|
1305 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
1306 |
|
---|
1307 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1308 |
|
---|
1309 | rc = vdScriptParseMultiplicativeExpression(pThis, &pExpr);
|
---|
1310 | if (RT_SUCCESS(rc))
|
---|
1311 | {
|
---|
1312 | PVDSCRIPTASTEXPR pExprNew = NULL;
|
---|
1313 | while (RT_SUCCESS(rc))
|
---|
1314 | {
|
---|
1315 | if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "+"))
|
---|
1316 | {
|
---|
1317 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1318 | if (pExprNew)
|
---|
1319 | pExprNew->enmType = VDSCRIPTEXPRTYPE_ADDITION;
|
---|
1320 | else
|
---|
1321 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1322 | }
|
---|
1323 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "-"))
|
---|
1324 | {
|
---|
1325 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1326 | if (pExprNew)
|
---|
1327 | pExprNew->enmType = VDSCRIPTEXPRTYPE_SUBTRACTION;
|
---|
1328 | else
|
---|
1329 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1330 | }
|
---|
1331 | else
|
---|
1332 | break;
|
---|
1333 |
|
---|
1334 | pExprNew->BinaryOp.pLeftExpr = pExpr;
|
---|
1335 | pExpr = pExprNew;
|
---|
1336 | rc = vdScriptParseMultiplicativeExpression(pThis, &pExprNew);
|
---|
1337 | if (RT_SUCCESS(rc))
|
---|
1338 | pExpr->BinaryOp.pRightExpr = pExprNew;
|
---|
1339 | }
|
---|
1340 |
|
---|
1341 | if (RT_SUCCESS(rc))
|
---|
1342 | *ppAstNodeExpr = pExpr;
|
---|
1343 | else
|
---|
1344 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
1345 | }
|
---|
1346 |
|
---|
1347 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1348 | return rc;
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | /**
|
---|
1352 | * Parse a shift expression.
|
---|
1353 | *
|
---|
1354 | * @returns VBox status code.
|
---|
1355 | * @param pThis The script context.
|
---|
1356 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1357 | *
|
---|
1358 | * @note Syntax:
|
---|
1359 | * shift-expression:
|
---|
1360 | * additive-expression
|
---|
1361 | * shift-expression << additive-expression
|
---|
1362 | * shift-expression >> additive-expression
|
---|
1363 | */
|
---|
1364 | static int vdScriptParseShiftExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1365 | {
|
---|
1366 | int rc = VINF_SUCCESS;
|
---|
1367 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
1368 |
|
---|
1369 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1370 |
|
---|
1371 | rc = vdScriptParseAdditiveExpression(pThis, &pExpr);
|
---|
1372 | if (RT_SUCCESS(rc))
|
---|
1373 | {
|
---|
1374 | PVDSCRIPTASTEXPR pExprNew = NULL;
|
---|
1375 | while (RT_SUCCESS(rc))
|
---|
1376 | {
|
---|
1377 | if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "<<"))
|
---|
1378 | {
|
---|
1379 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1380 | if (pExprNew)
|
---|
1381 | pExprNew->enmType = VDSCRIPTEXPRTYPE_LSL;
|
---|
1382 | else
|
---|
1383 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1384 | }
|
---|
1385 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, ">>"))
|
---|
1386 | {
|
---|
1387 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1388 | if (pExprNew)
|
---|
1389 | pExprNew->enmType = VDSCRIPTEXPRTYPE_LSR;
|
---|
1390 | else
|
---|
1391 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1392 | }
|
---|
1393 | else
|
---|
1394 | break;
|
---|
1395 |
|
---|
1396 | pExprNew->BinaryOp.pLeftExpr = pExpr;
|
---|
1397 | pExpr = pExprNew;
|
---|
1398 | rc = vdScriptParseAdditiveExpression(pThis, &pExprNew);
|
---|
1399 | if (RT_SUCCESS(rc))
|
---|
1400 | pExpr->BinaryOp.pRightExpr = pExprNew;
|
---|
1401 | }
|
---|
1402 |
|
---|
1403 | if (RT_SUCCESS(rc))
|
---|
1404 | *ppAstNodeExpr = pExpr;
|
---|
1405 | else
|
---|
1406 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1410 | return rc;
|
---|
1411 | }
|
---|
1412 |
|
---|
1413 | /**
|
---|
1414 | * Parse a relational expression.
|
---|
1415 | *
|
---|
1416 | * @returns VBox status code.
|
---|
1417 | * @param pThis The script context.
|
---|
1418 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1419 | *
|
---|
1420 | * @note Syntax:
|
---|
1421 | * relational-expression:
|
---|
1422 | * shift-expression
|
---|
1423 | * relational-expression < shift-expression
|
---|
1424 | * relational-expression > shift-expression
|
---|
1425 | * relational-expression >= shift-expression
|
---|
1426 | * relational-expression <= shift-expression
|
---|
1427 | */
|
---|
1428 | static int vdScriptParseRelationalExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1429 | {
|
---|
1430 | int rc = VINF_SUCCESS;
|
---|
1431 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
1432 |
|
---|
1433 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1434 |
|
---|
1435 | rc = vdScriptParseShiftExpression(pThis, &pExpr);
|
---|
1436 | if (RT_SUCCESS(rc))
|
---|
1437 | {
|
---|
1438 | PVDSCRIPTASTEXPR pExprNew = NULL;
|
---|
1439 | while (RT_SUCCESS(rc))
|
---|
1440 | {
|
---|
1441 | if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "<"))
|
---|
1442 | {
|
---|
1443 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1444 | if (pExprNew)
|
---|
1445 | pExprNew->enmType = VDSCRIPTEXPRTYPE_LOWER;
|
---|
1446 | else
|
---|
1447 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1448 | }
|
---|
1449 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, ">"))
|
---|
1450 | {
|
---|
1451 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1452 | if (pExprNew)
|
---|
1453 | pExprNew->enmType = VDSCRIPTEXPRTYPE_HIGHER;
|
---|
1454 | else
|
---|
1455 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1456 | }
|
---|
1457 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, ">="))
|
---|
1458 | {
|
---|
1459 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1460 | if (pExprNew)
|
---|
1461 | pExprNew->enmType = VDSCRIPTEXPRTYPE_HIGHEREQUAL;
|
---|
1462 | else
|
---|
1463 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1464 | }
|
---|
1465 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "<="))
|
---|
1466 | {
|
---|
1467 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1468 | if (pExprNew)
|
---|
1469 | pExprNew->enmType = VDSCRIPTEXPRTYPE_LOWEREQUAL;
|
---|
1470 | else
|
---|
1471 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1472 | }
|
---|
1473 | else
|
---|
1474 | break;
|
---|
1475 |
|
---|
1476 | pExprNew->BinaryOp.pLeftExpr = pExpr;
|
---|
1477 | pExpr = pExprNew;
|
---|
1478 | rc = vdScriptParseShiftExpression(pThis, &pExprNew);
|
---|
1479 | if (RT_SUCCESS(rc))
|
---|
1480 | pExpr->BinaryOp.pRightExpr = pExprNew;
|
---|
1481 | }
|
---|
1482 |
|
---|
1483 | if (RT_SUCCESS(rc))
|
---|
1484 | *ppAstNodeExpr = pExpr;
|
---|
1485 | else
|
---|
1486 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1490 | return rc;
|
---|
1491 | }
|
---|
1492 |
|
---|
1493 | /**
|
---|
1494 | * Parse a equality expression.
|
---|
1495 | *
|
---|
1496 | * @returns VBox status code.
|
---|
1497 | * @param pThis The script context.
|
---|
1498 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1499 | *
|
---|
1500 | * @note Syntax:
|
---|
1501 | * equality-expression:
|
---|
1502 | * relational-expression
|
---|
1503 | * equality-expression == relational-expression
|
---|
1504 | * equality-expression != relational-expression
|
---|
1505 | */
|
---|
1506 | static int vdScriptParseEqualityExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1507 | {
|
---|
1508 | int rc = VINF_SUCCESS;
|
---|
1509 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
1510 |
|
---|
1511 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1512 |
|
---|
1513 | rc = vdScriptParseRelationalExpression(pThis, &pExpr);
|
---|
1514 | if (RT_SUCCESS(rc))
|
---|
1515 | {
|
---|
1516 | PVDSCRIPTASTEXPR pExprNew = NULL;
|
---|
1517 | while (RT_SUCCESS(rc))
|
---|
1518 | {
|
---|
1519 | if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "=="))
|
---|
1520 | {
|
---|
1521 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1522 | if (pExprNew)
|
---|
1523 | pExprNew->enmType = VDSCRIPTEXPRTYPE_EQUAL;
|
---|
1524 | else
|
---|
1525 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1526 | }
|
---|
1527 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "!="))
|
---|
1528 | {
|
---|
1529 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1530 | if (pExprNew)
|
---|
1531 | pExprNew->enmType = VDSCRIPTEXPRTYPE_NOTEQUAL;
|
---|
1532 | else
|
---|
1533 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1534 | }
|
---|
1535 | else
|
---|
1536 | break;
|
---|
1537 |
|
---|
1538 | pExprNew->BinaryOp.pLeftExpr = pExpr;
|
---|
1539 | pExpr = pExprNew;
|
---|
1540 | rc = vdScriptParseRelationalExpression(pThis, &pExprNew);
|
---|
1541 | if (RT_SUCCESS(rc))
|
---|
1542 | pExpr->BinaryOp.pRightExpr = pExprNew;
|
---|
1543 | }
|
---|
1544 |
|
---|
1545 | if (RT_SUCCESS(rc))
|
---|
1546 | *ppAstNodeExpr = pExpr;
|
---|
1547 | else
|
---|
1548 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
1549 | }
|
---|
1550 |
|
---|
1551 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1552 | return rc;
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | /**
|
---|
1556 | * Parse a bitwise and expression.
|
---|
1557 | *
|
---|
1558 | * @returns VBox status code.
|
---|
1559 | * @param pThis The script context.
|
---|
1560 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1561 | *
|
---|
1562 | * @note Syntax:
|
---|
1563 | * and-expression:
|
---|
1564 | * equality-expression
|
---|
1565 | * and-expression & equality-expression
|
---|
1566 | */
|
---|
1567 | static int vdScriptParseBitwiseAndExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1568 | {
|
---|
1569 | int rc = VINF_SUCCESS;
|
---|
1570 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
1571 |
|
---|
1572 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1573 |
|
---|
1574 | rc = vdScriptParseEqualityExpression(pThis, &pExpr);
|
---|
1575 | if (RT_SUCCESS(rc))
|
---|
1576 | {
|
---|
1577 | PVDSCRIPTASTEXPR pExprNew = NULL;
|
---|
1578 | while ( RT_SUCCESS(rc)
|
---|
1579 | && vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "&"))
|
---|
1580 | {
|
---|
1581 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1582 | if (pExprNew)
|
---|
1583 | {
|
---|
1584 | pExprNew->enmType = VDSCRIPTEXPRTYPE_EQUAL;
|
---|
1585 | pExprNew->BinaryOp.pLeftExpr = pExpr;
|
---|
1586 | pExpr = pExprNew;
|
---|
1587 | rc = vdScriptParseEqualityExpression(pThis, &pExprNew);
|
---|
1588 | if (RT_SUCCESS(rc))
|
---|
1589 | pExpr->BinaryOp.pRightExpr = pExprNew;
|
---|
1590 | }
|
---|
1591 | else
|
---|
1592 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1593 | }
|
---|
1594 |
|
---|
1595 | if (RT_SUCCESS(rc))
|
---|
1596 | *ppAstNodeExpr = pExpr;
|
---|
1597 | else
|
---|
1598 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
1599 | }
|
---|
1600 |
|
---|
1601 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1602 | return rc;
|
---|
1603 | }
|
---|
1604 |
|
---|
1605 | /**
|
---|
1606 | * Parse a bitwise xor expression.
|
---|
1607 | *
|
---|
1608 | * @returns VBox status code.
|
---|
1609 | * @param pThis The script context.
|
---|
1610 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1611 | *
|
---|
1612 | * @note Syntax:
|
---|
1613 | * xor-expression:
|
---|
1614 | * and-expression
|
---|
1615 | * xor-expression ^ equality-expression
|
---|
1616 | */
|
---|
1617 | static int vdScriptParseBitwiseXorExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1618 | {
|
---|
1619 | int rc = VINF_SUCCESS;
|
---|
1620 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
1621 |
|
---|
1622 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1623 |
|
---|
1624 | rc = vdScriptParseBitwiseAndExpression(pThis, &pExpr);
|
---|
1625 | if (RT_SUCCESS(rc))
|
---|
1626 | {
|
---|
1627 | PVDSCRIPTASTEXPR pExprNew = NULL;
|
---|
1628 | while ( RT_SUCCESS(rc)
|
---|
1629 | && vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "^"))
|
---|
1630 | {
|
---|
1631 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1632 | if (pExprNew)
|
---|
1633 | {
|
---|
1634 | pExprNew->enmType = VDSCRIPTEXPRTYPE_BITWISE_XOR;
|
---|
1635 | pExprNew->BinaryOp.pLeftExpr = pExpr;
|
---|
1636 | pExpr = pExprNew;
|
---|
1637 | rc = vdScriptParseBitwiseAndExpression(pThis, &pExprNew);
|
---|
1638 | if (RT_SUCCESS(rc))
|
---|
1639 | pExpr->BinaryOp.pRightExpr = pExprNew;
|
---|
1640 | }
|
---|
1641 | else
|
---|
1642 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | if (RT_SUCCESS(rc))
|
---|
1646 | *ppAstNodeExpr = pExpr;
|
---|
1647 | else
|
---|
1648 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
1649 | }
|
---|
1650 |
|
---|
1651 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1652 | return rc;
|
---|
1653 | }
|
---|
1654 |
|
---|
1655 | /**
|
---|
1656 | * Parse a bitwise or expression.
|
---|
1657 | *
|
---|
1658 | * @returns VBox status code.
|
---|
1659 | * @param pThis The script context.
|
---|
1660 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1661 | *
|
---|
1662 | * @note Syntax:
|
---|
1663 | * or-expression:
|
---|
1664 | * xor-expression
|
---|
1665 | * or-expression | xor-expression
|
---|
1666 | */
|
---|
1667 | static int vdScriptParseBitwiseOrExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1668 | {
|
---|
1669 | int rc = VINF_SUCCESS;
|
---|
1670 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
1671 |
|
---|
1672 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1673 |
|
---|
1674 | rc = vdScriptParseBitwiseXorExpression(pThis, &pExpr);
|
---|
1675 | if (RT_SUCCESS(rc))
|
---|
1676 | {
|
---|
1677 | PVDSCRIPTASTEXPR pExprNew = NULL;
|
---|
1678 | while ( RT_SUCCESS(rc)
|
---|
1679 | && vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "|"))
|
---|
1680 | {
|
---|
1681 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1682 | if (pExprNew)
|
---|
1683 | {
|
---|
1684 | pExprNew->enmType = VDSCRIPTEXPRTYPE_BITWISE_OR;
|
---|
1685 | pExprNew->BinaryOp.pLeftExpr = pExpr;
|
---|
1686 | pExpr = pExprNew;
|
---|
1687 | rc = vdScriptParseBitwiseXorExpression(pThis, &pExprNew);
|
---|
1688 | if (RT_SUCCESS(rc))
|
---|
1689 | pExpr->BinaryOp.pRightExpr = pExprNew;
|
---|
1690 | }
|
---|
1691 | else
|
---|
1692 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1693 | }
|
---|
1694 |
|
---|
1695 | if (RT_SUCCESS(rc))
|
---|
1696 | *ppAstNodeExpr = pExpr;
|
---|
1697 | else
|
---|
1698 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
1699 | }
|
---|
1700 |
|
---|
1701 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1702 | return rc;
|
---|
1703 | }
|
---|
1704 |
|
---|
1705 | /**
|
---|
1706 | * Parse a logical and expression.
|
---|
1707 | *
|
---|
1708 | * @returns VBox status code.
|
---|
1709 | * @param pThis The script context.
|
---|
1710 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1711 | *
|
---|
1712 | * @note Syntax:
|
---|
1713 | * logical-and-expression:
|
---|
1714 | * or-expression
|
---|
1715 | * logical-and-expression | or-expression
|
---|
1716 | */
|
---|
1717 | static int vdScriptParseLogicalAndExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1718 | {
|
---|
1719 | int rc = VINF_SUCCESS;
|
---|
1720 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
1721 |
|
---|
1722 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1723 |
|
---|
1724 | rc = vdScriptParseBitwiseOrExpression(pThis, &pExpr);
|
---|
1725 | if (RT_SUCCESS(rc))
|
---|
1726 | {
|
---|
1727 | PVDSCRIPTASTEXPR pExprNew = NULL;
|
---|
1728 | while ( RT_SUCCESS(rc)
|
---|
1729 | && vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "&&"))
|
---|
1730 | {
|
---|
1731 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1732 | if (pExprNew)
|
---|
1733 | {
|
---|
1734 | pExprNew->enmType = VDSCRIPTEXPRTYPE_LOGICAL_AND;
|
---|
1735 | pExprNew->BinaryOp.pLeftExpr = pExpr;
|
---|
1736 | pExpr = pExprNew;
|
---|
1737 | rc = vdScriptParseBitwiseOrExpression(pThis, &pExprNew);
|
---|
1738 | if (RT_SUCCESS(rc))
|
---|
1739 | pExpr->BinaryOp.pRightExpr = pExprNew;
|
---|
1740 | }
|
---|
1741 | else
|
---|
1742 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | if (RT_SUCCESS(rc))
|
---|
1746 | *ppAstNodeExpr = pExpr;
|
---|
1747 | else
|
---|
1748 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
1749 | }
|
---|
1750 |
|
---|
1751 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1752 | return rc;
|
---|
1753 | }
|
---|
1754 |
|
---|
1755 | /**
|
---|
1756 | * Parse a logical or expression.
|
---|
1757 | *
|
---|
1758 | * @returns VBox status code.
|
---|
1759 | * @param pThis The script context.
|
---|
1760 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1761 | *
|
---|
1762 | * @note Syntax:
|
---|
1763 | * logical-or-expression:
|
---|
1764 | * logical-and-expression
|
---|
1765 | * logical-or-expression | logical-and-expression
|
---|
1766 | */
|
---|
1767 | static int vdScriptParseLogicalOrExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1768 | {
|
---|
1769 | int rc = VINF_SUCCESS;
|
---|
1770 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
1771 |
|
---|
1772 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1773 |
|
---|
1774 | rc = vdScriptParseLogicalAndExpression(pThis, &pExpr);
|
---|
1775 | if (RT_SUCCESS(rc))
|
---|
1776 | {
|
---|
1777 | PVDSCRIPTASTEXPR pExprNew = NULL;
|
---|
1778 | while ( RT_SUCCESS(rc)
|
---|
1779 | && vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "||"))
|
---|
1780 | {
|
---|
1781 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1782 | if (pExprNew)
|
---|
1783 | {
|
---|
1784 | pExprNew->enmType = VDSCRIPTEXPRTYPE_LOGICAL_OR;
|
---|
1785 | pExprNew->BinaryOp.pLeftExpr = pExpr;
|
---|
1786 | pExpr = pExprNew;
|
---|
1787 | rc = vdScriptParseLogicalAndExpression(pThis, &pExprNew);
|
---|
1788 | if (RT_SUCCESS(rc))
|
---|
1789 | pExpr->BinaryOp.pRightExpr = pExprNew;
|
---|
1790 | }
|
---|
1791 | else
|
---|
1792 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1793 | }
|
---|
1794 |
|
---|
1795 | if (RT_SUCCESS(rc))
|
---|
1796 | *ppAstNodeExpr = pExpr;
|
---|
1797 | else
|
---|
1798 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
1799 | }
|
---|
1800 |
|
---|
1801 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1802 | return rc;
|
---|
1803 | }
|
---|
1804 |
|
---|
1805 | /**
|
---|
1806 | * Parse a conditional expression.
|
---|
1807 | *
|
---|
1808 | * @returns VBox status code.
|
---|
1809 | * @param pThis The script context.
|
---|
1810 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1811 | *
|
---|
1812 | * @note: VDScript doesn't support logical-or-expression ? expression : conditional-expression
|
---|
1813 | * so a conditional expression is equal to a logical-or-expression.
|
---|
1814 | */
|
---|
1815 | static int vdScriptParseCondExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1816 | {
|
---|
1817 | return vdScriptParseLogicalOrExpression(pThis, ppAstNodeExpr);
|
---|
1818 | }
|
---|
1819 |
|
---|
1820 | /**
|
---|
1821 | * Parse an assignment expression.
|
---|
1822 | *
|
---|
1823 | * @returns VBox status code.
|
---|
1824 | * @param pThis The script context.
|
---|
1825 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1826 | *
|
---|
1827 | * @note Syntax:
|
---|
1828 | * assignment-expression:
|
---|
1829 | * conditional-expression
|
---|
1830 | * unary-expression assignment-operator assignment-expression
|
---|
1831 | */
|
---|
1832 | static int vdScriptParseAssignmentExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1833 | {
|
---|
1834 | int rc = VINF_SUCCESS;
|
---|
1835 | PVDSCRIPTASTEXPR pExpr;
|
---|
1836 |
|
---|
1837 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1838 |
|
---|
1839 | rc = vdScriptParseLogicalOrExpression(pThis, &pExpr);
|
---|
1840 | if (RT_SUCCESS(rc))
|
---|
1841 | {
|
---|
1842 | PVDSCRIPTASTEXPR pExprNew = NULL;
|
---|
1843 | while (RT_SUCCESS(rc))
|
---|
1844 | {
|
---|
1845 | if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "="))
|
---|
1846 | {
|
---|
1847 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1848 | if (pExprNew)
|
---|
1849 | pExprNew->enmType = VDSCRIPTEXPRTYPE_ASSIGN;
|
---|
1850 | else
|
---|
1851 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1852 | }
|
---|
1853 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "*="))
|
---|
1854 | {
|
---|
1855 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1856 | if (pExprNew)
|
---|
1857 | pExprNew->enmType = VDSCRIPTEXPRTYPE_ASSIGN_MULT;
|
---|
1858 | else
|
---|
1859 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1860 | }
|
---|
1861 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "/="))
|
---|
1862 | {
|
---|
1863 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1864 | if (pExprNew)
|
---|
1865 | pExprNew->enmType = VDSCRIPTEXPRTYPE_ASSIGN_DIV;
|
---|
1866 | else
|
---|
1867 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1868 | }
|
---|
1869 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "%="))
|
---|
1870 | {
|
---|
1871 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1872 | if (pExprNew)
|
---|
1873 | pExprNew->enmType = VDSCRIPTEXPRTYPE_ASSIGN_MOD;
|
---|
1874 | else
|
---|
1875 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1876 | }
|
---|
1877 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "+="))
|
---|
1878 | {
|
---|
1879 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1880 | if (pExprNew)
|
---|
1881 | pExprNew->enmType = VDSCRIPTEXPRTYPE_ASSIGN_ADD;
|
---|
1882 | else
|
---|
1883 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1884 | }
|
---|
1885 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "-="))
|
---|
1886 | {
|
---|
1887 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1888 | if (pExprNew)
|
---|
1889 | pExprNew->enmType = VDSCRIPTEXPRTYPE_ASSIGN_SUB;
|
---|
1890 | else
|
---|
1891 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1892 | }
|
---|
1893 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "<<="))
|
---|
1894 | {
|
---|
1895 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1896 | if (pExprNew)
|
---|
1897 | pExprNew->enmType = VDSCRIPTEXPRTYPE_ASSIGN_LSL;
|
---|
1898 | else
|
---|
1899 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1900 | }
|
---|
1901 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, ">>="))
|
---|
1902 | {
|
---|
1903 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1904 | if (pExprNew)
|
---|
1905 | pExprNew->enmType = VDSCRIPTEXPRTYPE_ASSIGN_LSR;
|
---|
1906 | else
|
---|
1907 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1908 | }
|
---|
1909 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "&="))
|
---|
1910 | {
|
---|
1911 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1912 | if (pExprNew)
|
---|
1913 | pExprNew->enmType = VDSCRIPTEXPRTYPE_ASSIGN_AND;
|
---|
1914 | else
|
---|
1915 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1916 | }
|
---|
1917 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "^="))
|
---|
1918 | {
|
---|
1919 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1920 | if (pExprNew)
|
---|
1921 | pExprNew->enmType = VDSCRIPTEXPRTYPE_ASSIGN_XOR;
|
---|
1922 | else
|
---|
1923 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1924 | }
|
---|
1925 | else if (vdScriptTokenizerSkipIfIsOperatorEqual(pThis->pTokenizer, "|="))
|
---|
1926 | {
|
---|
1927 | pExprNew = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1928 | if (pExprNew)
|
---|
1929 | pExprNew->enmType = VDSCRIPTEXPRTYPE_ASSIGN_OR;
|
---|
1930 | else
|
---|
1931 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1932 | }
|
---|
1933 | else
|
---|
1934 | break;
|
---|
1935 |
|
---|
1936 | pExprNew->BinaryOp.pLeftExpr = pExpr;
|
---|
1937 | pExpr = pExprNew;
|
---|
1938 | rc = vdScriptParseLogicalOrExpression(pThis, &pExprNew);
|
---|
1939 | if (RT_SUCCESS(rc))
|
---|
1940 | pExpr->BinaryOp.pRightExpr = pExprNew;
|
---|
1941 | }
|
---|
1942 |
|
---|
1943 | if (RT_SUCCESS(rc))
|
---|
1944 | *ppAstNodeExpr = pExpr;
|
---|
1945 | else
|
---|
1946 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
1947 | }
|
---|
1948 |
|
---|
1949 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
1950 | return rc;
|
---|
1951 | }
|
---|
1952 |
|
---|
1953 | /**
|
---|
1954 | * Parse an expression.
|
---|
1955 | *
|
---|
1956 | * @returns VBox status code.
|
---|
1957 | * @param pThis The script context.
|
---|
1958 | * @param ppAstNodeExpr Where to store the expression AST node on success.
|
---|
1959 | *
|
---|
1960 | * @note Syntax:
|
---|
1961 | * expression:
|
---|
1962 | * assignment-expression
|
---|
1963 | * expression , assignment-expression
|
---|
1964 | */
|
---|
1965 | static int vdScriptParseExpression(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTEXPR *ppAstNodeExpr)
|
---|
1966 | {
|
---|
1967 | int rc = VINF_SUCCESS;
|
---|
1968 | PVDSCRIPTASTEXPR pAssignExpr = NULL;
|
---|
1969 |
|
---|
1970 | LogFlowFunc(("pThis=%p ppAstNodeExpr=%p\n"));
|
---|
1971 |
|
---|
1972 | rc = vdScriptParseAssignmentExpression(pThis, &pAssignExpr);
|
---|
1973 | if ( RT_SUCCESS(rc)
|
---|
1974 | && vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ','))
|
---|
1975 | {
|
---|
1976 | PVDSCRIPTASTEXPR pListAssignExpr = (PVDSCRIPTASTEXPR)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_EXPRESSION);
|
---|
1977 | if (pListAssignExpr)
|
---|
1978 | {
|
---|
1979 | pListAssignExpr->enmType = VDSCRIPTEXPRTYPE_ASSIGNMENT_LIST;
|
---|
1980 | RTListInit(&pListAssignExpr->ListExpr);
|
---|
1981 | RTListAppend(&pListAssignExpr->ListExpr, &pAssignExpr->Core.ListNode);
|
---|
1982 | do
|
---|
1983 | {
|
---|
1984 | rc = vdScriptParseAssignmentExpression(pThis, &pAssignExpr);
|
---|
1985 | if (RT_SUCCESS(rc))
|
---|
1986 | RTListAppend(&pListAssignExpr->ListExpr, &pAssignExpr->Core.ListNode);
|
---|
1987 | } while ( RT_SUCCESS(rc)
|
---|
1988 | && vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ','));
|
---|
1989 |
|
---|
1990 | if (RT_FAILURE(rc))
|
---|
1991 | vdScriptAstNodeFree(&pListAssignExpr->Core);
|
---|
1992 | else
|
---|
1993 | *ppAstNodeExpr = pListAssignExpr;
|
---|
1994 | }
|
---|
1995 | else
|
---|
1996 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating expression AST node\n");
|
---|
1997 | }
|
---|
1998 | else if (RT_SUCCESS(rc))
|
---|
1999 | *ppAstNodeExpr = pAssignExpr;
|
---|
2000 |
|
---|
2001 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
2002 | return rc;
|
---|
2003 | }
|
---|
2004 |
|
---|
2005 | /**
|
---|
2006 | * Parse an if statement.
|
---|
2007 | *
|
---|
2008 | * @returns VBox status code.
|
---|
2009 | * @param pThis The script context.
|
---|
2010 | * @param pAstNodeIf Uninitialized if AST node.
|
---|
2011 | *
|
---|
2012 | * @note The caller skipped the "if" token already.
|
---|
2013 | */
|
---|
2014 | static int vdScriptParseIf(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTIF pAstNodeIf)
|
---|
2015 | {
|
---|
2016 | int rc = VINF_SUCCESS;
|
---|
2017 |
|
---|
2018 | if (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, '('))
|
---|
2019 | {
|
---|
2020 | PVDSCRIPTASTEXPR pCondExpr = NULL;
|
---|
2021 | rc = vdScriptParseExpression(pThis, &pCondExpr);
|
---|
2022 | if (RT_SUCCESS(rc))
|
---|
2023 | {
|
---|
2024 | if (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ')'))
|
---|
2025 | {
|
---|
2026 | PVDSCRIPTASTSTMT pStmt = NULL;
|
---|
2027 | PVDSCRIPTASTSTMT pElseStmt = NULL;
|
---|
2028 | rc = vdScriptParseStatement(pThis, &pStmt);
|
---|
2029 | if ( RT_SUCCESS(rc)
|
---|
2030 | && vdScriptTokenizerSkipIfIsKeywordEqual(pThis->pTokenizer, VDSCRIPTTOKENKEYWORD_ELSE))
|
---|
2031 | rc = vdScriptParseStatement(pThis, &pElseStmt);
|
---|
2032 |
|
---|
2033 | if (RT_SUCCESS(rc))
|
---|
2034 | {
|
---|
2035 | pAstNodeIf->pCond = pCondExpr;
|
---|
2036 | pAstNodeIf->pTrueStmt = pStmt;
|
---|
2037 | pAstNodeIf->pElseStmt = pElseStmt;
|
---|
2038 | }
|
---|
2039 | else if (pStmt)
|
---|
2040 | vdScriptAstNodeFree(&pStmt->Core);
|
---|
2041 | }
|
---|
2042 | else
|
---|
2043 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \")\", got ...\n");
|
---|
2044 |
|
---|
2045 | if (RT_FAILURE(rc))
|
---|
2046 | vdScriptAstNodeFree(&pCondExpr->Core);
|
---|
2047 | }
|
---|
2048 | }
|
---|
2049 | else
|
---|
2050 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \"(\", got ...\n");
|
---|
2051 |
|
---|
2052 | return rc;
|
---|
2053 | }
|
---|
2054 |
|
---|
2055 | /**
|
---|
2056 | * Parse a switch statement.
|
---|
2057 | *
|
---|
2058 | * @returns VBox status code.
|
---|
2059 | * @param pThis The script context.
|
---|
2060 | * @param pAstNodeSwitch Uninitialized switch AST node.
|
---|
2061 | *
|
---|
2062 | * @note The caller skipped the "switch" token already.
|
---|
2063 | */
|
---|
2064 | static int vdScriptParseSwitch(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTSWITCH pAstNodeSwitch)
|
---|
2065 | {
|
---|
2066 | int rc = VINF_SUCCESS;
|
---|
2067 |
|
---|
2068 | if (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, '('))
|
---|
2069 | {
|
---|
2070 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
2071 |
|
---|
2072 | rc = vdScriptParseExpression(pThis, &pExpr);
|
---|
2073 | if ( RT_SUCCESS(rc)
|
---|
2074 | && vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ')'))
|
---|
2075 | {
|
---|
2076 | PVDSCRIPTASTSTMT pStmt = NULL;
|
---|
2077 | rc = vdScriptParseStatement(pThis, &pStmt);
|
---|
2078 | if (RT_SUCCESS(rc))
|
---|
2079 | {
|
---|
2080 | pAstNodeSwitch->pCond = pExpr;
|
---|
2081 | pAstNodeSwitch->pStmt = pStmt;
|
---|
2082 | }
|
---|
2083 | else
|
---|
2084 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
2085 | }
|
---|
2086 | else if (RT_SUCCESS(rc))
|
---|
2087 | {
|
---|
2088 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \")\", got ...\n");
|
---|
2089 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
2090 | }
|
---|
2091 | }
|
---|
2092 | else
|
---|
2093 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \"(\", got ...\n");
|
---|
2094 |
|
---|
2095 | return rc;
|
---|
2096 | }
|
---|
2097 |
|
---|
2098 | /**
|
---|
2099 | * Parse a while or do ... while statement.
|
---|
2100 | *
|
---|
2101 | * @returns VBox status code.
|
---|
2102 | * @param pThis The script context.
|
---|
2103 | * @param pAstNodeWhile Uninitialized while AST node.
|
---|
2104 | *
|
---|
2105 | * @note The caller skipped the "while" or "do" token already.
|
---|
2106 | */
|
---|
2107 | static int vdScriptParseWhile(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTWHILE pAstNodeWhile, bool fDoWhile)
|
---|
2108 | {
|
---|
2109 | int rc = VINF_SUCCESS;
|
---|
2110 |
|
---|
2111 | pAstNodeWhile->fDoWhile = fDoWhile;
|
---|
2112 |
|
---|
2113 | if (fDoWhile)
|
---|
2114 | {
|
---|
2115 | PVDSCRIPTASTSTMT pStmt = NULL;
|
---|
2116 | rc = vdScriptParseStatement(pThis, &pStmt);
|
---|
2117 | if ( RT_SUCCESS(rc)
|
---|
2118 | && vdScriptTokenizerSkipIfIsKeywordEqual(pThis->pTokenizer, VDSCRIPTTOKENKEYWORD_WHILE))
|
---|
2119 | {
|
---|
2120 | if (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, '('))
|
---|
2121 | {
|
---|
2122 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
2123 |
|
---|
2124 | rc = vdScriptParseExpression(pThis, &pExpr);
|
---|
2125 | if ( RT_SUCCESS(rc)
|
---|
2126 | && vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ')'))
|
---|
2127 | {
|
---|
2128 | if (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ';'))
|
---|
2129 | {
|
---|
2130 | pAstNodeWhile->pCond = pExpr;
|
---|
2131 | pAstNodeWhile->pStmt = pStmt;
|
---|
2132 | }
|
---|
2133 | else
|
---|
2134 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \";\", got ...\n");
|
---|
2135 | }
|
---|
2136 | else if (RT_SUCCESS(rc))
|
---|
2137 | {
|
---|
2138 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \")\", got ...\n");
|
---|
2139 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
2140 | }
|
---|
2141 | }
|
---|
2142 | else
|
---|
2143 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \"(\", got ...\n");
|
---|
2144 | }
|
---|
2145 | else if (RT_SUCCESS(rc))
|
---|
2146 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \"while\", got ...\n");
|
---|
2147 |
|
---|
2148 | if ( RT_FAILURE(rc)
|
---|
2149 | && pStmt)
|
---|
2150 | vdScriptAstNodeFree(&pStmt->Core);
|
---|
2151 | }
|
---|
2152 | else
|
---|
2153 | {
|
---|
2154 | if (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, '('))
|
---|
2155 | {
|
---|
2156 | PVDSCRIPTASTEXPR pExpr = NULL;
|
---|
2157 |
|
---|
2158 | rc = vdScriptParseExpression(pThis, &pExpr);
|
---|
2159 | if ( RT_SUCCESS(rc)
|
---|
2160 | && vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ')'))
|
---|
2161 | {
|
---|
2162 | PVDSCRIPTASTSTMT pStmt = NULL;
|
---|
2163 | rc = vdScriptParseStatement(pThis, &pStmt);
|
---|
2164 | if (RT_SUCCESS(rc))
|
---|
2165 | {
|
---|
2166 | pAstNodeWhile->pCond = pExpr;
|
---|
2167 | pAstNodeWhile->pStmt = pStmt;
|
---|
2168 | }
|
---|
2169 | else
|
---|
2170 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
2171 | }
|
---|
2172 | else if (RT_SUCCESS(rc))
|
---|
2173 | {
|
---|
2174 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \")\", got ...\n");
|
---|
2175 | vdScriptAstNodeFree(&pExpr->Core);
|
---|
2176 | }
|
---|
2177 | }
|
---|
2178 | else
|
---|
2179 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \"(\", got ...\n");
|
---|
2180 | }
|
---|
2181 |
|
---|
2182 | return rc;
|
---|
2183 | }
|
---|
2184 |
|
---|
2185 | /**
|
---|
2186 | * Parse a for statement.
|
---|
2187 | *
|
---|
2188 | * @returns VBox status code.
|
---|
2189 | * @param pThis The script context.
|
---|
2190 | * @param pAstNodeFor Uninitialized for AST node.
|
---|
2191 | *
|
---|
2192 | * @note The caller skipped the "for" token already.
|
---|
2193 | */
|
---|
2194 | static int vdScriptParseFor(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTFOR pAstNodeFor)
|
---|
2195 | {
|
---|
2196 | int rc = VINF_SUCCESS;
|
---|
2197 |
|
---|
2198 | if (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, '('))
|
---|
2199 | {
|
---|
2200 | PVDSCRIPTASTEXPR pExprStart = NULL;
|
---|
2201 | PVDSCRIPTASTEXPR pExprCond = NULL;
|
---|
2202 | PVDSCRIPTASTEXPR pExpr3 = NULL;
|
---|
2203 |
|
---|
2204 | rc = vdScriptParseExpression(pThis, &pExprStart);
|
---|
2205 | if ( RT_SUCCESS(rc)
|
---|
2206 | && vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ';'))
|
---|
2207 | {
|
---|
2208 | rc = vdScriptParseExpression(pThis, &pExprCond);
|
---|
2209 | if ( RT_SUCCESS(rc)
|
---|
2210 | && vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ';'))
|
---|
2211 | {
|
---|
2212 | rc = vdScriptParseExpression(pThis, &pExpr3);
|
---|
2213 | if ( RT_SUCCESS(rc)
|
---|
2214 | && vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ')'))
|
---|
2215 | {
|
---|
2216 | PVDSCRIPTASTSTMT pStmt = NULL;
|
---|
2217 | rc = vdScriptParseStatement(pThis, &pStmt);
|
---|
2218 | if (RT_SUCCESS(rc))
|
---|
2219 | {
|
---|
2220 | pAstNodeFor->pExprStart = pExprStart;
|
---|
2221 | pAstNodeFor->pExprCond = pExprCond;
|
---|
2222 | pAstNodeFor->pExpr3 = pExpr3;
|
---|
2223 | pAstNodeFor->pStmt = pStmt;
|
---|
2224 | }
|
---|
2225 | }
|
---|
2226 | }
|
---|
2227 | else if (RT_SUCCESS(rc))
|
---|
2228 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \";\", got ...\n");
|
---|
2229 | }
|
---|
2230 | else if (RT_SUCCESS(rc))
|
---|
2231 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \";\", got ...\n");
|
---|
2232 |
|
---|
2233 | if (RT_FAILURE(rc))
|
---|
2234 | {
|
---|
2235 | if (pExprStart)
|
---|
2236 | vdScriptAstNodeFree(&pExprStart->Core);
|
---|
2237 | if (pExprCond)
|
---|
2238 | vdScriptAstNodeFree(&pExprCond->Core);
|
---|
2239 | if (pExpr3)
|
---|
2240 | vdScriptAstNodeFree(&pExpr3->Core);
|
---|
2241 | }
|
---|
2242 | }
|
---|
2243 | else
|
---|
2244 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \"(\", got ...\n");
|
---|
2245 |
|
---|
2246 | return rc;
|
---|
2247 | }
|
---|
2248 |
|
---|
2249 | /**
|
---|
2250 | * Parse a declaration.
|
---|
2251 | *
|
---|
2252 | * @returns VBox status code.
|
---|
2253 | * @param pThis The script context.
|
---|
2254 | * @param ppAstNodeDecl Where to store the declaration AST node on success.
|
---|
2255 | */
|
---|
2256 | static int vdScriptParseDeclaration(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTDECL *ppAstNodeDecl)
|
---|
2257 | {
|
---|
2258 | int rc = VERR_NOT_IMPLEMENTED;
|
---|
2259 | return rc;
|
---|
2260 | }
|
---|
2261 |
|
---|
2262 | /**
|
---|
2263 | * Parse a statement.
|
---|
2264 | *
|
---|
2265 | * @returns VBox status code.
|
---|
2266 | * @param pThis The script context.
|
---|
2267 | * @param ppAstNodeStmt Where to store the statement AST node on success.
|
---|
2268 | */
|
---|
2269 | static int vdScriptParseStatement(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTSTMT *ppAstNodeStmt)
|
---|
2270 | {
|
---|
2271 | int rc = VINF_SUCCESS;
|
---|
2272 |
|
---|
2273 | /* Shortcut for a new compound statement. */
|
---|
2274 | if (vdScriptTokenizerIsPunctuatorEqual(pThis->pTokenizer, '{'))
|
---|
2275 | rc = vdScriptParseCompoundStatement(pThis, ppAstNodeStmt);
|
---|
2276 | else
|
---|
2277 | {
|
---|
2278 | PVDSCRIPTASTSTMT pAstNodeStmt = (PVDSCRIPTASTSTMT)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_STATEMENT);
|
---|
2279 |
|
---|
2280 | if (pAstNodeStmt)
|
---|
2281 | {
|
---|
2282 |
|
---|
2283 | if (vdScriptTokenizerSkipIfIsKeywordEqual(pThis->pTokenizer, VDSCRIPTTOKENKEYWORD_DEFAULT))
|
---|
2284 | {
|
---|
2285 | if (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ':'))
|
---|
2286 | {
|
---|
2287 | PVDSCRIPTASTSTMT pAstNodeStmtDef = NULL;
|
---|
2288 | pAstNodeStmt->enmStmtType = VDSCRIPTSTMTTYPE_DEFAULT;
|
---|
2289 | rc = vdScriptParseStatement(pThis, &pAstNodeStmtDef);
|
---|
2290 | if (RT_SUCCESS(rc))
|
---|
2291 | pAstNodeStmt->pStmt = pAstNodeStmtDef;
|
---|
2292 | }
|
---|
2293 | else
|
---|
2294 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \":\", got ...\n");
|
---|
2295 | }
|
---|
2296 | else if (vdScriptTokenizerSkipIfIsKeywordEqual(pThis->pTokenizer, VDSCRIPTTOKENKEYWORD_CASE))
|
---|
2297 | {
|
---|
2298 | PVDSCRIPTASTEXPR pAstNodeExpr = NULL;
|
---|
2299 | rc = vdScriptParseCondExpression(pThis, &pAstNodeExpr);
|
---|
2300 | if (RT_SUCCESS(rc))
|
---|
2301 | {
|
---|
2302 | if (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ':'))
|
---|
2303 | {
|
---|
2304 | PVDSCRIPTASTSTMT pAstNodeCaseStmt = NULL;
|
---|
2305 | rc = vdScriptParseStatement(pThis, &pAstNodeCaseStmt);
|
---|
2306 | if (RT_SUCCESS(rc))
|
---|
2307 | {
|
---|
2308 | pAstNodeStmt->enmStmtType = VDSCRIPTSTMTTYPE_CASE;
|
---|
2309 | pAstNodeStmt->Case.pExpr = pAstNodeExpr;
|
---|
2310 | pAstNodeStmt->Case.pStmt = pAstNodeCaseStmt;
|
---|
2311 | }
|
---|
2312 | }
|
---|
2313 | else
|
---|
2314 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \":\", got ...\n");
|
---|
2315 |
|
---|
2316 | if (RT_FAILURE(rc))
|
---|
2317 | vdScriptAstNodeFree(&pAstNodeExpr->Core);
|
---|
2318 | }
|
---|
2319 | }
|
---|
2320 | else if (vdScriptTokenizerSkipIfIsKeywordEqual(pThis->pTokenizer, VDSCRIPTTOKENKEYWORD_IF))
|
---|
2321 | {
|
---|
2322 | pAstNodeStmt->enmStmtType = VDSCRIPTSTMTTYPE_IF;
|
---|
2323 | rc = vdScriptParseIf(pThis, &pAstNodeStmt->If);
|
---|
2324 | }
|
---|
2325 | else if (vdScriptTokenizerSkipIfIsKeywordEqual(pThis->pTokenizer, VDSCRIPTTOKENKEYWORD_SWITCH))
|
---|
2326 | {
|
---|
2327 | pAstNodeStmt->enmStmtType = VDSCRIPTSTMTTYPE_SWITCH;
|
---|
2328 | rc = vdScriptParseSwitch(pThis, &pAstNodeStmt->Switch);
|
---|
2329 | }
|
---|
2330 | else if (vdScriptTokenizerSkipIfIsKeywordEqual(pThis->pTokenizer, VDSCRIPTTOKENKEYWORD_WHILE))
|
---|
2331 | {
|
---|
2332 | pAstNodeStmt->enmStmtType = VDSCRIPTSTMTTYPE_WHILE;
|
---|
2333 | rc = vdScriptParseWhile(pThis, &pAstNodeStmt->While, false /* fDoWhile */);
|
---|
2334 | }
|
---|
2335 | else if (vdScriptTokenizerSkipIfIsKeywordEqual(pThis->pTokenizer, VDSCRIPTTOKENKEYWORD_DO))
|
---|
2336 | {
|
---|
2337 | pAstNodeStmt->enmStmtType = VDSCRIPTSTMTTYPE_WHILE;
|
---|
2338 | rc = vdScriptParseWhile(pThis, &pAstNodeStmt->While, true /* fDoWhile */);
|
---|
2339 | }
|
---|
2340 | else if (vdScriptTokenizerSkipIfIsKeywordEqual(pThis->pTokenizer, VDSCRIPTTOKENKEYWORD_FOR))
|
---|
2341 | {
|
---|
2342 | pAstNodeStmt->enmStmtType = VDSCRIPTSTMTTYPE_FOR;
|
---|
2343 | rc = vdScriptParseFor(pThis, &pAstNodeStmt->For);
|
---|
2344 | }
|
---|
2345 | else if (vdScriptTokenizerSkipIfIsKeywordEqual(pThis->pTokenizer, VDSCRIPTTOKENKEYWORD_CONTINUE))
|
---|
2346 | {
|
---|
2347 | pAstNodeStmt->enmStmtType = VDSCRIPTSTMTTYPE_CONTINUE;
|
---|
2348 | if (!vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ';'))
|
---|
2349 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \";\", got ...\n");
|
---|
2350 | }
|
---|
2351 | else if (vdScriptTokenizerSkipIfIsKeywordEqual(pThis->pTokenizer, VDSCRIPTTOKENKEYWORD_BREAK))
|
---|
2352 | {
|
---|
2353 | pAstNodeStmt->enmStmtType = VDSCRIPTSTMTTYPE_BREAK;
|
---|
2354 | if (!vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ';'))
|
---|
2355 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \";\", got ...\n");
|
---|
2356 | }
|
---|
2357 | else if (vdScriptTokenizerSkipIfIsKeywordEqual(pThis->pTokenizer, VDSCRIPTTOKENKEYWORD_RETURN))
|
---|
2358 | {
|
---|
2359 | pAstNodeStmt->enmStmtType = VDSCRIPTSTMTTYPE_RETURN;
|
---|
2360 | if (!vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ';'))
|
---|
2361 | {
|
---|
2362 | rc = vdScriptParseExpression(pThis, &pAstNodeStmt->pExpr);
|
---|
2363 | if ( RT_SUCCESS(rc)
|
---|
2364 | && !vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ';'))
|
---|
2365 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \";\", got ...\n");
|
---|
2366 | }
|
---|
2367 | else
|
---|
2368 | pAstNodeStmt->pExpr = NULL; /* No expression for return. */
|
---|
2369 | }
|
---|
2370 | else
|
---|
2371 | {
|
---|
2372 | /* Must be an expression. */
|
---|
2373 | pAstNodeStmt->enmStmtType = VDSCRIPTSTMTTYPE_EXPRESSION;
|
---|
2374 | rc = vdScriptParseExpression(pThis, &pAstNodeStmt->pExpr);
|
---|
2375 | if ( RT_SUCCESS(rc)
|
---|
2376 | && !vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ';'))
|
---|
2377 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \";\", got ...\n");
|
---|
2378 | }
|
---|
2379 |
|
---|
2380 | if (RT_SUCCESS(rc))
|
---|
2381 | *ppAstNodeStmt = pAstNodeStmt;
|
---|
2382 | else
|
---|
2383 | vdScriptAstNodeFree(&pAstNodeStmt->Core);
|
---|
2384 | }
|
---|
2385 | else
|
---|
2386 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory creating statement node\n");
|
---|
2387 | }
|
---|
2388 |
|
---|
2389 | return rc;
|
---|
2390 | }
|
---|
2391 |
|
---|
2392 | /**
|
---|
2393 | * Parses a compound statement.
|
---|
2394 | *
|
---|
2395 | * @returns VBox status code.
|
---|
2396 | * @param pThis The script context.
|
---|
2397 | * @param ppAstNodeCompound Where to store the compound AST node on success.
|
---|
2398 | */
|
---|
2399 | static int vdScriptParseCompoundStatement(PVDSCRIPTCTXINT pThis, PVDSCRIPTASTSTMT *ppAstNodeCompound)
|
---|
2400 | {
|
---|
2401 | int rc = VINF_SUCCESS;
|
---|
2402 |
|
---|
2403 | if (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, '{'))
|
---|
2404 | {
|
---|
2405 | PVDSCRIPTASTSTMT pAstNodeCompound = (PVDSCRIPTASTSTMT)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_STATEMENT);
|
---|
2406 | if (pAstNodeCompound)
|
---|
2407 | {
|
---|
2408 | pAstNodeCompound->enmStmtType = VDSCRIPTSTMTTYPE_COMPOUND;
|
---|
2409 | RTListInit(&pAstNodeCompound->Compound.ListDecls);
|
---|
2410 | RTListInit(&pAstNodeCompound->Compound.ListStmts);
|
---|
2411 | while (!vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, '}'))
|
---|
2412 | {
|
---|
2413 | /*
|
---|
2414 | * Check whether we have a declaration or a statement.
|
---|
2415 | * For now we assume that 2 identifier tokens specify a declaration
|
---|
2416 | * (type + variable name). Having two consecutive identifers is not possible
|
---|
2417 | * for a statement.
|
---|
2418 | */
|
---|
2419 | if ( vdScriptTokenizerGetTokenClass(pThis->pTokenizer) == VDTOKENCLASS_IDENTIFIER
|
---|
2420 | && vdScriptTokenizerPeekNextClass(pThis->pTokenizer) == VDTOKENCLASS_IDENTIFIER)
|
---|
2421 | {
|
---|
2422 | PVDSCRIPTASTDECL pAstNodeDecl = NULL;
|
---|
2423 | rc = vdScriptParseDeclaration(pThis, &pAstNodeDecl);
|
---|
2424 | if (RT_SUCCESS(rc))
|
---|
2425 | RTListAppend(&pAstNodeCompound->Compound.ListDecls, &pAstNodeDecl->Core.ListNode);
|
---|
2426 | }
|
---|
2427 | else
|
---|
2428 | {
|
---|
2429 | PVDSCRIPTASTSTMT pAstNodeStmt = NULL;
|
---|
2430 | rc = vdScriptParseStatement(pThis, &pAstNodeStmt);
|
---|
2431 | if (RT_SUCCESS(rc))
|
---|
2432 | RTListAppend(&pAstNodeCompound->Compound.ListStmts, &pAstNodeStmt->Core.ListNode);
|
---|
2433 | }
|
---|
2434 |
|
---|
2435 | if (RT_FAILURE(rc))
|
---|
2436 | break;
|
---|
2437 | }
|
---|
2438 |
|
---|
2439 | if (RT_SUCCESS(rc))
|
---|
2440 | *ppAstNodeCompound = pAstNodeCompound;
|
---|
2441 | else
|
---|
2442 | vdScriptAstNodeFree(&pAstNodeCompound->Core);
|
---|
2443 | }
|
---|
2444 | else
|
---|
2445 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory creating compound statement node\n");
|
---|
2446 | }
|
---|
2447 | else
|
---|
2448 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \"{\" got...\n");
|
---|
2449 |
|
---|
2450 |
|
---|
2451 | return rc;
|
---|
2452 | }
|
---|
2453 |
|
---|
2454 | /**
|
---|
2455 | * Parses a function definition from the given tokenizer.
|
---|
2456 | *
|
---|
2457 | * @returns VBox status code.
|
---|
2458 | * @param pThis The script context.
|
---|
2459 | */
|
---|
2460 | static int vdScriptParseAddFnDef(PVDSCRIPTCTXINT pThis)
|
---|
2461 | {
|
---|
2462 | int rc = VINF_SUCCESS;
|
---|
2463 | PVDSCRIPTASTIDE pRetType = NULL;
|
---|
2464 | PVDSCRIPTASTIDE pFnIde = NULL;
|
---|
2465 |
|
---|
2466 | LogFlowFunc(("pThis=%p\n", pThis));
|
---|
2467 |
|
---|
2468 | /* Put return type on the stack. */
|
---|
2469 | rc = vdScriptParseIde(pThis, &pRetType);
|
---|
2470 | if (RT_SUCCESS(rc))
|
---|
2471 | {
|
---|
2472 | /* Function name */
|
---|
2473 | rc = vdScriptParseIde(pThis, &pFnIde);
|
---|
2474 | if (RT_SUCCESS(rc))
|
---|
2475 | {
|
---|
2476 | if (vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, '('))
|
---|
2477 | {
|
---|
2478 | PVDSCRIPTASTFN pAstNodeFn = (PVDSCRIPTASTFN)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_FUNCTION);
|
---|
2479 |
|
---|
2480 | if (pAstNodeFn)
|
---|
2481 | {
|
---|
2482 | pAstNodeFn->pFnIde = pFnIde;
|
---|
2483 | pAstNodeFn->pRetType = pRetType;
|
---|
2484 | RTListInit(&pAstNodeFn->ListArgs);
|
---|
2485 |
|
---|
2486 | pFnIde = NULL;
|
---|
2487 | pRetType = NULL;
|
---|
2488 |
|
---|
2489 | /* Parse parameter list, create empty parameter list AST node and put it on the stack. */
|
---|
2490 | while (!vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ')'))
|
---|
2491 | {
|
---|
2492 | PVDSCRIPTASTIDE pArgType = NULL;
|
---|
2493 | PVDSCRIPTASTIDE pArgIde = NULL;
|
---|
2494 | /* Parse two identifiers, first one is the type, second the name. */
|
---|
2495 | rc = vdScriptParseIde(pThis, &pArgType);
|
---|
2496 | if (RT_SUCCESS(rc))
|
---|
2497 | rc = vdScriptParseIde(pThis, &pArgIde);
|
---|
2498 |
|
---|
2499 | if (RT_SUCCESS(rc))
|
---|
2500 | {
|
---|
2501 | PVDSCRIPTASTFNARG pAstNodeFnArg = (PVDSCRIPTASTFNARG)vdScriptAstNodeAlloc(VDSCRIPTASTCLASS_FUNCTIONARG);
|
---|
2502 | if (pAstNodeFnArg)
|
---|
2503 | {
|
---|
2504 | pAstNodeFnArg->pArgIde = pArgIde;
|
---|
2505 | pAstNodeFnArg->pType = pArgType;
|
---|
2506 | RTListAppend(&pAstNodeFn->ListArgs, &pAstNodeFnArg->Core.ListNode);
|
---|
2507 | pAstNodeFn->cArgs++;
|
---|
2508 | }
|
---|
2509 | else
|
---|
2510 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating function argument AST node\n");
|
---|
2511 | }
|
---|
2512 |
|
---|
2513 | if (RT_FAILURE(rc))
|
---|
2514 | {
|
---|
2515 | if (pArgType)
|
---|
2516 | vdScriptAstNodeFree(&pArgType->Core);
|
---|
2517 | if (pArgIde)
|
---|
2518 | vdScriptAstNodeFree(&pArgIde->Core);
|
---|
2519 | }
|
---|
2520 |
|
---|
2521 | if ( !vdScriptTokenizerSkipIfIsPunctuatorEqual(pThis->pTokenizer, ',')
|
---|
2522 | && !vdScriptTokenizerIsPunctuatorEqual(pThis->pTokenizer, ')'))
|
---|
2523 | {
|
---|
2524 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \",\" or \")\" got...\n");
|
---|
2525 | break;
|
---|
2526 | }
|
---|
2527 | }
|
---|
2528 |
|
---|
2529 | /* Parse the compound or statement block now. */
|
---|
2530 | if (RT_SUCCESS(rc))
|
---|
2531 | {
|
---|
2532 | PVDSCRIPTASTSTMT pAstCompound = NULL;
|
---|
2533 |
|
---|
2534 | rc = vdScriptParseCompoundStatement(pThis, &pAstCompound);
|
---|
2535 | if (RT_SUCCESS(rc))
|
---|
2536 | {
|
---|
2537 | /*
|
---|
2538 | * Link compound statement block to function AST node and add it to the
|
---|
2539 | * list of functions.
|
---|
2540 | */
|
---|
2541 | pAstNodeFn->pCompoundStmts = pAstCompound;
|
---|
2542 | RTListAppend(&pThis->ListAst, &pAstNodeFn->Core.ListNode);
|
---|
2543 |
|
---|
2544 | PVDSCRIPTFN pFn = (PVDSCRIPTFN)RTMemAllocZ(sizeof(VDSCRIPTFN));
|
---|
2545 | if (pFn)
|
---|
2546 | {
|
---|
2547 | pFn->Core.pszString = pAstNodeFn->pFnIde->aszIde;
|
---|
2548 | pFn->Core.cchString = strlen(pFn->Core.pszString);
|
---|
2549 | pFn->fExternal = false;
|
---|
2550 | pFn->Type.Internal.pAstFn = pAstNodeFn;
|
---|
2551 | /** @todo: Parameters. */
|
---|
2552 | RTStrSpaceInsert(&pThis->hStrSpaceFn, &pFn->Core);
|
---|
2553 | }
|
---|
2554 | else
|
---|
2555 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Out of memory allocating memory for function\n");
|
---|
2556 | }
|
---|
2557 | }
|
---|
2558 |
|
---|
2559 | if (RT_FAILURE(rc))
|
---|
2560 | vdScriptAstNodeFree(&pAstNodeFn->Core);
|
---|
2561 | }
|
---|
2562 | else
|
---|
2563 | rc = vdScriptParserError(pThis, VERR_NO_MEMORY, RT_SRC_POS, "Parser: Out of memory allocating function AST node\n");
|
---|
2564 | }
|
---|
2565 | else
|
---|
2566 | rc = vdScriptParserError(pThis, VERR_INVALID_PARAMETER, RT_SRC_POS, "Parser: Expected \"(\" got...\n");
|
---|
2567 | }
|
---|
2568 | }
|
---|
2569 |
|
---|
2570 | if (RT_FAILURE(rc))
|
---|
2571 | {
|
---|
2572 | if (pRetType)
|
---|
2573 | vdScriptAstNodeFree(&pRetType->Core);
|
---|
2574 | if (pFnIde)
|
---|
2575 | vdScriptAstNodeFree(&pFnIde->Core);
|
---|
2576 | }
|
---|
2577 |
|
---|
2578 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
2579 | return rc;
|
---|
2580 | }
|
---|
2581 |
|
---|
2582 | /**
|
---|
2583 | * Parses the script from the given tokenizer.
|
---|
2584 | *
|
---|
2585 | * @returns VBox status code.
|
---|
2586 | * @param pThis The script context.
|
---|
2587 | */
|
---|
2588 | static int vdScriptParseFromTokenizer(PVDSCRIPTCTXINT pThis)
|
---|
2589 | {
|
---|
2590 | int rc = VINF_SUCCESS;
|
---|
2591 |
|
---|
2592 | LogFlowFunc(("pThis=%p\n", pThis));
|
---|
2593 |
|
---|
2594 | /* This is a very very simple LL(1) parser, don't expect much from it for now :). */
|
---|
2595 | while ( RT_SUCCESS(rc)
|
---|
2596 | && !vdScriptTokenizerIsEos(pThis->pTokenizer))
|
---|
2597 | rc = vdScriptParseAddFnDef(pThis);
|
---|
2598 |
|
---|
2599 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
2600 | return rc;
|
---|
2601 | }
|
---|
2602 |
|
---|
2603 | DECLHIDDEN(int) VDScriptCtxCreate(PVDSCRIPTCTX phScriptCtx)
|
---|
2604 | {
|
---|
2605 | int rc = VINF_SUCCESS;
|
---|
2606 |
|
---|
2607 | LogFlowFunc(("phScriptCtx=%p\n", phScriptCtx));
|
---|
2608 |
|
---|
2609 | AssertPtrReturn(phScriptCtx, VERR_INVALID_POINTER);
|
---|
2610 |
|
---|
2611 | PVDSCRIPTCTXINT pThis = (PVDSCRIPTCTXINT)RTMemAllocZ(sizeof(VDSCRIPTCTXINT));
|
---|
2612 | if (pThis)
|
---|
2613 | {
|
---|
2614 | pThis->hStrSpaceFn = NULL;
|
---|
2615 | RTListInit(&pThis->ListAst);
|
---|
2616 | *phScriptCtx = pThis;
|
---|
2617 | }
|
---|
2618 | else
|
---|
2619 | rc = VINF_SUCCESS;
|
---|
2620 |
|
---|
2621 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
2622 | return rc;
|
---|
2623 | }
|
---|
2624 |
|
---|
2625 | static int vdScriptCtxDestroyFnSpace(PRTSTRSPACECORE pStr, void *pvUser)
|
---|
2626 | {
|
---|
2627 | NOREF(pvUser);
|
---|
2628 |
|
---|
2629 | /*
|
---|
2630 | * Just free the whole structure, the AST for internal functions will be
|
---|
2631 | * destroyed later.
|
---|
2632 | */
|
---|
2633 | RTMemFree(pStr);
|
---|
2634 | return VINF_SUCCESS;
|
---|
2635 | }
|
---|
2636 |
|
---|
2637 | DECLHIDDEN(void) VDScriptCtxDestroy(VDSCRIPTCTX hScriptCtx)
|
---|
2638 | {
|
---|
2639 | PVDSCRIPTCTXINT pThis = hScriptCtx;
|
---|
2640 |
|
---|
2641 | AssertPtrReturnVoid(pThis);
|
---|
2642 |
|
---|
2643 | LogFlowFunc(("hScriptCtx=%p\n", pThis));
|
---|
2644 |
|
---|
2645 | RTStrSpaceDestroy(&pThis->hStrSpaceFn, vdScriptCtxDestroyFnSpace, NULL);
|
---|
2646 |
|
---|
2647 | /** @todo: Go through the list and destroy all ASTs. */
|
---|
2648 | RTMemFree(pThis);
|
---|
2649 | }
|
---|
2650 |
|
---|
2651 | DECLHIDDEN(int) VDScriptCtxCallbacksRegister(VDSCRIPTCTX hScriptCtx, PCVDSCRIPTCALLBACK paCallbacks,
|
---|
2652 | unsigned cCallbacks, void *pvUser)
|
---|
2653 | {
|
---|
2654 | int rc = VINF_SUCCESS;
|
---|
2655 | PVDSCRIPTCTXINT pThis = hScriptCtx;
|
---|
2656 |
|
---|
2657 | LogFlowFunc(("hScriptCtx=%p paCallbacks=%p cCallbacks=%u pvUser=%p\n",
|
---|
2658 | pThis, paCallbacks, cCallbacks, pvUser));
|
---|
2659 |
|
---|
2660 | AssertPtrReturn(pThis, VERR_INVALID_POINTER);
|
---|
2661 | AssertPtrReturn(paCallbacks, VERR_INVALID_POINTER);
|
---|
2662 | AssertReturn(cCallbacks > 0, VERR_INVALID_PARAMETER);
|
---|
2663 |
|
---|
2664 | /** @todo: Unregister already registered callbacks in case of an error. */
|
---|
2665 | do
|
---|
2666 | {
|
---|
2667 | PVDSCRIPTFN pFn = NULL;
|
---|
2668 |
|
---|
2669 | if (RTStrSpaceGet(&pThis->hStrSpaceFn, paCallbacks->pszFnName))
|
---|
2670 | {
|
---|
2671 | rc = VERR_DUPLICATE;
|
---|
2672 | break;
|
---|
2673 | }
|
---|
2674 |
|
---|
2675 | pFn = (PVDSCRIPTFN)RTMemAllocZ(RT_OFFSETOF(VDSCRIPTFN, aenmArgTypes[paCallbacks->cArgs]));
|
---|
2676 | if (!pFn)
|
---|
2677 | {
|
---|
2678 | rc = VERR_NO_MEMORY;
|
---|
2679 | break;
|
---|
2680 | }
|
---|
2681 |
|
---|
2682 | /** @todo: Validate argument and returns types. */
|
---|
2683 | pFn->Core.pszString = paCallbacks->pszFnName;
|
---|
2684 | pFn->Core.cchString = strlen(pFn->Core.pszString);
|
---|
2685 | pFn->fExternal = true;
|
---|
2686 | pFn->Type.External.pfnCallback = paCallbacks->pfnCallback;
|
---|
2687 | pFn->Type.External.pvUser = pvUser;
|
---|
2688 | pFn->enmTypeRetn = paCallbacks->enmTypeReturn;
|
---|
2689 | pFn->cArgs = paCallbacks->cArgs;
|
---|
2690 |
|
---|
2691 | for (unsigned i = 0; i < paCallbacks->cArgs; i++)
|
---|
2692 | pFn->aenmArgTypes[i] = paCallbacks->paArgs[i];
|
---|
2693 |
|
---|
2694 | RTStrSpaceInsert(&pThis->hStrSpaceFn, &pFn->Core);
|
---|
2695 | cCallbacks--;
|
---|
2696 | paCallbacks++;
|
---|
2697 | }
|
---|
2698 | while (cCallbacks);
|
---|
2699 |
|
---|
2700 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
2701 | return rc;
|
---|
2702 | }
|
---|
2703 |
|
---|
2704 | DECLHIDDEN(int) VDScriptCtxLoadScript(VDSCRIPTCTX hScriptCtx, const char *pszScript)
|
---|
2705 | {
|
---|
2706 | int rc = VINF_SUCCESS;
|
---|
2707 | PVDSCRIPTCTXINT pThis = hScriptCtx;
|
---|
2708 |
|
---|
2709 | LogFlowFunc(("hScriptCtx=%p pszScript=%p\n", pThis, pszScript));
|
---|
2710 |
|
---|
2711 | AssertPtrReturn(pThis, VERR_INVALID_POINTER);
|
---|
2712 | AssertPtrReturn(pszScript, VERR_INVALID_POINTER);
|
---|
2713 |
|
---|
2714 | PVDTOKENIZER pTokenizer = vdScriptTokenizerCreate(pszScript);
|
---|
2715 | if (pTokenizer)
|
---|
2716 | {
|
---|
2717 | pThis->pTokenizer = pTokenizer;
|
---|
2718 | rc = vdScriptParseFromTokenizer(pThis);
|
---|
2719 | pThis->pTokenizer = NULL;
|
---|
2720 | RTMemFree(pTokenizer);
|
---|
2721 | }
|
---|
2722 |
|
---|
2723 | LogFlowFunc(("returns rc=%Rrc\n", rc));
|
---|
2724 | return rc;
|
---|
2725 | }
|
---|
2726 |
|
---|
2727 | DECLHIDDEN(int) VDScriptCtxCallFn(VDSCRIPTCTX hScriptCtx, const char *pszFnCall,
|
---|
2728 | PVDSCRIPTARG paArgs, unsigned cArgs)
|
---|
2729 | {
|
---|
2730 | PVDSCRIPTCTXINT pThis = hScriptCtx;
|
---|
2731 | VDSCRIPTARG Ret;
|
---|
2732 | return vdScriptCtxInterprete(pThis, pszFnCall, paArgs, cArgs, &Ret);
|
---|
2733 | }
|
---|