public OperatorPrecedenceParserTests() { expression = new OperatorPrecedenceParser<Expression>(); expression.Atom(SampleLexer.Digit, token => new Constant(int.Parse(token.Literal))); expression.Atom(SampleLexer.Name, token => new Identifier(token.Literal)); expression.Unit(SampleLexer.LeftParen, Between(Token("("), expression, Token(")"))); expression.Binary(SampleLexer.Add, 3, (left, symbol, right) => new Form(symbol, left, right)); expression.Binary(SampleLexer.Subtract, 3, (left, symbol, right) => new Form(symbol, left, right)); expression.Binary(SampleLexer.Multiply, 4, (left, symbol, right) => new Form(symbol, left, right)); expression.Binary(SampleLexer.Divide, 4, (left, symbol, right) => new Form(symbol, left, right)); expression.Binary(SampleLexer.Exponent, 5, (left, symbol, right) => new Form(symbol, left, right), Associativity.Right); expression.Prefix(SampleLexer.Subtract, 6, (symbol, operand) => new Form(new Identifier(symbol.Literal), operand)); expression.Postfix(SampleLexer.Increment, 7, (symbol, operand) => new Form(new Identifier(symbol.Literal), operand)); expression.Postfix(SampleLexer.Decrement, 7, (symbol, operand) => new Form(new Identifier(symbol.Literal), operand)); expression.Extend(SampleLexer.LeftParen, 8, callable => from arguments in Between(Token("("), ZeroOrMore(expression, Token(",")), Token(")")) select new Form(callable, arguments)); }
public OperatorPrecedenceParserTests() { expression = new OperatorPrecedenceParser <Expression>(); expression.Atom(SampleLexer.Digit, token => new Constant(int.Parse(token.Literal))); expression.Atom(SampleLexer.Name, token => new Identifier(token.Literal)); expression.Unit(SampleLexer.LeftParen, Between(Token("("), expression, Token(")"))); expression.Binary(SampleLexer.Add, 3, (left, symbol, right) => new Form(symbol, left, right)); expression.Binary(SampleLexer.Subtract, 3, (left, symbol, right) => new Form(symbol, left, right)); expression.Binary(SampleLexer.Multiply, 4, (left, symbol, right) => new Form(symbol, left, right)); expression.Binary(SampleLexer.Divide, 4, (left, symbol, right) => new Form(symbol, left, right)); expression.Binary(SampleLexer.Exponent, 5, (left, symbol, right) => new Form(symbol, left, right), Associativity.Right); expression.Prefix(SampleLexer.Subtract, 6, (symbol, operand) => new Form(new Identifier(symbol.Literal), operand)); expression.Postfix(SampleLexer.Increment, 7, (symbol, operand) => new Form(new Identifier(symbol.Literal), operand)); expression.Postfix(SampleLexer.Decrement, 7, (symbol, operand) => new Form(new Identifier(symbol.Literal), operand)); expression.Extend(SampleLexer.LeftParen, 8, callable => from arguments in Between(Token("("), ZeroOrMore(expression, Token(",")), Token(")")) select new Form(callable, arguments)); }
public OperatorPrecedenceParserTests() { expression = new OperatorPrecedenceParser <IExpression>(); expression.Atom(SampleLexer.Digit, digit => new Constant(int.Parse(digit, CultureInfo.InvariantCulture))); expression.Atom(SampleLexer.Name, name => new Identifier(name)); expression.Unit(SampleLexer.LeftParen, Between(SampleLexer.LeftParen.Kind(), expression, SampleLexer.RightParen.Kind())); expression.Binary(SampleLexer.Add, 3, (left, symbol, right) => new Form(symbol, left, right)); expression.Binary(SampleLexer.Subtract, 3, (left, symbol, right) => new Form(symbol, left, right)); expression.Binary(SampleLexer.Multiply, 4, (left, symbol, right) => new Form(symbol, left, right)); expression.Binary(SampleLexer.Divide, 4, (left, symbol, right) => new Form(symbol, left, right)); expression.Binary(SampleLexer.Exponent, 5, (left, symbol, right) => new Form(symbol, left, right), Associativity.Right); expression.Prefix(SampleLexer.Subtract, 6, (subtract, operand) => new Form(new Identifier(subtract), operand)); expression.Postfix(SampleLexer.Increment, 7, (increment, operand) => new Form(new Identifier(increment), operand)); expression.Postfix(SampleLexer.Decrement, 7, (decrement, operand) => new Form(new Identifier(decrement), operand)); expression.Extend(SampleLexer.LeftParen, 8, callable => from arguments in Between(SampleLexer.LeftParen.Kind(), ZeroOrMore(expression, SampleLexer.Comma.Kind()), SampleLexer.RightParen.Kind()) select new Form(callable, arguments)); }