Skip to content
Prev Previous commit
Next Next commit
fix: !0 changed to true
feature: class name / namespace (package) can not begin with number
feature: added binary and underscore number format
feature: added operators `::`, `->`, `=>`, `<=>`, `...`, and some assignment operators
fix: moved `.` from punctuation to operators (concatenation operator)
feature: for `type` added subtypes `type-casting`, `type-hint`, `return-type`
feature: added backtick string quoted string (shell exec)
feature: for keywords `parent`, `self` and `static` added subtype `static-context` when a static prop/const/method accessed on them
feature: for class name added subtype `class-name-fully-qualified` when class name is fully qualified
feature: for class name added subtype `static-context` when a static prop/const/method accessed on it
  • Loading branch information
TomPavelec committed Oct 12, 2020
commit cd9d38b3b506296063f2371236184df674e3510d
101 changes: 78 additions & 23 deletions components/prism-php.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
lookbehind: true
},
'class-name': {
pattern: /(\b(?:class|interface|extends|implements|trait|instanceof|new\s+(?!self))\s+|\bcatch\s+\()[a-zA-Z_][\w\\]+/i,
pattern: /(\b(?:class|interface|extends|implements|trait|instanceof|new(?!\s+self|\s+static))\s+|\bcatch\s+\()\b[a-zA-Z_][\w]*(?!\\)\b/i,
lookbehind: true,
inside: {
'punctuation': /\\/
}
}
},
'number': /\b0b[01]+\b|\b0x[\da-f]+\b|(?:\b\d+(?:_\d+)*\.?(?:\d+(?:_\d+)*)*|\B\.\d+)(?:e[+-]?\d+)?/i,
'operator': /<?=>|\?\?=?|\.{3}|->|[!=]=?=?|::|\*\*=?|--|\+\+|&&|\|\||[?~]|[/^|%*&<>.+-]=?/,
'punctuation': /[{}\[\](),:;]/
});

