Skip to content

Commit

Permalink
Allow variables containing dollar signs
Browse files Browse the repository at this point in the history
  • Loading branch information
nussjustin-hmmh committed Sep 12, 2024
1 parent a0e552f commit 7ccf976
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Token/Tokenizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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 = '[^#"\\\\]*(?:(?:\\\\.|#(?!\{))[^#"\\\\]*)*';
Expand Down
3 changes: 3 additions & 0 deletions tests/Token/Tokenizer/Fixtures/test16.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% set $foo=1 %}
{% set $$foo=2 %}
{% set $$foo$$=3 %}
37 changes: 37 additions & 0 deletions tests/Token/Tokenizer/TokenizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];
}

/**
Expand Down

0 comments on commit 7ccf976

Please sign in to comment.