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 875b858 commit 663e62f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
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
45 changes: 45 additions & 0 deletions tests/snippets/php/variable_variable.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---input---
<?php
${'whatever'} = '';
${$whatever} = '';
${${'whatever' . 'whatever'}} = '';

---tokens---
'<?php' Comment.Preproc
'\n' Text

'${' Name.Variable
"'whatever'" Literal.String.Single
'}' Name.Variable
' ' Text
'=' Operator
' ' Text
"''" Literal.String.Single
';' Punctuation
'\n' Text

'${' Name.Variable
'$whatever' Name.Variable
'}' Name.Variable
' ' Text
'=' Operator
' ' Text
"''" Literal.String.Single
';' Punctuation
'\n' Text

'${' Name.Variable
'${' Name.Variable
"'whatever'" Literal.String.Single
' ' Text
'.' Operator
' ' Text
"'whatever'" Literal.String.Single
'}' Name.Variable
'}' Name.Variable
' ' Text
'=' Operator
' ' Text
"''" Literal.String.Single
';' Punctuation
'\n' Text

0 comments on commit 663e62f

Please sign in to comment.