@@ -28,6 +28,7 @@ public class ExpressionParser
28
28
private readonly MethodFinder _methodFinder ;
29
29
private readonly IKeywordsHelper _keywordsHelper ;
30
30
private readonly TextParser _textParser ;
31
+ private readonly NumberParser _numberParser ;
31
32
private readonly IExpressionHelper _expressionHelper ;
32
33
private readonly ITypeFinder _typeFinder ;
33
34
private readonly ITypeConverterFactory _typeConverterFactory ;
@@ -83,6 +84,7 @@ public ExpressionParser([CanBeNull] ParameterExpression[] parameters, [NotNull]
83
84
84
85
_keywordsHelper = new KeywordsHelper ( _parsingConfig ) ;
85
86
_textParser = new TextParser ( _parsingConfig , expression ) ;
87
+ _numberParser = new NumberParser ( parsingConfig ) ;
86
88
_methodFinder = new MethodFinder ( _parsingConfig ) ;
87
89
_expressionHelper = new ExpressionHelper ( _parsingConfig ) ;
88
90
_typeFinder = new TypeFinder ( _parsingConfig , _keywordsHelper ) ;
@@ -876,16 +878,14 @@ Expression ParseIntegerLiteral()
876
878
if ( ! string . IsNullOrEmpty ( qualifier ) )
877
879
{
878
880
if ( qualifier == "L" || qualifier == "l" )
881
+ {
879
882
return ConstantExpressionHelper . CreateLiteral ( value , text ) ;
883
+ }
880
884
881
- if ( qualifier == "F" || qualifier == "f" )
882
- return TryParseAsFloat ( text , qualifier [ 0 ] ) ;
883
-
884
- if ( qualifier == "D" || qualifier == "d" )
885
- return TryParseAsDouble ( text , qualifier [ 0 ] ) ;
886
-
887
- if ( qualifier == "M" || qualifier == "m" )
888
- return TryParseAsDecimal ( text , qualifier [ 0 ] ) ;
885
+ if ( qualifier == "F" || qualifier == "f" || qualifier == "D" || qualifier == "d" || qualifier == "M" || qualifier == "m" )
886
+ {
887
+ return ParseRealLiteral ( text , qualifier [ 0 ] , false ) ;
888
+ }
889
889
890
890
throw ParseError ( Res . MinusCannotBeAppliedToUnsignedInteger ) ;
891
891
}
@@ -904,54 +904,40 @@ Expression ParseRealLiteral()
904
904
_textParser . ValidateToken ( TokenId . RealLiteral ) ;
905
905
906
906
string text = _textParser . CurrentToken . Text ;
907
- char qualifier = text [ text . Length - 1 ] ;
908
907
909
908
_textParser . NextToken ( ) ;
910
- return TryParseAsFloat ( text , qualifier ) ;
911
- }
912
-
913
- Expression TryParseAsFloat ( string text , char qualifier )
914
- {
915
- if ( qualifier == 'F' || qualifier == 'f' )
916
- {
917
- if ( float . TryParse ( text . Substring ( 0 , text . Length - 1 ) , NumberStyles . Float , _parsingConfig . NumberParseCulture , out float f ) )
918
- {
919
- return ConstantExpressionHelper . CreateLiteral ( f , text ) ;
920
- }
921
- }
922
909
923
- // not possible to find float qualifier, so try to parse as double
924
- return TryParseAsDecimal ( text , qualifier ) ;
910
+ return ParseRealLiteral ( text , text [ text . Length - 1 ] , true ) ;
925
911
}
926
912
927
- Expression TryParseAsDecimal ( string text , char qualifier )
913
+ Expression ParseRealLiteral ( string text , char qualifier , bool stripQualifier )
928
914
{
929
- if ( qualifier == 'M' || qualifier == 'm' )
915
+ object o ;
916
+ switch ( qualifier )
930
917
{
931
- if ( decimal . TryParse ( text . Substring ( 0 , text . Length - 1 ) , NumberStyles . Number , _parsingConfig . NumberParseCulture , out decimal d ) )
932
- {
933
- return ConstantExpressionHelper . CreateLiteral ( d , text ) ;
934
- }
935
- }
918
+ case 'f' :
919
+ case 'F' :
920
+ o = _numberParser . ParseNumber ( stripQualifier ? text . Substring ( 0 , text . Length - 1 ) : text , typeof ( float ) ) ;
921
+ break ;
936
922
937
- // not possible to find float qualifier, so try to parse as double
938
- return TryParseAsDouble ( text , qualifier ) ;
939
- }
923
+ case 'm' :
924
+ case 'M' :
925
+ o = _numberParser . ParseNumber ( stripQualifier ? text . Substring ( 0 , text . Length - 1 ) : text , typeof ( decimal ) ) ;
926
+ break ;
940
927
941
- Expression TryParseAsDouble ( string text , char qualifier )
942
- {
943
- double d ;
944
- if ( qualifier == 'D' || qualifier == 'd' )
945
- {
946
- if ( double . TryParse ( text . Substring ( 0 , text . Length - 1 ) , NumberStyles . Number , _parsingConfig . NumberParseCulture , out d ) )
947
- {
948
- return ConstantExpressionHelper . CreateLiteral ( d , text ) ;
949
- }
928
+ case 'd' :
929
+ case 'D' :
930
+ o = _numberParser . ParseNumber ( stripQualifier ? text . Substring ( 0 , text . Length - 1 ) : text , typeof ( double ) ) ;
931
+ break ;
932
+
933
+ default :
934
+ o = _numberParser . ParseNumber ( text , typeof ( double ) ) ;
935
+ break ;
950
936
}
951
937
952
- if ( double . TryParse ( text , NumberStyles . Number , _parsingConfig . NumberParseCulture , out d ) )
938
+ if ( o != null )
953
939
{
954
- return ConstantExpressionHelper . CreateLiteral ( d , text ) ;
940
+ return ConstantExpressionHelper . CreateLiteral ( o , text ) ;
955
941
}
956
942
957
943
throw ParseError ( Res . InvalidRealLiteral , text ) ;
@@ -1045,42 +1031,6 @@ Expression ParseIdentifier()
1045
1031
return expr ;
1046
1032
}
1047
1033
1048
- //// This could be enum like "MyEnum.Value1"
1049
- //if (_textParser.CurrentToken.Id == TokenId.Identifier)
1050
- //{
1051
- // var parts = new List<string> { _textParser.CurrentToken.Text };
1052
-
1053
- // _textParser.NextToken();
1054
- // _textParser.ValidateToken(TokenId.Dot, Res.DotExpected);
1055
- // while (_textParser.CurrentToken.Id == TokenId.Dot || _textParser.CurrentToken.Id == TokenId.Plus)
1056
- // {
1057
- // parts.Add(_textParser.CurrentToken.Text);
1058
-
1059
- // _textParser.NextToken();
1060
- // _textParser.ValidateToken(TokenId.Identifier, Res.IdentifierExpected);
1061
-
1062
- // parts.Add(_textParser.CurrentToken.Text);
1063
-
1064
- // _textParser.NextToken();
1065
- // }
1066
-
1067
- // var enumTypeAsString = string.Join("", parts.Take(parts.Count - 2).ToArray());
1068
- // var enumType = _typeFinder.FindTypeByName(enumTypeAsString, null, true);
1069
- // if (enumType == null)
1070
- // {
1071
- // throw ParseError(_textParser.CurrentToken.Pos, Res.EnumTypeNotFound, enumTypeAsString);
1072
- // }
1073
-
1074
- // string enumValue = parts.Last();
1075
- // var @enum = TypeHelper.ParseEnum(enumValue, enumType);
1076
- // if (@enum == null)
1077
- // {
1078
- // throw ParseError(_textParser.CurrentToken.Pos, Res.EnumValueNotDefined, enumValue, enumTypeAsString);
1079
- // }
1080
-
1081
- // return Expression.Constant(@enum);
1082
- //}
1083
-
1084
1034
if ( _it != null )
1085
1035
{
1086
1036
return ParseMemberAccess ( null , _it ) ;
0 commit comments