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 parsing a string literal with a dot #687

Merged
merged 1 commit into from
Mar 7, 2023
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
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