Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: backtick is accepted to delimit quoted literals #242

Merged
merged 1 commit into from
Jan 1, 2024
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
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);
}