expressions 0.1.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 78

expressions #

Build Status

A library to parse and evaluate simple expressions.

This library can handle simple expressions, but no operations, blocks of code, control flow statements and so on. It supports a syntax that is common to most programming languages (so no special things like string interpolation, cascade notation, named parameters).

It is partly inspired by jsep.

Usage #

Example 1: evaluate expression with default evaluator

// Parse expression:
Expression expression = Expression.parse("cos(x)*cos(x)+sin(x)*sin(x)==1");

// Create context containing all the variables and functions used in the expression
var context = {
  "x": pi / 5,
  "cos": cos,
  "sin": sin
};

// Evaluate expression
final evaluator = const ExpressionEvaluator();
var r = evaluator.eval(expression, context);
print(r); // = true

Example 2: evaluate expression with custom evaluator

// Parse expression:
Expression expression = Expression.parse("'Hello '+person.name");

// Create context containing all the variables and functions used in the expression
var context = {
  "person": new Person("Jane")
};

// The default evaluator can not handle member expressions like `person.name`.
// When you want to use these kind of expressions, you'll need to create a
// custom evaluator that implements the `evalMemberExpression` to get property
// values of an object (e.g. with `dart:mirrors` or some other strategy).
final evaluator = const MyEvaluator();
var r = evaluator.eval(expression, context);
print(r); // = 'Hello Jane'

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

0.1.2 #

  • Support Dart 2 in pubspec.yaml
  • Use the petitparser version 2 package with fully typed parsers instead of the typedparser package

0.1.1 #

  • Dart 2 strong mode fixes
  • Evaluate right expression in a binary expression only when necessary

0.1.0 #

  • Initial version

example/expressions_example.dart

import 'package:expressions/expressions.dart';
import 'dart:math';

main() {
  example_1();
  example_2();
}

// Example 1: evaluate expression with default evaluator
example_1() {
  // Parse expression:
  Expression expression = Expression.parse("cos(x)*cos(x)+sin(x)*sin(x)==1");

  // Create context containing all the variables and functions used in the expression
  var context = {
    "x": pi / 5,
    "cos": cos,
    "sin": sin
  };

  // Evaluate expression
  final evaluator = const ExpressionEvaluator();
  var r = evaluator.eval(expression, context);


  print(r); // = true

}

// Example 2: evaluate expression with custom evaluator
example_2() {
  // Parse expression:
  Expression expression = Expression.parse("'Hello '+person.name");

  // Create context containing all the variables and functions used in the expression
  var context = {
    "person": new Person("Jane")
  };

  // The default evaluator can not handle member expressions like `person.name`.
  // When you want to use these kind of expressions, you'll need to create a
  // custom evaluator that implements the `evalMemberExpression` to get property
  // values of an object (e.g. with `dart:mirrors` or some other strategy).
  final evaluator = const MyEvaluator();
  var r = evaluator.eval(expression, context);


  print(r); // = 'Hello Jane'

}

class Person {
  final String name;

  Person(this.name);

  Map<String,dynamic> toJson() => {"name": name};
}

class MyEvaluator extends ExpressionEvaluator {
  const MyEvaluator();

  @override
  evalMemberExpression(MemberExpression expression, Map<String, dynamic> context) {
    var object = eval(expression.object, context).toJson();
    return object[expression.property.name];
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  expressions: ^0.1.2

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:expressions/expressions.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
80
Health:
Code health derived from static analysis. [more]
87
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
60
Overall:
Weighted score of the above. [more]
78
Learn more about scoring.

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

  • Dart: 2.6.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

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

Health issues and suggestions

Document public APIs. (-0.12 points)

64 out of 66 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/parser.dart. (-9.54 points)

Analysis of lib/src/parser.dart reported 20 hints, including:

line 12 col 39: Unnecessary new keyword.

line 20 col 21: Unnecessary new keyword.

line 30 col 16: Unnecessary new keyword.

line 35 col 51: Unnecessary new keyword.

line 41 col 21: Unnecessary new keyword.

Fix lib/src/evaluator.dart. (-2.48 points)

Analysis of lib/src/evaluator.dart reported 5 hints:

line 11 col 33: Unnecessary new keyword.

line 21 col 11: Unnecessary new keyword.

line 37 col 11: Unnecessary new keyword.

line 62 col 11: Unnecessary new keyword.

line 106 col 11: Unnecessary new keyword.

Fix lib/src/expressions.dart. (-1 points)

Analysis of lib/src/expressions.dart reported 2 hints:

line 26 col 43: Unnecessary new keyword.

line 117 col 61: Use = to separate a named parameter from its default value.

Format lib/expressions.dart.

Run dartfmt to format lib/expressions.dart.

Maintenance suggestions

Package is getting outdated. (-31.23 points)

The package was last published 68 weeks ago.

The package description is too short. (-9 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.20.1 <3.0.0
petitparser ^2.0.0 2.4.0
quiver >=0.24.0 <3.0.0 2.1.2+1
Transitive dependencies
matcher 0.12.6
meta 1.1.8
path 1.6.4
stack_trace 1.9.3
Dev dependencies
test ^1.0.0