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
25 changes: 22 additions & 3 deletions src/Ramstack.ExpressionParser/Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ namespace Ramstack.Parsing;
public abstract class Binder
{
/// <summary>
/// Gets the expression context associated with this binder instance.
/// Gets the expression whose instance members can be referenced in an expression without qualification.
/// </summary>
/// <value>
/// The context expression, or <see langword="null"/> if the binder has no context.
/// </value>
/// <remarks>
/// When <see cref="BindToMember"/> or <see cref="BindToMethod"/> returns an instance member
/// for an unqualified name, the member is accessed through this expression.
/// </remarks>
public virtual Expression? Context => null;

/// <summary>
Expand All @@ -26,7 +33,8 @@ public abstract class Binder
/// <summary>
/// Attempts to bind to a method based on the specified type, method name, and parameter types.
/// </summary>
/// <param name="type">The <see cref="Type"/> from which to select the method.</param>
/// <param name="type">The <see cref="Type"/> from which to select the method,
/// or <see langword="null"/> for an unqualified method name.</param>
/// <param name="methodName">The <see cref="Identifier"/> representing the name of the method to bind.</param>
/// <param name="parameterTypes">An array of <see cref="Type"/> objects representing
/// the types of arguments passed to the method.</param>
Expand All @@ -35,17 +43,28 @@ public abstract class Binder
/// <returns>
/// A <see cref="MethodInfo"/> representing the matching method if found; otherwise, <see langword="null"/>.
/// </returns>
/// <remarks>
/// For an unqualified method name, <paramref name="type"/> is <see langword="null"/> and <paramref name="isStatic"/>
/// is <see langword="true"/>. 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 <see cref="Context"/>.
/// </remarks>
public abstract MethodInfo? BindToMethod(Type? type, Identifier methodName, Type[] parameterTypes, bool isStatic);

/// <summary>
/// Attempts to bind to a member (field or property) based on the specified type and member name.
/// </summary>
/// <param name="type">The <see cref="Type"/> from which to select the member.</param>
/// <param name="type">The <see cref="Type"/> from which to select the member,
/// or <see langword="null"/> for an unqualified member name.</param>
/// <param name="memberName">The <see cref="Identifier"/> representing the name of the member to bind.</param>
/// <param name="isStatic">A <see cref="bool"/> indicating whether to match only static members
/// (<see langword="true"/>) or instance members (<see langword="false"/>).</param>
/// <returns>
/// A <see cref="MemberInfo"/> representing the matching member if found; otherwise, <see langword="null"/>.
/// </returns>
/// <remarks>
/// For an unqualified member name, <paramref name="type"/> is <see langword="null"/> and <paramref name="isStatic"/>
/// is <see langword="true"/>. 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 <see cref="Context"/>.
/// </remarks>
public abstract MemberInfo? BindToMember(Type? type, Identifier memberName, bool isStatic);
}
13 changes: 10 additions & 3 deletions src/Ramstack.ExpressionParser/ContextAwareBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
namespace Ramstack.Parsing;

/// <summary>
/// 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.
/// </summary>
public class ContextAwareBinder : DefaultBinder
{
Expand All @@ -18,7 +17,7 @@ public class ContextAwareBinder : DefaultBinder
/// <summary>
/// Initializes a new instance of the <see cref="ContextAwareBinder"/> class.
/// </summary>
/// <param name="context">The context expression used for binding operations.</param>
/// <param name="context">The expression whose members can be referenced without qualification.</param>
public ContextAwareBinder(Expression context)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
Expand All @@ -28,6 +27,10 @@ public ContextAwareBinder(Expression context)
}

/// <inheritdoc />
/// <remarks>
/// For an unqualified member name, the instance members of <see cref="Context"/> take precedence
/// over the static members of the types imported into scope.
/// </remarks>
public override MemberInfo? BindToMember(Type? type, Identifier memberName, bool isStatic)
{
if (type is null && isStatic)
Expand All @@ -41,6 +44,10 @@ public ContextAwareBinder(Expression context)
}

