Re: [PHP-DEV] One-line heredoc for better syntax highlightning
On 20/09/2022 19:29, Olle Härstedt wrote: Ya ok, this kills my idea. If heredoc was designed from start so that the delimiter would not be allowed at all in the text, then it would make sense. Weird that they did it like that, would be easy enough to come up with a unique delimiter so that it would not cause a problem... Obviously it can't be changed now without breaking backwards compatibility. For what it's worth, the "they" in question are the authors of the early Unix shells in the 1970s - from a quick search, it seems like the Bourne shell was the first to implement "here document" syntax, sometime around 1979. As the name suggests, they were not for defining strings, but for entering entire text files embedded in a script or interactive shell session. In that context, the original format is actually elegantly minimal: content is read one line at a time; if it exactly matches the delimiter, stop; else, add the line to the file buffer. No actual parsing is required. Regards, -- Rowan Tommins [IMSoP] -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php
Re: [PHP-DEV] One-line heredoc for better syntax highlightning
2022-09-20 17:00 GMT+02:00, Sara Golemon : > On 19 September 2022 15:24:26 BST, "Olle Härstedt" < > olle.haerst...@limesurvey.org> wrote: >>Some editors can guess the domain-specific language inside heredoc, e.g. > if you do >> >>$query = <<>SELECT * FROM foo >>MySQL; >> >>It would be nice if this feature could be used in single lines as well: >> >>$query = <<> > Good news! This feature exists and was introduced by Rasmus **checks > notes** about 25 years ago. > > To use it, you'll want to hold down your shift key, then press the key just > to the left of your enter key. This is called a "quote", and you can see > it demonstrated here around the word quote. > > Hope that helps! > > Seriously though, I know you're looking to help your editor find something > to syntax highlight, but this is an editor problem, not a PHP language > problem. If your editor can detect SQL in heredoc, then it should be able > to improve and find it in interpolated strings. Nope, because you need to be explicit which type of SQL you're working with - MySQL, Postgres, SQL Server, etc. Would be hard to write a regexp to guess that for the editor. :) In the Vim case, it reads your delimiter name and applies syntax highlight from that (mysql, javascript, html). > Making the parser more > complex for no benefit to the actual language (some detriment, I would > argue) is not the fix here. > > -Sara > > P.S. - Yes, I'm assuming a US layout, you know where your quote key is. I you read my text more carefully, you'd see that I thought the parser would be simplified by my suggestion, by removing what I thought was an arbitrary limitation. I was wrong. :( Olle -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php
Re: [PHP-DEV] One-line heredoc for better syntax highlightning
2022-09-20 11:54 GMT+02:00, Rowan Tommins : > 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 = << This is a normal heredoc > this is not the end FOO > this is: > FOO; Ya ok, this kills my idea. If heredoc was designed from start so that the delimiter would not be allowed at all in the text, then it would make sense. Weird that they did it like that, would be easy enough to come up with a unique delimiter so that it would not cause a problem... Obviously it can't be changed now without breaking backwards compatibility. Our product still supports PHP 7.2 sadly, but that's our own problem. :) Thank you. Olle -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php
Re: [PHP-DEV] One-line heredoc for better syntax highlightning
On 19 September 2022 15:24:26 BST, "Olle Härstedt" < olle.haerst...@limesurvey.org> wrote: >Some editors can guess the domain-specific language inside heredoc, e.g. if you do > >$query = <
Re: [PHP-DEV] One-line heredoc for better syntax highlightning
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 = <<(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 <
Re: [PHP-DEV] One-line heredoc for better syntax highlightning
2022-09-19 21:18 GMT+02:00, Tim Düsterhus : > Hi > > On 9/19/22 21:04, Olle Härstedt wrote: >> That looks good in isolation, but consider the following short snippet: >> >> class Foo >> { >> public function bar() >> { >> $query = <<> SELECT * FROM baz >> MySQL; >> } >> } >> >> As you can see, the heredoc breaks the reading flow completely. For > > The closing marker of the heredoc may be indented as of PHP 7.3, so the > appropriate comparison would be this: > > > class Foo > { > public function __construct( > private \PDO $dbConnection > ) {} > > public function bar() > { > $query = << SELECT * > FROMbaz > MySQL; > $statement = $this->dbConnection->prepare($query); > $statement->execute(); > } > } Pff, that's the second time today 3v4l gives me complete faulty results in its syntax checker. Guess it's out of date. Thanks for correcting me here. Olle -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php
Re: [PHP-DEV] One-line heredoc for better syntax highlightning
Hi On 9/19/22 21:04, Olle Härstedt wrote: That looks good in isolation, but consider the following short snippet: class Foo { public function bar() { $query = << The closing marker of the heredoc may be indented as of PHP 7.3, so the appropriate comparison would be this: dbConnection->prepare($query); $statement->execute(); } } smaller SQL queries, using one line would be an improvement, I think. A condition-less SELECT should be rare, a condition-less UPDATE or DELETE even rarer. So this "improvement" would only affect a very small number of statements in the first place and I disagree with that being an improvement even for that small number of statements, because it would be inconsistent with all the other larger statements which *are* spread across multiple lines. Also consider that the one line will look better in your color highlight editor. :) I believe that code should be readable no matter the editor it is viewed in. The suggestion fails at that. Best regards Tim Düsterhus -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php
Re: [PHP-DEV] One-line heredoc for better syntax highlightning
> While I can't immediately think of a practical problem with this, I'm hesi > tant at the idea of adding yet more ways of quoting strings; it means more > for users to learn, more for third party parsers to implement (including ex > actly the syntax highlighting tools you're going to help), and in the worst > case, more edge cases that can lead to security issues 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 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. Olle -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php
Re: [PHP-DEV] One-line heredoc for better syntax highlightning
> I don't find that example particularly readable or convincing. It mushes > together PHP and MySQL keywords too much. > > Once you add a WHERE clause that SQL query should be split across > multiple multiple lines for readability anyway and even for that very > simple example I personally would add a linebreak in front of the FROM: > > $query = << SELECT * > FROM foo > MySQL; > > Best regards >Tim Düsterhus That looks good in isolation, but consider the following short snippet: class Foo { public function bar() { $query = <
Re: [PHP-DEV] One-line heredoc for better syntax highlightning
Hi On 9/19/22 16:24, Olle Härstedt wrote: $query = << I don't find that example particularly readable or convincing. It mushes together PHP and MySQL keywords too much. Once you add a WHERE clause that SQL query should be split across multiple multiple lines for readability anyway and even for that very simple example I personally would add a linebreak in front of the FROM: $query = <
Re: [PHP-DEV] One-line heredoc for better syntax highlightning
On 19 September 2022 15:24:26 BST, "Olle Härstedt" wrote: >Hi internals! > >Some editors can guess the domain-specific language inside heredoc, e.g. if >you do > >$query = <
[PHP-DEV] One-line heredoc for better syntax highlightning
Hi internals! Some editors can guess the domain-specific language inside heredoc, e.g. if you do $query = <