/** * 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); }
/** * Tests a token and returns it or throws a syntax error. * * @return IfwPsn_Vendor_Twig_Token */ public function expect($type, $value = null, $message = null) { $token = $this->tokens[$this->current]; if (!$token->test($type, $value)) { $line = $token->getLine(); throw new IfwPsn_Vendor_Twig_Error_Syntax(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s)', $message ? $message . '. ' : '', IfwPsn_Vendor_Twig_Token::typeToEnglish($token->getType()), $token->getValue(), IfwPsn_Vendor_Twig_Token::typeToEnglish($type), $value ? sprintf(' with value "%s"', $value) : ''), $line, $this->filename); } $this->next(); return $token; }
/** * 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; }
public function decideBlockEnd(IfwPsn_Vendor_Twig_Token $token) { return $token->test('endmacro'); }
/** * 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()); }
public function decideIfEnd(IfwPsn_Vendor_Twig_Token $token) { return $token->test(array('endif')); }
/** * 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()); }
public function decideForEnd(IfwPsn_Vendor_Twig_Token $token) { return $token->test('endfor'); }
/** * 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()); }
public function decideSpacelessEnd(IfwPsn_Vendor_Twig_Token $token) { return $token->test('endspaceless'); }
public function parseHashExpression() { $stream = $this->parser->getStream(); $stream->expect(IfwPsn_Vendor_Twig_Token::PUNCTUATION_TYPE, '{', 'A hash element was expected'); $node = new IfwPsn_Vendor_Twig_Node_Expression_Array(array(), $stream->getCurrent()->getLine()); $first = true; while (!$stream->test(IfwPsn_Vendor_Twig_Token::PUNCTUATION_TYPE, '}')) { if (!$first) { $stream->expect(IfwPsn_Vendor_Twig_Token::PUNCTUATION_TYPE, ',', 'A hash value must be followed by a comma'); // trailing ,? if ($stream->test(IfwPsn_Vendor_Twig_Token::PUNCTUATION_TYPE, '}')) { break; } } $first = false; // a hash key can be: // // * a number -- 12 // * a string -- 'a' // * a name, which is equivalent to a string -- a // * an expression, which must be enclosed in parentheses -- (1 + 2) if (($token = $stream->nextIf(IfwPsn_Vendor_Twig_Token::STRING_TYPE)) || ($token = $stream->nextIf(IfwPsn_Vendor_Twig_Token::NAME_TYPE)) || ($token = $stream->nextIf(IfwPsn_Vendor_Twig_Token::NUMBER_TYPE))) { $key = new IfwPsn_Vendor_Twig_Node_Expression_Constant($token->getValue(), $token->getLine()); } elseif ($stream->test(IfwPsn_Vendor_Twig_Token::PUNCTUATION_TYPE, '(')) { $key = $this->parseExpression(); } else { $current = $stream->getCurrent(); throw new IfwPsn_Vendor_Twig_Error_Syntax(sprintf('A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "%s" of value "%s"', IfwPsn_Vendor_Twig_Token::typeToEnglish($current->getType()), $current->getValue()), $current->getLine(), $this->parser->getFilename()); } $stream->expect(IfwPsn_Vendor_Twig_Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)'); $value = $this->parseExpression(); $node->addElement($value, $key); } $stream->expect(IfwPsn_Vendor_Twig_Token::PUNCTUATION_TYPE, '}', 'An opened hash is not properly closed'); return $node; }