| 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 $stack = array();
 | 
  
    | 22 |     protected $stream;
 | 
  
    | 23 |     protected $parent;
 | 
  
    | 24 |     protected $handlers;
 | 
  
    | 25 |     protected $visitors;
 | 
  
    | 26 |     protected $expressionParser;
 | 
  
    | 27 |     protected $blocks;
 | 
  
    | 28 |     protected $blockStack;
 | 
  
    | 29 |     protected $macros;
 | 
  
    | 30 |     protected $env;
 | 
  
    | 31 |     protected $reservedMacroNames;
 | 
  
    | 32 |     protected $importedFunctions;
 | 
  
    | 33 |     protected $tmpVarCount;
 | 
  
    | 34 |     protected $traits;
 | 
  
    | 35 | 
 | 
  
    | 36 |     /**
 | 
  
    | 37 |      * Constructor.
 | 
  
    | 38 |      *
 | 
  
    | 39 |      * @param Twig_Environment $env A Twig_Environment instance
 | 
  
    | 40 |      */
 | 
  
    | 41 |     public function __construct(Twig_Environment $env)
 | 
  
    | 42 |     {
 | 
  
    | 43 |         $this->env = $env;
 | 
  
    | 44 |     }
 | 
  
    | 45 | 
 | 
  
    | 46 |     public function getEnvironment()
 | 
  
    | 47 |     {
 | 
  
    | 48 |         return $this->env;
 | 
  
    | 49 |     }
 | 
  
    | 50 | 
 | 
  
    | 51 |     public function getVarName()
 | 
  
    | 52 |     {
 | 
  
    | 53 |         return sprintf('__internal_%s_%d', substr($this->env->getTemplateClass($this->stream->getFilename()), strlen($this->env->getTemplateClassPrefix())), ++$this->tmpVarCount);
 | 
  
    | 54 |     }
 | 
  
    | 55 | 
 | 
  
    | 56 |     /**
 | 
  
    | 57 |      * Converts a token stream to a node tree.
 | 
  
    | 58 |      *
 | 
  
    | 59 |      * @param  Twig_TokenStream $stream A token stream instance
 | 
  
    | 60 |      *
 | 
  
    | 61 |      * @return Twig_Node_Module A node tree
 | 
  
    | 62 |      */
 | 
  
    | 63 |     public function parse(Twig_TokenStream $stream)
 | 
  
    | 64 |     {
 | 
  
    | 65 |         // push all variables into the stack to keep the current state of the parser
 | 
  
    | 66 |         $vars = get_object_vars($this);
 | 
  
    | 67 |         unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser']);
 | 
  
    | 68 |         $this->stack[] = $vars;
 | 
  
    | 69 | 
 | 
  
    | 70 |         $this->tmpVarCount = 0;
 | 
  
    | 71 | 
 | 
  
    | 72 |         // tag handlers
 | 
  
    | 73 |         if (null === $this->handlers) {
 | 
  
    | 74 |             $this->handlers = $this->env->getTokenParsers();
 | 
  
    | 75 |             $this->handlers->setParser($this);
 | 
  
    | 76 |         }
 | 
  
    | 77 | 
 | 
  
    | 78 |         // node visitors
 | 
  
    | 79 |         if (null === $this->visitors) {
 | 
  
    | 80 |             $this->visitors = $this->env->getNodeVisitors();
 | 
  
    | 81 |         }
 | 
  
    | 82 | 
 | 
  
    | 83 |         if (null === $this->expressionParser) {
 | 
  
    | 84 |             $this->expressionParser = new Twig_ExpressionParser($this, $this->env->getUnaryOperators(), $this->env->getBinaryOperators());
 | 
  
    | 85 |         }
 | 
  
    | 86 | 
 | 
  
    | 87 |         $this->stream = $stream;
 | 
  
    | 88 |         $this->parent = null;
 | 
  
    | 89 |         $this->blocks = array();
 | 
  
    | 90 |         $this->macros = array();
 | 
  
    | 91 |         $this->traits = array();
 | 
  
    | 92 |         $this->blockStack = array();
 | 
  
    | 93 |         $this->importedFunctions = array(array());
 | 
  
    | 94 | 
 | 
  
    | 95 |         try {
 | 
  
    | 96 |             $body = $this->subparse(null);
 | 
  
    | 97 | 
 | 
  
    | 98 |             if (null !== $this->parent) {
 | 
  
    | 99 |                 if (null === $body = $this->filterBodyNodes($body)) {
 | 
  
    | 100 |                     $body = new Twig_Node();
 | 
  
    | 101 |                 }
 | 
  
    | 102 |             }
 | 
  
    | 103 |         } catch (Twig_Error_Syntax $e) {
 | 
  
    | 104 |             if (null === $e->getTemplateFile()) {
 | 
  
    | 105 |                 $e->setTemplateFile($this->stream->getFilename());
 | 
  
    | 106 |             }
 | 
  
    | 107 | 
 | 
  
    | 108 |             throw $e;
 | 
  
    | 109 |         }
 | 
  
    | 110 | 
 | 
  
    | 111 |         $node = new Twig_Node_Module(new Twig_Node_Body(array($body)), $this->parent, new Twig_Node($this->blocks), new Twig_Node($this->macros), new Twig_Node($this->traits), $this->stream->getFilename());
 | 
  
    | 112 | 
 | 
  
    | 113 |         $traverser = new Twig_NodeTraverser($this->env, $this->visitors);
 | 
  
    | 114 | 
 | 
  
    | 115 |         $node = $traverser->traverse($node);
 | 
  
    | 116 | 
 | 
  
    | 117 |         // restore previous stack so previous parse() call can resume working
 | 
  
    | 118 |         foreach (array_pop($this->stack) as $key => $val) {
 | 
  
    | 119 |             $this->$key = $val;
 | 
  
    | 120 |         }
 | 
  
    | 121 | 
 | 
  
    | 122 |         return $node;
 | 
  
    | 123 |     }
 | 
  
    | 124 | 
 | 
  
    | 125 |     public function subparse($test, $dropNeedle = false)
 | 
  
    | 126 |     {
 | 
  
    | 127 |         $lineno = $this->getCurrentToken()->getLine();
 | 
  
    | 128 |         $rv = array();
 | 
  
    | 129 |         while (!$this->stream->isEOF()) {
 | 
  
    | 130 |             switch ($this->getCurrentToken()->getType()) {
 | 
  
    | 131 |                 case Twig_Token::TEXT_TYPE:
 | 
  
    | 132 |                     $token = $this->stream->next();
 | 
  
    | 133 |                     $rv[] = new Twig_Node_Text($token->getValue(), $token->getLine());
 | 
  
    | 134 |                     break;
 | 
  
    | 135 | 
 | 
  
    | 136 |                 case Twig_Token::VAR_START_TYPE:
 | 
  
    | 137 |                     $token = $this->stream->next();
 | 
  
    | 138 |                     $expr = $this->expressionParser->parseExpression();
 | 
  
    | 139 |                     $this->stream->expect(Twig_Token::VAR_END_TYPE);
 | 
  
    | 140 |                     $rv[] = new Twig_Node_Print($expr, $token->getLine());
 | 
  
    | 141 |                     break;
 | 
  
    | 142 | 
 | 
  
    | 143 |                 case Twig_Token::BLOCK_START_TYPE:
 | 
  
    | 144 |                     $this->stream->next();
 | 
  
    | 145 |                     $token = $this->getCurrentToken();
 | 
  
    | 146 | 
 | 
  
    | 147 |                     if ($token->getType() !== Twig_Token::NAME_TYPE) {
 | 
  
    | 148 |                         throw new Twig_Error_Syntax('A block must start with a tag name', $token->getLine(), $this->stream->getFilename());
 | 
  
    | 149 |                     }
 | 
  
    | 150 | 
 | 
  
    | 151 |                     if (null !== $test && call_user_func($test, $token)) {
 | 
  
    | 152 |                         if ($dropNeedle) {
 | 
  
    | 153 |                             $this->stream->next();
 | 
  
    | 154 |                         }
 | 
  
    | 155 | 
 | 
  
    | 156 |                         if (1 === count($rv)) {
 | 
  
    | 157 |                             return $rv[0];
 | 
  
    | 158 |                         }
 | 
  
    | 159 | 
 | 
  
    | 160 |                         return new Twig_Node($rv, array(), $lineno);
 | 
  
    | 161 |                     }
 | 
  
    | 162 | 
 | 
  
    | 163 |                     $subparser = $this->handlers->getTokenParser($token->getValue());
 | 
  
    | 164 |                     if (null === $subparser) {
 | 
  
    | 165 |                         if (null !== $test) {
 | 
  
    | 166 |                             throw new Twig_Error_Syntax(sprintf('Unexpected tag name "%s" (expecting closing tag for the "%s" tag defined near line %s)', $token->getValue(), $test[0]->getTag(), $lineno), $token->getLine(), $this->stream->getFilename());
 | 
  
    | 167 |                         }
 | 
  
    | 168 | 
 | 
  
    | 169 |                         $message = sprintf('Unknown tag name "%s"', $token->getValue());
 | 
  
    | 170 |                         if ($alternatives = $this->env->computeAlternatives($token->getValue(), array_keys($this->env->getTags()))) {
 | 
  
    | 171 |                             $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
 | 
  
    | 172 |                         }
 | 
  
    | 173 | 
 | 
  
    | 174 |                         throw new Twig_Error_Syntax($message, $token->getLine(), $this->stream->getFilename());
 | 
  
    | 175 |                     }
 | 
  
    | 176 | 
 | 
  
    | 177 |                     $this->stream->next();
 | 
  
    | 178 | 
 | 
  
    | 179 |                     $node = $subparser->parse($token);
 | 
  
    | 180 |                     if (null !== $node) {
 | 
  
    | 181 |                         $rv[] = $node;
 | 
  
    | 182 |                     }
 | 
  
    | 183 |                     break;
 | 
  
    | 184 | 
 | 
  
    | 185 |                 default:
 | 
  
    | 186 |                     throw new Twig_Error_Syntax('Lexer or parser ended up in unsupported state.', -1, $this->stream->getFilename());
 | 
  
    | 187 |             }
 | 
  
    | 188 |         }
 | 
  
    | 189 | 
 | 
  
    | 190 |         if (1 === count($rv)) {
 | 
  
    | 191 |             return $rv[0];
 | 
  
    | 192 |         }
 | 
  
    | 193 | 
 | 
  
    | 194 |         return new Twig_Node($rv, array(), $lineno);
 | 
  
    | 195 |     }
 | 
  
    | 196 | 
 | 
  
    | 197 |     public function addHandler($name, $class)
 | 
  
    | 198 |     {
 | 
  
    | 199 |         $this->handlers[$name] = $class;
 | 
  
    | 200 |     }
 | 
  
    | 201 | 
 | 
  
    | 202 |     public function addNodeVisitor(Twig_NodeVisitorInterface $visitor)
 | 
  
    | 203 |     {
 | 
  
    | 204 |         $this->visitors[] = $visitor;
 | 
  
    | 205 |     }
 | 
  
    | 206 | 
 | 
  
    | 207 |     public function getBlockStack()
 | 
  
    | 208 |     {
 | 
  
    | 209 |         return $this->blockStack;
 | 
  
    | 210 |     }
 | 
  
    | 211 | 
 | 
  
    | 212 |     public function peekBlockStack()
 | 
  
    | 213 |     {
 | 
  
    | 214 |         return $this->blockStack[count($this->blockStack) - 1];
 | 
  
    | 215 |     }
 | 
  
    | 216 | 
 | 
  
    | 217 |     public function popBlockStack()
 | 
  
    | 218 |     {
 | 
  
    | 219 |         array_pop($this->blockStack);
 | 
  
    | 220 |     }
 | 
  
    | 221 | 
 | 
  
    | 222 |     public function pushBlockStack($name)
 | 
  
    | 223 |     {
 | 
  
    | 224 |         $this->blockStack[] = $name;
 | 
  
    | 225 |     }
 | 
  
    | 226 | 
 | 
  
    | 227 |     public function hasBlock($name)
 | 
  
    | 228 |     {
 | 
  
    | 229 |         return isset($this->blocks[$name]);
 | 
  
    | 230 |     }
 | 
  
    | 231 | 
 | 
  
    | 232 |     public function getBlock($name)
 | 
  
    | 233 |     {
 | 
  
    | 234 |         return $this->blocks[$name];
 | 
  
    | 235 |     }
 | 
  
    | 236 | 
 | 
  
    | 237 |     public function setBlock($name, $value)
 | 
  
    | 238 |     {
 | 
  
    | 239 |         $this->blocks[$name] = new Twig_Node_Body(array($value), array(), $value->getLine());
 | 
  
    | 240 |     }
 | 
  
    | 241 | 
 | 
  
    | 242 |     public function hasMacro($name)
 | 
  
    | 243 |     {
 | 
  
    | 244 |         return isset($this->macros[$name]);
 | 
  
    | 245 |     }
 | 
  
    | 246 | 
 | 
  
    | 247 |     public function setMacro($name, Twig_Node_Macro $node)
 | 
  
    | 248 |     {
 | 
  
    | 249 |         if (null === $this->reservedMacroNames) {
 | 
  
    | 250 |             $this->reservedMacroNames = array();
 | 
  
    | 251 |             $r = new ReflectionClass($this->env->getBaseTemplateClass());
 | 
  
    | 252 |             foreach ($r->getMethods() as $method) {
 | 
  
    | 253 |                 $this->reservedMacroNames[] = $method->getName();
 | 
  
    | 254 |             }
 | 
  
    | 255 |         }
 | 
  
    | 256 | 
 | 
  
    | 257 |         if (in_array($name, $this->reservedMacroNames)) {
 | 
  
    | 258 |             throw new Twig_Error_Syntax(sprintf('"%s" cannot be used as a macro name as it is a reserved keyword', $name), $node->getLine());
 | 
  
    | 259 |         }
 | 
  
    | 260 | 
 | 
  
    | 261 |         $this->macros[$name] = $node;
 | 
  
    | 262 |     }
 | 
  
    | 263 | 
 | 
  
    | 264 |     public function addTrait($trait)
 | 
  
    | 265 |     {
 | 
  
    | 266 |         $this->traits[] = $trait;
 | 
  
    | 267 |     }
 | 
  
    | 268 | 
 | 
  
    | 269 |     public function hasTraits()
 | 
  
    | 270 |     {
 | 
  
    | 271 |         return count($this->traits) > 0;
 | 
  
    | 272 |     }
 | 
  
    | 273 | 
 | 
  
    | 274 |     public function addImportedFunction($alias, $name, Twig_Node_Expression $node)
 | 
  
    | 275 |     {
 | 
  
    | 276 |         $this->importedFunctions[0][$alias] = array('name' => $name, 'node' => $node);
 | 
  
    | 277 |     }
 | 
  
    | 278 | 
 | 
  
    | 279 |     public function getImportedFunction($alias)
 | 
  
    | 280 |     {
 | 
  
    | 281 |         foreach ($this->importedFunctions as $functions) {
 | 
  
    | 282 |             if (isset($functions[$alias])) {
 | 
  
    | 283 |                 return $functions[$alias];
 | 
  
    | 284 |             }
 | 
  
    | 285 |         }
 | 
  
    | 286 |     }
 | 
  
    | 287 | 
 | 
  
    | 288 |     public function isMainScope()
 | 
  
    | 289 |     {
 | 
  
    | 290 |         return 1 === count($this->importedFunctions);
 | 
  
    | 291 |     }
 | 
  
    | 292 | 
 | 
  
    | 293 |     public function pushLocalScope()
 | 
  
    | 294 |     {
 | 
  
    | 295 |         array_unshift($this->importedFunctions, array());
 | 
  
    | 296 |     }
 | 
  
    | 297 | 
 | 
  
    | 298 |     public function popLocalScope()
 | 
  
    | 299 |     {
 | 
  
    | 300 |         array_shift($this->importedFunctions);
 | 
  
    | 301 |     }
 | 
  
    | 302 | 
 | 
  
    | 303 |     /**
 | 
  
    | 304 |      * Gets the expression parser.
 | 
  
    | 305 |      *
 | 
  
    | 306 |      * @return Twig_ExpressionParser The expression parser
 | 
  
    | 307 |      */
 | 
  
    | 308 |     public function getExpressionParser()
 | 
  
    | 309 |     {
 | 
  
    | 310 |         return $this->expressionParser;
 | 
  
    | 311 |     }
 | 
  
    | 312 | 
 | 
  
    | 313 |     public function getParent()
 | 
  
    | 314 |     {
 | 
  
    | 315 |         return $this->parent;
 | 
  
    | 316 |     }
 | 
  
    | 317 | 
 | 
  
    | 318 |     public function setParent($parent)
 | 
  
    | 319 |     {
 | 
  
    | 320 |         $this->parent = $parent;
 | 
  
    | 321 |     }
 | 
  
    | 322 | 
 | 
  
    | 323 |     /**
 | 
  
    | 324 |      * Gets the token stream.
 | 
  
    | 325 |      *
 | 
  
    | 326 |      * @return Twig_TokenStream The token stream
 | 
  
    | 327 |      */
 | 
  
    | 328 |     public function getStream()
 | 
  
    | 329 |     {
 | 
  
    | 330 |         return $this->stream;
 | 
  
    | 331 |     }
 | 
  
    | 332 | 
 | 
  
    | 333 |     /**
 | 
  
    | 334 |      * Gets the current token.
 | 
  
    | 335 |      *
 | 
  
    | 336 |      * @return Twig_Token The current token
 | 
  
    | 337 |      */
 | 
  
    | 338 |     public function getCurrentToken()
 | 
  
    | 339 |     {
 | 
  
    | 340 |         return $this->stream->getCurrent();
 | 
  
    | 341 |     }
 | 
  
    | 342 | 
 | 
  
    | 343 |     protected function filterBodyNodes(Twig_NodeInterface $node)
 | 
  
    | 344 |     {
 | 
  
    | 345 |         // check that the body does not contain non-empty output nodes
 | 
  
    | 346 |         if (
 | 
  
    | 347 |             ($node instanceof Twig_Node_Text && !ctype_space($node->getAttribute('data')))
 | 
  
    | 348 |             ||
 | 
  
    | 349 |             (!$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface)
 | 
  
    | 350 |         ) {
 | 
  
    | 351 |             if (false !== strpos((string) $node, chr(0xEF).chr(0xBB).chr(0xBF))) {
 | 
  
    | 352 |                 throw new Twig_Error_Syntax('A template that extends another one cannot have a body but a byte order mark (BOM) has been detected; it must be removed.', $node->getLine(), $this->stream->getFilename());
 | 
  
    | 353 |             }
 | 
  
    | 354 | 
 | 
  
    | 355 |             throw new Twig_Error_Syntax('A template that extends another one cannot have a body.', $node->getLine(), $this->stream->getFilename());
 | 
  
    | 356 |         }
 | 
  
    | 357 | 
 | 
  
    | 358 |         // bypass "set" nodes as they "capture" the output
 | 
  
    | 359 |         if ($node instanceof Twig_Node_Set) {
 | 
  
    | 360 |             return $node;
 | 
  
    | 361 |         }
 | 
  
    | 362 | 
 | 
  
    | 363 |         if ($node instanceof Twig_NodeOutputInterface) {
 | 
  
    | 364 |             return;
 | 
  
    | 365 |         }
 | 
  
    | 366 | 
 | 
  
    | 367 |         foreach ($node as $k => $n) {
 | 
  
    | 368 |             if (null !== $n && null === $n = $this->filterBodyNodes($n)) {
 | 
  
    | 369 |                 $node->removeNode($k);
 | 
  
    | 370 |             }
 | 
  
    | 371 |         }
 | 
  
    | 372 | 
 | 
  
    | 373 |         return $node;
 | 
  
    | 374 |     }
 | 
  
    | 375 | }
 |