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

PHP NOWDOC Support #3679

Merged
merged 5 commits into from
Mar 19, 2023
Merged
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 @@ -17,11 +17,13 @@ Grammars:
- added 3rd party Candid grammar to SUPPORTED_LANGUAGES [rvanasa][]
- fix(haskell) Added support for characters [CrystalSplitter][]
- enh(dart) Add `base`, `interface`, `sealed`, and `when` keywords [Sam Rawlins][]
- enh(php) detect newer more flexible NOWdoc syntax (#3679) [Timur Kamaev][]

Parser:

- add removePlugin api [faga295][]

[Timur Kamaev]: https://github.com/doiftrue
[WisamMechano]: https://github.com/wisammechano
[faga295]: https://github.com/faga295
[AdamRaichu]: https://github.com/AdamRaichu
Expand Down
15 changes: 12 additions & 3 deletions src/languages/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,18 @@ export default function(hljs) {
illegal: null,
contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
});
const HEREDOC = hljs.END_SAME_AS_BEGIN({
begin: /<<<[ \t]*(\w+)\n/,

const HEREDOC = {
begin: /<<<[ \t]*(?:(\w+)|"(\w+)")\n/,
end: /[ \t]*(\w+)\b/,
contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
'on:begin': (m, resp) => { resp.data._beginMatch = m[1] || m[2]; },
'on:end': (m, resp) => { if (resp.data._beginMatch !== m[1]) resp.ignoreMatch(); },
};

const NOWDOC = hljs.END_SAME_AS_BEGIN({
begin: /<<<[ \t]*'(\w+)'\n/,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It must always be quoted?

Copy link
Contributor Author

@doiftrue doiftrue Dec 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. If not - it's a HEREDOC not NOWDOC

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the end of now doc doesn't have to be quoted?

end: /[ \t]*(\w+)\b/,
});
// list of valid whitespaces because non-breaking space might be part of a IDENT_RE
const WHITESPACE = '[ \t\n]';
Expand All @@ -63,7 +71,8 @@ export default function(hljs) {
variants: [
DOUBLE_QUOTED,
SINGLE_QUOTED,
HEREDOC
HEREDOC,
NOWDOC
]
};
const NUMBER = {
Expand Down
13 changes: 12 additions & 1 deletion test/markup/php/strings.expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,18 @@ MSG</span>);
FROM table
SQL</span>);

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

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

<span class="hljs-variable">$var</span> = <span class="hljs-string">&lt;&lt;&lt;&#x27;TEXT&#x27;
Hello {$person-&gt;name}! Welcome to $company!
TEXT</span>;

<span class="hljs-title function_ invoke__">var_dump</span>(<span class="hljs-string">&lt;&lt;&lt;&#x27;TEXT&#x27;
Hello {$person-&gt;name}! Welcome to $company!
TEXT</span>);

12 changes: 11 additions & 1 deletion test/markup/php/strings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ var_dump(<<<SQL
FROM table
SQL);

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

// nowdoc syntax

$var = <<<'TEXT'
Hello {$person->name}! Welcome to $company!
TEXT;

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