Security update to Drupal 8.4.6
[yaffs-website] / vendor / twig / twig / lib / Twig / TokenStream.php
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
13 /**
14  * Represents a token stream.
15  *
16  * @final
17  *
18  * @author Fabien Potencier <fabien@symfony.com>
19  */
20 class Twig_TokenStream
21 {
22     protected $tokens;
23     protected $current = 0;
24     protected $filename;
25
26     private $source;
27
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
32      */
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
60     /**
61      * Sets the pointer to the next token and returns the old one.
62      *
63      * @return Twig_Token
64      */
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
74     /**
75      * Tests a token, sets the pointer to the next one and returns it or throws a syntax error.
76      *
77      * @return Twig_Token|null The next token if the condition is true, null otherwise
78      */
79     public function nextIf($primary, $secondary = null)
80     {
81         if ($this->tokens[$this->current]->test($primary, $secondary)) {
82             return $this->next();
83         }
84     }
85
86     /**
87      * Tests a token and returns it or throws a syntax error.
88      *
89      * @return Twig_Token
90      */
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
109     /**
110      * Looks at the next token.
111      *
112      * @param int $number
113      *
114      * @return Twig_Token
115      */
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
125     /**
126      * Tests the current token.
127      *
128      * @return bool
129      */
130     public function test($primary, $secondary = null)
131     {
132         return $this->tokens[$this->current]->test($primary, $secondary);
133     }
134
135     /**
136      * Checks if end of stream was reached.
137      *
138      * @return bool
139      */
140     public function isEOF()
141     {
142         return Twig_Token::EOF_TYPE === $this->tokens[$this->current]->getType();
143     }
144
145     /**
146      * @return Twig_Token
147      */
148     public function getCurrent()
149     {
150         return $this->tokens[$this->current];
151     }
152
153     /**
154      * Gets the name associated with this stream (null if not defined).
155      *
156      * @return string|null
157      *
158      * @deprecated since 1.27 (to be removed in 2.0)
159      */
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
167     /**
168      * Gets the source code associated with this stream.
169      *
170      * @return string
171      *
172      * @internal Don't use this as it might be empty depending on the environment configuration
173      *
174      * @deprecated since 1.27 (to be removed in 2.0)
175      */
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
183     /**
184      * Gets the source associated with this stream.
185      *
186      * @return Twig_Source
187      *
188      * @internal
189      */
190     public function getSourceContext()
191     {
192         return $this->source;
193     }
194 }
195
196 class_alias('Twig_TokenStream', 'Twig\TokenStream', false);