예제 #1
0
 public function subparse($test = null)
 {
     $nodes = [];
     while (!$this->stream->isEOF()) {
         $token = $this->stream->getToken();
         $node = null;
         if ($token->test([TokenTypes::T_CODE_BLOCK, TokenTypes::T_EOF])) {
             $this->stream->next();
             continue;
         }
         if ($test && call_user_func($test, $this->stream)) {
             break;
         }
         $found = false;
         foreach ($this->parsers as $parser) {
             if ($parser->canParse($token, $this)) {
                 $found = true;
                 $node = $parser->parse($token, $this);
                 break;
             }
         }
         if (!$found) {
             throw new Exception('Syntax error, invalid syntax \'%s\' (%s) on line %d column %d', $token->getValue(), $token->getTokenName(), $token->getLine(), $token->getColumn());
         }
         if ($node) {
             $nodes[] = $node;
         }
     }
     return new Node($nodes, [], 0);
 }
예제 #2
0
 public function testTokenStream()
 {
     $stream = new TokenStream([new Token(null, 'Hello', TokenTypes::T_NAME, 0, 0), new Token(null, '"World"', TokenTypes::T_STR, 0, 0), new Token(null, '', TokenTypes::T_EOF, 0, 0)]);
     $this->assertTrue($stream->test(TokenTypes::T_NAME, 'Hello'));
     $this->assertEquals(new Token(null, '"World"', TokenTypes::T_STR, 0, 0), $stream->nextIf(TokenTypes::T_STR));
     $this->assertEquals(new Token(null, '"World"', TokenTypes::T_STR, 0, 0), $stream->getCurrent());
     $this->assertEquals(new Token(null, '"World"', TokenTypes::T_STR, 0, 0), $stream->expect(TokenTypes::T_STR));
     $this->assertTrue($stream->isEOF());
 }