diff --git a/src/Token/Tokenizer.php b/src/Token/Tokenizer.php index 9136bb63..dc91825a 100644 --- a/src/Token/Tokenizer.php +++ b/src/Token/Tokenizer.php @@ -11,7 +11,7 @@ use Webmozart\Assert\Assert; /** - * An override of Twig\Lexer to add whitespace and new line detection. + * An override of Twig\Lexer to add whitespace and new line detection and allow variable names containing dollar signs. */ final class Tokenizer implements TokenizerInterface { @@ -22,7 +22,7 @@ final class Tokenizer implements TokenizerInterface private const STATE_INTERPOLATION = 4; private const STATE_COMMENT = 5; - public const NAME_PATTERN = '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'; + public const NAME_PATTERN = '[a-zA-Z_\x7f-\xff$][a-zA-Z0-9_\x7f-\xff$]*'; public const NUMBER_PATTERN = '[0-9]+(?:\.[0-9]+)?([Ee][+\-][0-9]+)?'; private const SQ_STRING_PATTERN = '[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*'; private const DQ_STRING_PATTERN = '[^#"\\\\]*(?:(?:\\\\.|#(?!\{))[^#"\\\\]*)*'; diff --git a/tests/Token/Tokenizer/Fixtures/test16.twig b/tests/Token/Tokenizer/Fixtures/test16.twig new file mode 100644 index 00000000..185481d9 --- /dev/null +++ b/tests/Token/Tokenizer/Fixtures/test16.twig @@ -0,0 +1,3 @@ +{% set $foo=1 %} +{% set $$foo=2 %} +{% set $$foo$$=3 %} diff --git a/tests/Token/Tokenizer/TokenizerTest.php b/tests/Token/Tokenizer/TokenizerTest.php index 640c595a..aea70ea9 100644 --- a/tests/Token/Tokenizer/TokenizerTest.php +++ b/tests/Token/Tokenizer/TokenizerTest.php @@ -773,6 +773,43 @@ public static function tokenizeDataProvider(): iterable 78 => Token::EOF_TYPE, ], ]; + + yield [ + __DIR__.'/Fixtures/test16.twig', + [ + 0 => Token::BLOCK_START_TYPE, + 1 => Token::WHITESPACE_TYPE, + 2 => Token::BLOCK_NAME_TYPE, + 3 => Token::WHITESPACE_TYPE, + 4 => Token::NAME_TYPE, + 5 => Token::OPERATOR_TYPE, + 6 => Token::NUMBER_TYPE, + 7 => Token::WHITESPACE_TYPE, + 8 => Token::BLOCK_END_TYPE, + 9 => Token::EOL_TYPE, + 10 => Token::BLOCK_START_TYPE, + 11 => Token::WHITESPACE_TYPE, + 12 => Token::BLOCK_NAME_TYPE, + 13 => Token::WHITESPACE_TYPE, + 14 => Token::NAME_TYPE, + 15 => Token::OPERATOR_TYPE, + 16 => Token::NUMBER_TYPE, + 17 => Token::WHITESPACE_TYPE, + 18 => Token::BLOCK_END_TYPE, + 19 => Token::EOL_TYPE, + 20 => Token::BLOCK_START_TYPE, + 21 => Token::WHITESPACE_TYPE, + 22 => Token::BLOCK_NAME_TYPE, + 23 => Token::WHITESPACE_TYPE, + 24 => Token::NAME_TYPE, + 25 => Token::OPERATOR_TYPE, + 26 => Token::NUMBER_TYPE, + 27 => Token::WHITESPACE_TYPE, + 28 => Token::BLOCK_END_TYPE, + 29 => Token::EOL_TYPE, + 30 => Token::EOF_TYPE, + ], + ]; } /**