eq_parser 1.0.4

SDKdartflutter
Platformandroidioswindowslinuxmacosweb

A lightweight and fast mathematical equation parser with support for functions and variables.

A light, fast equation parser that supports functions and variables.

Features

  • Supports many number formats. E.g.:
    • 12
    • -1.234
    • 1.234e5
    • 0xff12
    • 0b1101101
  • Mathematical operators and brackets
    • + - * / ^ % ( )
  • Common functions are built-in
    • sin, acos, max, min, floor, round, log, etc
  • Supports user-defined functions
  • Supports user-defined variables (references)
  • User-defined error handling
  • Fast (see benchmark results)

Getting started

  • Add eq_parser to your pubspec.yaml
  • Add import 'package:eq_parser/eq_parser.dart'; to your code

Usage

num result = EqParser()..parse('12 + 3 ^ (5.12 * sin(pi/2))');
var parser = EqParser();
parser.onError = (m, p)=>throw Exception('$m at position $p');
parser.references.addAll({
    'x': 3,
    'y': 4,
});
parser.functions['multiply'] = FunctionDef((a, b)=>a * b, 2);
num result = parser.parse('multiply(x, y)');

Additional information

Although not using any existing technique as a reference, this parser is very likely an implementation of operator precedence parsing, such as Pratt Parsing. I have not studied parsing techniques, but reading summaries online it appears to work in the same way, without creating an intermediary format such as Reverse Polish Notation.

Tokens are pushed to the stack just once, and popped/processed just once, so performance is fast (see benchmarks) and linear with respect to the length of the equation.

There is a minimal implementation included for reference purposes. It can be used like this:

import 'package:eq_parser/src/eq_parser_lite.dart';

num result = EqParserLite()..parse('(17 - 7.12) * 2 ^ 4');

Benchmark results

Benchmarks are in the test folder and are run as part of test execution. EqParser is compared against several other equation/formula parsing libraries. Please let me know if you would like any other parsers compared, or if any of the tests for the other packages are incorrect or are unfairly un-optimised.

Results

Lower is better.

ParserTime (ms)Faster
eq_parser774
formula_parser49336.4x
math_expressions62288.0x
math_parser66988.7x
quds_formula_parser1995425.8x

Running the benchmarks

dart test .\test\eq_parser_benchmarks_test.dart