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