Skip to content
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
110 changes: 68 additions & 42 deletions src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override bool TryParse(ref ParseContext context, [NotNullWhen(true)] out
count = TryParseBinInteger(s, out number);
if (count == 0)
{
count = TryParseInteger(s, out number);
count = TryParseInteger(s, out number, out var overflow);
if (count == 0)
goto FAIL;

Expand All @@ -64,6 +64,9 @@ public override bool TryParse(ref ParseContext context, [NotNullWhen(true)] out
context.Advance(p);
return true;
}

if (overflow)
Error_IntegralConstantTooLarge();
}
}

Expand All @@ -76,43 +79,33 @@ public override bool TryParse(ref ParseContext context, [NotNullWhen(true)] out
return false;
}

private static int TryParseInteger(ReadOnlySpan<char> s, out ulong value)
private static int TryParseInteger(ReadOnlySpan<char> s, out ulong value, out bool overflow)
{
if (s.Length != 0)
{
var p = 0;
var r = 0ul;
var p = 0;
var r = 0ul;

for (; (uint)p < (uint)s.Length; p++)
{
var d = (uint)s[p] - '0';
if (d > 9)
{
if (p == 0)
{
goto FAIL;
}

break;
}

//
// Check for overflow
//
if (r > ulong.MaxValue / 10
|| (r == ulong.MaxValue / 10 && d > ulong.MaxValue % 10))
goto FAIL;
overflow = false;

for (; (uint)p < (uint)s.Length; p++)
{
var d = (uint)s[p] - '0';
if (d > 9)
break;

//
// A number too large for ulong is still read to the end, since its digits
// can be the integer part of a real literal, such as 100000000000000000000.0.
//
if (r > ulong.MaxValue / 10
|| (r == ulong.MaxValue / 10 && d > ulong.MaxValue % 10))
overflow = true;

if (!overflow)
r = r * 10 + d;
}

value = r;
return p;
}

FAIL:
value = 0;
return 0;
value = r;
return p;
}

private static int TryParseHexInteger(ReadOnlySpan<char> s, out ulong value)
Expand Down Expand Up @@ -141,7 +134,7 @@ private static int TryParseHexInteger(ReadOnlySpan<char> s, out ulong value)
// Check for overflow
//
if (r > ulong.MaxValue / 16)
goto FAIL;
Error_IntegralConstantTooLarge();

r = r * 16 + d;
}
Expand Down Expand Up @@ -179,7 +172,7 @@ private static int TryParseBinInteger(ReadOnlySpan<char> s, out ulong value)
// Check for overflow
//
if (r > ulong.MaxValue >> 1)
goto FAIL;
Error_IntegralConstantTooLarge();

r = (r << 1) + d;
}
Expand Down Expand Up @@ -236,11 +229,15 @@ private static int TryParseReal(ReadOnlySpan<char> s, int p, out object? value)
}
}

if (i != p)
var suffix = (uint)i < (uint)s.Length ? s[i] & ~0x20 : '\0';

