Skip to content

Commit

Permalink
enh(php) detect newer more flexible HEREdoc syntax (#2658)
Browse files Browse the repository at this point in the history
- separate tests along purpose + support of indented heredoc + heredoc as string variant
- share subst with double quoted string
  • Loading branch information
eytienne committed Aug 31, 2020
1 parent ea62ab2 commit 2a65144
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 50 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Language Improvements:
- fix(night) Prevent object prototypes method values from being returned in `getLanguage` (#2636) [night][]
- enh(java) Add support for `enum`, which will identify as a `class` now (#2643) [ezksd][]
- enh(nsis) Add support for NSIS 3.06 commands (#2653) [idleberg][]
- enh(php) detect newer more flexible HEREdoc syntax (#2658) [eytienne][]

[Youssef Victor]: https://github.com/Youssef1313
[Josh Goebel]: https://github.com/yyyc514
Expand All @@ -26,6 +27,7 @@ Language Improvements:
[night]: https://github.com/night
[ezksd]: https://github.com/ezksd
[idleberg]: https://github.com/idleberg
[eytienne]: https://github.com/eytienne


## Version 10.1.1
Expand Down
54 changes: 32 additions & 22 deletions src/languages/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Website: https://www.php.net
Category: common
*/

/**
* @param {HLJSApi} hljs
* @returns {LanguageDetail}
* */
export default function(hljs) {
var VARIABLE = {
begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
Expand All @@ -18,18 +22,38 @@ export default function(hljs) {
{ begin: /\?>/ } // end php tag
]
};
var SUBST = {
className: 'subst',
variants: [
{ begin: /\$\w+/ },
{ begin: /\{\$/, end: /\}/ }
]
};
var SINGLE_QUOTED = hljs.inherit(hljs.APOS_STRING_MODE, {
illegal: null,
});
var DOUBLE_QUOTED = hljs.inherit(hljs.QUOTE_STRING_MODE, {
illegal: null,
contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
});
var HEREDOC = hljs.END_SAME_AS_BEGIN({
begin: /<<<[ \t]*(\w+)\n/,
end: /[ \t]*(\w+)\b/,
contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
});
var STRING = {
className: 'string',
contains: [hljs.BACKSLASH_ESCAPE, PREPROCESSOR],
variants: [
{
begin: 'b"', end: '"'
},
{
begin: 'b\'', end: '\''
},
hljs.inherit(hljs.APOS_STRING_MODE, {illegal: null}),
hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null})
hljs.inherit(SINGLE_QUOTED, {
begin: "b'", end: "'",
}),
hljs.inherit(DOUBLE_QUOTED, {
begin: 'b"', end: '"',
}),
DOUBLE_QUOTED,
SINGLE_QUOTED,
HEREDOC
]
};
var NUMBER = {variants: [hljs.BINARY_NUMBER_MODE, hljs.C_NUMBER_MODE]};
Expand Down Expand Up @@ -87,20 +111,6 @@ export default function(hljs) {
keywords: '__halt_compiler'
}
),
{
className: 'string',
begin: /<<<['"]?\w+['"]?$/, end: /^\w+;?$/,
contains: [
hljs.BACKSLASH_ESCAPE,
{
className: 'subst',
variants: [
{begin: /\$\w+/},
{begin: /\{\$/, end: /\}/}
]
}
]
},
PREPROCESSOR,
{
className: 'keyword', begin: /\$this\b/
Expand Down
14 changes: 0 additions & 14 deletions test/markup/php/heredoc.expect.txt

This file was deleted.

14 changes: 0 additions & 14 deletions test/markup/php/heredoc.txt

This file was deleted.

23 changes: 23 additions & 0 deletions test/markup/php/strings.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<span class="hljs-meta">&lt;?php</span>

<span class="hljs-comment">// variable substitution</span>

<span class="hljs-keyword">echo</span> (<span class="hljs-string">&#x27;Hello {$person-&gt;name}! Welcome to $company!&#x27;</span>);

<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>);

<span class="hljs-keyword">echo</span> (<span class="hljs-string">&lt;&lt;&lt;MSG
Hello <span class="hljs-subst">{$person-&gt;name}</span>! Welcome to <span class="hljs-subst">$company</span>!
MSG</span>);

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

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

var_dump(<span class="hljs-string">&lt;&lt;&lt;SQL
SELECT *
FROM table
SQL</span>);
23 changes: 23 additions & 0 deletions test/markup/php/strings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

// variable substitution

echo ('Hello {$person->name}! Welcome to $company!');

echo ("Hello {$person->name}! Welcome to $company!");

echo (<<<MSG
Hello {$person->name}! Welcome to $company!
MSG);

// heredoc syntax

var_dump(<<<SQL
SELECT *
FROM table
SQL);

var_dump(<<<SQL
SELECT *
FROM table
SQL);

0 comments on commit 2a65144

Please sign in to comment.