function MacroTokenParser::parseDefinition
1 call to MacroTokenParser::parseDefinition()
- MacroTokenParser::parse in vendor/
twig/ twig/ src/ TokenParser/ MacroTokenParser.php - Parses a token and returns a node.
File
-
vendor/
twig/ twig/ src/ TokenParser/ MacroTokenParser.php, line 72
Class
- MacroTokenParser
- Defines a macro.
Namespace
Twig\TokenParserCode
private function parseDefinition() : ArrayExpression {
$arguments = new ArrayExpression([], $this->parser
->getCurrentToken()
->getLine());
$stream = $this->parser
->getStream();
$stream->expect(Token::PUNCTUATION_TYPE, '(', 'A list of arguments must begin with an opening parenthesis');
while (!$stream->test(Token::PUNCTUATION_TYPE, ')')) {
if (count($arguments)) {
$stream->expect(Token::PUNCTUATION_TYPE, ',', 'Arguments must be separated by a comma');
// if the comma above was a trailing comma, early exit the argument parse loop
if ($stream->test(Token::PUNCTUATION_TYPE, ')')) {
break;
}
}
$token = $stream->expect(Token::NAME_TYPE, null, 'An argument must be a name');
$name = new LocalVariable($token->getValue(), $this->parser
->getCurrentToken()
->getLine());
if ($token = $stream->nextIf(Token::OPERATOR_TYPE, '=')) {
$default = $this->parser
->getExpressionParser()
->parseExpression();
}
else {
$default = new ConstantExpression(null, $this->parser
->getCurrentToken()
->getLine());
$default->setAttribute('is_implicit', true);
}
if (!$this->checkConstantExpression($default)) {
throw new SyntaxError('A default value for an argument must be a constant (a boolean, a string, a number, a sequence, or a mapping).', $token->getLine(), $stream->getSourceContext());
}
$arguments->addElement($default, $name);
}
$stream->expect(Token::PUNCTUATION_TYPE, ')', 'A list of arguments must be closed by a parenthesis');
return $arguments;
}