function expectString($str) { $result = $this->lexer->expectString($this->s, $this->offset, $str); if (!$result) { throw new LexException($this->s[$this->offset], $this->line, $this->char_offset); } $tok = [$str, $str]; $this->processToken($tok); return $this->cur; }
function testExpectString() { $m = ['+', '-', 'd' => '\\d+']; $lexer = new Lexer($m); $this->assertFalse($lexer->expectString('123', 0, 'd')); $this->assertTrue($lexer->expectString('123', 0, '123')); $this->assertTrue($lexer->expectString('12+34', 2, '+')); $this->assertTrue($lexer->expectString('12+34', 3, '34')); $stream = $lexer->getTokenStream('1+2'); $stream->next(); $tok = $stream->expectString('+'); $this->assertEquals('+', $tok[0]); $this->assertEquals('+', $tok[1]); $this->assertEquals(1, $tok[2]); $this->assertEquals(1, $tok[3]); $tok = $stream->expectString('2'); $this->assertEquals('2', $tok[0]); $this->assertEquals('2', $tok[1]); $this->assertNotEmpty($tok); $stream = $lexer->getTokenStream('1+2'); $tok = $stream->expectString('1'); $this->assertEquals('1', $tok[0]); $this->assertEquals('1', $tok[1]); $this->assertNotEmpty($tok); }