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
22 changes: 4 additions & 18 deletions src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,24 +312,10 @@ private static int AdjustNumericType(ReadOnlySpan<char> 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';
Expand Down
17 changes: 17 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,14 @@ 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));

[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));
Expand Down