Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PHP 8.0 features #2591

Merged
merged 10 commits into from
Oct 16, 2020
63 changes: 57 additions & 6 deletions components/prism-php.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,48 @@
lookbehind: true
},
{
pattern: /([(,?]\s*)\b(?:bool|boolean|int|integer|float|string|object|array(?!\s*\()|mixed|self|static|callable|iterable)\b(?=\s*\$)/i,
pattern: /([(,?]\s*)\b(?:bool|int|float|string|object|array(?!\s*\()|mixed|self|static|callable|iterable|(?:null|false)(?=\s*\|))\b(?=\s*\$)/i,
alias: 'type-hint',
greedy: true,
lookbehind: true
},
{
pattern: /(\)\s*:\s*\?*\s*)\b(?:bool|boolean|int|integer|float|string|object|void|array(?!\s*\()|mixed|self|static|callable|iterable)\b/i,
pattern: /([(,?]\s*[a-z0-9_|]\|\s*)(?:null|false)\b(?=\s*\$)/i,
alias: 'type-hint',
greedy: true,
lookbehind: true
},
{
pattern: /(\)\s*:\s*\??\s*)\b(?:bool|int|float|string|object|void|array(?!\s*\()|mixed|self|static|callable|iterable|(?:null|false)(?=\s*\|))\b/i,
alias: 'return-type',
greedy: true,
lookbehind: true
},
{
pattern: /\b(?:bool|boolean|int|integer|float|string|object|void|array(?!\s*\()|mixed|iterable)\b/i,
pattern: /(\)\s*:\s*\??\s*[a-z0-9_|]\|\s*)(?:null|false)\b/i,
alias: 'return-type',
greedy: true,
lookbehind: true
},
{
pattern: /\b(?:bool|int|float|string|object|void|array(?!\s*\()|mixed|iterable|(?:null|false)(?=\s*\|))\b/i,
alias: 'type-declaration',
greedy: true
},
{
pattern: /(\|\s*)(?:null|false)\b/i,
alias: 'type-declaration',
greedy: true,
lookbehind: true
},
{
pattern: /\b(?:parent|self|static)(?=\s*::)/i,
alias: 'static-context',
greedy: true
},
/\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|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
],
'named-argument': /\b[a-z_]\w*(?=\s*:(?!:))/i,
'class-name': [
{
pattern: /(\b(?:class|interface|extends|implements|trait|instanceof|new(?!\s+self|\s+static))\s+|\bcatch\s+\()\b[a-z_]\w*(?!\\)\b/i,
Expand Down Expand Up @@ -110,13 +129,13 @@
}
},
{
pattern: /(\)\s*:\s*\?*\s*)\b[a-z_]\w*(?!\\)\b/i,
pattern: /(\)\s*:\s*\??\s*)\b[a-z_]\w*(?!\\)\b/i,
alias: 'return-type',
greedy: true,
lookbehind: true
},
{
pattern: /(\)\s*:\s*\?*\s*)(?:\\?\b[a-z_]\w*)+\b(?!\\)/i,
pattern: /(\)\s*:\s*\??\s*)(?:\\?\b[a-z_]\w*)+\b(?!\\)/i,
alias: ['class-name-fully-qualified', 'return-type'],
greedy: true,
lookbehind: true,
Expand All @@ -139,10 +158,42 @@
lookbehind: true
},
'number': /\b0b[01]+\b|\b0x[\da-f]+\b|(?:\b\d+(?:_\d+)*\.?(?:\d+(?:_\d+)*)*|\B\.\d+)(?:e[+-]?\d+)?/i,
'operator': /<?=>|\?\?=?|\.{3}|->|[!=]=?=?|::|\*\*=?|--|\+\+|&&|\|\||[?~]|[/^|%*&<>.+-]=?/,
'operator': /<?=>|\?\?=?|\.{3}|\??->|[!=]=?=?|::|\*\*=?|--|\+\+|&&|\|\||<<|>>|[?~]|[/^|%*&<>.+-]=?/,
'punctuation': /[{}\[\](),:;]/
};

Prism.languages.insertBefore('php', 'comment', {
'attribute': {
pattern: /#\[(?:(?!#\[)(?:\\[\s\S]|[^\\\]]))*]/,
RunDevelopment marked this conversation as resolved.
Show resolved Hide resolved
greedy: true,
inside: {
'interpolation': {
TomPavelec marked this conversation as resolved.
Show resolved Hide resolved
pattern: /\[(?:(?!#\[)(?:\\[\s\S]|[^\\\]]))*]/,
inside: {
rest: Prism.languages.php,
TomPavelec marked this conversation as resolved.
Show resolved Hide resolved
'attribute-class-name': [
{
pattern: /([\[)\s]\s*)\b[a-z_]\w*(?!\\)\b/i,
alias: 'class-name',
greedy: true,
lookbehind: true
},
{
pattern: /([\[)\s]\s*)(?:\\?\b[a-z_]\w*)+/i,
alias: 'class-name-fully-qualified',
greedy: true,
lookbehind: true,
inside: {
'punctuation': /\\/
}
}
]
}
}
}
},
});

var string_interpolation = {
pattern: /{\$(?:{(?:{[^{}]+}|[^{}]+)}|[^{}])+}|(^|[^\\{])\$+(?:\w+(?:\[[^\r\n\[\]]+\]|->\w+)*)/,
lookbehind: true,
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
["tag", [["tag", [["punctuation", "<"], "a"]],
["attr-name", ["href"]], ["attr-value", [["punctuation", "="], ["punctuation", "\""],
["latte", [["ld", [["punctuation", "{"], ["tag", "link"]]],
["php", ["Post", ["punctuation", ":"], ["class-name", "show"], ["variable", "$post"], ["operator", "->"], ["property", "id"]]],
["php", [["named-argument", "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
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
[
["tag", [["tag", [["punctuation", "<"], "a"]],
["n-attr", [["attr-name", "n:href"],
["attr-value", [["punctuation", "="], ["punctuation", "\""], ["php", ["Post", ["punctuation", ":"], ["class-name", "show"], ["variable", "$post"], ["operator", "->"], ["property", "id"]]], ["punctuation", "\""]]]]],
["attr-value", [["punctuation", "="], ["punctuation", "\""], ["php", [["named-argument", "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", ["Post", ["punctuation", ":"], ["class-name", "show"], ["variable", "$post"], ["operator", "->"], ["property", "id"]]], ["punctuation", "\""]]]]],
["attr-value", [["punctuation", "="], ["punctuation", "\""], ["php", [["named-argument", "Post"], ["punctuation", ":"], ["class-name", "show"], ["variable", "$post"], ["operator", "->"], ["property", "id"]]], ["punctuation", "\""]]]]],
["punctuation", ">"]]],
"link", ["tag", [["tag", [["punctuation", "</"], "a"]], ["punctuation", ">"]]],

Expand Down
113 changes: 113 additions & 0 deletions tests/languages/php/attribute_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#[comment
#[]
#[Foo]
#[Foo\Bar\Baz]
#[Route(Http::POST, '/products/create', 1)]
#[
Http\Route(Http::POST, '/products/create', 1),
Foo\Bar\Baz,
Attribute,
AttributeFoo('value'),
AttributeBaz
]

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

[
["comment", "#[comment"],

["attribute", [
"#",
["interpolation", [
["punctuation", "["],
["punctuation", "]"]
]]
]],

["attribute", [
"#",
["interpolation", [
["punctuation", "["],
["attribute-class-name", "Foo"],
["punctuation", "]"]
]]
]],

["attribute", [
"#",
["interpolation", [
["punctuation", "["],
["attribute-class-name", [
"Foo",
["punctuation", "\\"],
"Bar",
["punctuation", "\\"],
"Baz"
]],
["punctuation", "]"]
]]
]],

["attribute", [
"#",
["interpolation", [
["punctuation", "["],
["attribute-class-name", "Route"],
["punctuation", "("],
["class-name", "Http"],
["operator", "::"],
["constant", "POST"],
["punctuation", ","],
["string", "'/products/create'"],
["punctuation", ","],
["number", "1"],
["punctuation", ")"],
["punctuation", "]"]
]]
]],

["attribute", [
"#",
["interpolation", [
["punctuation", "["],
["attribute-class-name", [
"Http",
["punctuation", "\\"],
"Route"
]],
["punctuation", "("],
["class-name", "Http"],
["operator", "::"],
["constant", "POST"],
["punctuation", ","],
["string", "'/products/create'"],
["punctuation", ","],
["number", "1"],
["punctuation", ")"],
["punctuation", ","],
["attribute-class-name", [
"Foo",
["punctuation", "\\"],
"Bar",
["punctuation", "\\"],
"Baz"
]],
["punctuation", ","],
["attribute-class-name", "Attribute"],
["punctuation", ","],
["attribute-class-name", "AttributeFoo"],
["punctuation", "("],
["string", "'value'"],
["punctuation", ")"],
["punctuation", ","],
["attribute-class-name", "AttributeBaz"],
["punctuation", "]"]
]]
]]
]



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

Checks for attributes.
24 changes: 24 additions & 0 deletions tests/languages/php/named-argument_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
foo(
a: 'bar',
qux: 'baz'
);

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

[
["function", "foo"],
["punctuation", "("],
["named-argument", "a"],
["punctuation", ":"],
["string", "'bar'"],
["punctuation", ","],
["named-argument", "qux"],
["punctuation", ":"],
["string", "'baz'"],
["punctuation", ")"],
["punctuation", ";"]
]

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

Checks for named arguments.
6 changes: 6 additions & 0 deletions tests/languages/php/operators_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
??
??=
->
?->
::
...
/
Expand Down Expand Up @@ -32,6 +33,8 @@
+=
-
--
>>
<<
-=
?
~
Expand All @@ -48,6 +51,7 @@
["operator", "??"],
["operator", "??="],
["operator", "->"],
["operator", "?->"],
["operator", "::"],
["operator", "..."],
["operator", "/"],
Expand Down Expand Up @@ -77,6 +81,8 @@
["operator", "+="],
["operator", "-"],
["operator", "--"],
["operator", ">>"],
["operator", "<<"],
["operator", "-="],
["operator", "?"],
["operator", "~"],
Expand Down
Loading