Exemplo n.º 1
0
 private function assertTokens($grammar, array $expectations, $msg = "")
 {
     $scanner = new Scanner(trim($grammar));
     $count = 0;
     while ($scanner->hasNextToken()) {
         $scanner->nextToken();
         $token = $scanner->currentToken();
         $this->assertNotNull($token, $msg);
         $expectation = $expectations[$count];
         $this->assertInstanceOf("de\\weltraumschafbnf\\Token", $token, "{$msg} {$count}: {$token->getValue()}");
         $this->assertEquals($expectation["type"], $token->getType(), "{$msg} {$count} type: {$token->getValue()}");
         $this->assertEquals($expectation["value"], $token->getValue(), "{$msg} {$count} value: {$token->getValue()}");
         $position = $token->getPosition();
         $this->assertInstanceOf("de\\weltraumschafbnf\\Position", $position, "{$msg} {$count}: {$token->getValue()}");
         $this->assertNull($position->getFile(), $count);
         $this->assertEquals($expectation["line"], $position->getLine(), "{$msg} {$count} line: {$token->getValue()}");
         $this->assertEquals($expectation["col"], $position->getColumn(), "{$msg} {$count} col: {$token->getValue()}");
         $count++;
     }
     $this->assertEquals(count($expectations), $count, "{$msg}: Not enough tokens!");
     foreach (array(1, 3, 5, 20, 200000) as $backTracks) {
         $index = $count - ($backTracks + 1);
         if ($index < 0) {
             try {
                 $token = $scanner->backtrackToken($backTracks);
                 $this->fail("Expected excpetion not thrown!");
             } catch (\InvalidArgumentException $e) {
             }
             continue;
         } else {
             $token = $scanner->backtrackToken($backTracks);
         }
         $expectation = $expectations[$index];
         $this->assertInstanceOf("de\\weltraumschafbnf\\Token", $token);
         $this->assertEquals($expectation["type"], $token->getType());
         $this->assertEquals($expectation["value"], $token->getValue());
         $position = $token->getPosition();
         $this->assertInstanceOf("de\\weltraumschafbnf\\Position", $position);
         $this->assertNull($position->getFile(), $count);
         $this->assertEquals($expectation["line"], $position->getLine());
         $this->assertEquals($expectation["col"], $position->getColumn());
     }
 }