Prism.languages.insertBefore('php', 'string', {
Expand All @@ -53,15 +53,29 @@
Prism.languages.insertBefore('php', 'class-name', {
'variable': /\$+(?:\w+\b|(?={))/i,
'package': {
pattern: /(\\|namespace\s+|use\s+(function\s+)*)[a-zA-Z_][\w\\]+/,
pattern: /(namespace\s+|use\s+(function\s+)?)(\\*\b[a-zA-Z_][\w]*)+\b(?!\\)/i,
lookbehind: true,
inside: {
punctuation: /\\/
}
},
'type type-casting': {
pattern: /(?<=\(\s*)\b(?:bool|boolean|int|integer|float|string|object|array(?!\s*\())\b(?=\s*\))/i,
greedy: true
},
'type type-hint': {
pattern: /(?<=[(,?]\s*)\b(?:bool|boolean|int|integer|float|string|object|array(?!\s*\()|mixed|self|static)\b(?=\s*\$)/i,
greedy: true,
lookbehind: true
},
'type return-type': {
pattern: /(\)\s*:\s*\?*\s*)\b(?:bool|boolean|int|integer|float|string|object|void|array(?!\s*\()|mixed|self|static)\b/i,
greedy: true,
lookbehind: true
},
'type': {
pattern: /(bool|boolean|int|integer|float|string|object|void|array|mixed)(?!_)/,
greedy: !0
pattern: /\b(?:bool|boolean|int|integer|float|string|object|void|array(?!\s*\()|mixed)\b/i,
greedy: true
}
});

Expand Down Expand Up @@ -109,6 +123,11 @@
'interpolation': string_interpolation // See below
}
},
'backtick-quoted-string': {
pattern: /`(?:\\[\s\S]|[^\\`])*`/,
greedy: true,
alias: 'string'
},
'single-quoted-string': {
pattern: /'(?:\\[\s\S]|[^\\'])*'/,
greedy: true,
Expand All @@ -126,21 +145,57 @@
// The different types of PHP strings "replace" the C-like standard string
delete Prism.languages.php['string'];

Prism.languages.insertBefore('php', 'class-name', {
'class-static-call': {
pattern: /[a-zA-Z_][\w\\]+(?=\:\:)/,
greedy: !0,
alias: 'class-name'
},
'class-type-hint': {
pattern: /[a-zA-Z_][\w\\]+(?=\s*\$)/,
greedy: !0,
alias: 'class-name'
},
'class-return-type': {
pattern: /(\)\s*\:\s*\?*\s*)[a-zA-Z_][\w\\]+/,
greedy: !0,
alias: 'class-name',
Prism.languages.insertBefore('php', 'keyword', {
'keyword static-context': {
pattern: /\b(parent|self|static)(?=\s*::)/i,
greedy: true
}
});

Prism.languages.insertBefore('php', 'boolean', {
'class-name class-name-fully-qualified': {
pattern: /(\b(?:extends|implements|instanceof|new(?!\s+self|\s+static))\s+|\bcatch\s+\()(\\*\b[a-zA-Z_][\w]*)+\b(?!\\)/i,
greedy: true,
inside: {
punctuation: /\\/
},
lookbehind: true
},
'class-name static-context': {
pattern: /\b[a-zA-Z_][\w]*(?!\\)\b(?=\s*::)/i,
greedy: true,
},
'class-name class-name-fully-qualified static-context': {
pattern: /(\\*\b[a-zA-Z_][\w]*)+\b(?!\\)(?=\s*::)/i,
greedy: true,
inside: {
punctuation: /\\/
}
},
'class-name type-hint': {
pattern: /([(,?]\s*)\b[a-zA-Z_][\w]*(?!\\)\b(?=\s*\$)/i,
greedy: true,
lookbehind: true
},
'class-name class-name-fully-qualified type-hint': {
pattern: /([(,?]\s*)(\\*\b[a-zA-Z_][\w]*)+\b(?!\\)(?=\s*\$)/i,
greedy: true,
inside: {
punctuation: /\\/
},
lookbehind: true
},
'class-name return-type': {
pattern: /(\)\s*:\s*\?*\s*)\b[a-zA-Z_][\w]*(?!\\)\b/i,
greedy: true,
lookbehind: true
},
'class-name class-name-fully-qualified return-type': {
pattern: /(\)\s*:\s*\?*\s*)(\\*\b[a-zA-Z_][\w]*)+\b(?!\\)/i,
greedy: true,
inside: {
punctuation: /\\/
},
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.

8 changes: 4 additions & 4 deletions tests/languages/latte/html_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
["tag", [["tag", [["punctuation", "<"], "a"]],
["attr-name", ["href"]], ["attr-value", [["punctuation", "="], ["punctuation", "\""],
["latte", [["ld", [["punctuation", "{"], ["tag", "link"]]],
["php", ["Post", ["punctuation", ":"], "show ", ["variable", "$post"], ["operator", "-"], ["operator", ">"], ["property", "id"]]],
["php", ["Post", ["punctuation", ":"], "show ", ["variable", "$post"], ["operator", "->"], ["property", "id"]]],
["rd", [["punctuation", "}"]]]]],
["punctuation", "\""]]], ["punctuation", ">"]]],
["latte", [["ld", [["punctuation", "{"]]], ["php", [["variable", "$post"], ["operator", "-"], ["operator", ">"], ["property", "title"]]], ["rd", [["punctuation", "}"]]]]],
["latte", [["ld", [["punctuation", "{"]]], ["php", [["variable", "$post"], ["operator", "->"], ["property", "title"]]], ["rd", [["punctuation", "}"]]]]],
["tag", [["tag", [["punctuation", "</"], "a"]], ["punctuation", ">"]]],

["tag", [["tag", [["punctuation", "<"], "a"]],
["attr-name",
[["latte", [["ld", [["punctuation", "{"], ["tag", "if"]]], ["php", [["variable", "$post"], ["operator", "-"], ["operator", ">"], ["property", "id"]]], ["rd", [["punctuation", "}"]]]]], "title"]
[["latte", [["ld", [["punctuation", "{"], ["tag", "if"]]], ["php", [["variable", "$post"], ["operator", "->"], ["property", "id"]]], ["rd", [["punctuation", "}"]]]]], "title"]
],
["attr-value", [["punctuation", "="], ["punctuation", "\""], "ahoj", ["punctuation", "\""]]],
["attr-name", [["latte", [["ld", [["punctuation", "{/"], ["tag", "if"]]], ["rd", [["punctuation", "}"]]]]]]],
["punctuation", ">"]]],
["latte", [["ld", [["punctuation", "{"]]], ["php", [["variable", "$post"], ["operator", "-"], ["operator", ">"], ["property", "title"]]], ["rd", [["punctuation", "}"]]]]],
["latte", [["ld", [["punctuation", "{"]]], ["php", [["variable", "$post"], ["operator", "->"], ["property", "title"]]], ["rd", [["punctuation", "}"]]]]],
["tag", [["tag", [["punctuation", "</"], "a"]], ["punctuation", ">"]]],

["latte", [["ld", [["punctuation", "{"], ["tag", "tag"]]], ["php", [["variable", "$a"], ["operator", "="], ["boolean", "true"], ["operator", "?"], ["number", "10"], ["operator", "*"], ["number", "5"]]], ["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", ":"], "show ", ["variable", "$post"], ["operator", "-"], ["operator", ">"], ["property", "id"]]], ["punctuation", "\""]]]]],
["attr-value", [["punctuation", "="], ["punctuation", "\""], ["php", ["Post", ["punctuation", ":"], "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", ":"], "show ", ["variable", "$post"], ["operator", "-"], ["operator", ">"], ["property", "id"]]], ["punctuation", "\""]]]]],
["attr-value", [["punctuation", "="], ["punctuation", "\""], ["php", ["Post", ["punctuation", ":"], "show ", ["variable", "$post"], ["operator", "->"], ["property", "id"]]], ["punctuation", "\""]]]]],
["punctuation", ">"]]],
"link", ["tag", [["tag", [["punctuation", "</"], "a"]], ["punctuation", ">"]]],

Expand Down
104 changes: 97 additions & 7 deletions tests/languages/php/class-name_feature.test
Original file line number Diff line number Diff line change
@@ -1,32 +1,122 @@
Foo::bar();
\Foo::bar();
\Package\Foo::bar();

(Foo $variable)
(\Foo $variable)
(\Package\Foo $variable)

): Foo
): \Foo
): \Package\Foo
): ?Foo

class Foo extends Bar implements Baz

class Foo extends \Package\Bar implements App\Baz

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

[
["class-name", ["Foo"]],
["punctuation", ":"],
["punctuation", ":"],
["function", "foo"],
["class-name static-context", "Foo"],
["operator", "::"],
["function", "bar"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],

["class-name class-name-fully-qualified static-context", [
["punctuation", "\\"],
"Foo"
]],
["operator", "::"],
["function", "bar"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],

["class-name class-name-fully-qualified static-context", [
["punctuation", "\\"],
"Package",
["punctuation", "\\"],
"Foo"
]],
["operator", "::"],
["function", "bar"],
["punctuation", "("],
["punctuation", ")"],
["punctuation", ";"],

["punctuation", "("],
["class-name type-hint", "Foo"],
["variable", "$variable"],
["punctuation", ")"],

["punctuation", "("],
["class-name class-name-fully-qualified type-hint", [
["punctuation", "\\"],
"Foo"
]],
["variable", "$variable"],
["punctuation", ")"],

["punctuation", "("],
["class-name", ["Foo"]],
["class-name class-name-fully-qualified type-hint", [
["punctuation", "\\"],
"Package",
["punctuation", "\\"],
"Foo"
]],
["variable", "$variable"],
["punctuation", ")"],

["punctuation", ")"],
["punctuation", ":"],
["class-name", ["Foo"]],
["class-name return-type", "Foo"],

["punctuation", ")"],
["punctuation", ":"],
["class-name class-name-fully-qualified return-type", [
["punctuation", "\\"],
"Foo"
]],

["punctuation", ")"],
["punctuation", ":"],
["class-name class-name-fully-qualified return-type", [
["punctuation", "\\"],
"Package",
["punctuation", "\\"],
"Foo"
]],

["punctuation", ")"],
["punctuation", ":"],
["operator", "?"],
["class-name", ["Foo"]]
["class-name return-type", "Foo"],

["keyword", "class"],
["class-name", "Foo"],
["keyword", "extends"],
["class-name", "Bar"],
["keyword", "implements"],
["class-name", "Baz"],

["keyword", "class"],
["class-name", "Foo"],
["keyword", "extends"],
["class-name class-name-fully-qualified", [
["punctuation", "\\"],
"Package",
["punctuation", "\\"],
"Bar"
]],
["keyword", "implements"],
["class-name class-name-fully-qualified", [
"App",
["punctuation", "\\"],
"Baz"
]]
]

----------------------------------------------------
Expand Down
15 changes: 10 additions & 5 deletions tests/languages/php/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
__halt_compiler
abstract
and
array
array()
as
break
callable
Expand Down Expand Up @@ -48,6 +48,7 @@ namespace;
new;
or
parent
parent::
print
private
protected
Expand All @@ -57,7 +58,9 @@ require_once
return
self
new self
self::
static
static::
switch
throw
trait;
Expand All @@ -75,7 +78,7 @@ yield
["keyword", "__halt_compiler"],
["keyword", "abstract"],
["keyword", "and"],
["keyword", "array"],
["keyword", "array"], ["punctuation", "("], ["punctuation", ")"],
["keyword", "as"],
["keyword", "break"],
["keyword", "callable"],
Expand Down Expand Up @@ -122,6 +125,7 @@ yield
["keyword", "new"], ["punctuation", ";"],
["keyword", "or"],
["keyword", "parent"],
["keyword static-context", "parent"], ["operator", "::"],
["keyword", "print"],
["keyword", "private"],
["keyword", "protected"],
Expand All @@ -130,12 +134,13 @@ yield
["keyword", "require_once"],
["keyword", "return"],
["keyword", "self"],
["keyword", "new"],
["keyword", "self"],
["keyword", "new"], ["keyword", "self"],
["keyword static-context", "self"], ["operator", "::"],
["keyword", "static"],
["keyword static-context", "static"], ["operator", "::"],
["keyword", "switch"],
["keyword", "throw"],
["keyword", "trait"], ["punctuation", ";"],
["keyword", "trait"], ["punctuation", ";"],
["keyword", "try"],
["keyword", "unset"],
["keyword", "use"], ["punctuation", ";"],
Expand Down
Loading