Lusamine.SlimExpressionParser 1.0.0

dotnet add package Lusamine.SlimExpressionParser --version 1.0.0
                    
NuGet\Install-Package Lusamine.SlimExpressionParser -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lusamine.SlimExpressionParser" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lusamine.SlimExpressionParser" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Lusamine.SlimExpressionParser" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Lusamine.SlimExpressionParser --version 1.0.0
                    
#r "nuget: Lusamine.SlimExpressionParser, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Lusamine.SlimExpressionParser@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Lusamine.SlimExpressionParser&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Lusamine.SlimExpressionParser&version=1.0.0
                    
Install as a Cake Tool

Lusamine.SlimExpressionParser

A small, dependency-free evaluator for single short expressions such as cos(123) + sin(456), args or count() >= 2 && !muted. It targets net10.0 and netstandard2.0.

There are no built-in identifiers or functions: you register every one of them. The library evaluates one expression at a time. If your expressions sit inside surrounding text, such as {...} templates, extract them yourself first.

Usage

using Lusamine.SlimExpressionParser;

var context = new ExpressionContext()
    .Identifier("args", () => target)
    .Identifier("pi", () => Math.PI)
    .Function("cos", args => Math.Cos(args.Number(0)))
    .Function("repeat", args => string.Concat(Enumerable.Repeat(args.Text(0), (int)args.Number(1))))
    .Function("if", args => args.Boolean(0) ? args[1] : args[2]);

var number = Evaluator.Evaluate("cos(0) + 1", context);                 // 2.0
var text = Evaluator.Evaluate("args", context);                         // the value of target
var choice = Evaluator.Evaluate("if(pi > 3, 'big', 'small')", context); // "big"

Registering a name again replaces the previous registration. Identifiers and functions have separate namespaces. An identifier's delegate runs only when the expression references it, once per reference, so x + x can combine two different values. Function arguments are evaluated left to right before the call.

Values

Results are double, string, bool or any other object your delegates return. Numeric values from delegates (int, long, decimal, float, ...) become double, bool stays bool, and null becomes "".

Arguments is an IReadOnlyList<object> of the evaluated values, plus three helpers:

Method Accepts Otherwise
Number(i) a number, or text that parses to a finite number with CultureInfo.InvariantCulture throws ExpressionException
Text(i) anything: numbers use the invariant culture, booleans become true/false
Boolean(i) a boolean, or the text true/false in any case throws ExpressionException

The helpers also throw ExpressionException when the argument is missing. The indexer throws ArgumentOutOfRangeException instead.

Syntax

  • Numbers: 123, 1.5. The decimal separator is always .. There is no exponent notation.
  • Strings: 'text' or "text". A backslash escapes the quote character or another backslash, and nothing else.
  • Booleans: true and false, lowercase only. These names can't be registered.
  • Identifiers: a letter or _, then letters, digits or _. Names are case-sensitive.
  • Calls: name(), name(a, b). Arguments can be any expression.
  • Parentheses group expressions. Whitespace between tokens is ignored.

Operators, from lowest to highest precedence. All binary operators are left-associative.

Operators Operands Result
\|\| two booleans boolean
&& two booleans boolean
== != two numbers, two strings or two booleans boolean
< <= > >= two numbers boolean
+ - two numbers, or + with at least one string number or string
* / % two numbers number
unary - ! a number for -, a boolean for ! number or boolean
  • && and || always evaluate both operands. They don't short-circuit, so false && f() still calls f.
  • + concatenates when either operand is a string and converts the other operand to text like Arguments.Text.
  • Operators never convert values: true + 1, '2' * 2 and 1 == '1' are errors. Equality with other objects is an error too.
  • Strings compare ordinally. Numbers follow IEEE rules: 0 / 0 == 0 / 0 is false and 1 / 0 is infinity.

Errors

Invalid expressions throw ExpressionException. Its Position is the zero-based index of the problem:

  • syntax errors, empty expressions and trailing tokens;
  • unknown identifiers and functions, at the name;
  • operators used with the wrong kinds of values, at the operator;
  • Arguments conversion errors and missing arguments, at the function name;
  • nesting deeper than 64 levels, counting parentheses, function calls and each unary - or !. This keeps untrusted input such as ((((... from overflowing the stack.

Exceptions thrown by your delegates propagate unchanged.

Sample

Lusamine.SlimExpressionParser.Sample is a console app for trying expressions against a small sample context:

dotnet run --project Lusamine.SlimExpressionParser.Sample                   # interactive prompt
dotnet run --project Lusamine.SlimExpressionParser.Sample -- "cos(0) + 1"   # evaluate and exit

Tests

The tests run against either library build:

dotnet test                                          # net10.0 build
dotnet test -p:LibraryTargetFramework=netstandard2.0 # netstandard2.0 build

LibraryBuildTests fails if the loaded assembly isn't the selected build.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • net10.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 74 11/16/2026