Skip to content

Commit 2e93647

Browse files
committed
enh(php) Left and right-side of double colon
1 parent 2e3be43 commit 2e93647

File tree

4 files changed

+76
-12
lines changed

4 files changed

+76
-12
lines changed

src/languages/php.js

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ Category: common
1212
* */
1313
export default function(hljs) {
1414
const regex = hljs.regex;
15-
const IDENT_RE = '([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' +
15+
const IDENT_RE_CORE = '[a-zA-Z0-9_\x7f-\xff]*' +
1616
// negative look-ahead tries to avoid matching patterns that are not
1717
// Perl at all like $ident$, @ident@, etc.
1818
'(?![A-Za-z0-9])(?![$]))';
19+
const IDENT_RE = regex.concat("([a-zA-Z_\\x7f-\\xff]", IDENT_RE_CORE);
20+
// Will not detect camelCase classes
21+
const PASCAL_CASE_CLASS_NAME_RE = regex.concat("([A-Z]", IDENT_RE_CORE);
1922
const VARIABLE = {
2023
scope: 'variable',
2124
match: '\\$+' + IDENT_RE,
@@ -47,7 +50,7 @@ export default function(hljs) {
4750
end: /[ \t]*(\w+)\b/,
4851
contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
4952
});
50-
// list of valid whitespaces because non-breaking space might be part of a name
53+
// list of valid whitespaces because non-breaking space might be part of a IDENT_RE
5154
const WHITESPACE = '[ \t\n]';
5255
const STRING = {
5356
scope: 'string',
@@ -331,7 +334,7 @@ export default function(hljs) {
331334
/\b/,
332335
// to prevent keywords from being confused as the function title
333336
regex.concat("(?!fn\\b|function\\b|", normalizeKeywords(KWS).join("\\b|"), "|", normalizeKeywords(BUILT_INS).join("\\b|"), "\\b)"),
334-
regex.concat(IDENT_RE, "+"),
337+
IDENT_RE,
335338
regex.concat(WHITESPACE, "*"),
336339
regex.lookahead(/(?=\()/)
337340
],
@@ -340,6 +343,57 @@ export default function(hljs) {
340343
}
341344
};
342345

346+
const CONSTANT_REFERENCE = regex.concat(IDENT_RE, "\\b(?!\\()");
347+
348+
const LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON = {
349+
variants: [
350+
{
351+
match: [
352+
regex.concat(
353+
/::/,
354+
regex.lookahead(/(?!class\b)/)
355+
),
356+
CONSTANT_REFERENCE,
357+
],
358+
scope: {
359+
2: "variable.constant",
360+
},
361+
},
362+
{
363+
match: [
364+
/::/,
365+
/class/,
366+
],
367+
scope: {
368+
2: "variable.language",
369+
},
370+
},
371+
{
372+
match: [
373+
PASCAL_CASE_CLASS_NAME_RE,
374+
regex.concat(
375+
"::",
376+
regex.lookahead(/(?!class\b)/)
377+
),
378+
],
379+
scope: {
380+
1: "title.class",
381+
},
382+
},
383+
{
384+
match: [
385+
PASCAL_CASE_CLASS_NAME_RE,
386+
/::/,
387+
/class/,
388+
],
389+
scope: {
390+
1: "title.class",
391+
3: "variable.language",
392+
},
393+
}
394+
]
395+
};
396+
343397
return {
344398
case_insensitive: false,
345399
keywords: KEYWORDS,
@@ -380,16 +434,17 @@ export default function(hljs) {
380434
},
381435
VARIABLE,
382436
FUNCTION_INVOKE,
437+
LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON,
383438
{
384439
match: [
385440
/const/,
386-
regex.concat(WHITESPACE, "+"),
441+
/\s/,
387442
IDENT_RE,
388-
regex.concat(WHITESPACE, "*="),
443+
/\s*=/,
389444
],
390445
scope: {
391446
1: "keyword",
392-
3: "variable",
447+
3: "variable.constant",
393448
},
394449
},
395450
{
@@ -425,6 +480,7 @@ export default function(hljs) {
425480
contains: [
426481
'self',
427482
VARIABLE,
483+
LEFT_AND_RIGHT_SIDE_OF_DOUBLE_COLON,
428484
hljs.C_BLOCK_COMMENT_MODE,
429485
STRING,
430486
NUMBER

test/markup/php/functions.expect.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<span class="hljs-variable">$date</span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">DateTimeImmutable</span> ();
1414
<span class="hljs-variable">$date</span>-&gt;<span class="hljs-title function_ invoke__">format</span>(<span class="hljs-string">&#x27;Y-m-d&#x27;</span>);
1515

16-
DateTimeImmutable::<span class="hljs-title function_ invoke__">createFromMutable</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">\DateTime</span>(<span class="hljs-string">&#x27;now&#x27;</span>));
16+
<span class="hljs-title class_">DateTimeImmutable</span>::<span class="hljs-title function_ invoke__">createFromMutable</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">\DateTime</span>(<span class="hljs-string">&#x27;now&#x27;</span>));
1717

1818
<span class="hljs-title function_ invoke__">str_contains</span> (\<span class="hljs-title function_ invoke__">strtoupper</span>(<span class="hljs-title function_ 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>));
1919

test/markup/php/titles.expect.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Example</span> </span>{
2-
<span class="hljs-keyword">const</span> <span class="hljs-variable">FOO</span>=<span class="hljs-string">&#x27;foo&#x27;</span>;
1+
<span class="hljs-keyword">final</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Example</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Foo</span> </span>{
2+
<span class="hljs-keyword">const</span> <span class="hljs-variable constant_">FOO</span>=<span class="hljs-string">&#x27;foo&#x27;</span>;
33

44
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span>(<span class="hljs-params">
5-
<span class="hljs-keyword">public</span> <span class="hljs-keyword">readonly</span> <span class="hljs-keyword">string</span> <span class="hljs-variable">$name</span> = <span class="hljs-built_in">self</span>::<span class="hljs-variable">FOO</span>
5+
<span class="hljs-keyword">public</span> <span class="hljs-keyword">readonly</span> <span class="hljs-keyword">string</span> <span class="hljs-variable">$name</span> = <span class="hljs-built_in">self</span>::<span class="hljs-variable constant_">FOO</span>
66
</span>) </span>{}
77

88
<span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getClass</span>(<span class="hljs-params"></span>): <span class="hljs-title">string</span> </span>{
@@ -16,7 +16,11 @@
1616
<span class="hljs-keyword">public</span> <span class="hljs-built_in">static</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getClassFromStatic</span>(<span class="hljs-params"></span>): <span class="hljs-title">string</span> </span>{
1717
<span class="hljs-keyword">return</span> <span class="hljs-built_in">static</span>::<span class="hljs-variable language_">class</span>;
1818
}
19+
20+
<span class="hljs-keyword">public</span> <span class="hljs-built_in">static</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getParentClass</span>(<span class="hljs-params"></span>): <span class="hljs-title">string</span> </span>{
21+
<span class="hljs-keyword">return</span> <span class="hljs-built_in">parent</span>::<span class="hljs-variable language_">class</span>;
22+
}
1923
}
2024

21-
<span class="hljs-variable">$date</span> = DateTimeImmutable::<span class="hljs-title function_ invoke__">createFromMutable</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">\DateTime</span>());
25+
<span class="hljs-variable">$date</span> = <span class="hljs-title class_">DateTimeImmutable</span>::<span class="hljs-title function_ invoke__">createFromMutable</span>(<span class="hljs-keyword">new</span> <span class="hljs-title class_">\DateTime</span>());
2226
<span class="hljs-keyword">echo</span> <span class="hljs-variable">$date</span>::<span class="hljs-variable language_">class</span>;

test/markup/php/titles.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
final class Example {
1+
final class Example extends Foo {
22
const FOO='foo';
33

44
public function __construct(
@@ -16,6 +16,10 @@ final class Example {
1616
public static function getClassFromStatic(): string {
1717
return static::class;
1818
}
19+
20+
public static function getParentClass(): string {
21+
return parent::class;
22+
}
1923
}
2024

2125
$date = DateTimeImmutable::createFromMutable(new \DateTime());

0 commit comments

Comments
 (0)