From e7188d77b30a268d7f94c2a08a2cf9e013579161 Mon Sep 17 00:00:00 2001 From: wkania Date: Sun, 6 Oct 2024 21:13:32 +0200 Subject: [PATCH] enh(php) add support for Anonymous functions (closures) --- CHANGES.md | 2 + src/languages/php.js | 69 +++++++++++++++------------- test/markup/php/functions.expect.txt | 10 ++++ test/markup/php/functions.txt | 10 ++++ 4 files changed, 59 insertions(+), 32 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index cae070ee15..f026f44c75 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -22,6 +22,7 @@ Core Grammars: - fix(ruby) - fix `|=` operator false positives (as block arguments) [Aboobacker MK] - fix(sql) - Fixed sql primary key and foreign key spacing issue [Dxuian] - fix(cpp) added flat_set and flat_map as a part of cpp 23 version [Lavan] +- enh(php) add support for Anonymous functions (closures) [Wojciech Kania][] New Grammars: @@ -55,6 +56,7 @@ CONTRIBUTORS [Osmocom]: https://github.com/osmocom [Álvaro Mondéjar]: https://github.com/mondeja [Lavan]: https://github.com/jvlavan +[Wojciech Kania]: https://github.com/wkania ## Version 11.10.0 diff --git a/src/languages/php.js b/src/languages/php.js index 13fde3abb5..d58e2ad73b 100644 --- a/src/languages/php.js +++ b/src/languages/php.js @@ -401,6 +401,40 @@ export default function(hljs) { scope: 'attr', match: regex.concat(IDENT_RE, regex.lookahead(':'), regex.lookahead(/(?!::)/)), }; + + const FUNCTION_DECLARATION = { + scope: 'function', + relevance: 0, + beginKeywords: 'fn function', + end: /[;{]/, + excludeEnd: true, + illegal: '[$%\\[]', + contains: [ + { beginKeywords: 'use', }, + hljs.UNDERSCORE_TITLE_MODE, + { + begin: '=>', // No markup, just a relevance booster + endsParent: true + }, + { + scope: 'params', + begin: '\\(', + end: '\\)', + excludeBegin: true, + excludeEnd: true, + keywords: KEYWORDS, + contains: [ + 'self', + VARIABLE, + LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON, + hljs.C_BLOCK_COMMENT_MODE, + STRING, + NUMBER + ] + }, + ] + }; + const PARAMS_MODE = { relevance: 0, begin: /\(/, @@ -414,8 +448,10 @@ export default function(hljs) { STRING, NUMBER, CONSTRUCTOR_CALL, + FUNCTION_DECLARATION, ], }; + const FUNCTION_INVOKE = { relevance: 0, match: [ @@ -528,38 +564,7 @@ export default function(hljs) { }, }, CONSTRUCTOR_CALL, - { - scope: 'function', - relevance: 0, - beginKeywords: 'fn function', - end: /[;{]/, - excludeEnd: true, - illegal: '[$%\\[]', - contains: [ - { beginKeywords: 'use', }, - hljs.UNDERSCORE_TITLE_MODE, - { - begin: '=>', // No markup, just a relevance booster - endsParent: true - }, - { - scope: 'params', - begin: '\\(', - end: '\\)', - excludeBegin: true, - excludeEnd: true, - keywords: KEYWORDS, - contains: [ - 'self', - VARIABLE, - LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON, - hljs.C_BLOCK_COMMENT_MODE, - STRING, - NUMBER - ] - }, - ] - }, + FUNCTION_DECLARATION, { scope: 'class', variants: [ diff --git a/test/markup/php/functions.expect.txt b/test/markup/php/functions.expect.txt index c50ac29e48..138115ab84 100644 --- a/test/markup/php/functions.expect.txt +++ b/test/markup/php/functions.expect.txt @@ -7,6 +7,15 @@ return $x + $y; }; +/** + * Anonymous function + */ +function hello(string $name) { + return trim(function() use ($name) { + echo "Hello; $name"; + }); +} + /** * Function invoke */ @@ -49,3 +58,4 @@ label: 'foo', time:time() + array(5)[0] + Foo::HOUR, ); + diff --git a/test/markup/php/functions.txt b/test/markup/php/functions.txt index 55538096c8..3a2afdb937 100644 --- a/test/markup/php/functions.txt +++ b/test/markup/php/functions.txt @@ -7,6 +7,15 @@ $fn2 = function ($x) use ($y) { return $x + $y; }; +/** + * Anonymous function + */ +function hello(string $name) { + return trim(function() use ($name) { + echo "Hello; $name"; + }); +} + /** * Function invoke */ @@ -49,3 +58,4 @@ setAlarm( label: 'foo', time:time() + array(5)[0] + Foo::HOUR, ); +