Skip to content

Commit d75124f

Browse files
committed
issue #11
1 parent e422a82 commit d75124f

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/System.Linq.Dynamic.Core/ExpressionParser.cs

+11-8
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,9 @@ Expression ParseIn()
483483
while (_token.id != TokenId.CloseParen)
484484
{
485485
NextToken();
486-
Expression right = ParsePrimary();
486+
487+
//we need to parse unary expressions because otherwise 'in' clause will fail in use cases like 'in (-1, -1)' or 'in (!true)'
488+
Expression right = ParseUnary();
487489

488490
//check for direct type match
489491
if (identifier.Type != right.Type)
@@ -752,13 +754,11 @@ Expression ParseMultiplicative()
752754
// -, !, not unary operators
753755
Expression ParseUnary()
754756
{
755-
if (_token.id == TokenId.Minus || _token.id == TokenId.Exclamation ||
756-
TokenIdentifierIs("not"))
757+
if (_token.id == TokenId.Minus || _token.id == TokenId.Exclamation || TokenIdentifierIs("not"))
757758
{
758759
Token op = _token;
759760
NextToken();
760-
if (op.id == TokenId.Minus && (_token.id == TokenId.IntegerLiteral ||
761-
_token.id == TokenId.RealLiteral))
761+
if (op.id == TokenId.Minus && (_token.id == TokenId.IntegerLiteral || _token.id == TokenId.RealLiteral))
762762
{
763763
_token.text = "-" + _token.text;
764764
_token.pos = op.pos;
@@ -870,12 +870,14 @@ Expression ParseIntegerLiteral()
870870
ulong value;
871871
if (!ulong.TryParse(text, out value))
872872
throw ParseError(Res.InvalidIntegerLiteral, text);
873+
873874
NextToken();
874875
if (!string.IsNullOrEmpty(qualifier))
875876
{
876877
if (qualifier == "U") return CreateLiteral((uint)value, text);
877878
if (qualifier == "L") return CreateLiteral((long)value, text);
878-
else if (qualifier == "UL") return CreateLiteral(value, text);
879+
880+
return CreateLiteral(value, text);
879881
}
880882
if (value <= int.MaxValue) return CreateLiteral((int)value, text);
881883
if (value <= uint.MaxValue) return CreateLiteral((uint)value, text);
@@ -887,13 +889,14 @@ Expression ParseIntegerLiteral()
887889
long value;
888890
if (!long.TryParse(text, out value))
889891
throw ParseError(Res.InvalidIntegerLiteral, text);
892+
890893
NextToken();
891894
if (!string.IsNullOrEmpty(qualifier))
892895
{
893896
if (qualifier == "L")
894897
return CreateLiteral(value, text);
895-
else
896-
throw ParseError(Res.InvalidIntegerLiteral, qualifier);
898+
899+
throw ParseError(Res.MinusCannotBeAppliedToUnsignedInteger);
897900
}
898901

899902
if (value <= int.MaxValue) return CreateLiteral((int)value, text);

src/System.Linq.Dynamic.Core/Res.cs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ internal static class Res
88
public const string InvalidCharacterLiteral = "Character literal must contain exactly one character";
99
public const string InvalidIntegerLiteral = "Invalid integer literal '{0}'";
1010
public const string InvalidIntegerQualifier = "Invalid integer literal qualifier '{0}'";
11+
public const string MinusCannotBeAppliedToUnsignedInteger = "'-' cannot be applied to unsigned integers.";
1112
public const string InvalidRealLiteral = "Invalid real literal '{0}'";
1213
public const string UnknownIdentifier = "Unknown identifier '{0}'";
1314
public const string NoItInScope = "No 'it' is in scope";

test/System.Linq.Dynamic.Core.Tests/ExpressionTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,11 @@ public void ExpressionTests_IntegerQualifiers_Negative()
9797

9898
//Act
9999
var resultL = valuesL.Where("it <= -2L").ToDynamicArray<long>();
100-
//var resultIn = valuesL.Where("it in (-2L, -3L)").ToDynamicArray<long>();
100+
var resultIn = valuesL.Where("it in (-2L, -3L)").ToDynamicArray<long>();
101101

102102
//Assert
103103
Assert.Equal(resultValuesL.ToArray(), resultL);
104-
// Assert.Equal(resultValuesL.ToArray(), resultIn);
104+
Assert.Equal(resultValuesL.ToArray(), resultIn);
105105
}
106106

107107
[Fact]

0 commit comments

Comments
 (0)