diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs index d766052..4afc0c9 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs @@ -421,10 +421,15 @@ protected internal override Expression VisitUnary(Expr.Unary expr) switch (expr.UnaryType, operand) { - case (UnaryType.Negate, ConstantExpression { Value: 0x80000000u }): + // + // As in C#, the literals 2147483648 and 9223372036854775808 directly after a unary minus are the constants + // int.MinValue and long.MinValue. Parentheses or the U suffix keep their uint and ulong types. + // Other suffixes need no check: 2147483648 with L or UL is not a uint, and C# allows L for 9223372036854775808. + // + case (UnaryType.Negate, _) when expr.Operand is Expr.Literal { Value: 0x80000000u, HasUnsignedSuffix: false }: return Expression.Constant(int.MinValue); - case (UnaryType.Negate, ConstantExpression { Value: 0x8000000000000000ul }): + case (UnaryType.Negate, _) when expr.Operand is Expr.Literal { Value: 0x8000000000000000ul, HasUnsignedSuffix: false }: return Expression.Constant(long.MinValue); // diff --git a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs index 673f062..179f843 100644 --- a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs +++ b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs @@ -62,9 +62,7 @@ private static Parser CreateParser() ).Void(); var number_literal = - ConstantNumberParser - .NumericLiteral - .Do(Expr (v) => new Expr.Literal(v)); + ConstantNumberParser.NumericLiteral; var string_literal = Literal diff --git a/src/Ramstack.ExpressionParser/Expressions/Expr.Literal.cs b/src/Ramstack.ExpressionParser/Expressions/Expr.Literal.cs index 30423fc..bc5bdd1 100644 --- a/src/Ramstack.ExpressionParser/Expressions/Expr.Literal.cs +++ b/src/Ramstack.ExpressionParser/Expressions/Expr.Literal.cs @@ -14,6 +14,11 @@ public sealed class Literal(object? value) : Expr(ExprKind.Literal) /// public object? Value { get; } = value; + /// + /// Gets a value indicating whether the literal is an integer literal with the U or UL suffix. + /// + internal bool HasUnsignedSuffix { get; init; } + /// /// Deconstructs the literal expression into its value. /// diff --git a/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs b/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs index d5b7bc5..15d0918 100644 --- a/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs +++ b/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs @@ -7,12 +7,12 @@ namespace Ramstack.Parsing.Parsers; /// /// This parser matches only the number itself; a leading sign is parsed as a unary operator. /// -internal sealed class ConstantNumberParser : Parser +internal sealed class ConstantNumberParser : Parser { /// /// Gets the numeric literal parser. /// - public static Parser NumericLiteral { get; } = new ConstantNumberParser(); + public static Parser NumericLiteral { get; } = new ConstantNumberParser(); /// /// A lookup table for converting hexadecimal characters to their corresponding @@ -41,7 +41,7 @@ internal sealed class ConstantNumberParser : Parser ]; /// - public override bool TryParse(ref ParseContext context, [NotNullWhen(true)] out object? value) + public override bool TryParse(ref ParseContext context, [NotNullWhen(true)] out Expr? value) { var s = context.Remaining; if (s.Length == 0) @@ -60,7 +60,7 @@ public override bool TryParse(ref ParseContext context, [NotNullWhen(true)] out var p = TryParseReal(s, count, out var real); if (p != 0) { - value = real!; + value = new Expr.Literal(real); context.Advance(p); return true; } @@ -70,7 +70,8 @@ public override bool TryParse(ref ParseContext context, [NotNullWhen(true)] out } } - count += AdjustNumericType(s[count..], number, out value); + count += AdjustNumericType(s[count..], number, out var constant, out var hasUnsignedSuffix); + value = new Expr.Literal(constant) { HasUnsignedSuffix = hasUnsignedSuffix }; context.Advance(count); return true; @@ -303,7 +304,7 @@ static object ParseDecimal(ReadOnlySpan s) } } - private static int AdjustNumericType(ReadOnlySpan s, ulong number, out object value) + private static int AdjustNumericType(ReadOnlySpan s, ulong number, out object value, out bool hasUnsignedSuffix) { var hasL = false; var hasU = false; @@ -338,6 +339,8 @@ private static int AdjustNumericType(ReadOnlySpan s, ulong number, out obj } } + hasUnsignedSuffix = hasU; + // // https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#6453-integer-literals // @@ -352,7 +355,8 @@ private static int AdjustNumericType(ReadOnlySpan s, ulong number, out obj // // - // Note: The following part of the spec is not implemented here. It is implemented in the unary minus analysis. + // Note: The following part of the spec is not implemented here. It is implemented in the unary minus analysis + // (ExpressionBuilder.VisitUnary), which uses Expr.Literal.HasUnsignedSuffix to tell these literals apart. // The current parser matches only the number itself, since the unary sign (in this case, minus) is intercepted // by the parser responsible for unary operations. // diff --git a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt index 1bdffc0..95a44da 100644 --- a/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt +++ b/tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt @@ -137,6 +137,21 @@ Operator '+' cannot be applied to operands of type 'System.UInt64' and 'System.I -1ul Unary operator '-' cannot be applied to operand of type 'System.UInt64'. +-(9223372036854775808) +Unary operator '-' cannot be applied to operand of type 'System.UInt64'. + +-(0x8000000000000000) +Unary operator '-' cannot be applied to operand of type 'System.UInt64'. + +-9223372036854775808u +Unary operator '-' cannot be applied to operand of type 'System.UInt64'. + +-9223372036854775808ul +Unary operator '-' cannot be applied to operand of type 'System.UInt64'. + +-0x8000000000000000ul +Unary operator '-' cannot be applied to operand of type 'System.UInt64'. + -DayOfWeek.Friday Unary operator '-' cannot be applied to operand of type 'System.DayOfWeek'. diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs index 59c60a1..b9bb7ad 100644 --- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs +++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs @@ -28,8 +28,10 @@ private static List EvaluatesSource list.Add(["""Math.Max(1d, 15)""", 15d]); list.Add(["""sbyte.MinValue""", sbyte.MinValue]); list.Add(["""-0x80000000""", int.MinValue]); + list.Add(["""- 2147483648""", int.MinValue]); list.Add([""" 0x80000000""", 0x80000000]); list.Add(["""-0x8000000000000000""", long.MinValue]); + list.Add(["""-9223372036854775808L""", long.MinValue]); list.Add([""" 0x8000000000000000""", 0x8000000000000000L]); list.Add(["""ulong.MaxValue""", ulong.MaxValue]); list.Add(["""Guid.Empty""", Guid.Empty]); @@ -447,6 +449,11 @@ static TestCaseData Case(string name, Func evaluate, string expected) = [TestCase("-4294967295u", -4294967295L)] [TestCase("-3000000000", -3000000000L)] [TestCase("-(5 :> uint)", -5L)] + [TestCase("-(2147483648)", -2147483648L)] + [TestCase("-(0x80000000)", -2147483648L)] + [TestCase("-2147483648u", -2147483648L)] + [TestCase("-0x80000000u", -2147483648L)] + [TestCase("-(2147483648) - 1", -2147483649L)] public void Evaluate_NegatedUInt32_ReturnsNegatedInt64(string expression, long expected) => Assert.That(ExpressionParser.Evaluate(expression), Is.TypeOf().And.EqualTo(expected));