include:"../../../Source/Parsing/Parsing.h"
include:"../../../Source/Parsing/ParsingDefinitions.h"
include:"../../../Source/Parsing/ParsingAutomaton.h"
namespace:test.parser
reflection:System
classPrefix:Cal
parser:ParseExpression(Exp)
grammar:

class Expression
{
}

class NumberExpression : Expression
{
    token value;
}

class BinaryExpression : Expression
{
    enum BinaryOperator
    {
        Add,
        Sub,
        Mul,
        Div,
    }

    Expression firstOperand;
    Expression secondOperand;
    BinaryOperator binaryOperator;
}

class FunctionExpression : Expression
{
    token functionName;
    Expression[] arguments;
}

token NAME = "[a-zA-Z_]/w*";
token NUMBER = "/d+(./d+)?";
token ADD = "/+";
token SUB = "-";
token MUL = "/*";
token DIV = "//";
token LEFT = "/(";
token RIGHT = "/)";
token COMMA = ",";
discardtoken SPACE = "/s+";

rule NumberExpression Number
        = NUMBER : value as NumberExpression
        ;
rule FunctionExpression Call
        = NAME : functionName "(" [ Exp : arguments { "," Exp : arguments } ] ")" as FunctionExpression
        ;
rule Expression Factor
        = !Number | !Call
        = "(" !Exp ")"
        ;
rule Expression Term
        = !Factor
        = Term : firstOperand "*" Factor : secondOperand as BinaryExpression with { binaryOperator = "Mul" }
        = Term : firstOperand "/" Factor : secondOperand as BinaryExpression with { binaryOperator = "Div" }
        ;
rule Expression Exp
        = !Term
        = Exp : firstOperand "+" Term : secondOperand as BinaryExpression with { binaryOperator = "Add" }
        = Exp : firstOperand "-" Term : secondOperand as BinaryExpression with { binaryOperator = "Sub" }
        ;
