From e8489fdb0c861adaf8cf65555969337a4fc616b4 Mon Sep 17 00:00:00 2001 From: rameel Date: Thu, 19 Nov 2026 15:41:48 +0500 Subject: [PATCH] docs: describe the binder contract and how errors are reported The XML comments left out how the expression builder uses a binder and how parsing and evaluation report errors: - Binder did not explain Context, or that an unqualified name is bound with a null type and isStatic set to true, in which case the binder can return an instance member of the context. - ContextAwareBinder and DefaultBinder did not say what they resolve: the members of the context, a predefined set of types, and the registered types, matched without regard to case. RegisterType did not say that a type is registered under its simple name, and the description of importAsStatic read as if instance members became static. - Parse did not say that its result describes the error, Evaluate did not mention the ArgumentException it throws for an invalid expression, and ExpressionBuilder.Visit mentioned ParseErrorException only for a stack that is too small. - ParseErrorException was described as a parsing error only, although it also reports errors found while building the expression tree. It also explains that Evaluate with an object context uses its runtime type, how Subparser differs from Parser, that Identifier holds operators as well as names, that MemberAccess covers members of types and methods in calls, that Unary covers conversions, and that a literal can be a character or null. Co-Authored-By: Claude Opus 5.5 --- src/Ramstack.ExpressionParser/Binder.cs | 25 ++++++++++++++++--- .../ContextAwareBinder.cs | 13 +++++++--- .../DefaultBinder.cs | 16 +++++++++--- .../ExpressionBuilder.cs | 9 ++++--- .../ExpressionParser.Parser.cs | 4 ++- .../ExpressionParser.cs | 19 +++++++++++--- .../Expressions/Expr.Literal.cs | 5 ++-- .../Expressions/ExprKind.cs | 6 +++-- .../Expressions/Identifier.cs | 5 ++-- .../ParseErrorException.cs | 2 +- 10 files changed, 79 insertions(+), 25 deletions(-) diff --git a/src/Ramstack.ExpressionParser/Binder.cs b/src/Ramstack.ExpressionParser/Binder.cs index eb49e26..44001dc 100644 --- a/src/Ramstack.ExpressionParser/Binder.cs +++ b/src/Ramstack.ExpressionParser/Binder.cs @@ -9,8 +9,15 @@ namespace Ramstack.Parsing; public abstract class Binder { /// - /// Gets the expression context associated with this binder instance. + /// Gets the expression whose instance members can be referenced in an expression without qualification. /// + /// + /// The context expression, or if the binder has no context. + /// + /// + /// When or returns an instance member + /// for an unqualified name, the member is accessed through this expression. + /// public virtual Expression? Context => null; /// @@ -26,7 +33,8 @@ public abstract class Binder /// /// Attempts to bind to a method based on the specified type, method name, and parameter types. /// - /// The from which to select the method. + /// The from which to select the method, + /// or for an unqualified method name. /// The representing the name of the method to bind. /// An array of objects representing /// the types of arguments passed to the method. @@ -35,17 +43,28 @@ public abstract class Binder /// /// A representing the matching method if found; otherwise, . /// + /// + /// For an unqualified method name, is and + /// is . The binder can then return a static method of a type whose members are available + /// without qualification, or an instance method of the type of . + /// public abstract MethodInfo? BindToMethod(Type? type, Identifier methodName, Type[] parameterTypes, bool isStatic); /// /// Attempts to bind to a member (field or property) based on the specified type and member name. /// - /// The from which to select the member. + /// The from which to select the member, + /// or for an unqualified member name. /// The representing the name of the member to bind. /// A indicating whether to match only static members /// () or instance members (). /// /// A representing the matching member if found; otherwise, . /// + /// + /// For an unqualified member name, is and + /// is . The binder can then return a static member of a type whose members are available + /// without qualification, or an instance member of the type of . + /// public abstract MemberInfo? BindToMember(Type? type, Identifier memberName, bool isStatic); } diff --git a/src/Ramstack.ExpressionParser/ContextAwareBinder.cs b/src/Ramstack.ExpressionParser/ContextAwareBinder.cs index 6edc1f6..d0e076f 100644 --- a/src/Ramstack.ExpressionParser/ContextAwareBinder.cs +++ b/src/Ramstack.ExpressionParser/ContextAwareBinder.cs @@ -4,8 +4,7 @@ namespace Ramstack.Parsing; /// -/// Represents a binder that is aware of a specific context expression, -/// allows resolution of members and methods based on the provided context. +/// Represents a binder that also resolves unqualified names to the members of a context expression. /// public class ContextAwareBinder : DefaultBinder { @@ -18,7 +17,7 @@ public class ContextAwareBinder : DefaultBinder /// /// Initializes a new instance of the class. /// - /// The context expression used for binding operations. + /// The expression whose members can be referenced without qualification. public ContextAwareBinder(Expression context) { _context = context ?? throw new ArgumentNullException(nameof(context)); @@ -28,6 +27,10 @@ public ContextAwareBinder(Expression context) } /// + /// + /// For an unqualified member name, the instance members of take precedence + /// over the static members of the types imported into scope. + /// public override MemberInfo? BindToMember(Type? type, Identifier memberName, bool isStatic) { if (type is null && isStatic) @@ -41,6 +44,10 @@ public ContextAwareBinder(Expression context) } /// + /// + /// For an unqualified method name, the instance methods of take precedence + /// over the static methods of the types imported into scope. + /// public override MethodInfo? BindToMethod(Type? type, Identifier methodName, Type[] parameterTypes, bool isStatic) { if (type is null && isStatic) diff --git a/src/Ramstack.ExpressionParser/DefaultBinder.cs b/src/Ramstack.ExpressionParser/DefaultBinder.cs index 3508f54..c0636f7 100644 --- a/src/Ramstack.ExpressionParser/DefaultBinder.cs +++ b/src/Ramstack.ExpressionParser/DefaultBinder.cs @@ -7,8 +7,13 @@ namespace Ramstack.Parsing; /// -/// Represents a default implementation of the class. +/// Represents the default binder, which resolves a predefined set of common .NET types +/// and the types registered with . /// +/// +/// Names of types, members, and methods are matched without regard to case. +/// Only public members are resolved. +/// public class DefaultBinder : Binder { private static readonly Dictionary Empty = new(); @@ -82,8 +87,13 @@ public class DefaultBinder : Binder /// Registers a custom type for use in binding operations, allowing it to be resolved by name. /// /// The to register for binding. - /// A indicating whether the type's members - /// should be imported directly into scope as static members (default is ). + /// A indicating whether the static members of the type + /// can be referenced without the type name, as with a using static directive + /// (default is ). + /// + /// The type is registered under its simple name, , without the namespace. + /// Registering another type with the same name replaces the earlier registration. + /// public void RegisterType(Type type, bool importAsStatic = false) { if (!PredefinedType.ContainsKey(type.Name)) diff --git a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs index e76ba54..52f8928 100644 --- a/src/Ramstack.ExpressionParser/ExpressionBuilder.cs +++ b/src/Ramstack.ExpressionParser/ExpressionBuilder.cs @@ -8,18 +8,19 @@ namespace Ramstack.Parsing; /// /// Represents a visitor that builds an from an tree. /// -/// The binder instance. +/// The binder that resolves the types, members, and methods referenced in the expression. public partial class ExpressionBuilder(Binder binder) : ExprVisitor { /// - /// Gets the binder instance. + /// Gets the binder that resolves the types, members, and methods referenced in the expression. /// public Binder Binder { get; } = binder; /// /// - /// Throws a if the thread's stack is too small - /// to visit the expression safely. + /// Errors in the expression, such as a name that cannot be resolved or an operator that cannot be applied + /// to its operands, are reported by throwing a . The exception is also thrown + /// if the thread's stack is too small to visit the expression safely. /// public override Expression Visit(Expr expr) { diff --git a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs index 9d62834..0720bfc 100644 --- a/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs +++ b/src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs @@ -18,9 +18,11 @@ partial class ExpressionParser private const int MaxDepth = 256; /// - /// Gets the sub-expression parser, which serves as the base parser for expressions. + /// Gets the expression parser that does not require the expression to extend to the end of the input. /// /// + /// Unlike , this parser leaves the input that follows the expression unconsumed, + /// so it can be used as part of a larger grammar. /// Parsing stops with a fatal error if the expression tree is deeper than 256 levels. /// Each operator, member access, call, indexer, and pair of parentheses adds a level, /// so a chain such as a + b + c gets one level deeper with each operator. diff --git a/src/Ramstack.ExpressionParser/ExpressionParser.cs b/src/Ramstack.ExpressionParser/ExpressionParser.cs index 8cbdb40..834df33 100644 --- a/src/Ramstack.ExpressionParser/ExpressionParser.cs +++ b/src/Ramstack.ExpressionParser/ExpressionParser.cs @@ -12,7 +12,7 @@ public static partial class ExpressionParser /// /// The expression string to parse. /// - /// The result of the parsing operation. + /// The result of the parsing operation, which contains either the expression tree or a description of the error. /// public static ParseResult Parse(ReadOnlySpan source) => Parse(source, new ExpressionBuilder(new DefaultBinder())); @@ -21,9 +21,9 @@ public static ParseResult Parse(ReadOnlySpan source) => /// Parses the specified expression into an using the specified . /// /// The expression string to parse. - /// The binder instance used for resolving members and methods. + /// The binder that resolves the types, members, and methods referenced in the expression. /// - /// The result of the parsing operation. + /// The result of the parsing operation, which contains either the expression tree or a description of the error. /// public static ParseResult Parse(ReadOnlySpan source, Binder binder) => Parse(source, new ExpressionBuilder(binder)); @@ -34,7 +34,7 @@ public static ParseResult Parse(ReadOnlySpan source, Binder bi /// The expression string to parse. /// The expression builder responsible for constructing the expression tree. /// - /// The result of the parsing operation. + /// The result of the parsing operation, which contains either the expression tree or a description of the error. /// public static ParseResult Parse(ReadOnlySpan source, ExpressionBuilder builder) { @@ -84,6 +84,9 @@ public static ParseResult Parse(ReadOnlySpan source, Expressio /// /// The evaluated result. /// + /// + /// If the expression is invalid, an that describes the error is thrown. + /// public static object? Evaluate(ReadOnlySpan source) => Evaluate(source); @@ -95,6 +98,10 @@ public static ParseResult Parse(ReadOnlySpan source, Expressio /// /// The evaluated result. /// + /// + /// The expression can access the members of the runtime type of . + /// If the expression is invalid, an that describes the error is thrown. + /// public static object? Evaluate(ReadOnlySpan source, object context) { var p = Expression.Parameter(typeof(object), "p"); @@ -128,6 +135,9 @@ public static ParseResult Parse(ReadOnlySpan source, Expressio /// /// The evaluated result of type . /// + /// + /// If the expression is invalid, an that describes the error is thrown. + /// public static TResult? Evaluate(ReadOnlySpan source) { var r = Parse(source, new DefaultBinder()); @@ -159,6 +169,7 @@ public static ParseResult Parse(ReadOnlySpan source, Expressio /// /// The expression can access the members of , not of the runtime type /// of . Members declared only in a derived or implementing type are not available. + /// If the expression is invalid, an that describes the error is thrown. /// public static TResult? Evaluate(ReadOnlySpan source, TContext context) where TContext : notnull { diff --git a/src/Ramstack.ExpressionParser/Expressions/Expr.Literal.cs b/src/Ramstack.ExpressionParser/Expressions/Expr.Literal.cs index 36f2dd7..2ad53d3 100644 --- a/src/Ramstack.ExpressionParser/Expressions/Expr.Literal.cs +++ b/src/Ramstack.ExpressionParser/Expressions/Expr.Literal.cs @@ -3,12 +3,13 @@ namespace Ramstack.Parsing.Expressions; partial class Expr { /// - /// Represents a literal value in an expression tree, such as a number, string, or boolean. + /// Represents a literal value in an expression tree, such as a number, a string, a character, + /// a Boolean value, or . /// public sealed class Literal(object? value) : Expr(ExprKind.Literal) { /// - /// Gets the constant value of the literal expression. + /// Gets the constant value of the literal expression, or for the null literal. /// public object? Value { get; } = value; diff --git a/src/Ramstack.ExpressionParser/Expressions/ExprKind.cs b/src/Ramstack.ExpressionParser/Expressions/ExprKind.cs index 33a8560..10fc11c 100644 --- a/src/Ramstack.ExpressionParser/Expressions/ExprKind.cs +++ b/src/Ramstack.ExpressionParser/Expressions/ExprKind.cs @@ -46,15 +46,17 @@ public enum ExprKind Index, /// - /// Represents a read operation on a field or property of an object. + /// Represents access to a member of an object or a type. /// /// /// In the expression obj.Property, this denotes accessing the "Property" member. + /// In the expression Math.Abs(x), Math.Abs is a member access as well. /// MemberAccess, /// - /// Represents a unary operation, such as negation or logical NOT, involving a single operand. + /// Represents a unary operation on a single operand, such as negation, logical NOT, + /// or a conversion with the :> operator. /// /// /// In the expression -x, this indicates the negation operation. diff --git a/src/Ramstack.ExpressionParser/Expressions/Identifier.cs b/src/Ramstack.ExpressionParser/Expressions/Identifier.cs index cc0736f..269a6da 100644 --- a/src/Ramstack.ExpressionParser/Expressions/Identifier.cs +++ b/src/Ramstack.ExpressionParser/Expressions/Identifier.cs @@ -1,12 +1,13 @@ namespace Ramstack.Parsing.Expressions; /// -/// Represents an identifier in an expression, encapsulating its name as a string. +/// Represents a name in an expression: an identifier, such as a member or type name, +/// or an operator, such as + or ??. /// public sealed class Identifier(string name) { /// - /// Gets the name of the identifier. + /// Gets the name of the identifier or the symbol of the operator. /// public string Name { get; } = name; diff --git a/src/Ramstack.ExpressionParser/ParseErrorException.cs b/src/Ramstack.ExpressionParser/ParseErrorException.cs index 0ea15fa..f27513f 100644 --- a/src/Ramstack.ExpressionParser/ParseErrorException.cs +++ b/src/Ramstack.ExpressionParser/ParseErrorException.cs @@ -1,7 +1,7 @@ namespace Ramstack.Parsing; /// -/// Represents an exception that occurs during parsing. +/// Represents an exception that occurs when an expression cannot be parsed or translated into an expression tree. /// public sealed class ParseErrorException : Exception {