expression_language 0.2.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 90

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 =;

//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.2.1 #

Fixed analyzer issues.

0.2.0 #

  • Added ExpressionParser class to abstract underlying parser library.
  • Increased minimum dart SDK to 2.6.0.

0.1.4 #

  • Added custom function expression.
  • Increased minimum dart SDK to 2.4.0.

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.


import 'package:expression_language/expression_language.dart';

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

Use this package as a library

1. Depend on it

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

  expression_language: ^0.2.1

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';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 16, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (petitparser).


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.6.0 <3.0.0
petitparser ^2.4.0 2.4.0 3.0.0
rational ^0.3.7 0.3.7
Transitive dependencies
meta 1.1.8
Dev dependencies
gherkin ^1.1.5+1
glob ^1.2.0
pedantic ^1.9.0
test ^1.9.4
test_coverage ^0.4.0