/// <inheritdoc />
/// <remarks>
/// For an unqualified method name, the instance methods of <see cref="Context"/> take precedence
/// over the static methods of the types imported into scope.
/// </remarks>
public override MethodInfo? BindToMethod(Type? type, Identifier methodName, Type[] parameterTypes, bool isStatic)
{
if (type is null && isStatic)
Expand Down
16 changes: 13 additions & 3 deletions src/Ramstack.ExpressionParser/DefaultBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
namespace Ramstack.Parsing;

/// <summary>
/// Represents a default implementation of the <see cref="Binder"/> class.
/// Represents the default binder, which resolves a predefined set of common .NET types
/// and the types registered with <see cref="RegisterType"/>.
/// </summary>
/// <remarks>
/// Names of types, members, and methods are matched without regard to case.
/// Only public members are resolved.
/// </remarks>
public class DefaultBinder : Binder
{
private static readonly Dictionary<string, (Type type, bool isStatic)> Empty = new();
Expand Down Expand Up @@ -82,8 +87,13 @@ public class DefaultBinder : Binder
/// Registers a custom type for use in binding operations, allowing it to be resolved by name.
/// </summary>
/// <param name="type">The <see cref="Type"/> to register for binding.</param>
/// <param name="importAsStatic">A <see cref="bool"/> indicating whether the type's members
/// should be imported directly into scope as static members (default is <see langword="false"/>).</param>
/// <param name="importAsStatic">A <see cref="bool"/> indicating whether the static members of the type
/// can be referenced without the type name, as with a <c>using static</c> directive
/// (default is <see langword="false"/>).</param>
/// <remarks>
/// The type is registered under its simple name, <see cref="MemberInfo.Name"/>, without the namespace.
/// Registering another type with the same name replaces the earlier registration.
/// </remarks>
public void RegisterType(Type type, bool importAsStatic = false)
{
if (!PredefinedType.ContainsKey(type.Name))
Expand Down
9 changes: 5 additions & 4 deletions src/Ramstack.ExpressionParser/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ namespace Ramstack.Parsing;
/// <summary>
/// Represents a visitor that builds an <see cref="Expression"/> from an <see cref="Expr"/> tree.
/// </summary>
/// <param name="binder">The binder instance.</param>
/// <param name="binder">The binder that resolves the types, members, and methods referenced in the expression.</param>
public partial class ExpressionBuilder(Binder binder) : ExprVisitor<Expression>
{
/// <summary>
/// Gets the binder instance.
/// Gets the binder that resolves the types, members, and methods referenced in the expression.
/// </summary>
public Binder Binder { get; } = binder;

/// <inheritdoc />
/// <remarks>
/// Throws a <see cref="ParseErrorException"/> 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 <see cref="ParseErrorException"/>. The exception is also thrown
/// if the thread's stack is too small to visit the expression safely.
/// </remarks>
public override Expression Visit(Expr expr)
{
Expand Down
4 changes: 3 additions & 1 deletion src/Ramstack.ExpressionParser/ExpressionParser.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ partial class ExpressionParser
private const int MaxDepth = 256;

/// <summary>
/// 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.
/// </summary>
/// <remarks>
/// Unlike <see cref="Parser"/>, 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 <c>a + b + c</c> gets one level deeper with each operator.
Expand Down
19 changes: 15 additions & 4 deletions src/Ramstack.ExpressionParser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public static partial class ExpressionParser
/// </summary>
/// <param name="source">The expression string to parse.</param>
/// <returns>
/// The result of the parsing operation.
/// The result of the parsing operation, which contains either the expression tree or a description of the error.
/// </returns>
public static ParseResult<Expression> Parse(ReadOnlySpan<char> source) =>
Parse(source, new ExpressionBuilder(new DefaultBinder()));
Expand All @@ -21,9 +21,9 @@ public static ParseResult<Expression> Parse(ReadOnlySpan<char> source) =>
/// Parses the specified expression into an <see cref="Expression"/> using the specified <see cref="Binder"/>.
/// </summary>
/// <param name="source">The expression string to parse.</param>
/// <param name="binder">The binder instance used for resolving members and methods.</param>
/// <param name="binder">The binder that resolves the types, members, and methods referenced in the expression.</param>
/// <returns>
/// The result of the parsing operation.
/// The result of the parsing operation, which contains either the expression tree or a description of the error.
/// </returns>
public static ParseResult<Expression> Parse(ReadOnlySpan<char> source, Binder binder) =>
Parse(source, new ExpressionBuilder(binder));
Expand All @@ -34,7 +34,7 @@ public static ParseResult<Expression> Parse(ReadOnlySpan<char> source, Binder bi
/// <param name="source">The expression string to parse.</param>
/// <param name="builder">The expression builder responsible for constructing the expression tree.</param>
/// <returns>
/// The result of the parsing operation.
/// The result of the parsing operation, which contains either the expression tree or a description of the error.
/// </returns>
public static ParseResult<Expression> Parse(ReadOnlySpan<char> source, ExpressionBuilder builder)
{
Expand Down Expand Up @@ -84,6 +84,9 @@ public static ParseResult<Expression> Parse(ReadOnlySpan<char> source, Expressio
/// <returns>
/// The evaluated result.
/// </returns>
/// <remarks>
/// If the expression is invalid, an <see cref="ArgumentException"/> that describes the error is thrown.
/// </remarks>
public static object? Evaluate(ReadOnlySpan<char> source) =>
Evaluate<object>(source);

Expand All @@ -95,6 +98,10 @@ public static ParseResult<Expression> Parse(ReadOnlySpan<char> source, Expressio
/// <returns>
/// The evaluated result.
/// </returns>
/// <remarks>
/// The expression can access the members of the runtime type of <paramref name="context"/>.
/// If the expression is invalid, an <see cref="ArgumentException"/> that describes the error is thrown.
/// </remarks>
public static object? Evaluate(ReadOnlySpan<char> source, object context)
{
var p = Expression.Parameter(typeof(object), "p");
Expand Down Expand Up @@ -128,6 +135,9 @@ public static ParseResult<Expression> Parse(ReadOnlySpan<char> source, Expressio
/// <returns>
/// The evaluated result of type <typeparamref name="TResult"/>.
/// </returns>
/// <remarks>
/// If the expression is invalid, an <see cref="ArgumentException"/> that describes the error is thrown.
/// </remarks>
public static TResult? Evaluate<TResult>(ReadOnlySpan<char> source)
{
var r = Parse(source, new DefaultBinder());
Expand Down Expand Up @@ -159,6 +169,7 @@ public static ParseResult<Expression> Parse(ReadOnlySpan<char> source, Expressio
/// <remarks>
/// The expression can access the members of <typeparamref name="TContext"/>, not of the runtime type
/// of <paramref name="context"/>. Members declared only in a derived or implementing type are not available.
/// If the expression is invalid, an <see cref="ArgumentException"/> that describes the error is thrown.
/// </remarks>
public static TResult? Evaluate<TContext, TResult>(ReadOnlySpan<char> source, TContext context) where TContext : notnull
{
Expand Down
5 changes: 3 additions & 2 deletions src/Ramstack.ExpressionParser/Expressions/Expr.Literal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ namespace Ramstack.Parsing.Expressions;
partial class Expr
{
/// <summary>
/// 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 <see langword="null" />.
/// </summary>
public sealed class Literal(object? value) : Expr(ExprKind.Literal)
{
/// <summary>
/// Gets the constant value of the literal expression.
/// Gets the constant value of the literal expression, or <see langword="null" /> for the <c>null</c> literal.
/// </summary>
public object? Value { get; } = value;

Expand Down
6 changes: 4 additions & 2 deletions src/Ramstack.ExpressionParser/Expressions/ExprKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ public enum ExprKind
Index,

/// <summary>
/// Represents a read operation on a field or property of an object.
/// Represents access to a member of an object or a type.
/// </summary>
/// <example>
/// In the expression <c>obj.Property</c>, this denotes accessing the "Property" member.
/// In the expression <c>Math.Abs(x)</c>, <c>Math.Abs</c> is a member access as well.
/// </example>
MemberAccess,

/// <summary>
/// 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 <c>:></c> operator.
/// </summary>
/// <example>
/// In the expression <c>-x</c>, this indicates the negation operation.
Expand Down
5 changes: 3 additions & 2 deletions src/Ramstack.ExpressionParser/Expressions/Identifier.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
namespace Ramstack.Parsing.Expressions;

/// <summary>
/// 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 <c>+</c> or <c>??</c>.
/// </summary>
public sealed class Identifier(string name)
{
/// <summary>
/// Gets the name of the identifier.
/// Gets the name of the identifier or the symbol of the operator.
/// </summary>
public string Name { get; } = name;

Expand Down
2 changes: 1 addition & 1 deletion src/Ramstack.ExpressionParser/ParseErrorException.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Ramstack.Parsing;

/// <summary>
/// Represents an exception that occurs during parsing.
/// Represents an exception that occurs when an expression cannot be parsed or translated into an expression tree.
/// </summary>
public sealed class ParseErrorException : Exception
{
Expand Down