summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard van Velzen <richard@goreply.com>2023-03-01 00:09:29 +0100
committerGitHub <noreply@github.com>2023-03-01 00:09:29 +0100
commit3cf25c36fce6ea55759c781a517ce95a89a267bb (patch)
tree00597dfe49611612b46f7f93b9aab8bcb70fc921
parent5b96a7f12bff42234054fd3a3f6751c1ac3aed18 (diff)
downloadpygments-git-3cf25c36fce6ea55759c781a517ce95a89a267bb.tar.gz
Improve lexing PHP attributes (#2360)
-rw-r--r--pygments/lexers/php.py12
-rw-r--r--tests/examplefiles/php/test.php5
-rw-r--r--tests/examplefiles/php/test.php.output18
-rw-r--r--tests/snippets/php/attributes.txt38
4 files changed, 49 insertions, 24 deletions
diff --git a/pygments/lexers/php.py b/pygments/lexers/php.py
index 702bdd8d..4ee9bdd1 100644
--- a/pygments/lexers/php.py
+++ b/pygments/lexers/php.py
@@ -192,7 +192,7 @@ class PhpLexer(RegexLexer):
bygroups(String, String, String.Delimiter, String, String.Delimiter,
Punctuation, Text)),
(r'\s+', Text),
- (r'#\[', Punctuation),
+ (r'#\[', Punctuation, 'attribute'),
(r'#.*?\n', Comment.Single),
(r'//.*?\n', Comment.Single),
# put the empty comment here, it is otherwise seen as
@@ -282,6 +282,16 @@ class PhpLexer(RegexLexer):
bygroups(String.Interpol, Name.Variable, String.Interpol)),
(r'[${\\]', String.Double)
],
+ 'attribute': [
+ (r'\]', Punctuation, '#pop'),
+ (r'\(', Punctuation, 'attributeparams'),
+ (_ident_inner, Name.Decorator),
+ include('php')
+ ],
+ 'attributeparams': [
+ (r'\)', Punctuation, '#pop'),
+ include('php')
+ ],
}
def __init__(self, **options):
diff --git a/tests/examplefiles/php/test.php b/tests/examplefiles/php/test.php
index 0076e04b..05a6b573 100644
--- a/tests/examplefiles/php/test.php
+++ b/tests/examplefiles/php/test.php
@@ -540,9 +540,4 @@ more heredoc testing
continues on this line
some_delimiter;
-?>
-
-<?php
-#[Attribute]
-class Extra {}
?> \ No newline at end of file
diff --git a/tests/examplefiles/php/test.php.output b/tests/examplefiles/php/test.php.output
index 8337da7b..7d63ddb3 100644
--- a/tests/examplefiles/php/test.php.output
+++ b/tests/examplefiles/php/test.php.output
@@ -3101,22 +3101,4 @@
'\n' Text
'?>' Comment.Preproc
-'\n\n' Other
-
-'<?php' Comment.Preproc
-'\n' Text
-
-'#[' Punctuation
-'Attribute' Name.Other
-']' Punctuation
-'\n' Text
-
-'class' Keyword
-' ' Text
-'Extra' Name.Class
-' ' Text
-'{}' Punctuation
-'\n' Text
-
-'?>' Comment.Preproc
'\n' Other
diff --git a/tests/snippets/php/attributes.txt b/tests/snippets/php/attributes.txt
new file mode 100644
index 00000000..cd70176d
--- /dev/null
+++ b/tests/snippets/php/attributes.txt
@@ -0,0 +1,38 @@
+---input---
+<?php
+#[Attribute]
+#[Attribute(true)]
+#[Attribute, Attribute]
+class Extra {}
+
+---tokens---
+'<?php' Comment.Preproc
+'\n' Text
+
+'#[' Punctuation
+'Attribute' Name.Decorator
+']' Punctuation
+'\n' Text
+
+'#[' Punctuation
+'Attribute' Name.Decorator
+'(' Punctuation
+'true' Keyword
+')' Punctuation
+']' Punctuation
+'\n' Text
+
+'#[' Punctuation
+'Attribute' Name.Decorator
+',' Punctuation
+' ' Text
+'Attribute' Name.Decorator
+']' Punctuation
+'\n' Text
+
+'class' Keyword
+' ' Text
+'Extra' Name.Class
+' ' Text
+'{}' Punctuation
+'\n' Text