public function testIsEofEmptyQueue() { $queue = new TokenQueue(); $this->assertTrue($queue->isEof()); $queue->add(new Token(0, 'token')); $this->assertFalse($queue->isEof()); }
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.'); }
/** * Check the next token without consuming it and return true if it matches the given type and data. * If the data is not provided (equal to null) then only the token type is checked. * Return false otherwise. * * @param int $type The expected token type * @param null|string $data The expected data or null * @param bool $ignoreCase whether to do string comparisons case insensitive or sensitive * * @return boolean */ protected function checkToken($type, $data = null, $ignoreCase = false) { if ($this->tokenQueue->isEof()) { return false; } $token = $this->tokenQueue->peek(); if ($token->getType() !== $type) { return false; } if ($data && $token->getData() !== $data) { if ($ignoreCase && is_string($data) && is_string($token->getData())) { return strcasecmp($data, $token->getData()); } return false; } return true; }
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); }