Skip to content
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
12 changes: 12 additions & 0 deletions Fluid.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,5 +1029,17 @@ public void ShouldNotParseFunctionCall()
Assert.False(parser.TryParse("{{ a() }}", out var template, out var errors));
Assert.Contains(ErrorMessages.FunctionsNotAllowed, errors);
}

[Fact]
public void KeywordsShouldNotConflictWithIdentifiers()
{
// Ensure the parser doesn't read 'empty' when identifiers start with this keywork
// Same for blank, true, false

var source = "{% assign emptyThing = 'this is not empty' %}{{ emptyThing }}{{ empty.size }}";
var context = new TemplateContext(new { empty = "eric" });
var template = _parser.Parse(source);
Assert.Equal("this is not empty4", template.Render(context));
}
}
}
25 changes: 15 additions & 10 deletions Fluid/FluidParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ public class FluidParser

protected static readonly Parser<TextSpan> String = Terms.String(StringLiteralQuotes.SingleOrDouble);
protected static readonly Parser<decimal> Number = Terms.Decimal(NumberOptions.AllowSign);
protected static readonly Parser<string> True = Terms.Text("true");
protected static readonly Parser<string> False = Terms.Text("false");
protected static readonly Parser<string> Empty = Terms.Text("empty");
protected static readonly Parser<string> Blank = Terms.Text("blank");

protected static readonly Parser<string> DoubleEquals = Terms.Text("==");
protected static readonly Parser<string> NotEquals = Terms.Text("!=");
Expand Down Expand Up @@ -116,14 +112,23 @@ public FluidParser(FluidParserOptions parserOptions)
.AndSkip(RParen)
.Then(x => new RangeExpression(x.Item1, x.Item2));

// primary => NUMBER | STRING | BOOLEAN | property
// primary => NUMBER | STRING | property
Primary.Parser =
String.Then<Expression>(x => new LiteralExpression(StringValue.Create(x)))
.Or(True.Then<Expression>(x => new LiteralExpression(BooleanValue.True)))
.Or(False.Then<Expression>(x => new LiteralExpression(BooleanValue.False)))
.Or(Empty.Then<Expression>(x => new LiteralExpression(EmptyValue.Instance)))
.Or(Blank.Then<Expression>(x => new LiteralExpression(BlankValue.Instance)))
.Or(Member.Then<Expression>(x => x))
.Or(Member.Then<Expression>(static x => {
if (x.Segments.Count == 1)
{
switch ((x.Segments[0] as IdentifierSegment).Identifier)
{
case "empty": return new LiteralExpression(EmptyValue.Instance);
Copy link
Copy Markdown
Collaborator

@lahma lahma Jan 12, 2022

Choose a reason for hiding this comment

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

would it make sense to have internal static fields, for like return LiteralExpression.EmptyValue?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Yes, these are immutable.

case "blank": return new LiteralExpression(BlankValue.Instance);
case "true": return new LiteralExpression(BooleanValue.True);
case "false": return new LiteralExpression(BooleanValue.False);
}
}

return x;
}))
.Or(Number.Then<Expression>(x => new LiteralExpression(NumberValue.Create(x))))
;

Expand Down