Skip to content

Commit

Permalink
fix: backtick is accepted to delimit quoted literals (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
Seddryck authored Jan 1, 2024
1 parent ae76573 commit f2a9430
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
8 changes: 6 additions & 2 deletions Expressif.Testing/Parsers/GrammarTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ public void Parse_Variable_Invalid(string value)
[TestCase("\" foo bar \"")]
[TestCase("\"foo , bar\"")]
[TestCase("\"(foo)\"")]
[TestCase("`foo`")]
[TestCase("` foo bar `")]
[TestCase("`foo , bar`")]
[TestCase("`(foo)`")]
public void Parse_Literal_Valid(string value)
=> Assert.That(Grammar.Literal.End().Parse(value), Is.EqualTo(value.Trim().Trim('\"')));
=> Assert.That(Grammar.Literal.End().Parse(value), Is.EqualTo(value.Trim().Trim('\"').Trim('`')));

[Test]
[TestCase("@foo")]
Expand All @@ -87,4 +91,4 @@ public void Parse_Literal_Valid(string value)
[TestCase("(foo)")]
public void Parse_Literal_Invalid(string value)
=> Assert.That(() => Grammar.Literal.End().Parse(value), Throws.TypeOf<ParseException>());
}
}
14 changes: 10 additions & 4 deletions Expressif/Parsers/Grammar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ namespace Expressif.Parsers;

public class Grammar
{
public static readonly char[] OpeningQuotedChars = ['\"', '@', ',', '(', '[', '{'];
public static readonly char[] ClosingQuotedChars = ['\"', '@', ',', ')', ']', '}'];
public static readonly char[] AlongQuotedChars = ['\"', '@', ',', '|', ' '];
public static readonly char[] OpeningQuotedChars = ['\"', '`', '@', ',', '(', '[', '{'];
public static readonly char[] ClosingQuotedChars = ['\"', '`', '@', ',', ')', ']', '}'];
public static readonly char[] AlongQuotedChars = ['\"', '`', '@', ',', '|', ' '];

public static readonly Parser<string> FunctionName =
from tokens in Parse.Letter.AtLeastOnce().Text().DelimitedBy(Parse.Char('-')).Token()
Expand All @@ -29,8 +29,14 @@ from firstChar in Parse.CharExcept(OpeningQuotedChars.Union(AlongQuotedChars)).T
from otherChars in Parse.CharExcept(ClosingQuotedChars.Union(AlongQuotedChars)).Many().Token().Optional()
select string.Concat(firstChar.ToString().Concat(otherChars.GetOrElse(string.Empty)));

protected static readonly Parser<string> QuotedLiteral =
protected static readonly Parser<string> DoubleQuotedLiteral =
Parse.CharExcept("\"").AtLeastOnce().Text().Contained(Parse.Char('\"'), Parse.Char('\"')).Token();

protected static readonly Parser<string> BacktickQuotedLiteral =
Parse.CharExcept("`").AtLeastOnce().Text().Contained(Parse.Char('`'), Parse.Char('`')).Token();

protected static readonly Parser<string> QuotedLiteral =
DoubleQuotedLiteral.Or(BacktickQuotedLiteral);

public static readonly Parser<string> Literal = UnquotedLiteral.Or(QuotedLiteral);
}

0 comments on commit f2a9430

Please sign in to comment.