Skip to content

Commit d6c5d50

Browse files
committed
issue #12
1 parent d75124f commit d6c5d50

File tree

2 files changed

+124
-54
lines changed

2 files changed

+124
-54
lines changed

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

+70-16
Original file line numberDiff line numberDiff line change
@@ -874,14 +874,17 @@ Expression ParseIntegerLiteral()
874874
NextToken();
875875
if (!string.IsNullOrEmpty(qualifier))
876876
{
877-
if (qualifier == "U") return CreateLiteral((uint)value, text);
878-
if (qualifier == "L") return CreateLiteral((long)value, text);
877+
if (qualifier == "U" || qualifier == "u") return CreateLiteral((uint)value, text);
878+
if (qualifier == "L" || qualifier == "l") return CreateLiteral((long)value, text);
879879

880+
// in case of UL, just return
880881
return CreateLiteral(value, text);
881882
}
883+
882884
if (value <= int.MaxValue) return CreateLiteral((int)value, text);
883885
if (value <= uint.MaxValue) return CreateLiteral((uint)value, text);
884886
if (value <= long.MaxValue) return CreateLiteral((long)value, text);
887+
885888
return CreateLiteral(value, text);
886889
}
887890
else
@@ -893,9 +896,15 @@ Expression ParseIntegerLiteral()
893896
NextToken();
894897
if (!string.IsNullOrEmpty(qualifier))
895898
{
896-
if (qualifier == "L")
899+
if (qualifier == "L" || qualifier == "l")
897900
return CreateLiteral(value, text);
898901

902+
if (qualifier == "F" || qualifier == "f")
903+
return TryParseAsFloat(text, qualifier[0]);
904+
905+
if (qualifier == "D" || qualifier == "d")
906+
return TryParseAsDouble(text, qualifier[0]);
907+
899908
throw ParseError(Res.MinusCannotBeAppliedToUnsignedInteger);
900909
}
901910

@@ -905,30 +914,75 @@ Expression ParseIntegerLiteral()
905914
}
906915
}
907916

917+
//Expression ParseRealLiteral()
918+
//{
919+
// ValidateToken(TokenId.RealLiteral);
920+
// string text = _token.text;
921+
// object value = null;
922+
// char last = text[text.Length - 1];
923+
// if (last == 'F' || last == 'f')
924+
// {
925+
// float f;
926+
// if (float.TryParse(text.Substring(0, text.Length - 1), out f)) value = f;
927+
// }
928+
// else if (last == 'D' || last == 'd')
929+
// {
930+
// double d;
931+
// if (double.TryParse(text.Substring(0, text.Length - 1), out d)) value = d;
932+
// }
933+
// else
934+
// {
935+
// double d;
936+
// if (double.TryParse(text, out d)) value = d;
937+
// }
938+
// if (value == null) throw ParseError(Res.InvalidRealLiteral, text);
939+
// NextToken();
940+
// return CreateLiteral(value, text);
941+
//}
942+
908943
Expression ParseRealLiteral()
909944
{
910945
ValidateToken(TokenId.RealLiteral);
946+
911947
string text = _token.text;
912-
object value = null;
913-
char last = text[text.Length - 1];
914-
if (last == 'F' || last == 'f')
948+
char qualifier = text[text.Length - 1];
949+
950+
NextToken();
951+
return TryParseAsFloat(text, qualifier);
952+
}
953+
954+
Expression TryParseAsFloat(string text, char qualifier)
955+
{
956+
if (qualifier == 'F' || qualifier == 'f')
915957
{
916958
float f;
917-
if (float.TryParse(text.Substring(0, text.Length - 1), out f)) value = f;
959+
if (float.TryParse(text.Substring(0, text.Length - 1), out f))
960+
{
961+
return CreateLiteral(f, text);
962+
}
918963
}
919-
else if (last == 'D' || last == 'd')
964+
965+
// not possible to find float qualifier, so try to parse as double
966+
return TryParseAsDouble(text, qualifier);
967+
}
968+
969+
Expression TryParseAsDouble(string text, char qualifier)
970+
{
971+
double d;
972+
if (qualifier == 'D' || qualifier == 'd')
920973
{
921-
double d;
922-
if (double.TryParse(text.Substring(0, text.Length - 1), out d)) value = d;
974+
if (double.TryParse(text.Substring(0, text.Length - 1), out d))
975+
{
976+
return CreateLiteral(d, text);
977+
}
923978
}
924-
else
979+
980+
if (double.TryParse(text, out d))
925981
{
926-
double d;
927-
if (double.TryParse(text, out d)) value = d;
982+
return CreateLiteral(d, text);
928983
}
929-
if (value == null) throw ParseError(Res.InvalidRealLiteral, text);
930-
NextToken();
931-
return CreateLiteral(value, text);
984+
985+
throw ParseError(Res.InvalidRealLiteral, text);
932986
}
933987

