Expressions module

Provides syntax and lexical analyzers, and an expression calculator optimized for repeated calculations.

Packages

The module contains the following packages:

  • Calculator - expression calculator
  • CSV - CSV tokenizer
  • IO - input/output utility classes to support lexical analysis
  • Mustache - Mustache templating engine
  • Tokenizers - lexical analyzers to break incoming character streams into tokens
  • Variants - dynamic objects that can hold any values and operators for them

Use

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

dependencies:
  pip_services4_expressions: version

Now you can install package from the command line:

pub get

The example below shows how to use expression calculator to dynamically calculate user-defined expressions.

import 'package:pip_services4_expressions/src/calculator/calculator.dart';
import 'package:pip_services4_expressions/src/calculator/variables/variables.dart';
import 'package:pip_services4_expressions/src/variants/variants.dart';

...
var calculator = ExpressionCalculator();

calculator.expression = 'A + b / (5 - Max(-123, 1)*2)';

var vars = VariableCollection();
vars.add(Variable('A', Variant('1')));
vars.add(Variable('B', Variant(3)));

var result = await calculator.evaluateWithVariables(vars);
print('The result of the expression is ' +
    result.asString); // The result of the expression is 11
...

And, this example shows hot to process mustache templates:

import 'package:pip_services4_expressions/src/mustache/mustache.dart';

var mustache = MustacheTemplate();
mustache.template =
    'Hello, {{{NAME}}}{{#ESCLAMATION}}!{{/ESCLAMATION}}{{#unless ESCLAMATION}}.{{/unless}}';
var result =
    mustache.evaluateWithVariables({'NAME': 'Mike', 'ESCLAMATION': true});
print("The result of template evaluation is '" + result.toString() + "'");