/** * Attempts to match the given token with the current lookahead token. * * If they match, updates the lookahead token; otherwise raises a syntax * error. * * @param int $token The token type. * * @return void * * @throws QueryException If the tokens don't match. */ public function match($token) { $lookaheadType = $this->lexer->lookahead['type']; // short-circuit on first condition, usually types match if ($lookaheadType !== $token && $token !== Lexer::T_IDENTIFIER && $lookaheadType <= Lexer::T_IDENTIFIER) { $this->syntaxError($this->lexer->getLiteral($token)); } $this->lexer->moveNext(); }
/** * QueryLanguage ::= SelectStatement | UpdateStatement | DeleteStatement * * @return \Doctrine\ORM\Query\AST\SelectStatement | * \Doctrine\ORM\Query\AST\UpdateStatement | * \Doctrine\ORM\Query\AST\DeleteStatement */ public function QueryLanguage() { $this->lexer->moveNext(); switch ($this->lexer->lookahead['type']) { case Lexer::T_SELECT: $statement = $this->SelectStatement(); break; case Lexer::T_UPDATE: $statement = $this->UpdateStatement(); break; case Lexer::T_DELETE: $statement = $this->DeleteStatement(); break; default: $this->syntaxError('SELECT, UPDATE or DELETE'); break; } // Check for end of string if ($this->lexer->lookahead !== null) { $this->syntaxError('end of string'); } return $statement; }
public function testScannerTokenizesASimpleQueryCorrectly() { $dql = "SELECT u FROM My\\Namespace\\User u WHERE u.name = 'Jack O''Neil'"; $lexer = new Lexer($dql); $tokens = array(array('value' => 'SELECT', 'type' => Lexer::T_SELECT, 'position' => 0), array('value' => 'u', 'type' => Lexer::T_IDENTIFIER, 'position' => 7), array('value' => 'FROM', 'type' => Lexer::T_FROM, 'position' => 9), array('value' => 'My\\Namespace\\User', 'type' => Lexer::T_IDENTIFIER, 'position' => 14), array('value' => 'u', 'type' => Lexer::T_IDENTIFIER, 'position' => 32), array('value' => 'WHERE', 'type' => Lexer::T_WHERE, 'position' => 34), array('value' => 'u', 'type' => Lexer::T_IDENTIFIER, 'position' => 40), array('value' => '.', 'type' => Lexer::T_DOT, 'position' => 41), array('value' => 'name', 'type' => Lexer::T_IDENTIFIER, 'position' => 42), array('value' => '=', 'type' => Lexer::T_EQUALS, 'position' => 47), array('value' => "Jack O'Neil", 'type' => Lexer::T_STRING, 'position' => 49)); foreach ($tokens as $expected) { $lexer->moveNext(); $actual = $lexer->lookahead; $this->assertEquals($expected['value'], $actual['value']); $this->assertEquals($expected['type'], $actual['type']); $this->assertEquals($expected['position'], $actual['position']); } $this->assertFalse($lexer->moveNext()); }