Giter Site home page Giter Site logo

expressionparser's Introduction

C# Expression Parser

The project provides a simple expression parser that transforms a string into a valid C# expression representing a function. The function can be called with or without a parameter or as part of a LINQ query.

Codacy Badge Build Status

Getting Started

The library contains a static class with 4 methods for parsing:

Func<TOutput> Parse<TOutput>(string input)
Delegate Parse(string input)
Func<TInput, TOutput> ParseFor<TInput, TOutput>(string input)
Delegate ParseFor<TInput>(string input)

And 3 methods used to give support to external types:

IExpressionParser Using(Type type, string alias = null)
IExpressionParser Using(IEnumerable<Type> types)
IExpressionParser Using(IDictionary<Type, string> typeMap)

All methods are also exposed by the public interface IExpressionParser.

Prerequisites

There is no prerequisite to install and use the methods included in this library.

Installing

You can install the ExpressionParser by downloading it as a NuGet package:

Install-Package CSharp.ExpressionParser

After that, you can just use the call directly from your code. Here is a couple of usage examples:

var result1 = ExpressionParser.Parse<int>("(3 + 2) * 3")(); //result1 should be an integer of value 15
var result2 = ExpressionParser.ParseFor<SomeClass, bool>("Id == 23 && IsActive == true")(instance);  //result2 should be a boolean that the value shoul depend on the instance provided as input

If you don't know the output of the result in advance you can use:

var expression = ExpressionParser.Parse(someStringToBeParsed); //expression will have a delegate returning an object.
var result = expression.DynamicInvoke();  //result will have the result of the expression as an object.

Here are a few samples of expressions that will be accepted by the case above:

"2.0 / 5.0" ==> Expected result: 0.4 (decimal)
"3 + 2 * 3" ==> Expected result: 9 (int)
"1 >= 1" ==> Expected result: true (bool)

This is also supported when an input value is provided, but in this case, the type of the input has to be informed. Here is an example:

var expression = ExpressionParser.ParseFor<SomeClass>(someStringToBeParsed);
var result = expression.DynamicInvoke(instaceOfTypeSomeClass);

In order to support external types or interfaces you can use the Using method to add them. For example:

var result = ExpressionParser.Using(new { typeof(IPerson), type(IMovie) }).Parse<bool>("((IPerson)record.Person).Age > ((IMovie)record.Movie).AgeLimit")(record);

More examples can be found in the test project.

Supported Operators and Types

Here is a list of supported operators.

The Tests

A NUnit 3 test project is provided in the solution.
The tests currently provide 100% of code coverage, but they are not complete.
We plan to include more positive test cases and many more negative test cases in the future commits.

Contributing

Please read CONTRIBUTING for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

expressionparser's People

Contributors

andrevianna avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

expressionparser's Issues

Needs more Unit Test test cases

Need to add more unit tests with a variety of expressions.

1 - Positive cases (edge/extreme/obscure cases).
2 - Negative cases (where an exception should be thrown).

Always keep the code coverage at 100%.

Add support to reserved words

Add the list of reserved words for name tokens and add the special symbol @ for using the special words as properties.

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.