summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard van Velzen <richard@goreply.com>2023-03-01 00:07:24 +0100
committerGitHub <noreply@github.com>2023-03-01 00:07:24 +0100
commit80357823f6252a8b99c05bbd5338cf3be80bd052 (patch)
tree139b5d1ed3b52ce0519ce9cd21ae992c7f31264f
parent875b85895fa5ab2094434796379288cd73ef0274 (diff)
downloadpygments-git-80357823f6252a8b99c05bbd5338cf3be80bd052.tar.gz
Improve lexing PHP's variable variable syntax (#2358)
-rw-r--r--pygments/lexers/php.py6
-rw-r--r--tests/snippets/php/variable_variable.txt45
2 files changed, 50 insertions, 1 deletions
diff --git a/pygments/lexers/php.py b/pygments/lexers/php.py
index c99daa4d..f48fa382 100644
--- a/pygments/lexers/php.py
+++ b/pygments/lexers/php.py
@@ -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),
@@ -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((
diff --git a/tests/snippets/php/variable_variable.txt b/tests/snippets/php/variable_variable.txt
new file mode 100644
index 00000000..6ad748ca
--- /dev/null
+++ b/tests/snippets/php/variable_variable.txt
@@ -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