Skip to content

Commit

Permalink
Fix parsing a string literal with a dot (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH authored Mar 7, 2023
1 parent e0e2f9a commit 7f45e14
Show file tree
Hide file tree
Showing 3 changed files with 462 additions and 415 deletions.
29 changes: 27 additions & 2 deletions src/System.Linq.Dynamic.Core/Parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,7 @@ private Expression ParsePrimaryStart()
return ParseIdentifier();

case TokenId.StringLiteral:
var expressionOrType = ParseStringLiteral(false);
return expressionOrType.IsFirst ? expressionOrType.First : ParseTypeAccess(expressionOrType.Second, false);
return ParseStringLiteralAsStringExpressionOrTypeExpression();

case TokenId.IntegerLiteral:
return ParseIntegerLiteral();
Expand All @@ -819,6 +818,32 @@ private Expression ParsePrimaryStart()
}
}

private Expression ParseStringLiteralAsStringExpressionOrTypeExpression()
{
var clonedTextParser = _textParser.Clone();
clonedTextParser.NextToken();

// Check if next token is a "(" or a "?(".
// Used for casting like $"\"System.DateTime\"(Abc)" or $"\"System.DateTime\"?(Abc)".
// In that case, the string value is NOT forced to stay a string.
bool forceParseAsString = true;
if (clonedTextParser.CurrentToken.Id == TokenId.OpenParen)
{
forceParseAsString = false;
}
else if (clonedTextParser.CurrentToken.Id == TokenId.Question)
{
clonedTextParser.NextToken();
if (clonedTextParser.CurrentToken.Id == TokenId.OpenParen)
{
forceParseAsString = false;
}
}

var expressionOrType = ParseStringLiteral(forceParseAsString);
return expressionOrType.IsFirst ? expressionOrType.First : ParseTypeAccess(expressionOrType.Second, false);
}

private AnyOf<Expression, Type> ParseStringLiteral(bool forceParseAsString)
{
_textParser.ValidateToken(TokenId.StringLiteral);
Expand Down
Loading

0 comments on commit 7f45e14

Please sign in to comment.