From 719273f01db9940ae251b7304c402560bf00a2c1 Mon Sep 17 00:00:00 2001 From: rameel Date: Thu, 19 Nov 2026 17:18:19 +0500 Subject: [PATCH] docs: describe Evaluate and the available types in README README showed only Parse and did not mention Evaluate, which parses, compiles, and runs an expression in one call. It also did not say which types an expression can use by default or how to make other types available. Add sections for both: the predefined types of DefaultBinder, and RegisterType with importAsStatic. It also explains how Parse reports errors, adds the using directives to the Usage example and the null-forgiving operator to both examples, so they compile without nullable warnings, and moves the note on case-insensitive names from the ContextAwareBinder section to Usage, since DefaultBinder resolves names the same way. Co-Authored-By: Claude Opus 5.5 --- README.md | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a0a8844..a4a9f40 100644 --- a/README.md +++ b/README.md @@ -17,23 +17,69 @@ dotnet add package Ramstack.ExpressionParser ## Usage ```csharp +using System.Linq.Expressions; +using Ramstack.Parsing; + var result = ExpressionParser.Parse("math.min(2 + 3, 2 * 3)"); if (result.Success) { - var lambda = Expression.Lambda>(result.Value); + var lambda = Expression.Lambda>(result.Value!); var fn = lambda.Compile(); Console.WriteLine(fn()); } ``` +If the expression is invalid, `Success` is `false`, and `ErrorMessage` describes the error. + +Identifiers are resolved in a case-insensitive manner, so `math.min`, `Math.Min`, and `MATH.MIN` +refer to the same method. + +### Evaluating expressions + +`Evaluate` parses, compiles, and runs an expression in a single call: + +```csharp +var sum = ExpressionParser.Evaluate("2 + 3 * 4"); +var area = ExpressionParser.Evaluate("Math.PI * radius * radius", circle); +``` + +The second call evaluates the expression against `circle`, so the expression can use its members +without qualification. If the expression is invalid, `Evaluate` throws an `ArgumentException` +that describes the error. + +`Evaluate` parses and compiles the expression on every call. To evaluate the same expression repeatedly, +use `Parse` and compile the resulting expression once. + +### Available types + +By default, expressions can use the following types: + +- the built-in types `bool`, `byte`, `sbyte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `nint`, `nuint`, + `float`, `double`, `decimal`, `char`, `string`, and `object`, also by their .NET names, such as `Int32` + and `Boolean`; +- `Half` and `BigInteger`; +- `Convert`, `CultureInfo`, `DateTime`, `DateTimeOffset`, `DateOnly`, `TimeOnly`, `DateTimeKind`, `DayOfWeek`, + `Guid`, `Math`, `MathF`, `Regex`, `RegexOptions`, `StringComparer`, `StringComparison`, `StringSplitOptions`, + `TimeSpan`, `TimeZoneInfo`, and `Version`. + +Other types are registered with `RegisterType` under their simple name, without the namespace: + +```csharp +var binder = new DefaultBinder(); +binder.RegisterType(typeof(Path)); + +var result = ExpressionParser.Parse("Path.GetFileName(\"logs/app.txt\")", binder); +``` + +With `importAsStatic: true`, the static members of the type can also be used without the type name, +as with a `using static` directive, for example `GetFileName("logs/app.txt")`. + ### Using `ContextAwareBinder` `ContextAwareBinder` allows binding expressions to a specific context, making it possible to reference its properties, fields, and methods directly within expressions. - -- The provided context acts as an implicit **this**, meaning you can access its members without qualification. -- Identifiers are resolved in a case-insensitive manner, so `level`, `Level`, and `LEVEL` are treated the same. +The provided context acts as an implicit **this**, meaning you can access its members without qualification. ```csharp var parameter = Expression.Parameter(typeof(LogEvent), "logEvent"); @@ -42,7 +88,7 @@ binder.RegisterType(typeof(LogLevel)); var result = ExpressionParser.Parse("level == LogLevel.Error && string.IsNullOrEmpty(source)", binder); _predicate = Expression - .Lambda>(result.Value, parameter) + .Lambda>(result.Value!, parameter) .Compile(); ```