/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $macro = $this->parser->getExpressionParser()->parseExpression(); $this->parser->getStream()->expect('as'); $var = new IfwPsn_Vendor_Twig_Node_Expression_AssignName($this->parser->getStream()->expect(IfwPsn_Vendor_Twig_Token::NAME_TYPE)->getValue(), $token->getLine()); $this->parser->getStream()->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $this->parser->addImportedSymbol('template', $var->getAttribute('name')); return new IfwPsn_Vendor_Twig_Node_Import($macro, $var, $token->getLine(), $this->getTag()); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { if (!$this->parser->isMainScope()) { throw new IfwPsn_Vendor_Twig_Error_Syntax('Cannot extend from a block', $token->getLine(), $this->parser->getFilename()); } if (null !== $this->parser->getParent()) { throw new IfwPsn_Vendor_Twig_Error_Syntax('Multiple extends tags are forbidden', $token->getLine(), $this->parser->getFilename()); } $this->parser->setParent($this->parser->getExpressionParser()->parseExpression()); $this->parser->getStream()->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $name = $this->parser->getVarName(); $ref = new IfwPsn_Vendor_Twig_Node_Expression_BlockReference(new IfwPsn_Vendor_Twig_Node_Expression_Constant($name, $token->getLine()), true, $token->getLine(), $this->getTag()); $filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref, $this->getTag()); $this->parser->getStream()->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true); $this->parser->getStream()->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $block = new IfwPsn_Vendor_Twig_Node_Block($name, $body, $token->getLine()); $this->parser->setBlock($name, $block); return new IfwPsn_Vendor_Twig_Node_Print($filter, $token->getLine(), $this->getTag()); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $stream = $this->parser->getStream(); $parent = $this->parser->getExpressionParser()->parseExpression(); list($variables, $only, $ignoreMissing) = $this->parseArguments(); // inject a fake parent to make the parent() function work $stream->injectTokens(array(new IfwPsn_Vendor_Twig_Token(IfwPsn_Vendor_Twig_Token::BLOCK_START_TYPE, '', $token->getLine()), new IfwPsn_Vendor_Twig_Token(IfwPsn_Vendor_Twig_Token::NAME_TYPE, 'extends', $token->getLine()), new IfwPsn_Vendor_Twig_Token(IfwPsn_Vendor_Twig_Token::STRING_TYPE, '__parent__', $token->getLine()), new IfwPsn_Vendor_Twig_Token(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE, '', $token->getLine()))); $module = $this->parser->parse($stream, array($this, 'decideBlockEnd'), true); // override the parent with the correct one $module->setNode('parent', $parent); $this->parser->embedTemplate($module); $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); return new IfwPsn_Vendor_Twig_Node_Embed($module->getAttribute('filename'), $module->getAttribute('index'), $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag()); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $lineno = $token->getLine(); $expr = $this->parser->getExpressionParser()->parseExpression(); $stream = $this->parser->getStream(); $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'decideIfFork')); $tests = array($expr, $body); $else = null; $end = false; while (!$end) { switch ($stream->next()->getValue()) { case 'else': $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $else = $this->parser->subparse(array($this, 'decideIfEnd')); break; case 'elseif': $expr = $this->parser->getExpressionParser()->parseExpression(); $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'decideIfFork')); $tests[] = $expr; $tests[] = $body; break; case 'endif': $end = true; break; default: throw new IfwPsn_Vendor_Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "else", "elseif", or "endif" to close the "if" block started at line %d)', $lineno), $stream->getCurrent()->getLine(), $stream->getFilename()); } } $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); return new IfwPsn_Vendor_Twig_Node_If(new IfwPsn_Vendor_Twig_Node($tests), $else, $lineno, $this->getTag()); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $lineno = $token->getLine(); $stream = $this->parser->getStream(); $name = $stream->expect(IfwPsn_Vendor_Twig_Token::NAME_TYPE)->getValue(); if ($this->parser->hasBlock($name)) { throw new IfwPsn_Vendor_Twig_Error_Syntax(sprintf("The block '{$name}' has already been defined line %d", $this->parser->getBlock($name)->getLine()), $stream->getCurrent()->getLine(), $stream->getFilename()); } $this->parser->setBlock($name, $block = new IfwPsn_Vendor_Twig_Node_Block($name, new IfwPsn_Vendor_Twig_Node(array()), $lineno)); $this->parser->pushLocalScope(); $this->parser->pushBlockStack($name); if ($stream->nextIf(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE)) { $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true); if ($token = $stream->nextIf(IfwPsn_Vendor_Twig_Token::NAME_TYPE)) { $value = $token->getValue(); if ($value != $name) { throw new IfwPsn_Vendor_Twig_Error_Syntax(sprintf("Expected endblock for block '{$name}' (but %s given)", $value), $stream->getCurrent()->getLine(), $stream->getFilename()); } } } else { $body = new IfwPsn_Vendor_Twig_Node(array(new IfwPsn_Vendor_Twig_Node_Print($this->parser->getExpressionParser()->parseExpression(), $lineno))); } $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $block->setNode('body', $body); $this->parser->popBlockStack(); $this->parser->popLocalScope(); return new IfwPsn_Vendor_Twig_Node_BlockReference($name, $lineno, $this->getTag()); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $lineno = $token->getLine(); $stream = $this->parser->getStream(); if ($stream->test(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE)) { $value = 'html'; } else { $expr = $this->parser->getExpressionParser()->parseExpression(); if (!$expr instanceof IfwPsn_Vendor_Twig_Node_Expression_Constant) { throw new IfwPsn_Vendor_Twig_Error_Syntax('An escaping strategy must be a string or a Boolean.', $stream->getCurrent()->getLine(), $stream->getFilename()); } $value = $expr->getAttribute('value'); $compat = true === $value || false === $value; if (true === $value) { $value = 'html'; } if ($compat && $stream->test(IfwPsn_Vendor_Twig_Token::NAME_TYPE)) { if (false === $value) { throw new IfwPsn_Vendor_Twig_Error_Syntax('Unexpected escaping strategy as you set autoescaping to false.', $stream->getCurrent()->getLine(), $stream->getFilename()); } $value = $stream->next()->getValue(); } } $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true); $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); return new IfwPsn_Vendor_Twig_Node_AutoEscape($value, $body, $lineno, $this->getTag()); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $lineno = $token->getLine(); $this->parser->getStream()->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'decideSpacelessEnd'), true); $this->parser->getStream()->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); return new IfwPsn_Vendor_Twig_Node_Spaceless($body, $lineno, $this->getTag()); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $macro = $this->parser->getExpressionParser()->parseExpression(); $stream = $this->parser->getStream(); $stream->expect('import'); $targets = array(); do { $name = $stream->expect(IfwPsn_Vendor_Twig_Token::NAME_TYPE)->getValue(); $alias = $name; if ($stream->nextIf('as')) { $alias = $stream->expect(IfwPsn_Vendor_Twig_Token::NAME_TYPE)->getValue(); } $targets[$name] = $alias; if (!$stream->nextIf(IfwPsn_Vendor_Twig_Token::PUNCTUATION_TYPE, ',')) { break; } } while (true); $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $node = new IfwPsn_Vendor_Twig_Node_Import($macro, new IfwPsn_Vendor_Twig_Node_Expression_AssignName($this->parser->getVarName(), $token->getLine()), $token->getLine(), $this->getTag()); foreach ($targets as $name => $alias) { $this->parser->addImportedSymbol('function', $alias, 'get' . $name, $node->getNode('var')); } return $node; }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $this->parser->getStream()->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true); $this->parser->getStream()->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); // in a sandbox tag, only include tags are allowed if (!$body instanceof IfwPsn_Vendor_Twig_Node_Include) { foreach ($body as $node) { if ($node instanceof IfwPsn_Vendor_Twig_Node_Text && ctype_space($node->getAttribute('data'))) { continue; } if (!$node instanceof IfwPsn_Vendor_Twig_Node_Include) { throw new IfwPsn_Vendor_Twig_Error_Syntax('Only "include" tags are allowed within a "sandbox" section', $node->getLine(), $this->parser->getFilename()); } } } return new IfwPsn_Vendor_Twig_Node_Sandbox($body, $token->getLine(), $this->getTag()); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $lineno = $token->getLine(); $stream = $this->parser->getStream(); $name = $stream->expect(IfwPsn_Vendor_Twig_Token::NAME_TYPE)->getValue(); $arguments = $this->parser->getExpressionParser()->parseArguments(true, true); $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $this->parser->pushLocalScope(); $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true); if ($token = $stream->nextIf(IfwPsn_Vendor_Twig_Token::NAME_TYPE)) { $value = $token->getValue(); if ($value != $name) { throw new IfwPsn_Vendor_Twig_Error_Syntax(sprintf("Expected endmacro for macro '{$name}' (but %s given)", $value), $stream->getCurrent()->getLine(), $stream->getFilename()); } } $this->parser->popLocalScope(); $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $this->parser->setMacro($name, new IfwPsn_Vendor_Twig_Node_Macro($name, new IfwPsn_Vendor_Twig_Node_Body(array($body)), $arguments, $lineno, $this->getTag())); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $lineno = $token->getLine(); $stream = $this->parser->getStream(); $names = $this->parser->getExpressionParser()->parseAssignmentExpression(); $capture = false; if ($stream->nextIf(IfwPsn_Vendor_Twig_Token::OPERATOR_TYPE, '=')) { $values = $this->parser->getExpressionParser()->parseMultitargetExpression(); $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); if (count($names) !== count($values)) { throw new IfwPsn_Vendor_Twig_Error_Syntax("When using set, you must have the same number of variables and assignments.", $stream->getCurrent()->getLine(), $stream->getFilename()); } } else { $capture = true; if (count($names) > 1) { throw new IfwPsn_Vendor_Twig_Error_Syntax("When using set with a block, you cannot have a multi-target.", $stream->getCurrent()->getLine(), $stream->getFilename()); } $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $values = $this->parser->subparse(array($this, 'decideBlockEnd'), true); $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); } return new IfwPsn_Vendor_Twig_Node_Set($capture, $names, $values, $lineno, $this->getTag()); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $lineno = $token->getLine(); $stream = $this->parser->getStream(); $targets = $this->parser->getExpressionParser()->parseAssignmentExpression(); $stream->expect(IfwPsn_Vendor_Twig_Token::OPERATOR_TYPE, 'in'); $seq = $this->parser->getExpressionParser()->parseExpression(); $ifexpr = null; if ($stream->nextIf(IfwPsn_Vendor_Twig_Token::NAME_TYPE, 'if')) { $ifexpr = $this->parser->getExpressionParser()->parseExpression(); } $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'decideForFork')); if ($stream->next()->getValue() == 'else') { $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); $else = $this->parser->subparse(array($this, 'decideForEnd'), true); } else { $else = null; } $stream->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); if (count($targets) > 1) { $keyTarget = $targets->getNode(0); $keyTarget = new IfwPsn_Vendor_Twig_Node_Expression_AssignName($keyTarget->getAttribute('name'), $keyTarget->getLine()); $valueTarget = $targets->getNode(1); $valueTarget = new IfwPsn_Vendor_Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine()); } else { $keyTarget = new IfwPsn_Vendor_Twig_Node_Expression_AssignName('_key', $lineno); $valueTarget = $targets->getNode(0); $valueTarget = new IfwPsn_Vendor_Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine()); } if ($ifexpr) { $this->checkLoopUsageCondition($stream, $ifexpr); $this->checkLoopUsageBody($stream, $body); } return new IfwPsn_Vendor_Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, $lineno, $this->getTag()); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $this->parser->getStream()->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); return new IfwPsn_Vendor_Twig_Node_Flush($token->getLine(), $this->getTag()); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $expr = $this->parser->getExpressionParser()->parseExpression(); list($variables, $only, $ignoreMissing) = $this->parseArguments(); return new IfwPsn_Vendor_Twig_Node_Include($expr, $variables, $only, $ignoreMissing, $token->getLine(), $this->getTag()); }
/** * Parses a token and returns a node. * * @param IfwPsn_Vendor_Twig_Token $token A IfwPsn_Vendor_Twig_Token instance * * @return IfwPsn_Vendor_Twig_NodeInterface A IfwPsn_Vendor_Twig_NodeInterface instance */ public function parse(IfwPsn_Vendor_Twig_Token $token) { $expr = $this->parser->getExpressionParser()->parseExpression(); $this->parser->getStream()->expect(IfwPsn_Vendor_Twig_Token::BLOCK_END_TYPE); return new IfwPsn_Vendor_Twig_Node_Do($expr, $token->getLine(), $this->getTag()); }