Example #1
0
 /**
  * Parse the operand.
  *
  * @param Grammar $grammar The grammar of the parser.
  * @param TokenStream $stream The token stream to parse.
  * @return OperandNode The operand node.
  */
 public function parse(Grammar $grammar, TokenStream $stream)
 {
     /* @var \com\mohiva\pyramid\Token $token */
     $token = $stream->current();
     $node = new OperandNode($token->getValue());
     return $node;
 }
Example #2
0
 /**
  * When the given token is the next token then move
  * to it, otherwise throw a syntax error exception.
  *
  * @param int $token The token code to check.
  * @return AnnotationToken The next token.
  */
 private function next($token)
 {
     /* @var \com\mohiva\common\lang\AnnotationToken $lookahead */
     $lookahead = $this->stream->getLookahead();
     if ($lookahead && $lookahead->getCode() !== $token || !$lookahead) {
         $this->syntaxError(array($token), $lookahead);
     }
     $this->stream->next();
     return $this->stream->current();
 }
Example #3
0
 /**
  * Create an array from the token stream which contains only the tokens and the operators/values.
  *
  * @param TokenStream $stream The stream containing the lexer tokens.
  * @return array The actual list with tokens and operators/values.
  */
 private function buildActualTokens(TokenStream $stream)
 {
     $actual = array();
     while ($stream->valid()) {
         /* @var \com\mohiva\pyramid\Token $current */
         $current = $stream->current();
         $stream->next();
         $actual[] = array($current->getCode() => $current->getValue());
     }
     return $actual;
 }
Example #4
0
 /**
  * Parse the operand.
  *
  * @return Node The node object for the operand.
  * @throws SyntaxErrorException if no operand parser can be found for
  * the current token.
  */
 private function parseOperand()
 {
     /* @var Token $token */
     $token = $this->stream->current();
     try {
         /* @var Operand $operand */
         $operand = $this->operandTable->getOperand($token);
     } catch (InvalidIdentifierException $e) {
         $message = "Cannot find operand parser for token `{$token->getValue()}`";
         throw new SyntaxErrorException($message, 0, $e);
     }
     return $operand->parse($this->grammar, $this->stream);
 }
Example #5
0
 /**
  * Test if can move to a specified token.
  *
  * @param \com\mohiva\common\parser\TokenStream $stream The token stream to use for this test.
  * @dataProvider tokenStreamProvider
  */
 public function testMoveTo(TokenStream $stream)
 {
     // user.name.split(" ").join("-")
     $moved = $stream->moveTo(self::T_POINT);
     $current = $stream->current();
     $this->assertTrue($moved);
     $this->assertSame($current->getCode(), self::T_POINT);
     $moved = $stream->moveTo(self::T_POINT);
     $current = $stream->current();
     $this->assertTrue($moved);
     $this->assertSame($current->getCode(), self::T_POINT);
     $moved = $stream->moveTo(self::T_VALUE);
     $current = $stream->current();
     $this->assertTrue($moved);
     $this->assertSame($current->getCode(), self::T_VALUE);
     $moved = $stream->moveTo(self::T_OPEN_PARENTHESIS);
     $current = $stream->current();
     $this->assertTrue($moved);
     $this->assertSame($current->getCode(), self::T_OPEN_PARENTHESIS);
     $moved = $stream->moveTo(self::T_CLOSE_PARENTHESIS);
     $current = $stream->current();
     $this->assertTrue($moved);
     $this->assertSame($current->getCode(), self::T_CLOSE_PARENTHESIS);
     $moved = $stream->moveTo(self::T_NAME);
     $this->assertFalse($moved);
 }