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
9 changes: 7 additions & 2 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

//
Expand Down
4 changes: 1 addition & 3 deletions src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ private static Parser<Expr> CreateParser()
).Void();

var number_literal =
ConstantNumberParser
.NumericLiteral
.Do(Expr (v) => new Expr.Literal(v));
ConstantNumberParser.NumericLiteral;

var string_literal =
Literal
Expand Down
5 changes: 5 additions & 0 deletions src/Ramstack.ExpressionParser/Expressions/Expr.Literal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public sealed class Literal(object? value) : Expr(ExprKind.Literal)
/// </summary>
public object? Value { get; } = value;

/// <summary>
/// Gets a value indicating whether the literal is an integer literal with the <c>U</c> or <c>UL</c> suffix.
/// </summary>
internal bool HasUnsignedSuffix { get; init; }

/// <summary>
/// Deconstructs the literal expression into its value.
/// </summary>
Expand Down
18 changes: 11 additions & 7 deletions src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace Ramstack.Parsing.Parsers;
/// <remarks>
/// This parser matches only the number itself; a leading sign is parsed as a unary operator.
/// </remarks>
internal sealed class ConstantNumberParser : Parser<object>
internal sealed class ConstantNumberParser : Parser<Expr>
{
/// <summary>
/// Gets the numeric literal parser.
/// </summary>
public static Parser<object> NumericLiteral { get; } = new ConstantNumberParser();
public static Parser<Expr> NumericLiteral { get; } = new ConstantNumberParser();

/// <summary>
/// A lookup table for converting hexadecimal characters to their corresponding
Expand Down Expand Up @@ -41,7 +41,7 @@ internal sealed class ConstantNumberParser : Parser<object>
];

/// <inheritdoc />
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)
Expand All @@ -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;
}
Expand All @@ -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;

Expand Down Expand Up @@ -303,7 +304,7 @@ static object ParseDecimal(ReadOnlySpan<char> s)
}
}

private static int AdjustNumericType(ReadOnlySpan<char> s, ulong number, out object value)
private static int AdjustNumericType(ReadOnlySpan<char> s, ulong number, out object value, out bool hasUnsignedSuffix)
{
var hasL = false;
var hasU = false;
Expand Down Expand Up @@ -338,6 +339,8 @@ private static int AdjustNumericType(ReadOnlySpan<char> s, ulong number, out obj
}
}

hasUnsignedSuffix = hasU;

//
// https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/lexical-structure#6453-integer-literals
//
Expand All @@ -352,7 +355,8 @@ private static int AdjustNumericType(ReadOnlySpan<char> 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.
//
Expand Down
15 changes: 15 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/Data/Errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'.

Expand Down
7 changes: 7 additions & 0 deletions tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ private static List<object[]> 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]);
Expand Down Expand Up @@ -447,6 +449,11 @@ static TestCaseData Case(string name, Func<object?> 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<long>().And.EqualTo(expected));

Expand Down