diff --git a/src/Ramstack.ExpressionParser/DefaultBinder.cs b/src/Ramstack.ExpressionParser/DefaultBinder.cs
index 5a6ec85..f833f9b 100644
--- a/src/Ramstack.ExpressionParser/DefaultBinder.cs
+++ b/src/Ramstack.ExpressionParser/DefaultBinder.cs
@@ -161,11 +161,9 @@ public void RegisterType(Type type, bool importAsStatic = false)
&& m.MemberType is MemberTypes.Field or MemberTypes.Property)
.ToArray();
- //
// As in C#, a field or property of a derived type hides matching members of its
// base types. For static imports, keep matches from each imported type so that
// a name found in two imports is ambiguous, as with using static in C#.
- //
if (type is not null && candidates.Length > 1)
candidates = candidates
.Where(m => !candidates.Any(c => m.DeclaringType!.IsBaseTypeOf(c.DeclaringType!)))
diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
index e51d946..8864306 100644
--- a/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
+++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.Helpers.cs
@@ -65,10 +65,8 @@ private object ResolveSymbol(Expr.Reference expr)
///
private static MemberExpression CreateMemberAccess(Expression? expression, MemberInfo member)
{
- //
// As in C#, a property is read through its get accessor, so a property whose get accessor
// is missing or not public cannot be read, even if its set accessor is public.
- //
if (member is PropertyInfo property)
{
var getter = property.GetMethod;
@@ -151,9 +149,7 @@ private static Type ApplyConditionalConversions(Expr.Conditional expr, ref Expre
isFalseNull ? null : ifFalse.Type);
}
- //
// A branch of type void has no value, so the conditional expression would have none either.
- //
if (ifTrue.Type == typeof(void) || ifFalse.Type == typeof(void))
Error.NonApplicableConditionalOperator(ifTrue.Type, ifFalse.Type);
@@ -227,10 +223,8 @@ private static BinaryExpression ApplyNullCoalescing(Identifier op, Expression lh
if (underlyingType is not null)
{
- //
// A non-null value of a is unwrapped and converted to B. Convert a to the nullable form of B instead,
// which keeps its null value. For a reference type B, the conversion is a boxing conversion.
- //
converted = rhsType.IsValueType
? ApplyImplicitConversion(lhs, rhsType.IsNullable() ? rhsType : rhsType.MakeNullableType())
: rhsType.IsAssignableFrom(underlyingType)
@@ -290,11 +284,9 @@ private static Expr SkipParentheses(Expr expr)
/// The expression of the right operand.
private static void ApplyNullLiteralConversion(Expr.Binary expr, ref Expression lhs, ref Expression rhs)
{
- //
// The null literal converts to a nullable value type, so opposite an operand of such a type
// it takes that type, as in 'x == null' or 'x ?? null'. For an enum type E, only E + U and U + E
// are defined, so in 'x + null' it takes the nullable form of the underlying type U instead.
- //
if (lhs.Type.IsNullable() && IsNullLiteral(expr.Right))
rhs = Expression.Constant(null, GetNullLiteralType(expr.Operator, lhs.Type));
else if (rhs.Type.IsNullable() && IsNullLiteral(expr.Left))
@@ -334,18 +326,14 @@ private static void ApplyEnumZeroConversion(Expr.Binary expr, ref Expression lhs
if (lhsType.IsEnum && IsZeroLiteral(expr.Right))
{
- //
// Both E - E and E - U accept 'x - 0'. As in the C# compiler, E - U is selected and returns E
// if the literal is of the underlying type U; otherwise, E - E is selected and returns U, as in 'x - 0L'.
- //
if (expr.Operator.Name != "-" || rhsType != lhsType.GetEnumUnderlyingType())
rhs = Expression.Convert(rhs, lhsType);
}
else if (rhsType.IsEnum && IsZeroLiteral(expr.Left))
{
- //
// As in the C# compiler, E - E is selected for '0 - x' over U - E, so the result is of the underlying type.
- //
lhs = Expression.Convert(lhs, rhsType);
}
}
@@ -360,10 +348,8 @@ private static void ApplyEnumZeroConversion(Expr.Binary expr, ref Expression lhs
///
private static Expression? ApplyBranchConversion(Expression expression, Type conversionType)
{
- //
// ApplyImplicitConversion does not box values, so handle the boxing conversion here,
// for example of a number to a branch explicitly converted to object.
- //
if (expression.Type.IsValueType
&& !conversionType.IsValueType
&& conversionType.IsAssignableFrom(expression.Type))
@@ -431,10 +417,8 @@ private static List CreateInjectingArguments(MethodInfo method, IRea
list.Add(Convert(arg, parameter.ParameterType));
}
- //
// As in C#, a parameter array without arguments gets an empty array,
// and an omitted optional parameter gets its default value.
- //
for (var i = list.Count; i < parameters.Length; i++)
{
var parameter = parameters[i];
@@ -449,10 +433,8 @@ private static List CreateInjectingArguments(MethodInfo method, IRea
}
else
{
- //
// A required parameter is left without an argument only for a method that does not
// accept these arguments, so let Expression.Call report the argument count.
- //
break;
}
}
@@ -464,26 +446,20 @@ static Expression CreateDefaultArgument(ParameterInfo parameter)
var type = parameter.ParameterType;
var value = parameter.DefaultValue;
- //
// A parameter declared with [Optional] and without a default value gets Type.Missing
// if its type is object, and the default value of its type otherwise, as in C#.
- //
if (value is Missing)
return type == typeof(object)
? Expression.Constant(value, type)
: Expression.Default(type);
- //
// The default value of a struct, as in 'Guid guid = default', is also stored as null.
- //
if (value is null)
return type.IsValueType && !type.IsNullable()
? Expression.Default(type)
: Expression.Constant(null, type);
- //
// The default value of a nullable enum is stored as its underlying integer.
- //
var underlyingType = Nullable.GetUnderlyingType(type) ?? type;
if (underlyingType.IsEnum && value.GetType() != underlyingType)
value = Enum.ToObject(underlyingType, value);
@@ -530,10 +506,8 @@ private static Expression ApplyBinaryExpression(Identifier op, Func apply, Expression lhs, Expression rhs)
{
- //
// The predefined shift operators take an int, uint, long, or ulong value and an int count,
// so an operand of a smaller integral type, including char, is converted to int, as in C#.
- //
var lhsType = lhs.Type;
var rhsType = rhs.Type;
var lifted = lhsType.IsNullable() || rhsType.IsNullable();
@@ -743,10 +713,8 @@ private static void ApplyBinaryNumericPromotions(Identifier op, ref Expression l
if (x == typeof(long) || y == typeof(long))
return typeof(long);
- //
// Neither operand is of type long here, so IsSignedInteger matches only sbyte, short, or int,
// as the uint rule requires.
- //
if (x == typeof(uint) || y == typeof(uint))
return IsSignedInteger(x) || IsSignedInteger(y) ? typeof(long) : typeof(uint);
diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs
index c825a28..c569607 100644
--- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs
+++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs
@@ -138,10 +138,8 @@ void Flatten(Expression e)
}
else
{
- //
// The only argument is not an array of operands created for the expanded form,
// as in string.Concat(value) or string.Concat(array), so the call itself is the operand.
- //
arguments.Add(e);
argumentTypes.Add(e.Type);
}
@@ -223,10 +221,8 @@ Expression ProcessIndex()
if (method is null)
Error.AmbiguousMatch(indexers);
- //
// As in C#, an indexer whose get accessor is not public takes part in overload resolution,
// but cannot be read.
- //
if (!method.IsPublic)
Error.InaccessibleGetAccessor(indexers[methods.IndexOf(method)]);
@@ -260,10 +256,8 @@ Expression Invoke(Expression? instance, MethodInfo? method)
if (instance is not null)
{
- //
// Without a method, the instance is a delegate to invoke. A method can also be called
// on a delegate, as in 'func.Invoke(1)' or 'func.ToString()'.
- //
Debug.Assert(method is not null || typeof(Delegate).IsAssignableFrom(instance.Type));
if (method is null)
@@ -296,9 +290,7 @@ Expression ProcessMemberAccess(Expr.MemberAccess e)
if (!typeof(Delegate).IsAssignableFrom(memberInfo.GetMemberType()))
Error.NonInvocableMember(member);
- //
// A member accessed through a type is static, so it is accessed without an instance.
- //
return Invoke(CreateMemberAccess(instance, memberInfo), null);
}
@@ -357,19 +349,15 @@ protected internal override Expression VisitUnary(Expr.Unary expr)
{
var operand = Visit(expr.Operand);
- //
// For an operand of a nullable value type, the operators below are lifted:
// they apply to the underlying type and return the nullable form of their result type.
- //
var operandType = operand.Type;
var nonNullableType = operandType.GetNonNullableType();
var lifted = operandType.IsNullable();
- //
// C# does not define the unary '-' and '+' operators for an enum, and '~' applies to its underlying type.
// A conversion takes the enum itself, so that converting it to object boxes the enum
// rather than its underlying value.
- //
if (nonNullableType.IsEnum && expr.UnaryType != UnaryType.Convert)
{
if (expr.UnaryType is UnaryType.Negate or UnaryType.UnaryPlus)
@@ -380,21 +368,17 @@ protected internal override Expression VisitUnary(Expr.Unary expr)
switch (expr.UnaryType)
{
- //
// 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 when expr.Operand is Expr.Literal { Value: 0x8000000000000000ul, HasUnsignedSuffix: false }:
return Expression.Constant(long.MinValue);
- //
// C# has no negation operator for uint, so the operand is converted to long
// and then negated, which keeps the sign of the result.
- //
case UnaryType.Negate when operand.Type.GetNonNullableType() == typeof(uint):
operand = ConvertOperand(operand, typeof(long), lifted);
break;
@@ -409,12 +393,10 @@ protected internal override Expression VisitUnary(Expr.Unary expr)
switch (expr.UnaryType)
{
case UnaryType.Not:
- //
// Expression.Not performs a bitwise complement on integers and falls back to op_OnesComplement
// when a type does not define op_LogicalNot, so '!' would silently work as '~'.
// Allow only the logical negation, as C# does. For a nullable operand,
// Expression.Not lifts the negation of the underlying type.
- //
if (nonNullableType == typeof(bool))
return Expression.Not(operand);
diff --git a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs
index 901dc07..1f32222 100644
--- a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs
+++ b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs
@@ -55,11 +55,9 @@ private static Parser CreateParser()
identifier_body.Many()
).Map(m => new Identifier(m.ToString()));
- //
// As in C#, the '@' prefix makes a verbatim identifier, which can be a keyword, as in 'x.@null'.
// The prefix is not part of the name. The keyword check does not match the prefix, so a verbatim
// identifier passes Not(keyword) wherever an identifier is expected.
- //
var identifier =
L('@').Optional()
.Then(identifier_name)
@@ -134,10 +132,8 @@ private static Parser CreateParser()
.Separated(
Seq(L(','), S)).As("expression_list");
- //
// Member access, calls, and indexers share one precedence level, so each of them
// is parsed as a postfix operator that wraps the expression to its left.
- //
var call =
expression_list
.Between(
@@ -152,12 +148,10 @@ private static Parser CreateParser()
Seq(L(']'), S))
.Do(Func (parameters) => expr => new Expr.Indexer(expr, parameters));
- //
// Not(keyword) reports no expected token when it fails, so the member name is labeled
// to report "Expected identifier" for a keyword after the dot. Unlike Fail, a labeled parser
// fails without stopping the parse, so Subparser still stops before a dot that an enclosing
// grammar uses, as in 'a..b'.
- //
var member_access =
Not(keyword).Then(identifier).As("identifier")
.Between(
@@ -181,10 +175,8 @@ private static Parser CreateParser()
return expr;
});
- //
// The conversion operator is ':>' rather than ':', which would conflict with the conditional
// operator: in 'flag ? 0 : count', ': count' would be taken for a conversion to the type 'count'.
- //
var cast_expression =
Seq(
postfix_expression,
diff --git a/src/Ramstack.ExpressionParser/Internal/MethodResolver.cs b/src/Ramstack.ExpressionParser/Internal/MethodResolver.cs
index 11cd8c2..21b0c19 100644
--- a/src/Ramstack.ExpressionParser/Internal/MethodResolver.cs
+++ b/src/Ramstack.ExpressionParser/Internal/MethodResolver.cs
@@ -92,26 +92,25 @@ internal static class MethodResolver
// Excess provided parameters
// Test for the ParamArray case
- var lastParameterIndex = parameters.Length - 1;
+ var lastParameter = parameters[^1];
+ var lastParameterType = lastParameter.ParameterType;
- if (!parameters[lastParameterIndex].ParameterType.IsArray)
+ if (!lastParameterType.IsArray
+ || !lastParameter.IsDefined(typeof(ParamArrayAttribute), true))
continue;
- if (!parameters[lastParameterIndex].IsDefined(typeof(ParamArrayAttribute), true))
- continue;
-
- paramArrayType = parameters[lastParameterIndex].ParameterType.GetElementType();
+ paramArrayType = lastParameterType.GetElementType();
}
else
{
// Test for paramArray, save paramArray type
- var lastParameterIndex = parameters.Length - 1;
- var lastParameterType = parameters[lastParameterIndex].ParameterType;
+ var lastParameter = parameters[^1];
+ var lastParameterType = lastParameter.ParameterType;
- if (lastParameterType.IsArray)
- if (parameters[lastParameterIndex].IsDefined(typeof(ParamArrayAttribute), true))
- if (!lastParameterType.IsAssignableFrom(argumentTypes[lastParameterIndex]))
- paramArrayType = lastParameterType.GetElementType();
+ if (lastParameterType.IsArray
+ && lastParameter.IsDefined(typeof(ParamArrayAttribute), true)
+ && !lastParameterType.IsAssignableFrom(argumentTypes[^1]))
+ paramArrayType = lastParameterType.GetElementType();
}
var argumentsToCheck = paramArrayType is null ? argumentTypes.Length : parameters.Length - 1;
@@ -206,12 +205,10 @@ internal static class MethodResolver
currentIndex = RemoveMethodsOfBaseTypes(candidates, parameterArrayTypes, currentIndex);
- //
// As in C#, the method to invoke must be more specific than every other candidate.
// Such a method wins each comparison, so the walk below always ends on it. A candidate
// that ties with the current winner does not stop the walk, since a later candidate
// can be more specific than both.
- //
var min = 0;
var ambiguous = false;
@@ -238,10 +235,8 @@ internal static class MethodResolver
if (ambiguous)
return null;
- //
// The candidates before the winner may not have been compared with it,
// so check that the winner is also more specific than each of them.
- //
for (var i = 0; i < min; i++)
{
var result = FindMostSpecificMethod(
@@ -269,10 +264,8 @@ internal static class MethodResolver
///
private static int RemoveMethodsOfBaseTypes(MethodBase[] candidates, Type?[] parameterArrayTypes, int count)
{
- //
// As in C#, only the methods of the most derived types remain, even if a removed method
// matches the arguments better.
- //
var declaringTypes = new Type[count];
for (var i = 0; i < count; i++)
declaringTypes[i] = GetIntroducingType(candidates[i]);
@@ -301,10 +294,8 @@ static bool IsHidden(Type type, Type[] declaringTypes)
static bool HidesMethodsOf(Type other, Type type)
{
- //
// As in C#, a method of a class other than Object also removes the methods declared
// in interfaces, although an interface is not a base type of a class.
- //
return type.IsBaseTypeOf(other)
|| type.IsInterface && other.IsClass && other != typeof(object);
}
@@ -324,10 +315,8 @@ private static Type GetIntroducingType(MethodBase method)
{
var definition = mi.GetBaseDefinition();
- //
// A covariant return override occupies a new slot, so GetBaseDefinition returns
// the override itself. The compiler marks such an override with PreserveBaseOverridesAttribute.
- //
if (definition.IsDefined(typeof(PreserveBaseOverridesAttribute), inherit: false)
&& FindOverriddenMethod(definition) is { } overridden)
return GetIntroducingType(overridden);
@@ -340,9 +329,7 @@ private static Type GetIntroducingType(MethodBase method)
static MethodInfo? FindOverriddenMethod(MethodInfo method)
{
- //
// An override overrides the nearest virtual method with the same parameter types.
- //
var parameters = method.GetParameters();
for (var type = method.DeclaringType!.BaseType; type is not null; type = type.BaseType)
@@ -417,17 +404,13 @@ private static int FindMostSpecific(
var usesDefaults1 = UsesDefaultArguments(p1, paramArrayType1, types.Length);
var usesDefaults2 = UsesDefaultArguments(p2, paramArrayType2, types.Length);
- //
// As in C#, a method whose parameters all get arguments is better than a method
// that needs default arguments, and methods that both need them are ambiguous.
- //
if (usesDefaults1 != usesDefaults2)
return usesDefaults1 ? 2 : 1;
- //
// Methods without default arguments differ in length only in their expanded form,
// and the one with more declared parameters is better.
- //
if (!usesDefaults1)
{
if (p1.Length > p2.Length)
@@ -445,9 +428,7 @@ private static int FindMostSpecific(
static bool UsesDefaultArguments(ParameterInfo[] parameters, Type? paramArrayType, int argumentCount)
{
- //
// In the expanded form, the parameter array takes the remaining arguments, possibly none.
- //
var count = paramArrayType is not null
? parameters.Length - 1
: parameters.Length;
@@ -532,11 +513,9 @@ private static int FindMostSpecificMethod(
if (result != 0)
return result;
- //
// Reflection lists a covariant return override along with the method it overrides.
// Both belong to the type that introduced the method, so neither removes the other,
// and the override is chosen for its more specific return type.
- //
if (HasSameParameterTypes(p1, p2))
{
Debug.Assert(m1.DeclaringType is not null);
diff --git a/src/Ramstack.ExpressionParser/Internal/TypeUtils.cs b/src/Ramstack.ExpressionParser/Internal/TypeUtils.cs
index 0762217..a81f9f2 100644
--- a/src/Ramstack.ExpressionParser/Internal/TypeUtils.cs
+++ b/src/Ramstack.ExpressionParser/Internal/TypeUtils.cs
@@ -124,10 +124,8 @@ public static bool IsInteger(Type type)
///
public static bool CanConvertPrimitive(Type source, Type target)
{
- //
// Type.GetTypeCode returns the code of the underlying type for an enum,
// so without this check a number would convert to any enum and back.
- //
if (source.IsEnum || target.IsEnum)
return false;
diff --git a/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs b/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs
index 15d0918..1ae220d 100644
--- a/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs
+++ b/src/Ramstack.ExpressionParser/Parsers/ConstantNumberParser.cs
@@ -93,10 +93,8 @@ private static int TryParseInteger(ReadOnlySpan s, out ulong value, out bo
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;
@@ -131,9 +129,7 @@ private static int TryParseHexInteger(ReadOnlySpan s, out ulong value)
break;
}
- //
// Check for overflow
- //
if (r > ulong.MaxValue / 16)
Error_IntegralConstantTooLarge();
@@ -169,9 +165,7 @@ private static int TryParseBinInteger(ReadOnlySpan s, out ulong value)
break;
}
- //
// Check for overflow
- //
if (r > ulong.MaxValue >> 1)
Error_IntegralConstantTooLarge();
@@ -189,9 +183,7 @@ private static int TryParseBinInteger(ReadOnlySpan s, out ulong value)
private static int TryParseReal(ReadOnlySpan s, int p, out object? value)
{
- //
// Float number pattern: \d+(\.\d+)?([Ee][-+]?\d+)?
- //
var i = p;
@@ -232,9 +224,7 @@ private static int TryParseReal(ReadOnlySpan s, int p, out object? value)
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)
@@ -270,9 +260,7 @@ 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)
@@ -313,10 +301,8 @@ private static int AdjustNumericType(ReadOnlySpan s, ulong number, out obj
{
var c0 = s[0] & ~0x20;
- //
// 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';
@@ -371,10 +357,8 @@ private static int AdjustNumericType(ReadOnlySpan s, ulong number, out obj
// In all other situations, such an integer-literal is of type ulong.
//
- //
// If the literal has no suffix, it has the first of these types
// in which its value can be represented: int, uint, long, ulong.
- //
if (!hasL && !hasU)
{
switch (number)
@@ -399,10 +383,8 @@ private static int AdjustNumericType(ReadOnlySpan s, ulong number, out obj
return 0;
}
- //
// If the literal is suffixed by U or u, it has the first of these types
// in which its value can be represented: uint, ulong.
- //
if (hasU && !hasL)
{
if (number <= uint.MaxValue)
@@ -413,10 +395,8 @@ private static int AdjustNumericType(ReadOnlySpan s, ulong number, out obj
return 1;
}
- //
// If the literal is suffixed by L or l, it has the first of these types
// in which its value can be represented: long, ulong.
- //
if (hasL && !hasU)
{
switch (number)
@@ -433,9 +413,7 @@ private static int AdjustNumericType(ReadOnlySpan s, ulong number, out obj
return 1;
}
- //
// If the literal is suffixed by UL, Ul, uL, ul, LU, Lu, lU, or lu, it is of type ulong.
- //
value = number;
return 2;
}
diff --git a/src/Ramstack.ExpressionParser/Parsers/StackGuardParser.cs b/src/Ramstack.ExpressionParser/Parsers/StackGuardParser.cs
index 7375218..68dab11 100644
--- a/src/Ramstack.ExpressionParser/Parsers/StackGuardParser.cs
+++ b/src/Ramstack.ExpressionParser/Parsers/StackGuardParser.cs
@@ -21,7 +21,5 @@ public override bool TryParse(ref ParseContext context, [NotNullWhen(true)] out
return parser.TryParse(ref context, out value);
}
- //
// NOTE: The default void wrapper calls TryParse, so the check also runs when the value is discarded.
- //
}
diff --git a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
index f4f622a..eee2f3d 100644
--- a/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
+++ b/tests/Ramstack.ExpressionParser.Tests/ExpressionParserTest.cs
@@ -180,9 +180,7 @@ public void EvaluateExpression(string expression, object result) =>
[Test]
public void Evaluate_DateTimeToday_ReturnsCurrentDate()
{
- //
// The date can change during evaluation, so the result is compared with the dates before and after it.
- //
var before = DateTime.Today;
var result = ExpressionParser.Evaluate("DateTime.Today");
var after = DateTime.Today;
@@ -473,9 +471,7 @@ 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);
@@ -484,9 +480,7 @@ private static IEnumerable RealLiteralSource
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);
@@ -884,9 +878,7 @@ public void ToString_VerbatimIdentifier_PrefixesOnlyKeywords(string source, stri
[Test]
public void MaxDepthTest()
{
- //
// The literal is the first level, and each operator adds one more level.
- //
Assert.That(ExpressionParser.Parse(new string('-', 255) + "1").Success, Is.True);
Assert.That(ExpressionParser.Parse(new string('-', 256) + "1").ToString(),
Is.EqualTo("(1:258) The expression exceeds the maximum depth of 256."));
@@ -916,10 +908,8 @@ public void DeepNestingTest(string expression, int stackSize)
[TestCaseSource(nameof(StackBudgetSource))]
public void StackBudgetTest(string expression)
{
- //
// The default stack of a thread on Windows is 1 MB, so the expressions at the maximum depth
// and moderately nested parentheses must be parsed and built on such a stack.
- //
var result = RunOnThread(1024 * 1024, () => ExpressionParser.Parse(expression, new DefaultBinder()));
Assert.That(result.Success, Is.True, result.ErrorMessage);
}
@@ -927,9 +917,7 @@ public void StackBudgetTest(string expression)
[Test]
public void DeepTreeBuildTest()
{
- //
// The parser does not produce such a tree, but the tree can be created directly.
- //
Expr expr = new Expr.Literal(1);
for (var i = 0; i < 100_000; i++)
expr = new Expr.Unary(new Identifier("-"), UnaryType.Negate, expr);
@@ -981,9 +969,7 @@ private static IEnumerable BuiltOperandSource
{
get
{
- //
// The parser wraps such operands in Expr.Parenthesized, but a tree can be built without it.
- //
var conditional = new Expr.Conditional(Reference("a"), Reference("b"), Reference("c"));
var conversion = new Expr.Unary(new Identifier("object"), UnaryType.Convert, Reference("x"));
@@ -1018,9 +1004,7 @@ private static void AssertResult(TContext context, string expression,
Assert.That(result.Success, Is.True, result.ErrorMessage);
Assert.That(result.Value!.Type, Is.EqualTo(type));
- //
// Boxing loses the nullable type, so the type of the value is checked separately from the type of the expression.
- //
var body = Expression.Convert(result.Value, typeof(object));
var value = Expression.Lambda>(body, parameter).Compile()(context);
@@ -1109,10 +1093,8 @@ public interface IBaseInterface
string Pick(int value);
string Describe();
- //
// Declaring ToString in the interface makes the call 'Derived.ToString()' choose between
// this method and Object.ToString, which have the same signature.
- //
string ToString();
static string Create() => "IBaseInterface.Create";
@@ -1132,10 +1114,8 @@ public interface IConsumer
string Pick(T value);
}
- //
// IConsumer is assignable from IConsumer