//
// As in C#, digits followed by a real type suffix, such as 1d, also form a real literal.
//
if (i != p || suffix is 'F' or 'M' or 'D')
{
if ((uint)i <= (uint)s.Length)
{
var suffix = (uint)i < (uint)s.Length ? s[i] & ~0x20 : '\0';
s = s[..i];

switch (suffix)
Expand Down Expand Up @@ -272,17 +269,38 @@ private static int TryParseReal(ReadOnlySpan<char> s, int p, out object? value)
static bool IsAsciiDigit(char c) =>
(uint)c - '0' <= 9;

//
// As in C#, a value too large for the type is an error, and a value too small for it becomes zero.
//

[MethodImpl(MethodImplOptions.NoInlining)]
static object ParseSingle(ReadOnlySpan<char> s) =>
float.Parse(s, NumberStyles.Float, CultureInfo.InvariantCulture);
static object ParseSingle(ReadOnlySpan<char> s)
{
var value = float.Parse(s, NumberStyles.Float, CultureInfo.InvariantCulture);
if (float.IsInfinity(value))
Error_FloatingPointConstantOutOfRange(typeof(float));

return value;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static object ParseDouble(ReadOnlySpan<char> s) =>
double.Parse(s, NumberStyles.Float, CultureInfo.InvariantCulture);
static object ParseDouble(ReadOnlySpan<char> s)
{
var value = double.Parse(s, NumberStyles.Float, CultureInfo.InvariantCulture);
if (double.IsInfinity(value))
Error_FloatingPointConstantOutOfRange(typeof(double));

return value;
}

[MethodImpl(MethodImplOptions.NoInlining)]
static object ParseDecimal(ReadOnlySpan<char> s) =>
decimal.Parse(s, NumberStyles.Float, CultureInfo.InvariantCulture);
static object ParseDecimal(ReadOnlySpan<char> s)
{
if (!decimal.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out var value))
Error_FloatingPointConstantOutOfRange(typeof(decimal));

return value;
}
}

private static int AdjustNumericType(ReadOnlySpan<char> s, ulong number, out object value)
Expand Down Expand Up @@ -431,4 +449,12 @@ private static int AdjustNumericType(ReadOnlySpan<char> s, ulong number, out obj
value = number;
return 2;
}

[DoesNotReturn]
private static void Error_IntegralConstantTooLarge() =>
Parser.FatalError("Integral constant is too large.");

[DoesNotReturn]
private static void Error_FloatingPointConstantOutOfRange(Type type) =>
Parser.FatalError($"Floating-point constant is outside the range of type '{type}'.");
}
40 changes: 40 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,43 @@ Operator '?:' cannot be applied to operands of type 'System.Void' and 'System.Vo

(true ? "abc" : null :> object).Length
Cannot resolve symbol 'Length'.

#######################################
###### Numeric literals out of range ##
#######################################

100000000000000000000
(1:1) Integral constant is too large.

18446744073709551616
(1:1) Integral constant is too large.

100000000000000000000.ToString()
(1:1) Integral constant is too large.

0x10000000000000000
(1:1) Integral constant is too large.

0b10000000000000000000000000000000000000000000000000000000000000000
(1:1) Integral constant is too large.

1 + 0x10000000000000000
(1:5) Integral constant is too large.

1e400
(1:1) Floating-point constant is outside the range of type 'System.Double'.

1e400d
(1:1) Floating-point constant is outside the range of type 'System.Double'.

1e39f
(1:1) Floating-point constant is outside the range of type 'System.Single'.

1e100m
(1:1) Floating-point constant is outside the range of type 'System.Decimal'.

79228162514264337593543950336m
(1:1) Floating-point constant is outside the range of type 'System.Decimal'.

1 + 1e400
(1:5) Floating-point constant is outside the range of type 'System.Double'.
28 changes: 28 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,34 @@ static TestCaseData Case(string name, Func<object?> evaluate, string expected) =
public void Evaluate_NegatedUInt32_ReturnsNegatedInt64(string expression, long expected) =>
Assert.That(ExpressionParser.Evaluate(expression), Is.TypeOf<long>().And.EqualTo(expected));

[TestCaseSource(nameof(RealLiteralSource))]
public void Evaluate_RealLiteral_ReturnsValueOfLiteralType(string expression, object expected) =>
Assert.That(ExpressionParser.Evaluate(expression), Is.TypeOf(expected.GetType()).And.EqualTo(expected));

private static IEnumerable<TestCaseData> RealLiteralSource
{
get
{
//
// The integer part does not fit in ulong.
//
yield return new("100000000000000000000.0", 1e20);
yield return new("100000000000000000000e0", 1e20);
yield return new("100000000000000000000d", 1e20);
yield return new("100000000000000000000f", 1e20f);
yield return new("100000000000000000000m", 100000000000000000000m);
yield return new("79228162514264337593543950335m", decimal.MaxValue);
yield return new("18446744073709551616.5", 18446744073709551616.5);

//
// As in C#, a value too small for the type becomes zero.
//
yield return new("1e-400", 0d);
yield return new("1e-50f", 0f);
yield return new("1e-100m", 0m);
}
}

[TestCase("-'a'", -97)]
[TestCase("+'a'", 97)]
[TestCase("~'a'", -98)]
Expand Down