Ejemplo n.º 1
0
 /**
  * Parses a token and returns a node.
  *
  * @param Twig_Token $token A Twig_Token instance
  *
  * @return Twig_NodeInterface A Twig_NodeInterface instance
  */
 public function parse(Twig_Token $token)
 {
     $lineno = $token->getLine();
     $targets = $this->parser->getExpressionParser()->parseAssignmentExpression();
     $this->parser->getStream()->expect(Twig_Token::OPERATOR_TYPE, 'in');
     $seq = $this->parser->getExpressionParser()->parseExpression();
     $ifexpr = null;
     if ($this->parser->getStream()->test(Twig_Token::NAME_TYPE, 'if')) {
         $this->parser->getStream()->next();
         $ifexpr = $this->parser->getExpressionParser()->parseExpression();
     }
     $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
     $body = $this->parser->subparse(array($this, 'decideForFork'));
     if ($this->parser->getStream()->next()->getValue() == 'else') {
         $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
         $else = $this->parser->subparse(array($this, 'decideForEnd'), true);
     } else {
         $else = null;
     }
     $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
     if (count($targets) > 1) {
         $keyTarget = $targets->getNode(0);
         $keyTarget = new Twig_Node_Expression_AssignName($keyTarget->getAttribute('name'), $keyTarget->getLine());
         $valueTarget = $targets->getNode(1);
         $valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine());
     } else {
         $keyTarget = new Twig_Node_Expression_AssignName('_key', $lineno);
         $valueTarget = $targets->getNode(0);
         $valueTarget = new Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine());
     }
     return new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, $lineno, $this->getTag());
 }
Ejemplo n.º 2
0
 /**
  * Parses a token and returns a node.
  *
  * @param Twig_Token $token A Twig_Token instance
  *
  * @return Twig_NodeInterface A Twig_NodeInterface instance
  */
 public function parse(Twig_Token $token)
 {
     $macro = $this->parser->getExpressionParser()->parseExpression();
     $this->parser->getStream()->expect('as');
     $var = new Twig_Node_Expression_AssignName($this->parser->getStream()->expect(Twig_Token::NAME_TYPE)->getValue(), $token->getLine());
     $this->parser->getStream()->expect(Twig_Token::BLOCK_END_TYPE);
     $this->parser->addImportedSymbol('template', $var->getAttribute('name'));
     return new Twig_Node_Import($macro, $var, $token->getLine(), $this->getTag());
 }
Ejemplo n.º 3
0
 public function parse(\Twig_Token $token)
 {
     $lineno = $token->getLine();
     $stream = $this->parser->getStream();
     // key, item in items
     $targets = $this->parser->getExpressionParser()->parseAssignmentExpression();
     $stream->expect(\Twig_Token::OPERATOR_TYPE, 'in');
     $seq = $this->parser->getExpressionParser()->parseExpression();
     // as treeA
     $as = 'default';
     if ($stream->nextIf(\Twig_Token::NAME_TYPE, 'as')) {
         $as = $stream->expect(\Twig_Token::NAME_TYPE)->getValue();
     }
     // %}
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $data = array();
     while (true) {
         // backing up tag content
         $data[] = array('type' => 'body', 'node' => $this->parser->subparse(function (\Twig_Token $token) {
             return $token->test(array('subtree', 'endtree'));
         }));
         // {% subtree
         if ($stream->next()->getValue() == 'subtree') {
             // item
             $child = $this->parser->getExpressionParser()->parseExpression();
             // with treeA
             $with = $as;
             if ($stream->nextIf(\Twig_Token::NAME_TYPE, 'with')) {
                 $with = $stream->expect(\Twig_Token::NAME_TYPE)->getValue();
             }
             // %}
             $stream->expect(\Twig_Token::BLOCK_END_TYPE);
             // backing up subtree details
             $data[] = array('type' => 'subtree', 'with' => $with, 'child' => $child);
             // {% endtree
         } else {
             // %}
             $stream->expect(\Twig_Token::BLOCK_END_TYPE);
             break;
         }
     }
     // key, item
     if (count($targets) > 1) {
         $keyTarget = $targets->getNode(0);
         $keyTarget = new \Twig_Node_Expression_AssignName($keyTarget->getAttribute('name'), $keyTarget->getLine());
         $valueTarget = $targets->getNode(1);
         $valueTarget = new \Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine());
         // (implicit _key,) item
     } else {
         $keyTarget = new \Twig_Node_Expression_AssignName('_key', $lineno);
         $valueTarget = $targets->getNode(0);
         $valueTarget = new \Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine());
     }
     return new TreeNode($keyTarget, $valueTarget, $seq, $as, $data, $lineno, $this->getTag());
 }
Ejemplo n.º 4
0
 /**
  * Parses a token and returns a node.
  *
  * @param \Twig_Token $token
  *
  * @return \Twig_NodeInterface
  */
 public function parse(\Twig_Token $token)
 {
     $lineno = $token->getLine();
     $stream = $this->parser->getStream();
     $targets = $this->parser->getExpressionParser()->parseAssignmentExpression();
     $stream->expect(\Twig_Token::OPERATOR_TYPE, 'in');
     $seq = $this->parser->getExpressionParser()->parseExpression();
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $upperBody = $this->parser->subparse(array($this, 'decideNavFork'));
     $lowerBody = new \Twig_Node();
     $indent = new \Twig_Node();
     $outdent = new \Twig_Node();
     $nextValue = $stream->next()->getValue();
     if ($nextValue != 'endnav') {
         $stream->expect(\Twig_Token::BLOCK_END_TYPE);
         if ($nextValue == 'ifchildren') {
             $indent = $this->parser->subparse(array($this, 'decideChildrenFork'), true);
             $stream->expect(\Twig_Token::BLOCK_END_TYPE);
             $outdent = $this->parser->subparse(array($this, 'decideChildrenEnd'), true);
             $stream->expect(\Twig_Token::BLOCK_END_TYPE);
         }
         $lowerBody = $this->parser->subparse(array($this, 'decideNavEnd'), true);
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     if (count($targets) > 1) {
         $keyTarget = $targets->getNode(0);
         $keyTarget = new \Twig_Node_Expression_AssignName($keyTarget->getAttribute('name'), $keyTarget->getLine());
         $valueTarget = $targets->getNode(1);
         $valueTarget = new \Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine());
     } else {
         $keyTarget = new \Twig_Node_Expression_AssignName('_key', $lineno);
         $valueTarget = $targets->getNode(0);
         $valueTarget = new \Twig_Node_Expression_AssignName($valueTarget->getAttribute('name'), $valueTarget->getLine());
     }
     return new Nav_Node($keyTarget, $valueTarget, $seq, $upperBody, $lowerBody, $indent, $outdent, $lineno, $this->getTag());
 }
Ejemplo n.º 5
0
 public function testConstructor()
 {
     $node = new Twig_Node_Expression_AssignName('foo', 1);
     $this->assertEquals('foo', $node->getAttribute('name'));
 }