accordproject / concerto

Business schema language and runtime
https://concerto.accordproject.org
Apache License 2.0
119 stars 106 forks source link

Concerto Expression Language #520

Open dselman opened 2 years ago

dselman commented 2 years ago

Feature Request 🛍️

Implement an expression language that uses Concerto types as its type system, and Concerto JSON instances at runtime.

Use Case

We need a safe language to use within Cicero templates, based on the Concerto type system. The language should include a library of basic mathematical, string and date/time functions.

Inspiration: https://gist.githubusercontent.com/fkereki/1830c5ca8680d08b3e38cc4a994d7af2/raw/1fbcc60ef9589c86a4f9457247823508ccb0c68a/gistfile1.txt

Possible Solution

Add a new package concerto-expression-parser that includes a DSL implemented using peg.js. The parser AST will then be validated against the type definitions present in a ModelManager.

For example, given the model:

namespace test

concept Person {
   o String name
}

The following expression is valid:

person.name == 'Dan' // returns true or false

While the following expressions are invalid:

person.age == 10 // missing property
person.name == 10 // not a numeric property

Add a second new package concerto-expression-eval that evaluates a parser AST against a set of named input objects, producing an output scalar.

Context

Migrate Cicero templates away from inline Ergo.

Detailed Description

Target Excel macro type of user. Detect invalid expressions prior to evaluation. Start small and grow the function library progressively over time. Ensure safe evaluation within the web browser, Docker container, JS environments. E.g. no use of JS eval.

dselman commented 1 year ago

https://github.com/mozilla/mozjexl https://github.com/LeanyLabs/formula-engine http://mvel.documentnode.com https://github.com/NeilFraser/JS-Interpreter https://developer.mozilla.org/en-US/docs/WebAssembly/Using_the_JavaScript_API https://github.com/ipld/ipld/issues/75 https://github.com/WebAssembly/component-model/blob/main/design/high-level/UseCases.md