Skip to content

Commit

Permalink
PHP: Improved constant support for PHP 8.1 enums (#2770)
Browse files Browse the repository at this point in the history
This improves the highlighting of PHP 8.1 enums and class constants.
  • Loading branch information
TomPavelec committed Feb 19, 2021
1 parent b0a6ec8 commit 8019e2f
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 16 deletions.
17 changes: 15 additions & 2 deletions components/prism-php.js
Expand Up @@ -12,8 +12,18 @@
pattern: /\b(?:false|true)\b/i,
alias: 'boolean'
},
/\b[A-Z_][A-Z0-9_]*\b(?!\s*\()/,
{
pattern: /(::\s*)\b[a-z_]\w*\b(?!\s*\()/i,
greedy: true,
lookbehind: true,
},
{
pattern: /(\b(?:case|const)\s+)\b[a-z_]\w*(?=\s*[;=])/i,
greedy: true,
lookbehind: true,
},
/\b(?:null)\b/i,
/\b[A-Z_][A-Z0-9_]*\b(?!\s*\()/,
];
var number = /\b0b[01]+(?:_[01]+)*\b|\b0o[0-7]+(?:_[0-7]+)*\b|\b0x[\da-f]+(?:_[\da-f]+)*\b|(?:\b\d+(?:_\d+)*\.?(?:\d+(?:_\d+)*)?|\B\.\d+)(?:e[+-]?\d+)?/i;
var operator = /<?=>|\?\?=?|\.{3}|\??->|[!=]=?=?|::|\*\*=?|--|\+\+|&&|\|\||<<|>>|[?~]|[/^|%*&<>.+-]=?/;
Expand Down Expand Up @@ -87,7 +97,10 @@
},
/\b(?:__halt_compiler|abstract|and|array|as|break|callable|case|catch|class|clone|const|continue|declare|default|die|do|echo|else|elseif|empty|enddeclare|endfor|endforeach|endif|endswitch|endwhile|enum|eval|exit|extends|final|finally|for|foreach|function|global|goto|if|implements|include|include_once|instanceof|insteadof|interface|isset|list|namespace|match|new|or|parent|print|private|protected|public|require|require_once|return|self|static|switch|throw|trait|try|unset|use|var|while|xor|yield)\b/i
],
'argument-name': /\b[a-z_]\w*(?=\s*:(?!:))/i,
'argument-name': {
pattern: /([(,]\s+)\b[a-z_]\w*(?=\s*:(?!:))/i,
lookbehind: true
},
'class-name': [
{
pattern: /(\b(?:extends|implements|instanceof|new(?!\s+self|\s+static))\s+|\bcatch\s*\()\b[a-z_]\w*(?!\\)\b/i,
Expand Down
2 changes: 1 addition & 1 deletion components/prism-php.min.js

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

2 changes: 1 addition & 1 deletion tests/languages/latte/html_feature.test
Expand Up @@ -15,7 +15,7 @@
["tag", [["tag", [["punctuation", "<"], "a"]],
["attr-name", ["href"]], ["attr-value", [["punctuation", "="], ["punctuation", "\""],
["latte", [["ld", [["punctuation", "{"], ["tag", "link"]]],
["php", [["argument-name", "Post"], ["punctuation", ":"], ["class-name", "show"], ["variable", "$post"], ["operator", "->"], ["property", "id"]]],
["php", ["Post", ["punctuation", ":"], ["class-name", "show"], ["variable", "$post"], ["operator", "->"], ["property", "id"]]],
["rd", [["punctuation", "}"]]]]],
["punctuation", "\""]]], ["punctuation", ">"]]],
["latte", [["ld", [["punctuation", "{"]]], ["php", [["variable", "$post"], ["operator", "->"], ["property", "title"]]], ["rd", [["punctuation", "}"]]]]],
Expand Down
4 changes: 2 additions & 2 deletions tests/languages/latte/n-attr_feature.test
Expand Up @@ -9,13 +9,13 @@
[
["tag", [["tag", [["punctuation", "<"], "a"]],
["n-attr", [["attr-name", "n:href"],
["attr-value", [["punctuation", "="], ["punctuation", "\""], ["php", [["argument-name", "Post"], ["punctuation", ":"], ["class-name", "show"], ["variable", "$post"], ["operator", "->"], ["property", "id"]]], ["punctuation", "\""]]]]],
["attr-value", [["punctuation", "="], ["punctuation", "\""], ["php", ["Post", ["punctuation", ":"], ["class-name", "show"], ["variable", "$post"], ["operator", "->"], ["property", "id"]]], ["punctuation", "\""]]]]],
["punctuation", ">"]]],
"link", ["tag", [["tag", [["punctuation", "</"], "a"]], ["punctuation", ">"]]],

["tag", [["tag", [["punctuation", "<"], "a"]],
["n-attr", [["attr-name", "n:href"],
["attr-value", [["punctuation", "="], ["punctuation", "\""], ["php", [["argument-name", "Post"], ["punctuation", ":"], ["class-name", "show"], ["variable", "$post"], ["operator", "->"], ["property", "id"]]], ["punctuation", "\""]]]]],
["attr-value", [["punctuation", "="], ["punctuation", "\""], ["php", ["Post", ["punctuation", ":"], ["class-name", "show"], ["variable", "$post"], ["operator", "->"], ["property", "id"]]], ["punctuation", "\""]]]]],
["punctuation", ">"]]],
"link", ["tag", [["tag", [["punctuation", "</"], "a"]], ["punctuation", ">"]]],

Expand Down
9 changes: 0 additions & 9 deletions tests/languages/php/class-name_feature.test
Expand Up @@ -22,8 +22,6 @@ class Foo extends Bar implements Baz {}

class Foo extends \Package\Bar implements App\Baz {}

enum Foo implements Bar {}

----------------------------------------------------

[
Expand Down Expand Up @@ -196,13 +194,6 @@ enum Foo implements Bar {}
"Baz"
]],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "enum"],
["class-name-definition", "Foo"],
["keyword", "implements"],
["class-name", "Bar"],
["punctuation", "{"],
["punctuation", "}"]
]