934988
Expression CreateLiteral(object value, string text)

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

+54-38
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,49 @@ namespace System.Linq.Dynamic.Core.Tests
88
{
99
public class ExpressionTests
1010
{
11-
//[Fact]
12-
//public void ExpressionTests_DoubleQualifiers()
13-
//{
14-
// //Arrange
15-
// var values = new[] { 1d, 2D, 3d }.AsQueryable();
16-
// var resultValues = new[] { 2d, 3d }.AsQueryable();
17-
18-
// //Act
19-
// var result = values.Where("it == 2d or it == 3D").ToDynamicArray<double>();
20-
21-
// //Assert
22-
// Assert.Equal(resultValues.ToArray(), result);
23-
//}
24-
25-
//[Fact]
26-
//public void ExpressionTests_DoubleQualifiers_Negative()
27-
//{
28-
// //Arrange
29-
// var values = new[] { -1d, -2D, -3d }.AsQueryable();
30-
// var resultValues = new[] { -2d, -3d }.AsQueryable();
31-
32-
// //Act
33-
// var result = values.Where("it == -2d or it == -3D").ToDynamicArray<double>();
34-
35-
// //Assert
36-
// Assert.Equal(resultValues.ToArray(), result);
37-
//}
11+
[Fact]
12+
public void ExpressionTests_Double()
13+
{
14+
//Arrange
15+
var values = new[] { 1d, 2D, 3d }.AsQueryable();
16+
var resultValues = new[] { 2d, 3d }.AsQueryable();
17+
18+
//Act
19+
var result1 = values.Where("it == 2 or it == 3").ToDynamicArray<double>();
20+
//var result2 = values.Where("it > 1.99").ToDynamicArray<double>();
21+
22+
//Assert
23+
Assert.Equal(resultValues.ToArray(), result1);
24+
//Assert.Equal(resultValues.ToArray(), result2);
25+
}
26+
27+
[Fact]
28+
public void ExpressionTests_DoubleQualifiers()
29+
{
30+
//Arrange
31+
var values = new[] { 1d, 2D, 3d }.AsQueryable();
32+
var resultValues = new[] { 2d, 3d }.AsQueryable();
33+
34+
//Act
35+
var result = values.Where("it == 2d or it == 3D").ToDynamicArray<double>();
36+
37+
//Assert
38+
Assert.Equal(resultValues.ToArray(), result);
39+
}
40+
41+
[Fact]
42+
public void ExpressionTests_DoubleQualifiers_Negative()
43+
{
44+
//Arrange
45+
var values = new[] { -1d, -2D, -3d }.AsQueryable();
46+
var resultValues = new[] { -2d, -3d }.AsQueryable();
47+
48+
//Act
49+
var result = values.Where("it == -2d or it == -3D").ToDynamicArray<double>();
50+
51+
//Assert
52+
Assert.Equal(resultValues.ToArray(), result);
53+
}
3854

3955
[Fact]
4056
public void ExpressionTests_FloatQualifiers()
@@ -50,19 +66,19 @@ public void ExpressionTests_FloatQualifiers()
5066
Assert.Equal(resultValues.ToArray(), result);
5167
}
5268

53-
//[Fact]
54-
//public void ExpressionTests_FloatQualifiers_Negative()
55-
//{
56-
// //Arrange
57-
// var values = new[] { -1f, -2F, -3F }.AsQueryable();
58-
// var resultValues = new[] { -2f, -3f }.AsQueryable();
69+
[Fact]
70+
public void ExpressionTests_FloatQualifiers_Negative()
71+
{
72+
//Arrange
73+
var values = new[] { -1f, -2F, -3F }.AsQueryable();
74+
var resultValues = new[] { -2f, -3f }.AsQueryable();
5975

60-
// //Act
61-
// var result = values.Where("it == -2F or it == -3f").ToDynamicArray<float>();
76+
//Act
77+
var result = values.Where("it == -2F or it == -3f").ToDynamicArray<float>();
6278

63-
// //Assert
64-
// Assert.Equal(resultValues.ToArray(), result);
65-
//}
79+
//Assert
80+
Assert.Equal(resultValues.ToArray(), result);
81+
}
6682

6783
[Fact]
6884
public void ExpressionTests_IntegerQualifiers()

0 commit comments

Comments
 (0)