@tabnas/expr 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +89 -0
- package/dist/expr.d.ts +64 -0
- package/dist/expr.js +1221 -0
- package/dist/expr.js.map +1 -0
- package/package.json +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Richard Rodger and other contributors.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
# @tabnas/expr
|
|
2
|
+
|
|
3
|
+
An expression-syntax plugin for the [Tabnas](https://github.com/tabnas/jsonic)
|
|
4
|
+
parser, available in both TypeScript and Go.
|
|
5
|
+
|
|
6
|
+
Adds Pratt-parser expressions to Tabnas: infix, prefix, suffix, ternary,
|
|
7
|
+
and paren operators with a configurable binding-power (precedence) scale, so
|
|
8
|
+
values can include arithmetic/logical expressions. Expressions parse into
|
|
9
|
+
LISP-style S-expressions (arrays whose first element is the operator `Op`),
|
|
10
|
+
which a user-supplied evaluator can reduce to values.
|
|
11
|
+
|
|
12
|
+
[](https://npmjs.com/package/@tabnas/expr)
|
|
13
|
+
[](https://github.com/tabnas/expr/actions/workflows/build.yml)
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```sh
|
|
18
|
+
npm install @tabnas/expr @tabnas/parser @tabnas/jsonic
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Tiny example
|
|
22
|
+
|
|
23
|
+
```js
|
|
24
|
+
const { Tabnas } = require('@tabnas/parser')
|
|
25
|
+
const { jsonic } = require('@tabnas/jsonic')
|
|
26
|
+
const { Expr } = require('@tabnas/expr')
|
|
27
|
+
|
|
28
|
+
// Show parse trees with each Op replaced by its source string.
|
|
29
|
+
const S = (x) => Array.isArray(x) && x.length ? [x[0].src || x[0].osrc || S(x[0]), ...x.slice(1).map(S)] : x
|
|
30
|
+
|
|
31
|
+
const j = new Tabnas().use(jsonic).use(Expr)
|
|
32
|
+
const show = (s) => JSON.parse(JSON.stringify(S(j.parse(s))))
|
|
33
|
+
|
|
34
|
+
show('1+2*3') // => ['+', 1, ['*', 2, 3]]
|
|
35
|
+
show('-1+2') // => ['+', ['-', 1], 2]
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Add an `evaluate` callback to compute values instead:
|
|
39
|
+
|
|
40
|
+
```js
|
|
41
|
+
const { Tabnas } = require('@tabnas/parser')
|
|
42
|
+
const { jsonic } = require('@tabnas/jsonic')
|
|
43
|
+
const { Expr } = require('@tabnas/expr')
|
|
44
|
+
|
|
45
|
+
const math = (rule, ctx, op, terms) => {
|
|
46
|
+
switch (op.src) {
|
|
47
|
+
case '+': return op.prefix ? +terms[0] : terms[0] + terms[1]
|
|
48
|
+
case '*': return terms[0] * terms[1]
|
|
49
|
+
case '(': return terms[0]
|
|
50
|
+
default: return NaN
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const j = new Tabnas().use(jsonic).use(Expr, { evaluate: math })
|
|
55
|
+
|
|
56
|
+
j.parse('1+2*3') // => 7
|
|
57
|
+
j.parse('(1+2)*3') // => 9
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Documentation
|
|
61
|
+
|
|
62
|
+
Docs follow the [Diátaxis](https://diataxis.fr) framework:
|
|
63
|
+
|
|
64
|
+
- **[Tutorial](doc/tutorial.md)** — parse and evaluate your first
|
|
65
|
+
expression, step by step.
|
|
66
|
+
- **[Guide](doc/guide.md)** — recipes: add a custom operator, build
|
|
67
|
+
function-call syntax with paren-preval, add a ternary, restrict to strict
|
|
68
|
+
math, walk the tree yourself.
|
|
69
|
+
- **[Reference](doc/reference.md)** — exports, `OpDef` options, the `Op` /
|
|
70
|
+
`Evaluate` types, and the default operator table.
|
|
71
|
+
- **[Concepts](doc/concepts.md)** — how Pratt parsing works, the
|
|
72
|
+
binding-power scale, and the design trade-offs.
|
|
73
|
+
|
|
74
|
+
Go port docs: [tutorial](../go/doc/tutorial.md) ·
|
|
75
|
+
[guide](../go/doc/guide.md) · [reference](../go/doc/reference.md) ·
|
|
76
|
+
[concepts](../go/doc/concepts.md).
|
|
77
|
+
|
|
78
|
+
## Grammar diagram
|
|
79
|
+
|
|
80
|
+
The installed grammar as a railroad/syntax diagram, generated from the live
|
|
81
|
+
grammar with [`@tabnas/railroad`](https://github.com/tabnas/railroad):
|
|
82
|
+
|
|
83
|
+

|
|
84
|
+
|
|
85
|
+
A vertical ASCII version is in [`doc/grammar.txt`](doc/grammar.txt).
|
|
86
|
+
|
|
87
|
+
## License
|
|
88
|
+
|
|
89
|
+
MIT. See [LICENSE](LICENSE).
|
package/dist/expr.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Plugin, Rule, Context, Token } from '@tabnas/parser';
|
|
2
|
+
type OpDef = {
|
|
3
|
+
src?: string | string[];
|
|
4
|
+
osrc?: string;
|
|
5
|
+
csrc?: string;
|
|
6
|
+
left?: number;
|
|
7
|
+
right?: number;
|
|
8
|
+
use?: any;
|
|
9
|
+
prefix?: boolean;
|
|
10
|
+
suffix?: boolean;
|
|
11
|
+
infix?: boolean;
|
|
12
|
+
ternary?: boolean;
|
|
13
|
+
paren?: boolean;
|
|
14
|
+
preval?: {
|
|
15
|
+
active?: boolean;
|
|
16
|
+
required?: boolean;
|
|
17
|
+
allow?: string[];
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
type ExprOptions = {
|
|
21
|
+
op?: {
|
|
22
|
+
[name: string]: OpDef;
|
|
23
|
+
};
|
|
24
|
+
evaluate?: typeof evaluation;
|
|
25
|
+
};
|
|
26
|
+
type Op = {
|
|
27
|
+
name: string;
|
|
28
|
+
src: string;
|
|
29
|
+
left: number;
|
|
30
|
+
right: number;
|
|
31
|
+
use: any;
|
|
32
|
+
prefix: boolean;
|
|
33
|
+
suffix: boolean;
|
|
34
|
+
infix: boolean;
|
|
35
|
+
ternary: boolean;
|
|
36
|
+
paren: boolean;
|
|
37
|
+
terms: number;
|
|
38
|
+
tkn: string;
|
|
39
|
+
tin: number;
|
|
40
|
+
osrc: string;
|
|
41
|
+
csrc: string;
|
|
42
|
+
otkn: string;
|
|
43
|
+
otin: number;
|
|
44
|
+
ctkn: string;
|
|
45
|
+
ctin: number;
|
|
46
|
+
preval: {
|
|
47
|
+
active: boolean;
|
|
48
|
+
required: boolean;
|
|
49
|
+
allow?: string[];
|
|
50
|
+
};
|
|
51
|
+
token: Token;
|
|
52
|
+
OP_MARK: typeof OP_MARK;
|
|
53
|
+
};
|
|
54
|
+
type Evaluate = (rule: Rule, ctx: Context, op: Op, ...terms: any) => any;
|
|
55
|
+
declare const OP_MARK: {};
|
|
56
|
+
declare let Expr: Plugin;
|
|
57
|
+
declare function prattify(expr: any, op?: Op, whence?: string): any[];
|
|
58
|
+
declare function evaluation(rule: Rule, ctx: Context, expr: any, evaluate: Evaluate): any;
|
|
59
|
+
declare const testing: {
|
|
60
|
+
prattify: typeof prattify;
|
|
61
|
+
opify: (x: any) => any;
|
|
62
|
+
};
|
|
63
|
+
export { Expr, evaluation, testing };
|
|
64
|
+
export type { ExprOptions, OpDef, Op, Evaluate };
|