expression_language 0.1.3

expression_language #

pub package

Dart library for parsing and evaluating expressions.

Main goal #

Main goal of this library is to be able to parse and evaluate expressions like:

4 * 2.5 + 8.5 + 1.5 / 3.0
3* @control1.numberProperty1 < (length(@control2.stringProperty1 + "test string") - 42)
(!@control1.boolProperty1 && length(@control2.stringProperty1) == 21) ? "string1" : "string2"

Features #

Currently there are multiple supported data types and operations.

Data types #

  • String -> maps directly to the Dart String
  • bool -> maps directly to the Dart bool
  • Integer -> wrapper around the Dart int
  • Decimal -> Custom type
  • DateTime -> maps directly to the Dart DateTime
  • Duration -> maps directly to the Dart Duration

Note: To be able to easily work with financial data and not to lose precision we decided to use Decimal data type taken from dart-decimal instead of double. To keep our expression definitions strongly typed and to have a common way to work with all number data types we introduced base Number data type class which is simmilar to Dart num class. Since we can't modify definition of the Dart int we have also introduced Integer data type which is a simple wrapper around the int and which also extends Number. There is a conversion expression from Integer to int and from Decimal to double so higher layers can hide those data types as an implementation detail. To learn more about DateTime data type in expressions see this merge request.

Operations #

There are most of the standard operations working on the data types above. For example you can use most of the arithmetic operators like +,-, *, / , ~/, % or the logical operators like &&, ||, !, <, >, <=, >=, ==.

There are also special functions like length which returns length of the string, round which rounds the Decimal number, now which returns current date time, toString which converts numeric value to the string.

To be able to reference another expression from the expression itself we use a construct @element.propertyName. The element can map to any type extending ExpressionProviderElement.

Usage #

//Create expression parser and pass a map of the types extending ExpressionProviderElement which can hold other expressions.
var expressionGrammarDefinition =
    ExpressionGrammarParser({"element": TestFormElement()});
var parser = expressionGrammarDefinition.build();

//Parse the expression.
var result = parser
    .parse("(1 + @element.value < 3*5) && false || (2 + 3*(4 + 21)) >= 15");

//The expression now contains strongly typed expression tree representing the expression above.
var expression = result.value as Expression<bool>;

//Evaluate the expression.
bool value = expression.evaluate();

0.1.3 #

Added matches, contains, startsWith and endsWith functions.

0.1.2 #

Fixed small health suggestions.

0.1.1 #

Fixed formatting issues.

0.1.0 #

Initial Version of the library.

example/main.dart

import 'package:expression_language/expression_language.dart';

void main() {
  var expressionGrammarDefinition = ExpressionGrammarParser({});
  var parser = expressionGrammarDefinition.build();
  var result = parser.parse("\"Hello 1 + 1 equals: \" +  (1 + 1)");
  var expression = result.value as Expression;
  var value = expression.evaluate();
  print(value);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  expression_language: ^0.1.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:expression_language/expression_language.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
30
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
65
Learn more about scoring.

We analyzed this package on Aug 23, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:expression_language/expression_language.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
petitparser ^2.1.1 2.4.0
rational ^0.3.0 0.3.5
Transitive dependencies
meta 1.1.7
Dev dependencies
gherkin ^1.0.4
test ^1.6.3
test_coverage ^0.2.3