Re: One-line heredoc for better syntax highlightning
| From: | Rowan Tommins | Date: | Tue, 20 Sep 2022 09:54:59 +0000 |
| Subject: | Re: One-line heredoc for better syntax highlightning | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-118669@lists.php.net to get a copy of this message | ||
On 19/09/2022 20:10, Olle Härstedt wrote:
More for users to learn? Don't you mean less, haha? This is an arbitrary limitation of heredoc, that we can remove (it might not be arbitrary from a yacc perspective, someone would have to explain that if so).I don't think the rules for heredoc are arbitrary, but they are quite specific: the end delimiter has to appear at the *start* of a line, so it can appear anywhere else in the text without prematurely ending the string: $foo = <<<FOO This is a normal heredoc this is not the end FOO this is: FOO; (Prior to PHP 7.3, only certain characters could appear *after* the end delimiter, but that's now more relaxed, and there's some clever rules around indenting.) As far as I can see, a single-line heredoc would need to be a new syntax, with its own syntax rules. Something like * If there is at least one non-whitespace character after <<<FOO but before the next newline, this is a one-line heredoc * If so, the heredoc content is everything up to and including the last occurrence of "FOO", followed by an optional semicolon, and a required newline * If "FOO" does not appear again before the next newline; or there is other text between "FOO" and the newline, that is a syntax error For instance, the following would presumably be valid: $foo = <<<FOO one-line heredoc ending at end of line FOO ; $foo = <<<FOO one-line heredoc with semicolon FOO; $foo = <<<FOO this is a heredoc containing FOO and other text FOO; $foo = <<<FOO // I guess this is a string not a comment? FOO; But the following would not: $foo = <<<FOO one-line heredoc not ended $foo = <<<FOO this heredoc contains FOO and then doesn't end $foo = <<<FOO mixing one-line and multi-line syntax doesn't make sense FOO; $foo = <<<FOO this heredoc looks like it ends FOO . 'but actually it doesn't'; Perhaps you disagree with some of those rules and examples, but hopefully can see that the rules would need defining, and therefore learning.
It's more explicit in the sense that it is syntax dedicated for that purpose, in a standard format. If I write <<<FOO hello FOO, there's no way to know if "foo" is the name of a language I'm hoping to label, or just a token which I know doesn't appear in my content; if I write /** @lang foo */"hello", there's no ambiguity, even if you've never heard of "foo" as a language. Regards, -- Rowan Tommins [IMSoP]I don't know about other editors, but PhpStorm at least also recognises an in-line comment explicitly labelling any string, which if anything seems m ore explicit: $query = /** @lang mysql */"SELECT * FROM foo";Pja. I wouldn't call it more or less explicit than heredoc notation, personally.