From 4fb61138ee7868dc1e1e6b982e43ef819f92a05c Mon Sep 17 00:00:00 2001 From: rameel Date: Sat, 21 Nov 2026 00:15:21 +0500 Subject: [PATCH] parser: read large real literals and report out-of-range literals A real literal whose integer part did not fit in ulong was not recognized at all: 100000000000000000000.0, 100000000000000000000d, and even 79228162514264337593543950335m (decimal.MaxValue) failed with "Expected [!+\-~], ...". The integer scanner gave up on overflow before anything could see the fraction, exponent, or suffix after the digits. Read the digits to the end and decide afterwards whether the literal is real. As in C#, digits followed by d, f, or m form a real literal. Report literals out of range as C# does, at the position of the literal: - an integer too large for ulong, in any base, with "Integral constant is too large." Decimal digits failed with "Expected ...", and hexadecimal and binary ones were read as 0 followed by an unexpected x or b; - a real too large for its type, with "Floating-point constant is outside the range of type 'System.Double'." and the like. 1e400 and 1e39f evaluated to infinity, which double.PositiveInfinity and float.PositiveInfinity still give, and 1e100m put an OverflowException with its stack trace into the error message. A value too small for its type still becomes zero, as in C#. --- .../Parsers/ConstantNumberParser.cs | 110 +++++++++++------- .../Data/Errors.txt | 40 +++++++ .../ExpressionParserTest.cs | 28 +++++ 3 files changed, 136 insertions(+), 42 deletions(-) diff --git a/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs b/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs index b9d6e32..cb2da72 100644 --- a/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs +++ b/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs @@ -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; @@ -64,6 +64,9 @@ public override bool TryParse(ref ParseContext context, [NotNullWhen(true)] out context.Advance(p); return true; } + + if (overflow) + Error_IntegralConstantTooLarge(); } } @@ -76,43 +79,33 @@ public override bool TryParse(ref ParseContext context, [NotNullWhen(true)] out return false; } - private static int TryParseInteger(ReadOnlySpan s, out ulong value) + private static int TryParseInteger(ReadOnlySpan 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 s, out ulong value) @@ -141,7 +134,7 @@ private static int TryParseHexInteger(ReadOnlySpan s, out ulong value) // Check for overflow // if (r > ulong.MaxValue / 16) - goto FAIL; + Error_IntegralConstantTooLarge(); r = r * 16 + d; } @@ -179,7 +172,7 @@ private static int TryParseBinInteger(ReadOnlySpan s, out ulong value) // Check for overflow // if (r > ulong.MaxValue >> 1) - goto FAIL; + Error_IntegralConstantTooLarge(); r = (r << 1) + d; } @@ -236,11 +229,15 @@ private static int TryParseReal(ReadOnlySpan 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) @@ -272,17 +269,38 @@ private static int TryParseReal(ReadOnlySpan 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 s) => - float.Parse(s, NumberStyles.Float, CultureInfo.InvariantCulture); + static object ParseSingle(ReadOnlySpan 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 s) => - double.Parse(s, NumberStyles.Float, CultureInfo.InvariantCulture); + static object ParseDouble(ReadOnlySpan 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 s) => - decimal.Parse(s, NumberStyles.Float, CultureInfo.InvariantCulture); + static object ParseDecimal(ReadOnlySpan s) + { + if (!decimal.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out var value)) + Error_FloatingPointConstantOutOfRange(typeof(decimal)); + + return value; + } } private static int AdjustNumericType(ReadOnlySpan s, ulong number, out object value) @@ -431,4 +449,12 @@ private static int AdjustNumericType(ReadOnlySpan 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}'."); } diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt index fd43971..ce69d5f 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt @@ -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'. diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index 27ec403..21e3302 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -311,6 +311,34 @@ static TestCaseData Case(string name, Func evaluate, string expected) = public void Evaluate_NegatedUInt32_ReturnsNegatedInt64(string expression, long expected) => Assert.That(ExpressionParser.Evaluate(expression), Is.TypeOf().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 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)]