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