Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Nov 29, 2024
1 parent bbdf9ca commit be455bc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
23 changes: 12 additions & 11 deletions src/Token/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,16 @@ private function getBrackets(): array
return array_filter($this->bracketsAndTernary, static fn (Token $token): bool => '?' !== $token->getValue());
}

private function isInTernary(): bool
private function lastBracketMatch(string $value): bool
{
$lastBracket = end($this->bracketsAndTernary);

return false !== $lastBracket && '?' === $lastBracket->getValue();
return false !== $lastBracket && $value === $lastBracket->getValue();
}

private function isInTernary(): bool
{
return $this->lastBracketMatch('?');
}

/**
Expand Down Expand Up @@ -430,11 +435,8 @@ private function lexDqString(): void
*/
private function lexInterpolation(): void
{
$bracket = end($this->bracketsAndTernary);
Assert::notFalse($bracket, 'Interpolation always start with a bracket.');

if (
'#{' === $bracket->getValue()
$this->lastBracketMatch('#{')
&& 1 === preg_match(self::REGEX_INTERPOLATION_END, $this->code, $match, 0, $this->cursor)
) {
$bracket = array_pop($this->bracketsAndTernary);
Expand Down Expand Up @@ -592,8 +594,7 @@ private function lexOperator(string $operator): void
self::STATE_BLOCK !== $this->getState()
|| 'macro' !== $this->getStateParam('blockName')
) {
$bracket = end($this->bracketsAndTernary);
if (false !== $bracket && '(' === $bracket->getValue()) {
if ($this->lastBracketMatch('(')) {
// This is a named argument separator instead
$this->pushToken(Token::NAMED_ARGUMENT_SEPARATOR_TYPE, $operator);

Expand Down Expand Up @@ -643,6 +644,7 @@ private function lexName(string $name): void
} elseif (
self::STATE_BLOCK === $this->getState()
&& 'macro' === $this->getStateParam('blockName')
&& $this->lastBracketMatch('(')
) {
$this->pushToken(Token::MACRO_VAR_NAME_TYPE, $name);
} else {
Expand Down Expand Up @@ -708,14 +710,13 @@ private function lexPunctuation(): void
throw CannotTokenizeException::unexpectedCharacter($currentCode, $this->line);
}

$bracket = end($this->bracketsAndTernary);
if ('[' === $bracket->getValue()) {
if ($this->lastBracketMatch('[')) {
// This is a slice shortcut '[0:1]' instead
$this->lexOperator($currentCode);

return;
}
if ('(' === $bracket->getValue()) {
if ($this->lastBracketMatch('(')) {
// This is a named argument separator instead
$this->pushToken(Token::NAMED_ARGUMENT_SEPARATOR_TYPE, $currentCode);

Expand Down
2 changes: 1 addition & 1 deletion tests/Token/Tokenizer/Fixtures/test15.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
{{ foo(foo:1) }}
{{ foo({foo:1}) }}
{{ (foo==1) }}
{% macro input(name='foo') %}{{ name }}{% endmacro %}
{% macro input(name='foo', opts={foo: 1}) %}{{ name }}{% endmacro %}
30 changes: 20 additions & 10 deletions tests/Token/Tokenizer/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -758,19 +758,29 @@ public static function tokenizeDataProvider(): iterable
63 => Token::STRING_TYPE,
64 => Token::PUNCTUATION_TYPE,
65 => Token::WHITESPACE_TYPE,
66 => Token::BLOCK_END_TYPE,
67 => Token::VAR_START_TYPE,
68 => Token::WHITESPACE_TYPE,
66 => Token::MACRO_VAR_NAME_TYPE,
67 => Token::OPERATOR_TYPE,
68 => Token::PUNCTUATION_TYPE,
69 => Token::NAME_TYPE,
70 => Token::WHITESPACE_TYPE,
71 => Token::VAR_END_TYPE,
72 => Token::BLOCK_START_TYPE,
73 => Token::WHITESPACE_TYPE,
74 => Token::BLOCK_NAME_TYPE,
70 => Token::PUNCTUATION_TYPE,
71 => Token::WHITESPACE_TYPE,
72 => Token::NUMBER_TYPE,
73 => Token::PUNCTUATION_TYPE,
74 => Token::PUNCTUATION_TYPE,
75 => Token::WHITESPACE_TYPE,
76 => Token::BLOCK_END_TYPE,
77 => Token::EOL_TYPE,
78 => Token::EOF_TYPE,
77 => Token::VAR_START_TYPE,
78 => Token::WHITESPACE_TYPE,
79 => Token::NAME_TYPE,
80 => Token::WHITESPACE_TYPE,
81 => Token::VAR_END_TYPE,
82 => Token::BLOCK_START_TYPE,
83 => Token::WHITESPACE_TYPE,
84 => Token::BLOCK_NAME_TYPE,
85 => Token::WHITESPACE_TYPE,
86 => Token::BLOCK_END_TYPE,
87 => Token::EOL_TYPE,
88 => Token::EOF_TYPE,
],
];

Expand Down

0 comments on commit be455bc

Please sign in to comment.