function Lexer::lexExpression
3 calls to Lexer::lexExpression()
- Lexer::lexBlock in vendor/
twig/ twig/ src/ Lexer.php - Lexer::lexInterpolation in vendor/
twig/ twig/ src/ Lexer.php - Lexer::lexVar in vendor/
twig/ twig/ src/ Lexer.php
File
-
vendor/
twig/ twig/ src/ Lexer.php, line 316
Class
- Lexer
- @author Fabien Potencier <fabien@symfony.com>
Namespace
TwigCode
private function lexExpression() : void {
// whitespace
if (preg_match('/\\s+/A', $this->code, $match, 0, $this->cursor)) {
$this->moveCursor($match[0]);
if ($this->cursor >= $this->end) {
throw new SyntaxError(\sprintf('Unclosed "%s".', self::STATE_BLOCK === $this->state ? 'block' : 'variable'), $this->currentVarBlockLine, $this->source);
}
}
// spread operator
if ('.' === $this->code[$this->cursor] && $this->cursor + 2 < $this->end && '.' === $this->code[$this->cursor + 1] && '.' === $this->code[$this->cursor + 2]) {
$this->pushToken(Token::SPREAD_TYPE, '...');
$this->moveCursor('...');
}
elseif ('=' === $this->code[$this->cursor] && $this->cursor + 1 < $this->end && '>' === $this->code[$this->cursor + 1]) {
$this->pushToken(Token::ARROW_TYPE, '=>');
$this->moveCursor('=>');
}
elseif (preg_match($this->regexes['operator'], $this->code, $match, 0, $this->cursor)) {
$this->pushToken(Token::OPERATOR_TYPE, preg_replace('/\\s+/', ' ', $match[0]));
$this->moveCursor($match[0]);
}
elseif (preg_match(self::REGEX_NAME, $this->code, $match, 0, $this->cursor)) {
$this->pushToken(Token::NAME_TYPE, $match[0]);
$this->moveCursor($match[0]);
}
elseif (preg_match(self::REGEX_NUMBER, $this->code, $match, 0, $this->cursor)) {
$number = (double) $match[0];
// floats
if (ctype_digit($match[0]) && $number <= \PHP_INT_MAX) {
$number = (int) $match[0];
// integers lower than the maximum
}
$this->pushToken(Token::NUMBER_TYPE, $number);
$this->moveCursor($match[0]);
}
elseif (str_contains(self::PUNCTUATION, $this->code[$this->cursor])) {
// opening bracket
if (str_contains('([{', $this->code[$this->cursor])) {
$this->brackets[] = [
$this->code[$this->cursor],
$this->lineno,
];
}
elseif (str_contains(')]}', $this->code[$this->cursor])) {
if (!$this->brackets) {
throw new SyntaxError(\sprintf('Unexpected "%s".', $this->code[$this->cursor]), $this->lineno, $this->source);
}
[
$expect,
$lineno,
] = array_pop($this->brackets);
if ($this->code[$this->cursor] != strtr($expect, '([{', ')]}')) {
throw new SyntaxError(\sprintf('Unclosed "%s".', $expect), $lineno, $this->source);
}
}
$this->pushToken(Token::PUNCTUATION_TYPE, $this->code[$this->cursor]);
++$this->cursor;
}
elseif (preg_match(self::REGEX_STRING, $this->code, $match, 0, $this->cursor)) {
$this->pushToken(Token::STRING_TYPE, $this->stripcslashes(substr($match[0], 1, -1), substr($match[0], 0, 1)));
$this->moveCursor($match[0]);
}
elseif (preg_match(self::REGEX_DQ_STRING_DELIM, $this->code, $match, 0, $this->cursor)) {
$this->brackets[] = [
'"',
$this->lineno,
];
$this->pushState(self::STATE_STRING);
$this->moveCursor($match[0]);
}
elseif (preg_match(self::REGEX_INLINE_COMMENT, $this->code, $match, 0, $this->cursor)) {
$this->moveCursor($match[0]);
}
else {
throw new SyntaxError(\sprintf('Unexpected character "%s".', $this->code[$this->cursor]), $this->lineno, $this->source);
}
}