public function Twig_ExpressionParser::parsePrimaryExpression in Zircon Profile 8.0
Same name and namespace in other branches
- 8 vendor/twig/twig/lib/Twig/ExpressionParser.php \Twig_ExpressionParser::parsePrimaryExpression()
2 calls to Twig_ExpressionParser::parsePrimaryExpression()
- Twig_ExpressionParser::getPrimary in vendor/
twig/ twig/ lib/ Twig/ ExpressionParser.php - Twig_ExpressionParser::parseArguments in vendor/
twig/ twig/ lib/ Twig/ ExpressionParser.php - Parses arguments.
File
- vendor/
twig/ twig/ lib/ Twig/ ExpressionParser.php, line 118
Class
- Twig_ExpressionParser
- Parses expressions.
Code
public function parsePrimaryExpression() {
$token = $this->parser
->getCurrentToken();
switch ($token
->getType()) {
case Twig_Token::NAME_TYPE:
$this->parser
->getStream()
->next();
switch ($token
->getValue()) {
case 'true':
case 'TRUE':
$node = new Twig_Node_Expression_Constant(true, $token
->getLine());
break;
case 'false':
case 'FALSE':
$node = new Twig_Node_Expression_Constant(false, $token
->getLine());
break;
case 'none':
case 'NONE':
case 'null':
case 'NULL':
$node = new Twig_Node_Expression_Constant(null, $token
->getLine());
break;
default:
if ('(' === $this->parser
->getCurrentToken()
->getValue()) {
$node = $this
->getFunctionNode($token
->getValue(), $token
->getLine());
}
else {
$node = new Twig_Node_Expression_Name($token
->getValue(), $token
->getLine());
}
}
break;
case Twig_Token::NUMBER_TYPE:
$this->parser
->getStream()
->next();
$node = new Twig_Node_Expression_Constant($token
->getValue(), $token
->getLine());
break;
case Twig_Token::STRING_TYPE:
case Twig_Token::INTERPOLATION_START_TYPE:
$node = $this
->parseStringExpression();
break;
case Twig_Token::OPERATOR_TYPE:
if (preg_match(Twig_Lexer::REGEX_NAME, $token
->getValue(), $matches) && $matches[0] == $token
->getValue()) {
// in this context, string operators are variable names
$this->parser
->getStream()
->next();
$node = new Twig_Node_Expression_Name($token
->getValue(), $token
->getLine());
break;
}
elseif (isset($this->unaryOperators[$token
->getValue()])) {
$class = $this->unaryOperators[$token
->getValue()]['class'];
$ref = new ReflectionClass($class);
$negClass = 'Twig_Node_Expression_Unary_Neg';
$posClass = 'Twig_Node_Expression_Unary_Pos';
if (!(in_array($ref
->getName(), array(
$negClass,
$posClass,
)) || $ref
->isSubclassOf($negClass) || $ref
->isSubclassOf($posClass))) {
throw new Twig_Error_Syntax(sprintf('Unexpected unary operator "%s".', $token
->getValue()), $token
->getLine(), $this->parser
->getFilename());
}
$this->parser
->getStream()
->next();
$expr = $this
->parsePrimaryExpression();
$node = new $class($expr, $token
->getLine());
break;
}
default:
if ($token
->test(Twig_Token::PUNCTUATION_TYPE, '[')) {
$node = $this
->parseArrayExpression();
}
elseif ($token
->test(Twig_Token::PUNCTUATION_TYPE, '{')) {
$node = $this
->parseHashExpression();
}
else {
throw new Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s".', Twig_Token::typeToEnglish($token
->getType()), $token
->getValue()), $token
->getLine(), $this->parser
->getFilename());
}
}
return $this
->parsePostfixExpression($node);
}