/** * Test the `addOperand` and `getOperandTable` accessors. */ public function testOperandTableAccessors() { /* @var \PHPUnit_Framework_MockObject_MockObject $operand */ $operand = $this->getMock('com\\mohiva\\pyramid\\Operand'); $operand->expects($this->any())->method('getIdentifiers')->will($this->returnValue(array(1))); /* @var \com\mohiva\pyramid\Operand $operand */ $token = new Token(1, 1, 1); $grammar = new Grammar(); $grammar->addOperand($operand); $this->assertSame($operand, $grammar->getOperandTable()->getOperand($token)); }
/** * Creates the grammar. */ public function __construct() { parent::__construct(); // Note: unary +/- operators must have higher precedence as all binary operators // http://www.antlr.org/pipermail/antlr-dev/2009-April/002255.html $this->addOperator(new UnaryOperator(Lexer::T_PLUS, 5, function ($node) { return new UnaryPosNode($node); })); $this->addOperator(new UnaryOperator(Lexer::T_MINUS, 5, function ($node) { return new UnaryNegNode($node); })); $this->addOperator(new TernaryOperator(Lexer::T_QUESTION_MARK, Lexer::T_COLON, 1, TernaryOperator::RIGHT, true, function ($condition, $if, $else) { return new TernaryIfNode($condition, $if, $else); })); $this->addOperator(new BinaryOperator(Lexer::T_PLUS, 2, BinaryOperator::LEFT, function ($left, $right) { return new BinaryAddNode($left, $right); })); $this->addOperator(new BinaryOperator(Lexer::T_MINUS, 2, BinaryOperator::LEFT, function ($left, $right) { return new BinarySubNode($left, $right); })); $this->addOperator(new BinaryOperator(Lexer::T_MUL, 3, BinaryOperator::LEFT, function ($left, $right) { return new BinaryMulNode($left, $right); })); $this->addOperator(new BinaryOperator(Lexer::T_DIV, 3, BinaryOperator::LEFT, function ($left, $right) { return new BinaryDivNode($left, $right); })); $this->addOperator(new BinaryOperator(Lexer::T_MOD, 3, BinaryOperator::LEFT, function ($left, $right) { return new BinaryModNode($left, $right); })); $this->addOperator(new BinaryOperator(Lexer::T_POWER, 4, BinaryOperator::RIGHT, function ($left, $right) { return new BinaryPowerNode($left, $right); })); $this->addOperand(new NumberOperand()); $this->addOperand(new ParenthesesOperand()); }
/** * Test if the `parse` method throws an exception if the closing parentheses is missing * and the end of the stream is reached. * * @expectedException \com\mohiva\common\exceptions\SyntaxErrorException */ public function testParseThrowsExceptionIfEndOfStreamIsReached() { $tokenStream = new TokenStream(); $tokenStream->push(new Token(Lexer::T_OPEN_PARENTHESIS, '(', 1)); $tokenStream->push(new Token(Lexer::T_NUMBER, 100, 1)); $tokenStream->rewind(); $grammar = new Grammar(); $grammar->addOperand(new NumberOperand()); $operand = new ParenthesesOperand(); $operand->parse($grammar, $tokenStream); }
/** * The class constructor. * * @param Grammar $grammar The grammar used for this parser. */ public function __construct(Grammar $grammar) { $this->grammar = $grammar; $this->operatorTable = $this->grammar->getOperatorTable(); $this->operandTable = $this->grammar->getOperandTable(); }