Example #1
0
 /**
  * Parse the ternary operator.
  *
  * @param Node $node The expression node.
  * @param int $precedence The precedence level.
  * @return Node The ternary operator node or the given expression node.
  * @throws SyntaxErrorException if no ternary else can be found after a ternary if.
  */
 private function parseTernary(Node $node, $precedence)
 {
     /* @var Token $token */
     while (($token = $this->stream->current()) && $this->operatorTable->isTernary($token)) {
         $operator = $this->operatorTable->getTernaryOperator($token);
         if ($operator->getIfCode() != $token->getCode() || $operator->getPrecedence() < $precedence) {
             break;
         }
         $operatorNode = $operator->getNode();
         $subPrecedence = $operator->isRightAssociative() ? $operator->getPrecedence() : $operator->getPrecedence() + 1;
         // Parse the if expression
         if ($operator->isShorthandAllowed() && $this->stream->getLookaheadCode() == $operator->getElseCode()) {
             $this->stream->next();
             $if = null;
         } else {
             $this->stream->next();
             $if = $this->parseExpression($subPrecedence);
         }
         // Parse the else expression
         $this->stream->expect([$operator->getElseCode()], function (Token $current = null) {
             if ($current) {
                 $near = substr($this->stream->getSource(), 0, $current->getOffset());
                 $message = "Ternary else expected; got `{$current->getValue()}`; near: " . $near;
             } else {
                 $near = substr($this->stream->getSource(), 0, strlen($this->stream->getSource()));
                 $message = "Ternary else expected but end of stream reached; near: " . $near;
             }
             throw new SyntaxErrorException($message);
         });
         $this->stream->next();
         $else = $this->parseExpression($subPrecedence);
         // Create the ternary operator node
         $node = $operatorNode($node, $if, $else);
     }
     return $node;
 }
Example #2
0
 /**
  * Test if can get the code for the lookahead token.
  *
  * @param \com\mohiva\common\parser\TokenStream $stream The token stream to use for this test.
  * @dataProvider tokenStreamProvider
  */
 public function testGetLookaheadCode(TokenStream $stream)
 {
     // user.name.split(" ").join("-")
     $this->assertSame($stream->getLookaheadCode(), self::T_POINT);
     $this->assertSame($stream->getLookaheadCode(1), self::T_POINT);
     $stream->next();
     $this->assertSame($stream->getLookaheadCode(1), self::T_NAME);
     $stream->next();
     $this->assertSame($stream->getLookaheadCode(1), self::T_POINT);
     $stream->next();
     $this->assertSame($stream->getLookaheadCode(1), self::T_NAME);
     $this->assertSame($stream->getLookaheadCode(2), self::T_OPEN_PARENTHESIS);
     $this->assertSame($stream->getLookaheadCode(3), self::T_VALUE);
     $this->assertSame($stream->getLookaheadCode(4), self::T_CLOSE_PARENTHESIS);
     $this->assertSame($stream->getLookaheadCode(5), self::T_POINT);
     $this->assertSame($stream->getLookaheadCode(6), self::T_NAME);
     $this->assertSame($stream->getLookaheadCode(7), self::T_OPEN_PARENTHESIS);
     $this->assertSame($stream->getLookaheadCode(8), self::T_VALUE);
     $this->assertSame($stream->getLookaheadCode(9), self::T_CLOSE_PARENTHESIS);
     $this->assertNull($stream->getLookaheadCode(10));
 }