Skip to content

Commit 2a65144

Browse files
authored
enh(php) detect newer more flexible HEREdoc syntax (highlightjs#2658)
- separate tests along purpose + support of indented heredoc + heredoc as string variant - share subst with double quoted string
1 parent ea62ab2 commit 2a65144

File tree

6 files changed

+80
-50
lines changed

6 files changed

+80
-50
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Language Improvements:
1717
- fix(night) Prevent object prototypes method values from being returned in `getLanguage` (#2636) [night][]
1818
- enh(java) Add support for `enum`, which will identify as a `class` now (#2643) [ezksd][]
1919
- enh(nsis) Add support for NSIS 3.06 commands (#2653) [idleberg][]
20+
- enh(php) detect newer more flexible HEREdoc syntax (#2658) [eytienne][]
2021

2122
[Youssef Victor]: https://github.com/Youssef1313
2223
[Josh Goebel]: https://github.com/yyyc514
@@ -26,6 +27,7 @@ Language Improvements:
2627
[night]: https://github.com/night
2728
[ezksd]: https://github.com/ezksd
2829
[idleberg]: https://github.com/idleberg
30+
[eytienne]: https://github.com/eytienne
2931

3032

3133
## Version 10.1.1

src/languages/php.js

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Website: https://www.php.net
66
Category: common
77
*/
88

9+
/**
10+
* @param {HLJSApi} hljs
11+
* @returns {LanguageDetail}
12+
* */
913
export default function(hljs) {
1014
var VARIABLE = {
1115
begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
@@ -18,18 +22,38 @@ export default function(hljs) {
1822
{ begin: /\?>/ } // end php tag
1923
]
2024
};
25+
var SUBST = {
26+
className: 'subst',
27+
variants: [
28+
{ begin: /\$\w+/ },
29+
{ begin: /\{\$/, end: /\}/ }
30+
]
31+
};
32+
var SINGLE_QUOTED = hljs.inherit(hljs.APOS_STRING_MODE, {
33+
illegal: null,
34+
});
35+
var DOUBLE_QUOTED = hljs.inherit(hljs.QUOTE_STRING_MODE, {
36+
illegal: null,
37+
contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
38+
});
39+
var HEREDOC = hljs.END_SAME_AS_BEGIN({
40+
begin: /<<<[ \t]*(\w+)\n/,
41+
end: /[ \t]*(\w+)\b/,
42+
contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
43+
});
2144
var STRING = {
2245
className: 'string',
2346
contains: [hljs.BACKSLASH_ESCAPE, PREPROCESSOR],
2447
variants: [
25-
{
26-
begin: 'b"', end: '"'
27-
},
28-
{
29-
begin: 'b\'', end: '\''
30-
},
31-
hljs.inherit(hljs.APOS_STRING_MODE, {illegal: null}),
32-
hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null})
48+
hljs.inherit(SINGLE_QUOTED, {
49+
begin: "b'", end: "'",
50+
}),
51+
hljs.inherit(DOUBLE_QUOTED, {
52+
begin: 'b"', end: '"',
53+
}),
54+
DOUBLE_QUOTED,
55+
SINGLE_QUOTED,
56+
HEREDOC
3357
]
3458
};
3559
var NUMBER = {variants: [hljs.BINARY_NUMBER_MODE, hljs.C_NUMBER_MODE]};
@@ -87,20 +111,6 @@ export default function(hljs) {
87111
keywords: '__halt_compiler'
88112
}
89113
),
90-
{
91-
className: 'string',
92-
begin: /<<<['"]?\w+['"]?$/, end: /^\w+;?$/,
93-
contains: [
94-
hljs.BACKSLASH_ESCAPE,
95-
{
96-
className: 'subst',
97-
variants: [
98-
{begin: /\$\w+/},
99-
{begin: /\{\$/, end: /\}/}
100-
]
101-
}
102-
]
103-
},
104114
PREPROCESSOR,
105115
{
106116
className: 'keyword', begin: /\$this\b/

test/markup/php/heredoc.expect.txt

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/markup/php/heredoc.txt

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/markup/php/strings.expect.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<span class="hljs-meta">&lt;?php</span>
2+
3+
<span class="hljs-comment">// variable substitution</span>
4+
5+
<span class="hljs-keyword">echo</span> (<span class="hljs-string">&#x27;Hello {$person-&gt;name}! Welcome to $company!&#x27;</span>);
6+
7+
<span class="hljs-keyword">echo</span> (<span class="hljs-string">&quot;Hello <span class="hljs-subst">{$person-&gt;name}</span>! Welcome to <span class="hljs-subst">$company</span>!&quot;</span>);
8+
9+
<span class="hljs-keyword">echo</span> (<span class="hljs-string">&lt;&lt;&lt;MSG
10+
Hello <span class="hljs-subst">{$person-&gt;name}</span>! Welcome to <span class="hljs-subst">$company</span>!
11+
MSG</span>);
12+
13+
<span class="hljs-comment">// heredoc syntax</span>
14+
15+
var_dump(<span class="hljs-string">&lt;&lt;&lt;SQL
16+
SELECT *
17+
FROM table
18+
SQL</span>);
19+
20+
var_dump(<span class="hljs-string">&lt;&lt;&lt;SQL
21+
SELECT *
22+
FROM table
23+
SQL</span>);

test/markup/php/strings.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
// variable substitution
4+
5+
echo ('Hello {$person->name}! Welcome to $company!');
6+
7+
echo ("Hello {$person->name}! Welcome to $company!");
8+
9+
echo (<<<MSG
10+
Hello {$person->name}! Welcome to $company!
11+
MSG);
12+
13+
// heredoc syntax
14+
15+
var_dump(<<<SQL
16+
SELECT *
17+
FROM table
18+
SQL);
19+
20+
var_dump(<<<SQL
21+
SELECT *
22+
FROM table
23+
SQL);

0 commit comments

Comments
 (0)