View source
<?php
class Twig_TokenStream {
protected $tokens;
protected $current;
protected $filename;
public function __construct(array $tokens, $filename = null) {
$this->tokens = $tokens;
$this->current = 0;
$this->filename = $filename;
}
public function __toString() {
return implode("\n", $this->tokens);
}
public function injectTokens(array $tokens) {
$this->tokens = array_merge(array_slice($this->tokens, 0, $this->current), $tokens, array_slice($this->tokens, $this->current));
}
public function next() {
if (!isset($this->tokens[++$this->current])) {
throw new Twig_Error_Syntax('Unexpected end of template', $this->tokens[$this->current - 1]
->getLine(), $this->filename);
}
return $this->tokens[$this->current - 1];
}
public function nextIf($primary, $secondary = null) {
if ($this->tokens[$this->current]
->test($primary, $secondary)) {
return $this
->next();
}
}
public function expect($type, $value = null, $message = null) {
$token = $this->tokens[$this->current];
if (!$token
->test($type, $value)) {
$line = $token
->getLine();
throw new Twig_Error_Syntax(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s)', $message ? $message . '. ' : '', Twig_Token::typeToEnglish($token
->getType()), $token
->getValue(), Twig_Token::typeToEnglish($type), $value ? sprintf(' with value "%s"', $value) : ''), $line, $this->filename);
}
$this
->next();
return $token;
}
public function look($number = 1) {
if (!isset($this->tokens[$this->current + $number])) {
throw new Twig_Error_Syntax('Unexpected end of template', $this->tokens[$this->current + $number - 1]
->getLine(), $this->filename);
}
return $this->tokens[$this->current + $number];
}
public function test($primary, $secondary = null) {
return $this->tokens[$this->current]
->test($primary, $secondary);
}
public function isEOF() {
return $this->tokens[$this->current]
->getType() === Twig_Token::EOF_TYPE;
}
public function getCurrent() {
return $this->tokens[$this->current];
}
public function getFilename() {
return $this->filename;
}
}