]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/twig/twig/lib/Twig/TokenStream.php
4 * This file is part of Twig.
6 * (c) 2009 Fabien Potencier
7 * (c) 2009 Armin Ronacher
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
14 * Represents a token stream.
16 * @author Fabien Potencier <fabien@symfony.com>
18 class Twig_TokenStream
27 * @param array $tokens An array of tokens
28 * @param string $filename The name of the filename which tokens are associated with
30 public function __construct(array $tokens, $filename = null)
32 $this->tokens
= $tokens;
34 $this->filename
= $filename;
38 * Returns a string representation of the token stream.
42 public function __toString()
44 return implode("\n", $this->tokens
);
47 public function injectTokens(array $tokens)
49 $this->tokens
= array_merge(array_slice($this->tokens
, 0, $this->current
), $tokens, array_slice($this->tokens
, $this->current
));
53 * Sets the pointer to the next token and returns the old one.
57 public function next()
59 if (!isset($this->tokens
[++
$this->current
])) {
60 throw new Twig_Error_Syntax('Unexpected end of template', $this->tokens
[$this->current
- 1]->getLine(), $this->filename
);
63 return $this->tokens
[$this->current
- 1];
67 * Tests a token and returns it or throws a syntax error.
71 public function expect($type, $value = null, $message = null)
73 $token = $this->tokens
[$this->current
];
74 if (!$token->test($type, $value)) {
75 $line = $token->getLine();
76 throw new Twig_Error_Syntax(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s)',
77 $message ? $message.'. ' : '',
78 Twig_Token
::typeToEnglish($token->getType(), $line), $token->getValue(),
79 Twig_Token
::typeToEnglish($type, $line), $value ? sprintf(' with value "%s"', $value) : ''),
90 * Looks at the next token.
92 * @param integer $number
96 public function look($number = 1)
98 if (!isset($this->tokens
[$this->current +
$number])) {
99 throw new Twig_Error_Syntax('Unexpected end of template', $this->tokens
[$this->current +
$number - 1]->getLine(), $this->filename
);
102 return $this->tokens
[$this->current +
$number];
106 * Tests the current token
110 public function test($primary, $secondary = null)
112 return $this->tokens
[$this->current
]->test($primary, $secondary);
116 * Checks if end of stream was reached
120 public function isEOF()
122 return $this->tokens
[$this->current
]->getType() === Twig_Token
::EOF_TYPE
;
126 * Gets the current token
130 public function getCurrent()
132 return $this->tokens
[$this->current
];
136 * Gets the filename associated with this stream
140 public function getFilename()
142 return $this->filename
;