|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of Twig. |
|
|
5 |
* |
|
|
6 |
* (c) 2009 Fabien Potencier |
|
|
7 |
* (c) 2009 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 |
* Default parser implementation. |
|
|
15 |
* |
|
|
16 |
* @package twig |
|
|
17 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
18 |
*/ |
|
|
19 |
class Twig_Parser implements Twig_ParserInterface |
|
|
20 |
{ |
|
|
21 |
protected $stream; |
|
|
22 |
protected $parent; |
|
|
23 |
protected $handlers; |
|
|
24 |
protected $visitors; |
|
|
25 |
protected $expressionParser; |
|
|
26 |
protected $blocks; |
|
|
27 |
protected $blockStack; |
|
|
28 |
protected $macros; |
|
|
29 |
protected $env; |
|
|
30 |
protected $reservedMacroNames; |
|
|
31 |
protected $importedFunctions; |
|
|
32 |
protected $tmpVarCount; |
|
|
33 |
protected $traits; |
|
|
34 |
|
|
|
35 |
/** |
|
|
36 |
* Constructor. |
|
|
37 |
* |
|
|
38 |
* @param Twig_Environment $env A Twig_Environment instance |
|
|
39 |
*/ |
|
|
40 |
public function __construct(Twig_Environment $env) |
|
|
41 |
{ |
|
|
42 |
$this->env = $env; |
|
|
43 |
} |
|
|
44 |
|
|
|
45 |
public function getVarName() |
|
|
46 |
{ |
|
|
47 |
return sprintf('__internal_%s_%d', substr($this->env->getTemplateClass($this->stream->getFilename()), strlen($this->env->getTemplateClassPrefix())), ++$this->tmpVarCount); |
|
|
48 |
} |
|
|
49 |
|
|
|
50 |
/** |
|
|
51 |
* Converts a token stream to a node tree. |
|
|
52 |
* |
|
|
53 |
* @param Twig_TokenStream $stream A token stream instance |
|
|
54 |
* |
|
|
55 |
* @return Twig_Node_Module A node tree |
|
|
56 |
*/ |
|
|
57 |
public function parse(Twig_TokenStream $stream) |
|
|
58 |
{ |
|
|
59 |
$this->tmpVarCount = 0; |
|
|
60 |
|
|
|
61 |
// tag handlers |
|
|
62 |
$this->handlers = $this->env->getTokenParsers(); |
|
|
63 |
$this->handlers->setParser($this); |
|
|
64 |
|
|
|
65 |
// node visitors |
|
|
66 |
$this->visitors = $this->env->getNodeVisitors(); |
|
|
67 |
|
|
|
68 |
if (null === $this->expressionParser) { |
|
|
69 |
$this->expressionParser = new Twig_ExpressionParser($this, $this->env->getUnaryOperators(), $this->env->getBinaryOperators()); |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
$this->stream = $stream; |
|
|
73 |
$this->parent = null; |
|
|
74 |
$this->blocks = array(); |
|
|
75 |
$this->macros = array(); |
|
|
76 |
$this->traits = array(); |
|
|
77 |
$this->blockStack = array(); |
|
|
78 |
$this->importedFunctions = array(array()); |
|
|
79 |
|
|
|
80 |
try { |
|
|
81 |
$body = $this->subparse(null); |
|
|
82 |
|
|
|
83 |
if (null !== $this->parent) { |
|
|
84 |
if (null === $body = $this->filterBodyNodes($body)) { |
|
|
85 |
$body = new Twig_Node(); |
|
|
86 |
} |
|
|
87 |
} |
|
|
88 |
} catch (Twig_Error_Syntax $e) { |
|
|
89 |
if (null === $e->getTemplateFile()) { |
|
|
90 |
$e->setTemplateFile($this->stream->getFilename()); |
|
|
91 |
} |
|
|
92 |
|
|
|
93 |
throw $e; |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
$node = new Twig_Node_Module($body, $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->stream->getFilename()); |
|
|
97 |
|
|
|
98 |
$traverser = new Twig_NodeTraverser($this->env, $this->visitors); |
|
|
99 |
|
|
|
100 |
return $traverser->traverse($node); |
|
|
101 |
} |
|
|
102 |
|
|
|
103 |
public function subparse($test, $dropNeedle = false) |
|
|
104 |
{ |
|
|
105 |
$lineno = $this->getCurrentToken()->getLine(); |
|
|
106 |
$rv = array(); |
|
|
107 |
while (!$this->stream->isEOF()) { |
|
|
108 |
switch ($this->getCurrentToken()->getType()) { |
|
|
109 |
case Twig_Token::TEXT_TYPE: |
|
|
110 |
$token = $this->stream->next(); |
|
|
111 |
$rv[] = new Twig_Node_Text($token->getValue(), $token->getLine()); |
|
|
112 |
break; |
|
|
113 |
|
|
|
114 |
case Twig_Token::VAR_START_TYPE: |
|
|
115 |
$token = $this->stream->next(); |
|
|
116 |
$expr = $this->expressionParser->parseExpression(); |
|
|
117 |
$this->stream->expect(Twig_Token::VAR_END_TYPE); |
|
|
118 |
$rv[] = new Twig_Node_Print($expr, $token->getLine()); |
|
|
119 |
break; |
|
|
120 |
|
|
|
121 |
case Twig_Token::BLOCK_START_TYPE: |
|
|
122 |
$this->stream->next(); |
|
|
123 |
$token = $this->getCurrentToken(); |
|
|
124 |
|
|
|
125 |
if ($token->getType() !== Twig_Token::NAME_TYPE) { |
|
|
126 |
throw new Twig_Error_Syntax('A block must start with a tag name', $token->getLine(), $this->stream->getFilename()); |
|
|
127 |
} |
|
|
128 |
|
|
|
129 |
if (null !== $test && call_user_func($test, $token)) { |
|
|
130 |
if ($dropNeedle) { |
|
|
131 |
$this->stream->next(); |
|
|
132 |
} |
|
|
133 |
|
|
|
134 |
if (1 === count($rv)) { |
|
|
135 |
return $rv[0]; |
|
|
136 |
} |
|
|
137 |
|
|
|
138 |
return new Twig_Node($rv, array(), $lineno); |
|
|
139 |
} |
|
|
140 |
|
|
|
141 |
$subparser = $this->handlers->getTokenParser($token->getValue()); |
|
|
142 |
if (null === $subparser) { |
|
|
143 |
throw new Twig_Error_Syntax(sprintf('Unknown tag name "%s"', $token->getValue()), $token->getLine(), $this->stream->getFilename()); |
|
|
144 |
} |
|
|
145 |
|
|
|
146 |
$this->stream->next(); |
|
|
147 |
|
|
|
148 |
$node = $subparser->parse($token); |
|
|
149 |
if (null !== $node) { |
|
|
150 |
$rv[] = $node; |
|
|
151 |
} |
|
|
152 |
break; |
|
|
153 |
|
|
|
154 |
default: |
|
|
155 |
throw new Twig_Error_Syntax('Lexer or parser ended up in unsupported state.', -1, $this->stream->getFilename()); |
|
|
156 |
} |
|
|
157 |
} |
|
|
158 |
|
|
|
159 |
if (1 === count($rv)) { |
|
|
160 |
return $rv[0]; |
|
|
161 |
} |
|
|
162 |
|
|
|
163 |
return new Twig_Node($rv, array(), $lineno); |
|
|
164 |
} |
|
|
165 |
|
|
|
166 |
public function addHandler($name, $class) |
|
|
167 |
{ |
|
|
168 |
$this->handlers[$name] = $class; |
|
|
169 |
} |
|
|
170 |
|
|
|
171 |
public function addNodeVisitor(Twig_NodeVisitorInterface $visitor) |
|
|
172 |
{ |
|
|
173 |
$this->visitors[] = $visitor; |
|
|
174 |
} |
|
|
175 |
|
|
|
176 |
public function getBlockStack() |
|
|
177 |
{ |
|
|
178 |
return $this->blockStack; |
|
|
179 |
} |
|
|
180 |
|
|
|
181 |
public function peekBlockStack() |
|
|
182 |
{ |
|
|
183 |
return $this->blockStack[count($this->blockStack) - 1]; |
|
|
184 |
} |
|
|
185 |
|
|
|
186 |
public function popBlockStack() |
|
|
187 |
{ |
|
|
188 |
array_pop($this->blockStack); |
|
|
189 |
} |
|
|
190 |
|
|
|
191 |
public function pushBlockStack($name) |
|
|
192 |
{ |
|
|
193 |
$this->blockStack[] = $name; |
|
|
194 |
} |
|
|
195 |
|
|
|
196 |
public function hasBlock($name) |
|
|
197 |
{ |
|
|
198 |
return isset($this->blocks[$name]); |
|
|
199 |
} |
|
|
200 |
|
|
|
201 |
public function setBlock($name, $value) |
|
|
202 |
{ |
|
|
203 |
$this->blocks[$name] = $value; |
|
|
204 |
} |
|
|
205 |
|
|
|
206 |
public function hasMacro($name) |
|
|
207 |
{ |
|
|
208 |
return isset($this->macros[$name]); |
|
|
209 |
} |
|
|
210 |
|
|
|
211 |
public function setMacro($name, Twig_Node_Macro $node) |
|
|
212 |
{ |
|
|
213 |
if (null === $this->reservedMacroNames) { |
|
|
214 |
$this->reservedMacroNames = array(); |
|
|
215 |
$r = new ReflectionClass($this->env->getBaseTemplateClass()); |
|
|
216 |
foreach ($r->getMethods() as $method) { |
|
|
217 |
$this->reservedMacroNames[] = $method->getName(); |
|
|
218 |
} |
|
|
219 |
} |
|
|
220 |
|
|
|
221 |
if (in_array($name, $this->reservedMacroNames)) { |
|
|
222 |
throw new Twig_Error_Syntax(sprintf('"%s" cannot be used as a macro name as it is a reserved keyword', $name), $node->getLine()); |
|
|
223 |
} |
|
|
224 |
|
|
|
225 |
$this->macros[$name] = $node; |
|
|
226 |
} |
|
|
227 |
|
|
|
228 |
public function addTrait($trait) |
|
|
229 |
{ |
|
|
230 |
$this->traits[] = $trait; |
|
|
231 |
} |
|
|
232 |
|
|
|
233 |
public function addImportedFunction($alias, $name, Twig_Node_Expression $node) |
|
|
234 |
{ |
|
|
235 |
$this->importedFunctions[0][$alias] = array('name' => $name, 'node' => $node); |
|
|
236 |
} |
|
|
237 |
|
|
|
238 |
public function getImportedFunction($alias) |
|
|
239 |
{ |
|
|
240 |
foreach ($this->importedFunctions as $functions) { |
|
|
241 |
if (isset($functions[$alias])) { |
|
|
242 |
return $functions[$alias]; |
|
|
243 |
} |
|
|
244 |
} |
|
|
245 |
} |
|
|
246 |
|
|
|
247 |
public function pushLocalScope() |
|
|
248 |
{ |
|
|
249 |
array_unshift($this->importedFunctions, array()); |
|
|
250 |
} |
|
|
251 |
|
|
|
252 |
public function popLocalScope() |
|
|
253 |
{ |
|
|
254 |
array_shift($this->importedFunctions); |
|
|
255 |
} |
|
|
256 |
|
|
|
257 |
/** |
|
|
258 |
* Gets the expression parser. |
|
|
259 |
* |
|
|
260 |
* @return Twig_ExpressionParser The expression parser |
|
|
261 |
*/ |
|
|
262 |
public function getExpressionParser() |
|
|
263 |
{ |
|
|
264 |
return $this->expressionParser; |
|
|
265 |
} |
|
|
266 |
|
|
|
267 |
public function getParent() |
|
|
268 |
{ |
|
|
269 |
return $this->parent; |
|
|
270 |
} |
|
|
271 |
|
|
|
272 |
public function setParent($parent) |
|
|
273 |
{ |
|
|
274 |
$this->parent = $parent; |
|
|
275 |
} |
|
|
276 |
|
|
|
277 |
/** |
|
|
278 |
* Gets the token stream. |
|
|
279 |
* |
|
|
280 |
* @return Twig_TokenStream The token stream |
|
|
281 |
*/ |
|
|
282 |
public function getStream() |
|
|
283 |
{ |
|
|
284 |
return $this->stream; |
|
|
285 |
} |
|
|
286 |
|
|
|
287 |
/** |
|
|
288 |
* Gets the current token. |
|
|
289 |
* |
|
|
290 |
* @return Twig_Token The current token |
|
|
291 |
*/ |
|
|
292 |
public function getCurrentToken() |
|
|
293 |
{ |
|
|
294 |
return $this->stream->getCurrent(); |
|
|
295 |
} |
|
|
296 |
|
|
|
297 |
protected function filterBodyNodes(Twig_NodeInterface $node) |
|
|
298 |
{ |
|
|
299 |
// check that the body does not contain non-empty output nodes |
|
|
300 |
if ( |
|
|
301 |
($node instanceof Twig_Node_Text && !ctype_space($node->getAttribute('data'))) |
|
|
302 |
|| |
|
|
303 |
(!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface) |
|
|
304 |
) { |
|
|
305 |
throw new Twig_Error_Syntax(sprintf('A template that extends another one cannot have a body (%s).', $node), $node->getLine(), $this->stream->getFilename()); |
|
|
306 |
} |
|
|
307 |
|
|
|
308 |
if ($node instanceof Twig_NodeOutputInterface) { |
|
|
309 |
return null; |
|
|
310 |
} |
|
|
311 |
|
|
|
312 |
foreach ($node as $k => $n) { |
|
|
313 |
if (null !== $n && null === $n = $this->filterBodyNodes($n)) { |
|
|
314 |
$node->removeNode($k); |
|
|
315 |
} |
|
|
316 |
} |
|
|
317 |
|
|
|
318 |
return $node; |
|
|
319 |
} |
|
|
320 |
} |