Ejemplo n.º 1
0
 function expectToken($name)
 {
     $token = $this->lexer->expectToken($this->s, $this->offset, $name);
     if ($token === null) {
         throw new LexException($this->s[$this->offset], $this->line, $this->char_offset);
     }
     $this->processToken($token);
     return $this->cur;
 }
Ejemplo n.º 2
0
 function testExpectToken()
 {
     $m = ['+', '-', 'd' => '\\d+'];
     $lexer = new Lexer($m);
     $tok = $lexer->expectToken('123', 0, 'd');
     $this->assertNotNull($tok);
     $this->assertEquals('123', $tok[1]);
     $this->assertNotEmpty($lexer->expectToken('12+34', 0, 'd'));
     $this->assertNotEmpty($lexer->expectToken('12+34', 2, '+'));
     $this->assertNotEmpty($lexer->expectToken('12+34', 3, 'd'));
     $this->assertNull($lexer->expectToken('12+34', 3, '+'));
     try {
         $lexer->expectToken('12+34', 0, 'xx');
         $this->fail();
     } catch (\Exception $e) {
         //
     }
     $stream = $lexer->getTokenStream('1+2');
     $stream->next();
     $tok = $stream->expectToken('+');
     $this->assertEquals('+', $tok[0]);
     $this->assertEquals('+', $tok[1]);
     $this->assertEquals(1, $tok[2]);
     $this->assertEquals(1, $tok[3]);
     $tok = $stream->expectToken('d');
     $this->assertNotEmpty($tok);
     $stream = $lexer->getTokenStream('1+2');
     $tok = $stream->expectToken('d');
     $this->assertNotEmpty($tok);
 }