Skip to content

Commit

Permalink
ok?
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Dec 3, 2023
1 parent 125e712 commit 801955b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 43 deletions.
1 change: 1 addition & 0 deletions System.Linq.Dynamic.Core.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=DLL_0027s/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Formattable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=renamer/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Unescape/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Xunit/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Copyright>Copyright © ZZZ Projects</Copyright>
<DefaultLanguage>en-us</DefaultLanguage>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>11</LangVersion>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<PackageIcon>logo.png</PackageIcon>
<PackageReadmeFile>PackageReadme.md</PackageReadmeFile>
Expand Down
8 changes: 1 addition & 7 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,7 @@ private AnyOf<Expression, Type> ParseStringLiteral(bool forceParseAsString)
_textParser.NextToken();
}

parsedStringValue = StringParser.ParseString(text);
parsedStringValue = StringParser.ParseStringAndReplaceDoubleQuotes(text, _textParser.CurrentToken.Pos);

return ConstantExpressionHelper.CreateLiteral(parsedStringValue, parsedStringValue);
}
Expand Down Expand Up @@ -2182,14 +2182,10 @@ private Expression[] ParseArgumentList()
return args;
}

private bool _IsParsingArguments;

private Expression[] ParseArguments()
{
var argList = new List<Expression>();

_IsParsingArguments = true;

while (true)
{
var argumentExpression = ParseOutKeyword();
Expand All @@ -2206,8 +2202,6 @@ private Expression[] ParseArguments()
_textParser.NextToken();
}

_IsParsingArguments = false;

return argList.ToArray();
}

Expand Down
81 changes: 50 additions & 31 deletions src/System.Linq.Dynamic.Core/Parser/StringParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,59 @@
using System.Linq.Dynamic.Core.Exceptions;
using System.Text.RegularExpressions;

namespace System.Linq.Dynamic.Core.Parser
namespace System.Linq.Dynamic.Core.Parser;

/// <summary>
/// Parse a Double and Single Quoted string.
/// Some parts of the code is based on https://github.com/zzzprojects/Eval-Expression.NET
/// </summary>
internal static class StringParser
{
/// <summary>
/// Parse a Double and Single Quoted string.
/// Some parts of the code is based on https://github.com/zzzprojects/Eval-Expression.NET
/// </summary>
internal static class StringParser
private const string Pattern = @"""""";
private const string Replacement = "\"";

public static string ParseString(string s, int pos = default)
{
public static string ParseString(string s)
if (s == null || s.Length < 2)
{
throw new ParseException(string.Format(CultureInfo.CurrentCulture, Res.InvalidStringLength, s, 2), pos);
}

if (s[0] != '"' && s[0] != '\'')
{
throw new ParseException(string.Format(CultureInfo.CurrentCulture, Res.InvalidStringQuoteCharacter), pos);
}

char quote = s[0]; // This can be single or a double quote
if (s.Last() != quote)
{
throw new ParseException(string.Format(CultureInfo.CurrentCulture, Res.UnexpectedUnclosedString, s.Length, s), pos);
}

try
{
return Regex.Unescape(s.Substring(1, s.Length - 2));
}
catch (Exception ex)
{
throw new ParseException(ex.Message, pos, ex);
}
}

public static string ParseStringAndReplaceDoubleQuotes(string s, int pos)
{
return ReplaceDoubleQuotes(ParseString(s, pos), pos);
}

private static string ReplaceDoubleQuotes(string s, int pos)
{
try
{
return Regex.Replace(s, Pattern, Replacement);
}
catch (Exception ex)
{
if (s == null || s.Length < 2)
{
throw new ParseException(string.Format(CultureInfo.CurrentCulture, Res.InvalidStringLength, s, 2), 0);
}

if (s[0] != '"' && s[0] != '\'')
{
throw new ParseException(string.Format(CultureInfo.CurrentCulture, Res.InvalidStringQuoteCharacter), 0);
}

char quote = s[0]; // This can be single or a double quote
if (s.Last() != quote)
{
throw new ParseException(string.Format(CultureInfo.CurrentCulture, Res.UnexpectedUnclosedString, s.Length, s), s.Length);
}

try
{
return Regex.Unescape(s.Substring(1, s.Length - 2));
}
catch (Exception ex)
{
throw new ParseException(ex.Message, 0, ex);
}
throw new ParseException(ex.Message, pos, ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1368,7 +1368,7 @@ public void DynamicExpressionParser_ParseLambda_CustomType_Method_With_Expressio
var resultUserName = (string?)delegateUserName.DynamicInvoke(user);

// Assert : string
resultUserName.Should().Be(@"UserName == """"x""""");
resultUserName.Should().Be(@"UserName == ""x""");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void StringParser_With_UnexpectedUnrecognizedEscapeSequence_ThrowsExcepti

parseException.Which.InnerException!.Message.Should().Contain("hexadecimal digits");

parseException.Which.StackTrace.Should().Contain("at System.Linq.Dynamic.Core.Parser.StringParser.ParseString(String s) in ").And.Contain("System.Linq.Dynamic.Core\\Parser\\StringParser.cs:line ");
parseException.Which.StackTrace.Should().Contain("at System.Linq.Dynamic.Core.Parser.StringParser.ParseString(String s, Int32 pos) in ").And.Contain("System.Linq.Dynamic.Core\\Parser\\StringParser.cs:line ");
}

[Theory]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ private static string GetQuote(int subQueryLevel)
{
var quoteCount = (int)Math.Pow(2, subQueryLevel - 1);

var quote = string.Concat(Enumerable.Repeat("\"", quoteCount));
return quote;
//var quote = string.Concat(Enumerable.Repeat("\"", quoteCount));
//return quote;
return new string('"', quoteCount);
}
}
}

0 comments on commit 801955b

Please sign in to comment.