https://github.com/tupac-amaru/yacep
yet another csharp expression parser
https://github.com/tupac-amaru/yacep
ast cross-platform csharp dotnet-core evaluator expression-evaluator expression-parser netstandard20 parsing
Last synced: about 2 months ago
JSON representation
yet another csharp expression parser
- Host: GitHub
- URL: https://github.com/tupac-amaru/yacep
- Owner: tupac-amaru
- License: mit
- Created: 2019-03-03T05:13:11.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2022-07-15T17:16:16.000Z (almost 3 years ago)
- Last Synced: 2024-04-29T20:05:34.740Z (about 1 year ago)
- Topics: ast, cross-platform, csharp, dotnet-core, evaluator, expression-evaluator, expression-parser, netstandard20, parsing
- Language: C#
- Size: 171 KB
- Stars: 122
- Watchers: 12
- Forks: 18
- Open Issues: 2
-
Metadata Files:
- Readme: README-en_US.adoc
- License: LICENSE
Awesome Lists containing this project
README
English (United States) | link:README.adoc[中文]
# _YACEP_ : yet another csharp expression parser
image:https://dev.azure.com/tupac-amaru/yacep/_apis/build/status/tupac-amaru.yacep?branchName=master["Build Status", link="https://dev.azure.com/tupac-amaru/yacep/_build/latest?definitionId=5&branchName=master"]
image:https://img.shields.io/azure-devops/coverage/tupac-amaru/yacep/5.svg?label=azure%20pipelines%20coverage&color=#49bb1e[Azure DevOps coverage, link="https://dev.azure.com/tupac-amaru/yacep/_build/latest?definitionId=5&branchName=master"]
image:https://codecov.io/gh/tupac-amaru/yacep/branch/master/graph/badge.svg["Build Status", link="https://codecov.io/gh/tupac-amaru/yacep"]
image:https://travis-ci.com/tupac-amaru/yacep.svg?branch=master["Build Status", link="https://travis-ci.com/tupac-amaru/yacep"]
image:https://img.shields.io/appveyor/ci/wushilong/yacep.svg?color=#49bb1e[AppVeyor, link="https://ci.appveyor.com/project/wushilong/yacep/branch/master"]
image:https://img.shields.io/sonar/https/sonarcloud.io/tupac-amaru_yacep/quality_gate.svg?color=#49bb1e[Sonar Quality Gate, link="https://sonarcloud.io/dashboard?id=tupac-amaru_yacep"]
image:https://img.shields.io/appveyor/tests/wushilong/yacep.svg?color=#49bb1e[AppVeyor tests, link="https://ci.appveyor.com/project/wushilong/yacep/branch/master"]
image:https://img.shields.io/nuget/v/TupacAmaru.Yacep.svg?color=#49bb1e[Nuget, link="https://www.nuget.org/packages/TupacAmaru.Yacep"]
image:https://img.shields.io/github/license/tupac-amaru/yacep.svg?color=#49bb1e["License",link="https://opensource.org/licenses/MIT"]## Profile
_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.https://github.com/tupac-amaru/yacep/wiki[Document]
https://github.com/tupac-amaru/yacep/tree/_benchmark[Benchmark report]
[IMPORTANT]
_YACEP_ not supported multi-line expressions and will not be supported in the future. if you need multi-line expressions, my recommendation is https://github.com/IronLanguages[IronLanguages][source,csharp]
----
"x+', '+y".Compile().EvaluateAs(new { x = "hello", y = "world" });
// value is "hello, world"
"x + y".Compile().EvaluateAs(new { x = 1, y = 2 });
// value is 3var state = new
{
x = 7,
y = 43.0f,
z = new Dictionary
{
["yacep"] = "yet another csharp expression parser",
["tupac-amaru"] = "was the last indigenous monarch (Sapa Inca) of the Neo-Inca State"
},
rand = new Func(() => new Random().Next(1, 3)),
array = Enumerable.Range(1971, 1996 - 1971)
};
var expr = "x + y - z['yacep'].Length + max([1, 2, 3]) + (this.rand() > 2 ? 1971 : 1996) - len(array)";
var evaluator = expr.Compile();
var value = evaluator.EvaluateAs(state);
----### Why _YACEP_ ?
- I found a very interesting thing when using https://www.docker.com/[docker], https://casbin.org/[Casbin]. After reading the source code of https://casbin.org/[Casbin], found that https://casbin.org/[Casbin] used a very simple DSL to solve a series of authorization problems. You can see it on https://casbin.org/[Casbin]'s official website. There are many implementations of the language, but the work of the .net platform https://github.com/Devolutions/casbin-net[Casbin-Net] has been in the WIP state for long time. After reading the source code of https://github.com/Devolutions/casbin-net[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 https://en.wikibooks.org/wiki/Algorithms/Hill_Climbing[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 https://en.wikipedia.org/wiki/Order_of_operations#Programming_language[order] for it
- **Custom literal** - support custom a literal as a value
- **Custom function** - support custom function
- **Conditional expression** - like https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator[?:] operator in C#
- **In expression** - evaluates to true if it finds a variable in an array and false otherwise
- **Cross platform** - build with https://github.com/dotnet/standard/blob/master/docs/versions/netstandard2.0.md[netstandard2.0]
- **Small and tiny** - the core parser code is only 500+ lines
- **Low consumption** - use https://docs.microsoft.com/en-za/dotnet/api/system.readonlyspan-1?view=netcore-2.2[ReadOnlySpan Struct] to read string
- **High Performance** - good performance to access object's public method, field, property value instance over using C# reflection. https://github.com/tupac-amaru/yacep/tree/_benchmark[Benchmark report]## Quick Start
https://github.com/tupac-amaru/yacep/wiki[Document]- Create a console application
[source,shell]
----
mkdir yacep-demo
cd yacep-demo
dotnet new console
----- add TupacAmaru.Yacep
[source,shell]
----
dotnet add package TupacAmaru.Yacep
----- update Program.cs
[source,shell]
----
cat>Program.cs< System.Console.WriteLine("x+', '+y".Compile().EvaluateAs(new { x = "hello", y = "world" }));
}
}
EOF
----
#### _If using a windows system, please copy the following to Program.cs_[source,csharp]
----
using TupacAmaru.Yacep.Extensions;namespace yacep_demo
{
class Program
{
static void Main()
=> System.Console.WriteLine("x+', '+y".Compile().EvaluateAs(new { x = "hello", y = "world" }));
}
}
----- Run
[source,shell]
----
dotnet run
----
#### _Seeing the output `hello, world` means succeeded._
## Thanks### Tool&Library
- https://github.com/xunit/xunit[xUnit.net]: a free, open source, community-focused unit testing tool for the .NET Framework
- https://github.com/dotnet/BenchmarkDotNet[BenchmarkDotNet]: Powerful .NET library for benchmarking
- https://github.com/tonerdo/coverlet[Coverlet]: Cross platform code coverage for .NET Core
- https://github.com/danielpalme/ReportGenerator[ReportGenerator]: ReportGenerator converts coverage reports generated by OpenCover, dotCover, Visual Studio, NCover, Cobertura, JaCoCo or Clover into human readable reports in various formats.### Services
- https://dev.azure.com/[Azure DevOps]
- https://codecov.io/[Codecov]
- https://travis-ci.com[travis-ci]
- https://www.appveyor.com/[AppVeyor]
- https://sonarcloud.io/about[SonarCloud]