VirtualBox

source: vbox/trunk/src/VBox/Storage/testcase/VDScript.cpp@ 58090

Last change on this file since 58090 was 57415, checked in by vboxsync, 9 years ago

More DECLCALLBACK fixes.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette