Example #1
0
 /**
  * Parse the expression.
  *
  * @param int $precedence The precedence level.
  * @return Node The expression node.
  */
 private function parseExpression($precedence)
 {
     /* @var Token $token */
     $node = $this->parsePrimary();
     while (($token = $this->stream->current()) && $this->operatorTable->isBinary($token)) {
         $operator = $this->operatorTable->getBinaryOperator($token);
         if ($operator->getPrecedence() < $precedence) {
             break;
         }
         $this->stream->next();
         $operatorNode = $operator->getNode();
         $node = $operatorNode($node, $this->parseExpression($operator->isRightAssociative() ? $operator->getPrecedence() : $operator->getPrecedence() + 1));
     }
     $node = $this->parseTernary($node, $precedence);
     return $node;
 }
Example #2
0
 /**
  * Test if the `isBinary` method returns false if the given token isn't a binary operator.
  */
 public function testIsBinaryReturnsFalse()
 {
     $operator = new UnaryOperator(1, 10, function () {
     });
     $token = new Token($operator->getCode(), '+', 1);
     $table = new OperatorTable();
     $table->addOperator($operator);
     $this->assertFalse($table->isBinary($token));
 }