Esempio n. 1
0
 function testPrefer()
 {
     $m = ['test', 'name' => '[a-z]\\w+', 'd' => '\\d+', '0'];
     $lexer = new Lexer();
     $lexer->init($m);
     $m = $lexer->match('test', 0);
     $this->assertEquals('test', $m[0]);
     $this->assertEquals('test', $m[1]);
     $m = $lexer->match('test', 0, 'name');
     $this->assertEquals('name', $m[0]);
     $this->assertEquals('test', $m[1]);
     $m = $lexer->match('test', 0, ['name']);
     $this->assertEquals('name', $m[0]);
     $this->assertEquals('test', $m[1]);
     $m = $lexer->match('test', 0, ['test']);
     $this->assertEquals('test', $m[0]);
     $this->assertEquals('test', $m[1]);
     $m = $lexer->match('test', 0, ['test', 'name']);
     $this->assertEquals('test', $m[0]);
     $this->assertEquals('test', $m[1]);
     $m = $lexer->match('0', 0, ['name']);
     $this->assertEquals('0', $m[0]);
     $this->assertEquals('0', $m[1]);
 }
Esempio n. 2
0
 function next($prefer = null)
 {
     if ($this->end) {
         return null;
     }
     if (!empty($this->back)) {
         $token = array_pop($this->back);
         $this->cur = $token;
         if ($token === null) {
             $this->end = true;
         }
         return $this->cur;
     }
     $token = $this->lexer->match($this->s, $this->offset, $prefer);
     if ($token === null) {
         if ($this->offset != strlen($this->s)) {
             throw new LexException($this->s[$this->offset], $this->line, $this->char_offset);
         }
         $this->end = true;
         $this->cur = null;
         return null;
     }
     $this->processToken($token);
     return $this->cur;
 }