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); }
/** * Check if the next token matches the expected type and data. If it does, then consume and return it, * otherwise throw an exception. * * @param int $type The expected token type * @param null|string $data The expected token data or null * * @return Token * * @throws ParserException */ protected function expectToken($type, $data = null) { $token = $this->tokenQueue->peek(); if (!$this->checkToken($type, $data)) { throw new ParserException($this->tokenQueue, sprintf("Expected token [%s, '%s']", Token::getTypeName($type), $data)); } $this->tokenQueue->next(); return $token; }
protected function assertToken($type, $data, Token $token) { $this->assertEquals($type, $token->getType(), sprintf('Expected token [%s, %s], found [%s, %s]', Token::getTypeName($type), $data, Token::getTypeName($token->getType()), $token->getData())); $this->assertEquals($data, trim($token->getData()), sprintf('Expected token [%s, %s], found [%s, %s]', Token::getTypeName($type), $data, Token::getTypeName($token->getType()), $token->getData())); }