/** * Check if the next token matches the expected type and data. If it does, then consume it, otherwise * return false. * * @param int $type The expected token type * @param null|string $data The expected token data or null * * @return boolean|Token */ protected function checkAndExpectToken($type, $data = null) { if ($this->checkToken($type, $data)) { $token = $this->tokenQueue->peek(); $this->tokenQueue->next(); return $token; } return false; }
protected function assertTokens($tokens, TokenQueue $queue) { $queue->reset(); $it = new \ArrayIterator($tokens); $token = $queue->peek(); while ($it->valid()) { $expectedToken = $it->current(); $this->assertFalse($queue->isEof(), 'There is no more tokens, expected = ' . $expectedToken[1]); $this->assertToken($expectedToken[0], $expectedToken[1], $token); $token = $queue->next(); $it->next(); } $this->assertTrue($queue->isEof(), 'There are more unexpected tokens.'); }
public function __construct(TokenQueue $queue, $msg) { $token = $queue->peek(); $msg = sprintf("PARSER ERROR: %s. Current token is [%s, '%s'] at line %s, column %s", $msg, GenericToken::getTypeName($token->getType()), $token->getData(), $token->getLine(), $token->getRow()); // construct a lookup of the next tokens $lookup = ''; for ($i = 1; $i <= 5; $i++) { if ($queue->isEof()) { break; } $token = $queue->get(); $lookup .= $token->getData() . ' '; } $msg .= "\nBuffer lookup: \"{$lookup}\""; parent::__construct($msg); }