Expand Down
54 changes: 53 additions & 1 deletion tests/languages/php/constant_feature.test
Expand Up @@ -5,6 +5,22 @@ AZ
PRISM
FOOBAR_42

class Foo{
const BAR = 1;
const baz = 2;
}

Foo::BAR;
Foo::baz;


switch ($i) {
case NULL:
break;
case X:
break;
}

----------------------------------------------------

[
Expand All @@ -13,7 +29,43 @@ FOOBAR_42
["constant", "X"],
["constant", "AZ"],
["constant", "PRISM"],
["constant", "FOOBAR_42"]
["constant", "FOOBAR_42"],

["keyword", "class"],
["class-name-definition", "Foo"],
["punctuation", "{"],
["keyword", "const"],
["constant", "BAR"],
["operator", "="],
["number", "1"],
["punctuation", ";"],
["keyword", "const"],
["constant", "baz"],
["operator", "="],
["number", "2"],
["punctuation", ";"],
["punctuation", "}"],

["class-name", "Foo"], ["operator", "::"], ["constant", "BAR"], ["punctuation", ";"],
["class-name", "Foo"], ["operator", "::"], ["constant", "baz"], ["punctuation", ";"],

["keyword", "switch"],
["punctuation", "("],
["variable", "$i"],
["punctuation", ")"],
["punctuation", "{"],
["keyword", "case"],
["constant", "NULL"],
["punctuation", ":"],
["keyword", "break"],
["punctuation", ";"],
["keyword", "case"],
["constant", "X"],
["punctuation", ":"],
["keyword", "break"],
["punctuation", ";"],
["punctuation", "}"]

]

----------------------------------------------------
Expand Down
52 changes: 52 additions & 0 deletions tests/languages/php/enum_feature.test
@@ -0,0 +1,52 @@
enum Foo implements Bar {}

enum Suit {
case Hearts;
case Diamonds = 'D';
}

$val = Suit::Diamonds;

Suit::Spades->name;

----------------------------------------------------

[
["keyword", "enum"],
["class-name-definition", "Foo"],
["keyword", "implements"],
["class-name", "Bar"],
["punctuation", "{"],
["punctuation", "}"],

["keyword", "enum"],
["class-name-definition", "Suit"],
["punctuation", "{"],
["keyword", "case"],
["constant", "Hearts"],
["punctuation", ";"],
["keyword", "case"],
["constant", "Diamonds"],
["operator", "="],
["string", "'D'"],
["punctuation", ";"],
["punctuation", "}"],

["variable", "$val"],
["operator", "="],
["class-name", "Suit"],
["operator", "::"],
["constant", "Diamonds"],
["punctuation", ";"],

["class-name", "Suit"],
["operator", "::"],
["constant", "Spades"],
["operator", "->"],
["property", "name"],
["punctuation", ";"]
]

----------------------------------------------------

Checks for enums.

0 comments on commit 8019e2f

Please sign in to comment.