Skip to content

Commit

Permalink
enh(php) support First-class Callable Syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
wkania committed Dec 14, 2021
1 parent f5c885f commit 2eb5d1f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -13,6 +13,7 @@ These changes should be for the better and should not be super noticeable but if

Grammars:

- enh(php) support First-class Callable Syntax (#3427) [Wojciech Kania][]
- enh(php) support class constructor call (#3427) [Wojciech Kania][]
- enh(php) support function invoke (#3427) [Wojciech Kania][]
- enh(php) Switch highlighter to partially case-insensitive (#3427) [Wojciech Kania][]
Expand Down Expand Up @@ -48,6 +49,7 @@ Themes:

- Modified background color in css for Gradient Light and Gradient Dark themes [Samia Ali][]

[Wojciech Kania]: https://github.com/wkania
[Jeylani B]: https://github.com/jeyllani
[Richard Gibson]: https://github.com/gibson042
[Bradley Mackey]: https://github.com/bradleymackey
Expand Down
9 changes: 5 additions & 4 deletions src/languages/php.js
Expand Up @@ -303,11 +303,11 @@ export default function(hljs) {
{
match: [
/new/,
/\s+/,
/ +/,
// to prevent built ins from being confused as the class constructor call
regex.concat("(?!", normalizeKeywords(BUILT_INS).join("\\b|"), "\\b)"),
/\\?\w+/,
/\s*\(/,
/ *\(/,
],
scope: {
1: "keyword",
Expand All @@ -320,7 +320,7 @@ export default function(hljs) {
const FUNCTION_INVOKE = {
relevance: 0,
match: [
/(?:->|::|\s|\(|\\)/,
/\b/,
// to prevent keywords from being confused as the function title
regex.concat("(?!fn\\b|function\\b|", normalizeKeywords(KWS).join("\\b|"), "|", normalizeKeywords(BUILT_INS).join("\\b|"), "\\b)"),
/\w+/,
Expand Down Expand Up @@ -374,7 +374,8 @@ export default function(hljs) {
FUNCTION_INVOKE,
{
// swallow composed identifiers to avoid parsing them as keywords
begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/
begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(?! *\()(?![a-zA-Z0-9_\x7f-\xff])/,
// scope:"wrong"
},
CONSTRUCTOR_CALL,
{
Expand Down
6 changes: 6 additions & 0 deletions test/markup/php/functions.expect.txt
Expand Up @@ -35,3 +35,9 @@ DateTimeImmutable::<span class="hljs-function title_ invoke__">createFromMutable
<span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">\Exception</span>(<span class="hljs-string">&#x27;error&#x27;</span>);
}
}

<span class="hljs-comment">/**
* First-class Callable Syntax
*/</span>
<span class="hljs-variable">$fun</span> = <span class="hljs-function title_ invoke__">mb_strlen</span>();
<span class="hljs-variable">$fun</span>();
6 changes: 6 additions & 0 deletions test/markup/php/functions.txt
Expand Up @@ -35,3 +35,9 @@ function testMe(string|int $name): int
throw new \Exception('error');
}
}

/**
* First-class Callable Syntax
*/
$fun = mb_strlen();
$fun();

0 comments on commit 2eb5d1f

Please sign in to comment.