From 5cbabdba512a918fe6fff8e63df49d9c031cd110 Mon Sep 17 00:00:00 2001 From: rameel Date: Sat, 21 Nov 2026 00:34:32 +0500 Subject: [PATCH] parser: reject real type suffixes after hexadecimal and binary literals A hexadecimal or binary literal accepted the real type suffixes that C# allows only after decimal digits: 0b101d was the double 5, 0b1f a float, and 0b1m and 0x10m decimals. In a hexadecimal literal, d and f are digits, so only m was affected there. Decimal digits followed by d, f, or m are now read as real literals, so only hexadecimal and binary literals reached the suffix handling in AdjustNumericType. Remove it, so that the letter is left after the literal and reported as a syntax error at its position, as in C#. --- .../Parsers/ConstantNumberParser.cs | 22 ++++--------------- .../Data/Errors.txt | 17 ++++++++++++++ .../ExpressionParserTest.cs | 8 +++++++ 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs b/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs index cb2da72..d5b7bc5 100644 --- a/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs +++ b/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs @@ -312,24 +312,10 @@ private static int AdjustNumericType(ReadOnlySpan s, ulong number, out obj { var c0 = s[0] & ~0x20; - if (c0 == 'D') - { - value = (double)number; - return 1; - } - - if (c0 == 'F') - { - value = (float)number; - return 1; - } - - if (c0 == 'M') - { - value = new decimal(number); - return 1; - } - + // + // As in C#, the real type suffixes D, F, and M follow only decimal digits, which TryParseReal reads. + // After a hexadecimal or binary literal, such a letter is not part of the literal. + // if (c0 is 'L' or 'U') { var c1 = s.Length > 1 ? s[1] & ~0x20 : '\0'; diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt index ce69d5f..d4852ae 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt @@ -314,3 +314,20 @@ Cannot resolve symbol 'Length'. 1 + 1e400 (1:5) Floating-point constant is outside the range of type 'System.Double'. + +####################################### +# The real type suffixes d, f, and m follow +# only decimal digits. +####################################### + +0b101d +(1:6) Expected '(', '[', '.', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input + +0b1f +(1:4) Expected '(', '[', '.', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input + +0b1M +(1:4) Expected '(', '[', '.', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input + +0x10m + 1 +(1:5) Expected '(', '[', '.', ':>', [%*/], [+-], '<<', '>>', '>>>', '<', '>', '<=', '>=', '==', '!=', '&', '^', '|', '&&', '||', '??', '?', or end of input diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index 21e3302..441425a 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -311,6 +311,14 @@ 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)); + [TestCase("0x1d", 29)] + [TestCase("0x1F", 31)] + [TestCase("0b1L", 1L)] + [TestCase("0b1u", 1u)] + [TestCase("0xFFul", 255ul)] + public void Evaluate_HexadecimalOrBinaryLiteral_ReturnsInteger(string expression, object expected) => + Assert.That(ExpressionParser.Evaluate(expression), Is.TypeOf(expected.GetType()).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));