composer package updates
[openemr.git] / vendor / twig / twig / lib / Twig / TokenStream.php
blob81c043ca9f11f243708024277b7c8599a5915443
1 <?php
3 /*
4 * This file is part of Twig.
6 * (c) Fabien Potencier
7 * (c) Armin Ronacher
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
13 /**
14 * Represents a token stream.
16 * @final
18 * @author Fabien Potencier <fabien@symfony.com>
20 class Twig_TokenStream
22 protected $tokens;
23 protected $current = 0;
24 protected $filename;
26 private $source;
28 /**
29 * @param array $tokens An array of tokens
30 * @param string|null $name The name of the template which tokens are associated with
31 * @param string|null $source The source code associated with the tokens
33 public function __construct(array $tokens, $name = null, $source = null)
35 if (!$name instanceof Twig_Source) {
36 if (null !== $name || null !== $source) {
37 @trigger_error(sprintf('Passing a string as the $name argument of %s() is deprecated since version 1.27. Pass a Twig_Source instance instead.', __METHOD__), E_USER_DEPRECATED);
39 $this->source = new Twig_Source($source, $name);
40 } else {
41 $this->source = $name;
44 $this->tokens = $tokens;
46 // deprecated, not used anymore, to be removed in 2.0
47 $this->filename = $this->source->getName();
50 public function __toString()
52 return implode("\n", $this->tokens);
55 public function injectTokens(array $tokens)
57 $this->tokens = array_merge(array_slice($this->tokens, 0, $this->current), $tokens, array_slice($this->tokens, $this->current));
60 /**
61 * Sets the pointer to the next token and returns the old one.
63 * @return Twig_Token
65 public function next()
67 if (!isset($this->tokens[++$this->current])) {
68 throw new Twig_Error_Syntax('Unexpected end of template.', $this->tokens[$this->current - 1]->getLine(), $this->source);
71 return $this->tokens[$this->current - 1];
74 /**
75 * Tests a token, sets the pointer to the next one and returns it or throws a syntax error.
77 * @return Twig_Token|null The next token if the condition is true, null otherwise
79 public function nextIf($primary, $secondary = null)
81 if ($this->tokens[$this->current]->test($primary, $secondary)) {
82 return $this->next();
86 /**
87 * Tests a token and returns it or throws a syntax error.
89 * @return Twig_Token
91 public function expect($type, $value = null, $message = null)
93 $token = $this->tokens[$this->current];
94 if (!$token->test($type, $value)) {
95 $line = $token->getLine();
96 throw new Twig_Error_Syntax(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s).',
97 $message ? $message.'. ' : '',
98 Twig_Token::typeToEnglish($token->getType()), $token->getValue(),
99 Twig_Token::typeToEnglish($type), $value ? sprintf(' with value "%s"', $value) : ''),
100 $line,
101 $this->source
104 $this->next();
106 return $token;
110 * Looks at the next token.
112 * @param int $number
114 * @return Twig_Token
116 public function look($number = 1)
118 if (!isset($this->tokens[$this->current + $number])) {
119 throw new Twig_Error_Syntax('Unexpected end of template.', $this->tokens[$this->current + $number - 1]->getLine(), $this->source);
122 return $this->tokens[$this->current + $number];
126 * Tests the current token.
128 * @return bool
130 public function test($primary, $secondary = null)
132 return $this->tokens[$this->current]->test($primary, $secondary);
136 * Checks if end of stream was reached.
138 * @return bool
140 public function isEOF()
142 return Twig_Token::EOF_TYPE === $this->tokens[$this->current]->getType();
146 * @return Twig_Token
148 public function getCurrent()
150 return $this->tokens[$this->current];
154 * Gets the name associated with this stream (null if not defined).
156 * @return string|null
158 * @deprecated since 1.27 (to be removed in 2.0)
160 public function getFilename()
162 @trigger_error(sprintf('The %s() method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', __METHOD__), E_USER_DEPRECATED);
164 return $this->source->getName();
168 * Gets the source code associated with this stream.
170 * @return string
172 * @internal Don't use this as it might be empty depending on the environment configuration
174 * @deprecated since 1.27 (to be removed in 2.0)
176 public function getSource()
178 @trigger_error(sprintf('The %s() method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', __METHOD__), E_USER_DEPRECATED);
180 return $this->source->getCode();
184 * Gets the source associated with this stream.
186 * @return Twig_Source
188 * @internal
190 public function getSourceContext()
192 return $this->source;
196 class_alias('Twig_TokenStream', 'Twig\TokenStream', false);