From fb661efbd378db72c4ff2943f305c55d7062d6d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20L=2E=20Charlier?= Date: Mon, 1 Jan 2024 17:57:31 +0100 Subject: [PATCH] fix: backtick is accepted to delimit quoted literals --- Expressif.Testing/Parsers/GrammarTest.cs | 8 ++++++-- Expressif/Parsers/Grammar.cs | 14 ++++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Expressif.Testing/Parsers/GrammarTest.cs b/Expressif.Testing/Parsers/GrammarTest.cs index 1ea2b23..1140ab1 100644 --- a/Expressif.Testing/Parsers/GrammarTest.cs +++ b/Expressif.Testing/Parsers/GrammarTest.cs @@ -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")] @@ -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()); -} \ No newline at end of file +} diff --git a/Expressif/Parsers/Grammar.cs b/Expressif/Parsers/Grammar.cs index 661434d..7f16815 100644 --- a/Expressif/Parsers/Grammar.cs +++ b/Expressif/Parsers/Grammar.cs @@ -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 FunctionName = from tokens in Parse.Letter.AtLeastOnce().Text().DelimitedBy(Parse.Char('-')).Token() @@ -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 QuotedLiteral = + protected static readonly Parser DoubleQuotedLiteral = Parse.CharExcept("\"").AtLeastOnce().Text().Contained(Parse.Char('\"'), Parse.Char('\"')).Token(); + protected static readonly Parser BacktickQuotedLiteral = + Parse.CharExcept("`").AtLeastOnce().Text().Contained(Parse.Char('`'), Parse.Char('`')).Token(); + + protected static readonly Parser QuotedLiteral = + DoubleQuotedLiteral.Or(BacktickQuotedLiteral); + public static readonly Parser Literal = UnquotedLiteral.Or(QuotedLiteral); }