Giter Site home page Giter Site logo

csuffyy / lambdaparser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nreco/lambdaparser

0.0 0.0 0.0 59 KB

Runtime parser for string expressions (formulas, method calls). Builds dynamic LINQ expression tree and compiles it to lambda delegate.

Home Page: http://www.nrecosite.com/nrecolib_net.aspx

License: MIT License

C# 100.00%

lambdaparser's Introduction

NReco LambdaParser

Runtime parser for string expressions (formulas, method calls, properties/fields/arrays accessors). LambdaParser builds dynamic LINQ expression tree and compiles it to the lambda delegate. Types are resolved at run-time like in dynamic languages.

NuGet Release | AppVeyor

  • PCL (Portable) library: can be used with any .NET target: net40+sl5, net45+wp8+win8+wpa81, .NET Standards 1.3 (.NET Core apps)
  • any number of variables (values can be provided as dictionary or by callback delegate)
  • supports arithmetic operations (+, -, *, /, %), comparisons (==, !=, >, <, >=, <=), conditionals including (ternary) operator ( ? : )
  • access object properties, call methods and indexers, invoke delegates
  • dynamic typed variables: performs automatic type conversions to match method signature or arithmetic operations
  • create arrays and dictionaries with simplified syntax: new dictionary{ {"a", 1}, {"b", 2} } , new []{ 1, 2, 3}

Nuget package: NReco.LambdaParser

var lambdaParser = new NReco.Linq.LambdaParser();

var varContext = new Dictionary<string,object>();
varContext["pi"] = 3.14M;
varContext["one"] = 1M;
varContext["two"] = 2M;
varContext["test"] = "test";
Console.WriteLine( lambdaParser.Eval("pi>one && 0<one ? (1+8)/3+1*two : 0", varContext) ); // --> 5
Console.WriteLine( lambdaParser.Eval("test.ToUpper()", varContext) ); // --> TEST

(see unit tests for more expression examples)

Custom values comparison

By default LambdaParser uses ValueComparer for values comparison. You can provide your own implementation or configure its option to get desired behaviour:

  • ValueComparer.NullComparison determines how comparison with null is handled. 2 options:
    • MinValue: null is treated as minimal possible value for any type - like .NET IComparer
    • Sql: null is not comparable with any type, including another null - like in SQL
  • ValueComparer.SuppressErrors allows to avoid convert exception. If error appears during comparison exception is not thrown and this means that values are not comparable (= any condition leads to false).
var valComparer = new ValueComparer() { NullComparison = ValueComparer.NullComparisonMode.Sql };
var lambdaParser = new LambdaParser(valComparer); 

Who is using this?

NReco.LambdaParser is in production use at SeekTable.com and PivotData microservice (used for user-defined calculated cube members: formulas, custom formatting).

License

Copyright 2016-2020 Vitaliy Fedorchenko

Distributed under the MIT license

lambdaparser's People

Contributors

rducom avatar some1one avatar vitaliymf avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.