Skip to content

Commit

Permalink
Improve lexing PHP's variable variable syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanvelzen committed Feb 27, 2023
1 parent f589ac6 commit f861db8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
6 changes: 5 additions & 1 deletion pygments/lexers/php.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class PhpLexer(RegexLexer):
r'finally|match)\b', Keyword),
(r'(true|false|null)\b', Keyword.Constant),
include('magicconstants'),
(r'\$\{\$+' + _ident_inner + r'\}', Name.Variable),
(r'\$\{', Name.Variable, 'variablevariable'),
(r'\$+' + _ident_inner, Name.Variable),
(_ident_inner, Name.Other),
(r'(\d+\.\d*|\d*\.\d+)(e[+-]?[0-9]+)?', Number.Float),
Expand All @@ -237,6 +237,10 @@ class PhpLexer(RegexLexer):
(r'`([^`\\]*(?:\\.[^`\\]*)*)`', String.Backtick),
(r'"', String.Double, 'string'),
],
'variablevariable': [
(r'\}', Name.Variable, '#pop'),
include('php')
],
'magicfuncs': [
# source: http://php.net/manual/en/language.oop5.magic.php
(words((
Expand Down
7 changes: 6 additions & 1 deletion tests/examplefiles/php/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,4 +545,9 @@ public function nonMagic() {
<?php
#[Attribute]
class Extra {}
?>
?>

<?php
${'whatever'} = '';
${$whatever} = '';
${${'whatever' . 'whatever'}} = '';
41 changes: 40 additions & 1 deletion tests/examplefiles/php/test.php.output

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f861db8

Please sign in to comment.