ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
TokenStream.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
7  * (c) Armin Ronacher
8  *
9  * For the full copyright and license information, please view the LICENSE
10  * file that was distributed with this source code.
11  */
12 
21 {
22  protected $tokens;
23  protected $current = 0;
24  protected $filename;
25 
26  private $source;
27 
33  public function __construct(array $tokens, $name = null, $source = null)
34  {
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);
38  }
39  $this->source = new Twig_Source($source, $name);
40  } else {
41  $this->source = $name;
42  }
43 
44  $this->tokens = $tokens;
45 
46  // deprecated, not used anymore, to be removed in 2.0
47  $this->filename = $this->source->getName();
48  }
49 
50  public function __toString()
51  {
52  return implode("\n", $this->tokens);
53  }
54 
55  public function injectTokens(array $tokens)
56  {
57  $this->tokens = array_merge(array_slice($this->tokens, 0, $this->current), $tokens, array_slice($this->tokens, $this->current));
58  }
59 
65  public function next()
66  {
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);
69  }
70 
71  return $this->tokens[$this->current - 1];
72  }
73 
79  public function nextIf($primary, $secondary = null)
80  {
81  if ($this->tokens[$this->current]->test($primary, $secondary)) {
82  return $this->next();
83  }
84  }
85 
91  public function expect($type, $value = null, $message = null)
92  {
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
102  );
103  }
104  $this->next();
105 
106  return $token;
107  }
108 
116  public function look($number = 1)
117  {
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);
120  }
121 
122  return $this->tokens[$this->current + $number];
123  }
124 
130  public function test($primary, $secondary = null)
131  {
132  return $this->tokens[$this->current]->test($primary, $secondary);
133  }
134 
140  public function isEOF()
141  {
142  return Twig_Token::EOF_TYPE === $this->tokens[$this->current]->getType();
143  }
144 
148  public function getCurrent()
149  {
150  return $this->tokens[$this->current];
151  }
152 
160  public function getFilename()
161  {
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);
163 
164  return $this->source->getName();
165  }
166 
176  public function getSource()
177  {
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);
179 
180  return $this->source->getCode();
181  }
182 
190  public function getSourceContext()
191  {
192  return $this->source;
193  }
194 }
195 
196 class_alias('Twig_TokenStream', 'Twig\TokenStream', false);
__construct(array $tokens, $name=null, $source=null)
Definition: TokenStream.php:33
getSource()
Gets the source code associated with this stream.
$type
getFilename()
Gets the name associated with this stream (null if not defined).
look($number=1)
Looks at the next token.
test($primary, $secondary=null)
Tests the current token.
getSourceContext()
Gets the source associated with this stream.
Represents a token stream.
Definition: TokenStream.php:20
Exception thrown when a syntax error occurs during lexing or parsing of a template.
Definition: Syntax.php:18
expect($type, $value=null, $message=null)
Tests a token and returns it or throws a syntax error.
Definition: TokenStream.php:91
nextIf($primary, $secondary=null)
Tests a token, sets the pointer to the next one and returns it or throws a syntax error...
Definition: TokenStream.php:79
isEOF()
Checks if end of stream was reached.
next()
Sets the pointer to the next token and returns the old one.
Definition: TokenStream.php:65
if($format !==null) $name
Definition: metadata.php:146
catch(Exception $e) $message
injectTokens(array $tokens)
Definition: TokenStream.php:55
Create styles array
The data for the language used.
const EOF_TYPE
Definition: Token.php:26
static typeToEnglish($type)
Returns the English representation of a given type.
Definition: Token.php:172
Holds information about a non-compiled Twig template.
Definition: Source.php:19