ExpressionBuilder class

A builder that allows the simple definition of expression grammars with prefix, postfix, and left- and right-associative infix operators.

The following code creates the empty expression builder:

final builder = new ExpressionBuilder();

Then we define the operator-groups in descending precedence. The highest precedence have the literal numbers themselves:
    .flatten().trim().map((a) => double.parse(a)));

If we want to support parenthesis we can add a wrapper:
  ..wrapper(char('(').trim(), char(')').trim(),
      (left, value, right) => value);

Then come the normal arithmetic operators. Note, that the action blocks receive both, the terms and the parsed operator in the order they appear in the parsed input.

// negation is a prefix operator
  ..prefix(char('-').trim(), (op, a) => -a);

// power is right-associative
  ..right(char('^').trim(), (a, op, b) => math.pow(a, b));

// multiplication and addition is left-associative
  ..left(char('*').trim(), (a, op, b) => a * b)
  ..left(char('/').trim(), (a, op, b) => a / b);
  ..left(char('+').trim(), (a, op, b) => a + b)
  ..left(char('-').trim(), (a, op, b) => a - b);

Finally we can build the parser:

final parser =;

After executing the above code we get an efficient parser that correctly evaluates expressions like:

parser.parse('-8');      // -8
parser.parse('1+2*3');   // 7
parser.parse('1*2+3');   // 5
parser.parse('8/4/2');   // 2
parser.parse('2^2^3');   // 256




hashCode int
The hash code for this object. [...]
read-only, inherited
runtimeType Type
A representation of the runtime type of the object.
read-only, inherited


build() Parser
Builds the expression parser.
group() → ExpressionGroup
Creates a new group of operators that share the same priority.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
toString() String
Returns a string representation of this object.


operator ==(dynamic other) bool
The equality operator. [...]