1: // Outputs a root 'CompilationUnit' node that contains the children of the ZeroOrMore quantifier,
2: // which is zero or more FunctionDeclarations...
3: // If an error occurs in a FunctionDeclaration we will call the AdvanceToDefaultState method,
4: // that is defined below... it advances to the next 'Function' token and flags to continue parsing
5: this.Root.Production = functionDeclaration.OnError(AdvanceToDefaultState).ZeroOrMore().SetLabel("decl")
6: > Ast("CompilationUnit", AstChildrenFrom("decl"));
7:
8: // Outputs a 'FunctionDeclaration' node with children:
9: // 1) The function declaration name value
10: // 2) A 'Parameters' node that contains each parameter name; or nothing gets inserted
11: // if there are no parameters
12: // 3) The Block statement result
13: functionDeclaration.Production = @function + @identifier["name"] +
14: @openParenthesis.OnErrorContinue() + functionParameterList["params"].Optional() +
15: @closeParenthesis.OnErrorContinue() + block["block"].OnErrorContinue()
16: > Ast("FunctionDeclaration", AstFrom("name"),
17: AstConditionalFrom("Parameters", "params"), AstFrom("block"));
18:
19: // Outputs a 'FunctionParameterList' node whose children are parameter name values...
20: // Is an excellent sample of how to build node for a delimited list
21: functionParameterList.Production = @identifier["param"] +
22: (@comma + @identifier["param"].OnErrorContinue() > AstFrom("param")).ZeroOrMore().SetLabel("moreparams")
23: > Ast("FunctionParameterList", AstFrom("param"), AstChildrenFrom("moreparams"));
24:
25: // Outputs the result that came from the appropriate statement type that was matched...
26: // We don't want to wrap the results with a 'Statement' node so we use AstFrom
27: statement.Production = block["stmt"] > AstFrom("stmt")
28: | emptyStatement > null
29: | variableDeclarationStatement["stmt"] > AstFrom("stmt")
30: | assignmentStatement["stmt"] > AstFrom("stmt")
31: | returnStatement["stmt"] > AstFrom("stmt");
32:
33: // Outputs a 'Block' node whose children are the contained statements
34: block.Production = @openCurlyBrace + statement.OnErrorContinue().ZeroOrMore().SetLabel("stmt") +
35: @closeCurlyBrace.OnErrorContinue()
36: > Ast("Block", AstChildrenFrom("stmt"));
37:
38: // Don't output anything since we don't care about empty statements
39: emptyStatement.Production = semiColon > null;
40:
41: // Outputs a 'VariableDeclarationStatement' node whose child is the variable name
42: variableDeclarationStatement.Production = @var + @identifier["name"] + @semiColon.OnErrorContinue()
43: > Ast("VariableDeclarationStatement", AstFrom("name"));
44:
45: // Outputs a 'AssignmentStatement' node whose children are the variable name and the assigned expression
46: assignmentStatement.Production = @identifier["varname"] + @assignment +
47: expression["exp"].OnErrorContinue() + @semiColon.OnErrorContinue()
48: > Ast("AssignmentStatement", AstFrom("varname"), AstFrom("exp"));
49:
50: // Outputs a 'ReturnStatement' node whose child is the expression to return
51: returnStatement.Production = @return + expression["exp"].OnErrorContinue() + @semiColon.OnErrorContinue()
52: > Ast("ReturnStatement", AstFrom("exp"));
53:
54: // Outputs the resulting node from the EqualityExpression
55: expression.Production = equalityExpression["exp"] > AstFrom("exp");
56:
57: // Outputs the resulting node from the AdditiveExpression; or if an equality operator is found,
58: // outputs a '==' or '!=' node whose children are the left and right child expressions of the operator
59: equalityExpression.Production = additiveExpression["leftexp"] +
60: ((@equality | @inequality).SetLabel("op") +
61: equalityExpression["rightexp"].OnErrorContinue()).Optional()
62: > AstValueOfConditional(AstChildFrom("op"), AstFrom("leftexp"), AstFrom("rightexp"));
63:
64: // Outputs the resulting node from the MultiplicativeExpression; or if an additive operator is found,
65: // outputs a '+' or '-' node whose children are the left and right child expressions of the operator
66: additiveExpression.Production = multiplicativeExpression["leftexp"] +
67: ((@addition | @subtraction).SetLabel("op") +
68: additiveExpression["rightexp"].OnErrorContinue()).Optional()
69: > AstValueOfConditional(AstChildFrom("op"), AstFrom("leftexp"), AstFrom("rightexp"));
70:
71: // Outputs the resulting node from the PrimaryExpression; or if an multiplicative operator is found,
72: // outputs a '*' or '/' node whose children are the left and right child expressions of the operator
73: multiplicativeExpression.Production = primaryExpression["leftexp"] +
74: ((@multiplication | @division).SetLabel("op") +
75: multiplicativeExpression["rightexp"].OnErrorContinue()).Optional()
76: > AstValueOfConditional(AstChildFrom("op"), AstFrom("leftexp"), AstFrom("rightexp"));
77:
78: // Outputs the result that came from the appropriate expression type that was matched...
79: // We don't want to wrap the results with a 'Expression' node so we use AstFrom
80: primaryExpression.Production = numberExpression["exp"] > AstFrom("exp")
81: | functionAccessExpression["exp"] > AstFrom("exp")
82: | simpleName["exp"] > AstFrom("exp")
83: | parenthesizedExpression["exp"] > AstFrom("exp");
84:
85: // Outputs the numeric value
86: numberExpression.Production = @number.ToTerm().ToProduction();
87:
88: // Outputs the name value
89: simpleName.Production = @identifier.ToTerm().ToProduction();
90:
91: // Outputs a 'FunctionAccessExpression' node with children:
92: // 1) The function name
93: // 2) An 'Arguments' node that contains each argument name; or nothing gets inserted
94: // if there are no arguments
95: functionAccessExpression.Production = @identifier["name"] + @openParenthesis +
96: functionArgumentList["args"].Optional() + @closeParenthesis.OnErrorContinue()
97: > Ast("FunctionAccessExpression", AstFrom("name"), AstConditionalFrom("Arguments", "args"));
98:
99: // Outputs a 'FunctionArgumentList' node whose children are argument expressions...
100: // Is an excellent sample of how to build node for a delimited list
101: functionArgumentList.Production = expression["exp"] +
102: (@comma + expression["exp"].OnErrorContinue() > AstFrom("exp")).ZeroOrMore().SetLabel("moreexps")
103: > Ast("FunctionArgumentList", AstFrom("exp"), AstChildrenFrom("moreexps"));
104:
105: // Outputs a 'ParenthesizedExpression' node whose child is the contained expression
106: parenthesizedExpression.Production = @openParenthesis + expression["exp"] +
107: @closeParenthesis.OnErrorContinue()
108: > Ast("ParenthesizedExpression", AstFrom("exp"));