TupacAmaru.Yacep

Yacep is a small and tiny csharp expression parser, can parse a valid single-line string to an abstract syntax tree. It also provides a simple compiler that can compile an abstract syntax tree to an IEvaluator instance, IEvaluator instance can be executed as C# delegate.


Keywords
expression, parser, netstandard, core, csharp, csharp-library, dotnet, dotnet-core, expression-parser, netstandard20
License
MIT
Install
Install-Package TupacAmaru.Yacep -Version 0.2.0

Documentation

English (United States) | 中文

YACEP : yet another csharp expression parser

Build Status Azure DevOps coverage Build Status Build Status AppVeyor Sonar Quality Gate AppVeyor tests Nuget License

YACEP is a small and tiny csharp expression parser, can parse a valid single-line string to an abstract syntax tree. It also provides a simple compiler that can compile an abstract syntax tree to an IEvaluator instance, IEvaluator instance can be executed as C# delegate.

Important
YACEP not supported multi-line expressions and will not be supported in the future. if you need multi-line expressions, my recommendation is IronLanguages
"x+', '+y".Compile().EvaluateAs<string>(new { x = "hello", y = "world" });
// value is  "hello, world"
"x + y".Compile().EvaluateAs<int>(new { x = 1, y = 2 });
// value is  3

Why YACEP ?

  • I found a very interesting thing when using docker, Casbin. After reading the source code of Casbin, found that Casbin used a very simple DSL to solve a series of authorization problems. You can see it on Casbin's official website. There are many implementations of the language, but the work of the .net platform Casbin-Net has been in the WIP state for long time. After reading the source code of Casbin-Net, I found that the library did not continue to write down because it could not find a good expression parser. So I used a simplified hill climbing algorithm to write a simple implementation.

Features

  • Out of the box - Zero-Configuration

  • Custom unary operator - support custom a string as an unary operator

  • Custom binary operator - support custom a string as a binary operator and set an order for it

  • Custom literal - support custom a literal as a value

  • Custom function - support custom function

  • Conditional expression - like ?: operator in C#

  • In expression - evaluates to true if it finds a variable in an array and false otherwise

  • Cross platform - build with netstandard2.0

  • Small and tiny - the core parser code is only 500+ lines

  • Low consumption - use ReadOnlySpan<T> Struct to read string

  • High Performance - good performance to access object’s public method, field, property value instance over using C# reflection. Benchmark report

Quick Start

  • Create a console application

mkdir yacep-demo
cd yacep-demo
dotnet new console
  • add TupacAmaru.Yacep

dotnet add package TupacAmaru.Yacep
  • update Program.cs

cat>Program.cs<<EOF
using TupacAmaru.Yacep.Extensions;

namespace yacep_demo
{
    class Program
    {
        static void Main()
          => System.Console.WriteLine("x+', '+y".Compile().EvaluateAs<string>(new { x = "hello", y = "world" }));
    }
}
EOF

If using a windows system, please copy the following to Program.cs

using TupacAmaru.Yacep.Extensions;

namespace yacep_demo
{
    class Program
    {
        static void Main()
          => System.Console.WriteLine("x+', '+y".Compile().EvaluateAs<string>(new { x = "hello", y = "world" }));
    }
}
  • Run

dotnet run

Seeing the output hello, world means succeeded.

Build

  • Clone source code

git clone https://github.com/tupac-amaru/yacep.git

Linux & Mac

cd yacep
./coverage.sh

Windows

cd yacep
.\coverage.bat

Linux & Mac

cd yacep
./benchmark.sh

Windows

cd yacep
.\benchmark.bat

Thanks

Tool&Library

  • xUnit.net: a free, open source, community-focused unit testing tool for the .NET Framework

  • BenchmarkDotNet: Powerful .NET library for benchmarking

  • Coverlet: Cross platform code coverage for .NET Core

  • ReportGenerator: ReportGenerator converts coverage reports generated by OpenCover, dotCover, Visual Studio, NCover, Cobertura, JaCoCo or Clover into human readable reports in various formats.