Skip to content

Allow the user of multi-line pipes#327

Merged
xoofx merged 9 commits intoscriban:masterfrom
NeilMacMullen:multiline-pipe
Feb 5, 2021
Merged

Allow the user of multi-line pipes#327
xoofx merged 9 commits intoscriban:masterfrom
NeilMacMullen:multiline-pipe

Conversation

@NeilMacMullen
Copy link
Copy Markdown
Contributor

@NeilMacMullen NeilMacMullen commented Feb 4, 2021

This change allows pipes to be chained over multiple lines, for example

"test"    |-
       string.prepend "start"   |-
       string.append   "end |-
       string.upper  

This is particularly useful when trying to write text- or array- processing pipelines where otherwise you would need to write all operations on a single (very long!) line.

I've introduced a new token for this "|-" (the minus is interpreted similarly to the minus in "{{-". Though it would be possible to modify the logic for a bare "|" to skip trailing whitespace+newline (and would be more visually pleasing IMO) I'm not sure whether that might introduce breaking changes; by introducing a new token the user is forced to opt-in to the behaviour.

The new behaviour is only available in non-scientific mode but could be added to that quite easily if desired.

There is still an issue that it's possible to break existing code that looks like -123 |- math.plus 1 (space between '-' and 'math') or some variation thereof. If that is a concern, more aggressive lookahead can be introduced; if this is done then it would probably also be possible to revert to treating |\s*\r as a multi-line pipe for a nicer syntax...

"test"    |
       string.prepend "start"   |
       string.append   "end |
       string.upper  

FWIW I quite like the visual appearance of

"test"    |>
       string.prepend "start"   |>
       string.append   "end |>
       string.upper  

but I'm not sure if |> has a special meaning in Scientific mode?

@NeilMacMullen NeilMacMullen marked this pull request as draft February 4, 2021 15:11
@xoofx
Copy link
Copy Markdown
Member

xoofx commented Feb 4, 2021

I've introduced a new token for this "|-"

Yeah, we should avoid that. The old operator should be able to support it, right after parsing it, you can force to flush the whitespaces tokens coming and attach them.

@xoofx
Copy link
Copy Markdown
Member

xoofx commented Feb 4, 2021

but I'm not sure if |> has a special meaning in Scientific mode?

It's actually the pipe operator in scientific. We cannot change that for regular Scriban that was following Liquid syntax for that.

@NeilMacMullen
Copy link
Copy Markdown
Contributor Author

NeilMacMullen commented Feb 4, 2021

Yeah, we should avoid that. The old operator should be able to support it, right after parsing it, you can force to flush the whitespaces tokens coming and attach them.

Not sure if you had something else in mind but I solved this by simply looking ahead via the IsRemainderOfLineWhitespace method. There is one minor implication which is that the error message for an unterminated pipe now uses the position of the next non-whitespace token rather than the end of line (which seems sensible).

So the current syntax is:

"test"    |
       string.prepend "start"   |
       string.append   "end |
       string.upper  

but if you write

"test" |
string.prepend "start" |
}}

You get an error saying "expecting expression instead of '}}'" now instead of "expecting expression instead of <newline>" which actually seems clearer in any case.

@NeilMacMullen
Copy link
Copy Markdown
Contributor Author

Yeah, we should avoid that [introducing a new TokenType ...] right after parsing it, you can force to flush the whitespaces tokens coming and attach them

Possibly I have misunderstood you? DId you mean to try and handle this by looking ahead for whitespace tokens in ParserExpression when a VerticalBar is encountered? The reason to introduce a PipeOverLineBreak Token originally was to avoid breaking any code that assume a VerticalBar really was just a single-character token (as the name suggests).

Comment thread src/Scriban/Parsing/Lexer.cs Outdated
@xoofx
Copy link
Copy Markdown
Member

xoofx commented Feb 4, 2021

Possibly I have misunderstood you? DId you mean to try and handle this by looking ahead for whitespace tokens in ParserExpression when a VerticalBar is encountered? The reason to introduce a PipeOverLineBreak Token originally was to avoid breaking any code that assume a VerticalBar really was just a single-character token (as the name suggests).

See my comment above. | should stay a single character. The handling of newlines is done at the parser level.

@xoofx
Copy link
Copy Markdown
Member

xoofx commented Feb 4, 2021

LGTM, thanks!

Comment thread src/Scriban.Tests/TestParser.cs Outdated
Comment thread doc/language.md
@NeilMacMullen NeilMacMullen requested a review from xoofx February 5, 2021 19:31
Comment thread src/Scriban/Parsing/Parser.Expressions.cs Outdated
@xoofx xoofx marked this pull request as ready for review February 5, 2021 19:40
@xoofx xoofx merged commit db4e371 into scriban:master Feb 5, 2021
@xoofx
Copy link
Copy Markdown
Member

xoofx commented Feb 5, 2021

Thanks for pushing the last changes!

@NeilMacMullen
Copy link
Copy Markdown
Contributor Author

Thanks for pushing the last changes!

No problem - very much looking forward to using this feature :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants