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

enh(php) add support for Anonymous functions (closures) #4130

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
69 changes: 37 additions & 32 deletions src/languages/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: /\(/,
Expand All @@ -414,8 +448,10 @@ export default function(hljs) {
STRING,
NUMBER,
CONSTRUCTOR_CALL,
FUNCTION_DECLARATION,
],
};

const FUNCTION_INVOKE = {
relevance: 0,
match: [
Expand Down Expand Up @@ -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: [
Expand Down
10 changes: 10 additions & 0 deletions test/markup/php/functions.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
<span class="hljs-keyword">return</span> <span class="hljs-variable">$x</span> + <span class="hljs-variable">$y</span>;
};

<span class="hljs-comment">/**
* Anonymous function
*/</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">hello</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> <span class="hljs-variable">$name</span></span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-title function_ invoke__">trim</span>(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) <span class="hljs-keyword">use</span> (<span class="hljs-params"><span class="hljs-variable">$name</span></span>) </span>{
<span class="hljs-keyword">echo</span> <span class="hljs-string">&quot;Hello; <span class="hljs-subst">$name</span>&quot;</span>;
});
}

<span class="hljs-comment">/**
* Function invoke
*/</span>
Expand Down Expand Up @@ -49,3 +58,4 @@
<span class="hljs-attr">label</span>: <span class="hljs-string">&#x27;foo&#x27;</span>,
<span class="hljs-attr">time</span>:<span class="hljs-title function_ invoke__">time</span>() + <span class="hljs-keyword">array</span>(<span class="hljs-number">5</span>)[<span class="hljs-number">0</span>] + <span class="hljs-title class_">Foo</span>::<span class="hljs-variable constant_">HOUR</span>,
);

10 changes: 10 additions & 0 deletions test/markup/php/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -49,3 +58,4 @@ setAlarm(
label: 'foo',
time:time() + array(5)[0] + Foo::HOUR,
);