Skip to content

Commit

Permalink
enh(php) support function invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
wkania committed Dec 12, 2021
1 parent 2841fd4 commit 8614a10
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
33 changes: 25 additions & 8 deletions src/languages/php.js
Expand Up @@ -11,6 +11,7 @@ Category: common
* @returns {LanguageDetail}
* */
export default function(hljs) {
const regex = hljs.regex;
const VARIABLE = {
className: 'variable',
begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' +
Expand Down Expand Up @@ -145,7 +146,6 @@ export default function(hljs) {
"isset",
"iterable",
"list",
"match|0",
"mixed",
"new",
"object",
Expand All @@ -172,7 +172,6 @@ export default function(hljs) {
const BUILT_INS = [
// Standard PHP library:
// <https://www.php.net/manual/en/book.spl.php>
"Error|0",
"AppendIterator",
"ArgumentCountError",
"ArithmeticError",
Expand Down Expand Up @@ -273,21 +272,34 @@ export default function(hljs) {
/** @type string[] */
const result = [];
items.forEach(item => {
result.push(item);
if (item.toLowerCase() === item) {
result.push(item);
result.push(item.toUpperCase());
} else {
result.push(item);
result.push(item.toLowerCase());
}
});
return result;
};

const KEYWORDS = {
keyword: KWS,
keyword: KWS.concat([ "match|0" ]),
literal: dualCase(LITERALS),
built_in: BUILT_INS
built_in: BUILT_INS.concat([ "Error|0" ]),
};

const FUNCTION_INVOKE = {
relevance: 0,
match: [
/(?:->|::|\s|\(|\\)/,
regex.concat("(?!fn\\b|function\\b|match\\b|", KWS.join("\\b|"), "|", BUILT_INS.join("\\b|"), "\\b)"),
/\w+/,
/\s*/,
regex.lookahead(/(?=\()/)
],
scope: {
3: "function.title.invoke",
}
};
return {
case_insensitive: false,
Expand Down Expand Up @@ -328,9 +340,14 @@ export default function(hljs) {
begin: /\$this\b/
},
VARIABLE,
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]*(?!\()/
},
{
// swallow create object
begin: /new\s\\?[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\s?\(/
},
{
className: 'function',
Expand Down Expand Up @@ -378,7 +395,7 @@ export default function(hljs) {
},
// both use and namespace still use "old style" rules (vs multi-match)
// because the namespace name can include `\` and we still want each
// element to be treated as it's own *individual* title
// element to be treated as its own *individual* title
{
beginKeywords: 'namespace',
relevance: 0,
Expand Down
10 changes: 10 additions & 0 deletions test/markup/php/functions.expect.txt
Expand Up @@ -6,3 +6,13 @@
<span class="hljs-variable">$fn2</span> = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"><span class="hljs-variable">$x</span></span>) <span class="hljs-keyword">use</span> (<span class="hljs-params"><span class="hljs-variable">$y</span></span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-variable">$x</span> + <span class="hljs-variable">$y</span>;
};

<span class="hljs-comment">/**
* Function invoke
*/</span>
<span class="hljs-variable">$date</span> = new DateTimeImmutable ();
<span class="hljs-variable">$date</span>-&gt;<span class="hljs-function title_ invoke__">format</span>(<span class="hljs-string">&#x27;Y-m-d&#x27;</span>);

DateTimeImmutable::<span class="hljs-function title_ invoke__">createFromMutable</span>(new \DateTime());

<span class="hljs-function title_ invoke__">str_contains</span> (\<span class="hljs-function title_ invoke__">strtoupper</span>(<span class="hljs-function title_ invoke__">substr</span>(<span class="hljs-string">&#x27;abcdef&#x27;</span>, -<span class="hljs-number">2</span>), <span class="hljs-string">&#x27;f&#x27;</span>));
10 changes: 10 additions & 0 deletions test/markup/php/functions.txt
Expand Up @@ -6,3 +6,13 @@ $fn1 = fn($x) => $x + $y;
$fn2 = function ($x) use ($y) {
return $x + $y;
};

/**
* Function invoke
*/
$date = new DateTimeImmutable ();
$date->format('Y-m-d');

DateTimeImmutable::createFromMutable(new \DateTime());

str_contains (\strtoupper(substr('abcdef', -2), 'f'));
4 changes: 2 additions & 2 deletions test/markup/php/strings.expect.txt
Expand Up @@ -12,12 +12,12 @@ MSG</span>);

<span class="hljs-comment">// heredoc syntax</span>

var_dump(<span class="hljs-string">&lt;&lt;&lt;SQL
<span class="hljs-function title_ invoke__">var_dump</span>(<span class="hljs-string">&lt;&lt;&lt;SQL
SELECT *
FROM table
SQL</span>);

var_dump(<span class="hljs-string">&lt;&lt;&lt;SQL
<span class="hljs-function title_ invoke__">var_dump</span>(<span class="hljs-string">&lt;&lt;&lt;SQL
SELECT *
FROM table
SQL</span>);

0 comments on commit 8614a10

Please sign in to comment.