From 2eb5d1fd51efef84b6f176ec950386a70d9d0b0a Mon Sep 17 00:00:00 2001 From: Wojciech Kania Date: Tue, 14 Dec 2021 18:26:17 +0100 Subject: [PATCH] enh(php) support First-class Callable Syntax --- CHANGES.md | 2 ++ src/languages/php.js | 9 +++++---- test/markup/php/functions.expect.txt | 6 ++++++ test/markup/php/functions.txt | 6 ++++++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index ed7e898f2a..5174a719c5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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][] @@ -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 diff --git a/src/languages/php.js b/src/languages/php.js index e5339e0eb1..5ddad5b3e4 100644 --- a/src/languages/php.js +++ b/src/languages/php.js @@ -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", @@ -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+/, @@ -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, { diff --git a/test/markup/php/functions.expect.txt b/test/markup/php/functions.expect.txt index 2302fa6297..946c41bd67 100644 --- a/test/markup/php/functions.expect.txt +++ b/test/markup/php/functions.expect.txt @@ -35,3 +35,9 @@ DateTimeImmutable::createFromMutable throw new \Exception('error'); } } + +/** + * First-class Callable Syntax + */ +$fun = mb_strlen(); +$fun(); diff --git a/test/markup/php/functions.txt b/test/markup/php/functions.txt index 077283afeb..86f7fd48a4 100644 --- a/test/markup/php/functions.txt +++ b/test/markup/php/functions.txt @@ -35,3 +35,9 @@ function testMe(string|int $name): int throw new \Exception('error'); } } + +/** + * First-class Callable Syntax + */ +$fun = mb_strlen(); +$fun();