Skip to content
Merged
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
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
[![NuGet](https://img.shields.io/nuget/v/Ramstack.ExpressionParser.svg)](https://nuget.org/packages/Ramstack.ExpressionParser)
[![MIT](https://img.shields.io/github/license/rameel/ramstack.expressionparser)](https://github.com/rameel/ramstack.expressionparser/blob/main/LICENSE)

**Ramstack.ExpressionParser** is a flexible expression parser library for .NET,
allowing dynamic evaluation and binding of expressions with context-aware support.
**Ramstack.ExpressionParser** is a .NET library that parses expressions written in a C#-like syntax
into `System.Linq.Expressions` trees, which you can compile and evaluate,
optionally against a context object.

## Getting Started

To install the `Ramstack.ExpressionParser` [NuGet package](https://www.nuget.org/packages/Ramstack.ExpressionParser) to your project, run the following command:
To add the `Ramstack.ExpressionParser` [NuGet package](https://www.nuget.org/packages/Ramstack.ExpressionParser) to your project, run the following command:

```shell
dotnet add package Ramstack.ExpressionParser
Expand All @@ -31,20 +32,23 @@ if (result.Success)
`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 prefixes.
- Case-insensitive binding: identifiers in expressions are resolved in case-insensitive manner (e.g., level, Level, and LEVEL are treated the same).
- 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.

```csharp
var parameter = Expression.Parameter(typeof(LogEvent), "logEvent");
var binder = new ContextAwareBinder(parameter);
binder.RegisterType(typeof(LogLevel));

var result = ExpressionParser.Parse("level == LogLevel.Error && string.IsNullOrEmpty(source)", binder);
var predicate = Expression
_predicate = Expression
.Lambda<Predicate<LogEvent>>(result.Value, parameter)
.Compile();
```

Here, `IsEnabled` evaluates the parsed expression against a `LogEvent` instance:
Types that are not available by default, such as `LogLevel` here, are registered with `RegisterType`.

`IsEnabled` then evaluates the compiled predicate against a `LogEvent` instance:
```csharp
public bool IsEnabled(LogEvent logEvent)
{
Expand All @@ -60,14 +64,15 @@ Use the `:>` operator to convert a value to a type, for example `count :> long`
A conversion binds more tightly than prefix and binary operators, so `-x :> int` means `-(x :> int)`,
and `x :> long + 1` means `(x :> long) + 1`.

Earlier versions wrote conversions as `x:int`. That syntax conflicted with the conditional operator,
so expressions such as `flag ? 0 : count` failed to parse. It is no longer supported;
replace `x:int` with `x :> int`.
In earlier versions, conversions were written as `x:int`. That syntax conflicted
with the conditional operator, so expressions such as `flag ? 0 : count` failed to parse.
It is no longer supported; replace `x:int` with `x :> int`.

### Limits

An expression tree can be at most 256 levels deep. 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.
and pair of parentheses adds a level, so a chain such as `a + b + c` gets one level deeper
with each operator.
Parsing fails with an error rather than crashing the process when an expression is deeper than that,
or when it is nested too deeply to fit on the stack of the current thread.

Expand All @@ -76,13 +81,14 @@ so limit the length of expressions that come from untrusted sources.

## Supported Versions

| | Version |
|------|------------|
| .NET | 6, 7, 8, 9 |
| | Version |
|------|-------------|
| .NET | 6 and later |

## Contributions

Bug reports and contributions are welcome.
See [CONTRIBUTING.md](https://github.com/rameel/ramstack.expressionparser/blob/main/CONTRIBUTING.md) for details.

## License

Expand Down