ParserLibrary 1.0.5
See the version list below for details.
dotnet add package ParserLibrary --version 1.0.5
NuGet\Install-Package ParserLibrary -Version 1.0.5
<PackageReference Include="ParserLibrary" Version="1.0.5" />
paket add ParserLibrary --version 1.0.5
#r "nuget: ParserLibrary, 1.0.5"
// Install ParserLibrary as a Cake Addin #addin nuget:?package=ParserLibrary&version=1.0.5 // Install ParserLibrary as a Cake Tool #tool nuget:?package=ParserLibrary&version=1.0.5
ParserLibrary
No Other Expression Parser, Ever
How it began
I wanted to write my "custom terminal" that used interactive commands with expressions. Other expression builders used only "numbers" as basic entities which I did not want; this is something too common. I wanted some variables to represent "musical notes" or "musical chords", or even "vectors" and "matrices" and some other to represent numbers. The only way, was to build an Expression builder that could allow custom types. Obviously, the default capability of handling numerical values was needed as a start. Let's speed up with the some examples.
The library is based on dependency injection concepts and can be highly customized.
There are 2 main classes: the Tokenizer
and the Parser
. Both of them are base classes and adapt to the corresponding interfaces ITokenizer
and IParser
. Let's uncover all the potential by giving examples with incremental adding functionality.
Examples
Using the DefaultParser
//This is a simple expression, which uses variables and literals of type double, and the DefaultParser.
double result = (double)App.Evaluate( "-5.0+2*a", new() { { "a", 5.0 } });
Console.WriteLine(result); //5
//2 variables example (spaces are obviously ignored)
double result2 = (double)App.Evaluate("-a + 500 * b + 2^3", new() { { "a", 5 }, { "b", 1 } });
Console.WriteLine(result2); //503
The first example is the same with the example below: the second way uses explicitly the DefaultParser
, which can be later overriden in order to use a custom Parser.
//The example below uses explicitly the DefaultParser.
var app = App.GetParserApp<DefaultParser>();
var parser = app.Services.GetParser();
double result = (double)parser.Evaluate("-5.0+2*a", new() { { "a", 5.0 } });
Let's use some functions already defined in the DefaultParser
double result3 = (double)App.Evaluate("cosd(phi)^2+sind(phi)^2", new() { { "phi", 45 } });
Console.WriteLine(result3); // 1.0000000000000002
Adding new functions to the DefaultParser
That was the boring stuff, let's start adding some custom functionality. Let's add a custom function add3
that takes 3 arguments. For this purpose, we create a new subclass of DefaultParser
. Note that we can add custom logging via dependency injection (some more examples will follow on this). For the moment, ignore the constructor. We assume that the add3
functions sums its 3 arguments with a specific weight.
private class SimpleFunctionParser : DefaultParser
{
public SimpleFunctionParser(ILogger<Parser> logger, ITokenizer tokenizer, IOptions<TokenizerOptions> options) : base(logger, tokenizer, options)
{
}
protected override object EvaluateFunction(Node<Token> functionNode, Dictionary<Node<Token>, object> nodeValueDictionary)
{
double[] a = GetDoubleFunctionArguments(functionNode, nodeValueDictionary);
return functionNode.Text.ToLower() switch
{
"add3" => a[0] + 2 * a[1] + 3 * a[2],
//for all other functions use the base class stuff (DefaultParser)
_ => base.EvaluateFunction(functionNode, nodeValueDictionary)
};
}
}
Let's use our first customized Parser
:
var parser = App.GetCustomParser<SimpleFunctionParser>();
double result = (double)parser.Evaluate("8 + add3(5.0,g,3.0)", new() { { "g", 3 } }); // will return 8 + (5 + 2 * 3 + 3 * 3.0)
more examples to follow...
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
-
net6.0
- Microsoft.Extensions.Hosting (>= 6.0.1)
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.0)
- Serilog.AspNetCore (>= 5.0.0)
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.4.12 | 124 | 3/1/2024 | |
1.4.11 | 169 | 6/26/2023 | |
1.4.10 | 127 | 6/26/2023 | |
1.4.9 | 126 | 6/25/2023 | |
1.4.8 | 131 | 6/23/2023 | |
1.4.7 | 119 | 6/19/2023 | |
1.4.6 | 135 | 6/16/2023 | |
1.4.5 | 375 | 8/20/2022 | |
1.4.4 | 368 | 8/19/2022 | |
1.4.3 | 398 | 8/5/2022 | |
1.4.0 | 379 | 8/2/2022 | |
1.3.3 | 384 | 7/25/2022 | |
1.3.2 | 373 | 7/24/2022 | |
1.3.1 | 392 | 7/18/2022 | |
1.2.1 | 405 | 7/17/2022 | |
1.1.1 | 405 | 7/17/2022 | |
1.1.0 | 413 | 7/17/2022 | |
1.0.15 | 424 | 7/17/2022 | |
1.0.12 | 494 | 7/17/2022 | |
1.0.11 | 452 | 7/17/2022 | |
1.0.10 | 442 | 7/17/2022 | |
1.0.9 | 447 | 7/17/2022 | |
1.0.8 | 455 | 7/17/2022 | |
1.0.7 | 446 | 7/17/2022 | |
1.0.6 | 506 | 7/16/2022 | |
1.0.5 | 529 | 7/15/2022 | |
1.0.4 | 524 | 7/11/2022 | |
1.0.3 | 553 | 7/11/2022 | |
1.0.2 | 526 | 7/11/2022 | |
1.0.1 | 524 | 7/10/2022 | |
1.0.0 | 475 | 7/10/2022 |