Skip to content

Commit

Permalink
Merge pull request #327 from NeilMacMullen/multiline-pipe
Browse files Browse the repository at this point in the history
Allow the user of multi-line pipes
  • Loading branch information
xoofx committed Feb 5, 2021
2 parents 82c8874 + 239bf72 commit db4e371
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 33 deletions.
12 changes: 12 additions & 0 deletions doc/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,18 @@ The pipe operator `|` can also be used to pipe the result of an expression to a

> Notice that when a function receives the result of a pipe call (e.g `date.to_string` in the example above), it is passed as the **first argument of the call**. This is valid for both .NET custom functions as well as for Scriban integrated functions.
Pipes are *greedy* with respect to whitespace. This allow them to be chained across multiple lines:

```
{{-
"text" |
string.append "END" |
string.prepend "START"
-}}
```
will output `STARTtextEND`

#### Named arguments

When passing multiple arguments to an existing .NET function, you may want to use named arguments.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
text(4,4) : error : Error while parsing pipe expression: Expecting <expression> instead of `
` in: <expression> | <expression>
text(5,1) : error : Error while parsing pipe expression: Expecting <expression> instead of `}}` in: <expression> | <expression>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
a |
b
===
btesta
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
a |
b
===
{{
"test" |
string.append "a" |
string.prepend "b"
}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[1, 2, 3]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{-
func call_array(p)
ret [1,2,3] | array.filter @(do;ret p;end)
end
call_array true -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
99
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{-
a=99
fn=@(do;ret a;end)
fn
-}}
30 changes: 3 additions & 27 deletions src/Scriban.Tests/TestParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -630,33 +630,9 @@ public void EnsureMalformedFunctionDoesNotThrow()
{
Assert.DoesNotThrow(() =>Template.Parse("{{ func (t("));
}
[Test]
public void ArrayIteratorHasAccessToContext()
{

var templateString = @"{{-
func call_array(p)
ret [1,2,3] | array.filter @(do;ret p;end)
end
call_array true -}}";

var template = Template.Parse(templateString);
Assert.AreEqual("[1, 2, 3]",template.Render());
}


[Test]
public void TopLevelAnonymousFunctionCanAccessGloba()
{
var templateString = @"{{-
a=99
fn=@(do;ret a;end)
fn
-}}";
var template = Template.Parse(templateString);
Assert.AreEqual("99", template.Render());
}




[Test]
public void TestEvaluateProcessing()
{
Expand Down
16 changes: 12 additions & 4 deletions src/Scriban/Parsing/Parser.Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,14 @@ private ScriptExpression ParseExpression(ScriptNode parentNode, ScriptExpression
break;
}

if (_isScientific && Current.Type == TokenType.PipeGreater || !_isScientific && (Current.Type == TokenType.VerticalBar || Current.Type == TokenType.PipeGreater))
if (
(_isScientific &&
Current.Type == TokenType.PipeGreater)
||
(!_isScientific &&
Current.Type == TokenType.VerticalBar) ||
Current.Type == TokenType.PipeGreater
)
{
if (functionCall != null)
{
Expand All @@ -653,9 +660,10 @@ private ScriptExpression ParseExpression(ScriptNode parentNode, ScriptExpression
pipeCall.Span.Start = leftOperand.Span.Start;
}
pipeCall.From = leftOperand;

//Allow pipes to span lines
_allowNewLineLevel++;
pipeCall.PipeToken = ParseToken(Current.Type); // skip | or |>

_allowNewLineLevel--;
// unit test: 310-func-pipe-error1.txt
pipeCall.To = ExpectAndParseExpression(pipeCall);
return Close(pipeCall);
Expand Down Expand Up @@ -1239,4 +1247,4 @@ private enum ParseExpressionMode
BasicExpression,
}
}
}
}

0 comments on commit db4e371

Please sign in to comment.