https://github.com/rse/flowlink
Flow Expression Language
https://github.com/rse/flowlink
evaluator expression flow language link parser
Last synced: over 1 year ago
JSON representation
Flow Expression Language
- Host: GitHub
- URL: https://github.com/rse/flowlink
- Owner: rse
- Created: 2024-03-10T13:09:12.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-03-27T22:18:41.000Z (over 2 years ago)
- Last Synced: 2025-03-10T10:46:44.326Z (over 1 year ago)
- Topics: evaluator, expression, flow, language, link, parser
- Language: JavaScript
- Homepage: https://npmjs.com/flowlink
- Size: 36.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
FlowLink
====
**Flow Expression Language (FlowLink)**
About
-----
FlowLink is a JavaScript library for use in the Browser and Node.js to
parse/compile expressions describing data flows. The expressions are
based on sequental flows, parallel flows and groups of nodes. FlowLink
is primarily intended to be used for configuring a complex data flow.
What the particular nodes represent is up to the caller.
Installation
------------
```shell
$ npm install flowlink
```
Usage
-----
```
$ cat sample.js
const FlowLink = require("..")
const flowlink = new FlowLink({
trace: (msg) => console.log(msg)
})
class Node {
constructor (id, opts, args) {
this.id = id
this.opts = opts
this.args = args
this.piped = []
}
pipe (node) {
this.piped.push(node)
}
}
const expr = `
begin(42, id: \`foo\${sample}bar\`) |
{ foo1("foo"), foo2('bar') } |
{ bar1, bar2 } |
end,
sidechain
`
try {
const ast = flowlink.compile(expr)
const stream = flowlink.execute(ast, {
resolveVariable (id) {
if (id === "sample")
return "SAMPLE"
throw new Error(`failed to resolve variable "${id}"`)
},
createNode (id, opts, args) {
return new Node(id, opts, args)
},
connectNode (node1, node2) {
node1.pipe(node2)
}
})
}
catch (ex) {
console.log("ERROR", ex.toString())
}
```
Expression Language
-------------------
The following BNF-style grammar shows the supported expression language:
```
expr ::= parallel
| sequential
| node
| group
parallel ::= sequential ("," sequential)+
sequential ::= node ("|" node)+
node ::= id ("(" (param ("," param)*)? ")")?
param ::= array | object | variable | template | string | number | value
group ::= "{" expr "}"
id ::= /[a-zA-Z_][a-zA-Z0-9_-]*/
variable ::= id
array ::= "[" (param ("," param)*)? "]"
object ::= "{" (id ":" param ("," id ":" param)*)? "}"
template ::= "`" ("${" variable "}" / ("\\`"|.))* "`"
string ::= /"(\\"|.)*"/
| /'(\\'|.)*'/
number ::= /[+-]?/ number-value
number-value ::= "0b" /[01]+/
| "0o" /[0-7]+/
| "0x" /[0-9a-fA-F]+/
| /[0-9]*\.[0-9]+([eE][+-]?[0-9]+)?/
| /[0-9]+/
value ::= "true" | "false" | "null" | "NaN" | "undefined"
```
Application Programming Interface (API)
---------------------------------------
The following TypeScript definition shows the supported Application Programming Interface (API):
```ts
declare module "flowlink" {
type FlowLinkCallbacks = {
resolveVariable(id: string): string,
createNode(id: string, opts: { [ id: string ]: any }, args: any[]): T,
connectNode(node1: T, node2: T): void
}
class FlowLink {
constructor(
options?: {
cache?: number,
trace?: (msg: string) => void
}
)
compile(
expr: string
): any
execute(
ast: any,
callbacks: FlowLinkCallbacks
): any
evaluate(
expr: string,
callbacks: FlowLinkCallbacks
): any
}
export = FlowLink
}
```
Implementation Notice
---------------------
Although FlowLink is written in ECMAScript 2023, it is transpiled to older
environments and this way runs in really all current (as of 2024)
JavaScript environments, of course.
Additionally, there are two transpilation results: first, there is a
compressed `flowlink.browser.js` for Browser environments. Second, there is
an uncompressed `flowlink.node.js` for Node.js environments.
The Browser variant `flowlink.browser.js` has all external dependencies
`asty`, `pegjs-otf`, `pegjs-util`, and `cache-lru` directly embedded.
The Node.js variant `flowlink.node.js` still requires the external
dependencies `asty`, `pegjs-otf`, `pegjs-util`, and `cache-lru`.
License
-------
Copyright © 2024 Dr. Ralf S. Engelschall (http://engelschall.com/)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.