subscript 8.6.0 → 9.0.1
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/README.md +38 -31
- package/feature/access.js +1 -1
- package/feature/array.js +1 -1
- package/feature/call.js +1 -1
- package/feature/group.js +1 -1
- package/feature/increment.js +4 -4
- package/feature/object.js +1 -1
- package/feature/optional.js +2 -2
- package/justin.min.js +1 -1
- package/package.json +2 -1
- package/src/compile.js +3 -3
- package/src/parse.js +5 -4
- package/src/stringify.js +31 -0
- package/subscript.d.ts +6 -0
- package/subscript.js +1 -0
- package/subscript.min.js +1 -1
package/README.md
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
# sub<em>script</em> <a href="https://github.com/spectjs/subscript/actions/workflows/node.js.yml"><img src="https://github.com/spectjs/subscript/actions/workflows/node.js.yml/badge.svg"/></a> <a href="https://bundlejs.com/?q=subscript"><img alt="npm bundle size" src="https://img.shields.io/bundlejs/size/subscript"/></a> <a href="http://npmjs.org/subscript"><img src="https://img.shields.io/npm/v/subscript"/></a> <a href="http://microjs.com/#subscript"><img src="https://img.shields.io/badge/microjs-subscript-blue?color=darkslateblue"/></a>
|
|
2
2
|
|
|
3
|
-
> _Subscript_ is fast, tiny & extensible
|
|
3
|
+
> _Subscript_ is fast, tiny & extensible parser / evaluator / microlanguage with standard syntax.
|
|
4
4
|
|
|
5
5
|
#### Used for:
|
|
6
6
|
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* custom DSL (eg. [lino](https://github.com/dy/lino)) <!-- uneural -->
|
|
7
|
+
* expressions evaluators, calculators
|
|
8
|
+
* subsets of languages (eg. [jasm](https://github.com/dy/jasm), [justin](#justin))
|
|
9
|
+
* sandboxes, playgrounds, safe eval (eg. [glsl-transpiler](https://github.com/stackgl/glsl-transpiler))
|
|
10
|
+
* custom DSL (eg. [piezo](https://github.com/dy/piezo)) <!-- uneural -->
|
|
12
11
|
* preprocessors (eg. [prepr](https://github.com/dy/prepr))
|
|
12
|
+
* templates (eg. [sprae](https://github.com/dy/sprae))
|
|
13
13
|
|
|
14
14
|
_Subscript_ has [3.5kb](https://npmfs.com/package/subscript/7.4.3/subscript.min.js) footprint (compare to [11.4kb](https://npmfs.com/package/jsep/1.2.0/dist/jsep.min.js) _jsep_ + [4.5kb](https://npmfs.com/package/expression-eval/5.0.0/dist/expression-eval.module.js) _expression-eval_), best [performance](#performance) and extensive test coverage.
|
|
15
15
|
|
|
@@ -104,13 +104,32 @@ const fn = compile(['+', ['*', 'min', [,60]], [,'sec']])
|
|
|
104
104
|
fn({min: 5}) // min*60 + "sec" == "300sec"
|
|
105
105
|
|
|
106
106
|
// node kinds
|
|
107
|
-
['+', a];
|
|
108
|
-
['+', a, b];
|
|
109
|
-
['+', a, b, c];
|
|
110
|
-
['()', a];
|
|
111
|
-
['(', a, b];
|
|
112
|
-
[, a]; // literal value `'a'`
|
|
113
|
-
a;
|
|
107
|
+
['+', a]; // unary operator `+a`
|
|
108
|
+
['+', a, b]; // binary operator `a + b`
|
|
109
|
+
['+', a, b, c]; // n-ary operator `a + b + c`
|
|
110
|
+
['()', a]; // group operator `(a)`
|
|
111
|
+
['()', a, b]; // access operator `a(b)`
|
|
112
|
+
[, 'a']; // literal value `'a'`
|
|
113
|
+
a; // variable (from scope)
|
|
114
|
+
null|empty; // placeholder
|
|
115
|
+
|
|
116
|
+
// eg.
|
|
117
|
+
['()', 'a'] // (a)
|
|
118
|
+
['()', 'a',,] // a()
|
|
119
|
+
['()', 'a', 'b'] // a(b)
|
|
120
|
+
['+', 'a'] // +a
|
|
121
|
+
['+','a',,] // a+
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Stringify
|
|
125
|
+
|
|
126
|
+
To convert tree back to code, there's codegenerator function:
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
import { stringify } from 'subscript.js'
|
|
130
|
+
|
|
131
|
+
stringify(['+', ['*', 'min', [,60]], [,'sec']])
|
|
132
|
+
// 'min*60 + "sec" == "300sec"'
|
|
114
133
|
```
|
|
115
134
|
|
|
116
135
|
## Extending
|
|
@@ -154,23 +173,12 @@ See [`./feature/*`](./feature) or [`./justin.js`](./justin.js) for examples.
|
|
|
154
173
|
<!--
|
|
155
174
|
## Ideas
|
|
156
175
|
|
|
157
|
-
These are custom DSL operators snippets for your inspiration:
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
```html
|
|
161
|
-
template-parts proposal
|
|
162
|
-
<template id="timer">
|
|
163
|
-
<time datetime="{{ date.toUTCString() }}">{{ date.toLocaleTimeString() }}</time>
|
|
164
|
-
</template>
|
|
165
|
-
```
|
|
166
|
-
|
|
167
176
|
* Keyed arrays <code>[a:1, b:2, c:3]</code>
|
|
168
177
|
* 7!` (factorial)
|
|
169
178
|
* `5s`, `5rem` (units)
|
|
170
|
-
* `?`, `?.`, `??`
|
|
171
179
|
* `arrᵀ` - transpose
|
|
172
180
|
* `int 5` (typecast)
|
|
173
|
-
* `$a` (
|
|
181
|
+
* `$a` (parameter expansion)
|
|
174
182
|
* `1 to 10 by 2`
|
|
175
183
|
* `a if b else c`
|
|
176
184
|
* `a, b in c`
|
|
@@ -178,12 +186,11 @@ template-parts proposal
|
|
|
178
186
|
* vector operators
|
|
179
187
|
* set operators
|
|
180
188
|
* polynomial operators
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
2a as `2*a`
|
|
185
|
-
|
|
186
|
-
string interpolation ` ${} 1 ${} `
|
|
189
|
+
* versions
|
|
190
|
+
* hashes, urls
|
|
191
|
+
* regexes
|
|
192
|
+
* 2a as `2*a`
|
|
193
|
+
* string interpolation ` ${} 1 ${} `
|
|
187
194
|
-->
|
|
188
195
|
|
|
189
196
|
## Performance
|
package/feature/access.js
CHANGED
|
@@ -4,7 +4,7 @@ import { CBRACK, PREC_ACCESS } from '../src/const.js'
|
|
|
4
4
|
|
|
5
5
|
// a[b]
|
|
6
6
|
access('[]', PREC_ACCESS)
|
|
7
|
-
operator('[', (a, b) => !b ? err() : (a = compile(a), b = compile(b), ctx => a(ctx)[b(ctx)]))
|
|
7
|
+
operator('[]', (a, b) => !b ? err() : (a = compile(a), b = compile(b), ctx => a(ctx)[b(ctx)]))
|
|
8
8
|
|
|
9
9
|
// a.b
|
|
10
10
|
binary('.', PREC_ACCESS)
|
package/feature/array.js
CHANGED
|
@@ -4,7 +4,7 @@ import { PREC_TOKEN } from '../src/const.js'
|
|
|
4
4
|
|
|
5
5
|
// [a,b,c]
|
|
6
6
|
group('[]', PREC_TOKEN)
|
|
7
|
-
operator('[]', (a, b) => (
|
|
7
|
+
operator('[]', (a, b) => b === undefined && (
|
|
8
8
|
a = !a ? [] : a[0] === ',' ? a.slice(1) : [a],
|
|
9
9
|
a = a.map(a => a[0] === '...' ? (a = compile(a[1]), ctx => a(ctx)) : (a = compile(a), ctx => [a(ctx)])),
|
|
10
10
|
ctx => a.flatMap(a => (a(ctx))))
|
package/feature/call.js
CHANGED
|
@@ -4,7 +4,7 @@ import { PREC_ACCESS } from '../src/const.js'
|
|
|
4
4
|
|
|
5
5
|
// a(b,c,d), a()
|
|
6
6
|
access('()', PREC_ACCESS)
|
|
7
|
-
operator('(', (a, b, args) => (
|
|
7
|
+
operator('()', (a, b, args) => b !== undefined && (
|
|
8
8
|
args = !b ? () => [] : // a()
|
|
9
9
|
b[0] === ',' ? (b = b.slice(1).map(b => !b ? err() : compile(b)), ctx => b.map(arg => arg(ctx))) : // a(b,c)
|
|
10
10
|
(b = compile(b), ctx => [b(ctx)]), // a(b)
|
package/feature/group.js
CHANGED
|
@@ -5,7 +5,7 @@ import { PREC_ACCESS, PREC_GROUP, PREC_SEQ, PREC_STATEMENT } from '../src/const.
|
|
|
5
5
|
// (a,b,c), (a)
|
|
6
6
|
// FIXME: try raising group precedence (it causes conflict in ?. though)
|
|
7
7
|
group('()', PREC_ACCESS)
|
|
8
|
-
operator('()', (a) => (!a && err('Empty ()'), compile(a)))
|
|
8
|
+
operator('()', (a, b) => b === undefined && (!a && err('Empty ()'), compile(a)))
|
|
9
9
|
|
|
10
10
|
const last = (...args) => (args = args.map(compile), ctx => args.map(arg => arg(ctx)).pop())
|
|
11
11
|
nary(',', PREC_SEQ), operator(',', last)
|
package/feature/increment.js
CHANGED
|
@@ -5,10 +5,10 @@ import { PREC_POSTFIX } from "../src/const.js"
|
|
|
5
5
|
let inc, dec
|
|
6
6
|
token('++', PREC_POSTFIX, a => a ? ['++-', a] : ['++', expr(PREC_POSTFIX - 1)])
|
|
7
7
|
// ++a, ++((a)), ++a.b, ++a[b]
|
|
8
|
-
operator('++', inc = (a) => prop(a, (obj, path
|
|
9
|
-
operator('++-', inc = (a) => prop(a, (obj, path
|
|
8
|
+
operator('++', inc = (a) => prop(a, (obj, path) => ++obj[path]))
|
|
9
|
+
operator('++-', inc = (a) => prop(a, (obj, path) => obj[path]++))
|
|
10
10
|
|
|
11
11
|
token('--', PREC_POSTFIX, a => a ? ['--+', a] : ['--', expr(PREC_POSTFIX - 1)])
|
|
12
12
|
// --a, --a.b, --a[b]
|
|
13
|
-
operator('--', dec = (a) => (prop(a, (obj, path
|
|
14
|
-
operator('--+', dec = (a) => (prop(a, (obj, path
|
|
13
|
+
operator('--', dec = (a) => (prop(a, (obj, path) => --obj[path])))
|
|
14
|
+
operator('--+', dec = (a) => (prop(a, (obj, path) => obj[path]--)))
|
package/feature/object.js
CHANGED
|
@@ -5,7 +5,7 @@ import { PREC_ASSIGN, PREC_SEQ, PREC_TOKEN } from '../src/const.js'
|
|
|
5
5
|
|
|
6
6
|
// {a:1, b:2, c:3}
|
|
7
7
|
group('{}', PREC_TOKEN)
|
|
8
|
-
operator('{}', (a, b) => (
|
|
8
|
+
operator('{}', (a, b) => b === undefined && (
|
|
9
9
|
// {}, {a:b}, {a}, {a, b}
|
|
10
10
|
a = (!a ? [] : a[0] !== ',' ? [a] : a.slice(1)),
|
|
11
11
|
a = a.map(p => compile(typeof p === 'string' ? [':', p, p] : p)),
|
package/feature/optional.js
CHANGED
|
@@ -13,7 +13,7 @@ token('?.', PREC_ACCESS, (a, b) => a && (b = expr(PREC_ACCESS), !b?.map) && ['?.
|
|
|
13
13
|
operator('?.', (a, b) => b && (a = compile(a), ctx => a(ctx)?.[b]))
|
|
14
14
|
|
|
15
15
|
// a?.x() - keep context, but watch out a?.()
|
|
16
|
-
operator('(', (a, b, container, args, path, optional) => (a[0] === '?.') && (a[2] || Array.isArray(a[1])) && (
|
|
16
|
+
operator('()', (a, b, container, args, path, optional) => b !== undefined && (a[0] === '?.') && (a[2] || Array.isArray(a[1])) && (
|
|
17
17
|
args = !b ? () => [] : // a()
|
|
18
18
|
b[0] === ',' ? (b = b.slice(1).map(compile), ctx => b.map(a => a(ctx))) : // a(b,c)
|
|
19
19
|
(b = compile(b), ctx => [b(ctx)]), // a(b)
|
|
@@ -22,7 +22,7 @@ operator('(', (a, b, container, args, path, optional) => (a[0] === '?.') && (a[2
|
|
|
22
22
|
!a[2] && (optional = true, a = a[1]),
|
|
23
23
|
|
|
24
24
|
// a?.['x']?.()
|
|
25
|
-
a[0] === '[' ? (path = compile(a[2])) : (path = () => a[2]),
|
|
25
|
+
a[0] === '[]' && a.length === 3 ? (path = compile(a[2])) : (path = () => a[2]),
|
|
26
26
|
(container = compile(a[1]), optional ?
|
|
27
27
|
ctx => (container(ctx)?.[path(ctx)]?.(...args(ctx))) :
|
|
28
28
|
ctx => (container(ctx)?.[path(ctx)](...args(ctx)))
|
package/justin.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let r,e,t=t=>(r=0,e=t,t=
|
|
1
|
+
let r,e,t=t=>(r=0,e=t,t=a(),e[r]?n():t||""),n=(t="Bad syntax",n=e.slice(0,r).split("\n"),l=n.pop())=>{let i=e.slice(r-108,r).split("\n").pop(),a=e.slice(r,r+108).split("\n").shift();throw EvalError(`${t} at ${n.length}:${l.length} \`${r>=108?"…":""}${i}┃${a}\``,"font-weight: bold")},l=(t,n=r,l)=>{for(;l=t(e.charCodeAt(r));)r+=l;return e.slice(n,r)},i=(t=1,n=r)=>(r+=t,e.slice(n,r)),a=(e=0,i)=>{let a,p,c,d;for(;(a=o())&&(c=((d=s[a])&&d(p,e))??(!p&&l(t.id)));)p=c;return i&&(a==i?r++:n()),p},o=t=>{for(;(t=e.charCodeAt(r))<=32;)r++;return t};t.id=r=>r>=48&&r<=57||r>=65&&r<=90||r>=97&&r<=122||36==r||95==r||r>=192&&215!=r&&247!=r;let s=[],p=(l,i=32,a,o=l.charCodeAt(0),p=l.length,c=s[o],d=l.toUpperCase()!==l)=>s[o]=(o,s,f,h=r)=>(f?l==f:(p<2||e.substr(r,p)==l)&&(f=l))&&s<i&&!(d&&t.id(e.charCodeAt(r+p)))&&(r+=p,a(o)||(r=h,!c&&n()))||c?.(o,s,f),c=(r,e,t=!1)=>p(r,e,((n,l)=>n&&(l=a(e-(t?.5:0)))&&[r,n,l])),d=(r,e,t)=>p(r,e,(n=>t?n&&[r,n]:!n&&(n=a(e-.5))&&[r,n])),f=(r,e,t)=>{p(r,e,((t,n)=>(n=a(e),t?.[0]!==r&&(t=[r,t||null]),n?.[0]===r?t.push(...n.slice(1)):t.push(n||null),t)))},h=(r,e)=>p(r[0],e,(e=>!e&&[r,a(0,r.charCodeAt(1))])),u=(r,e)=>p(r[0],e,(e=>e&&[r,e,a(0,r.charCodeAt(1))||null]));const A=r=>Array.isArray(r)?r[0]?g[r[0]](...r.slice(1)):()=>r[1]:A.id(r);A.id=r=>e=>e?.[r];const g={},m=(r,e,t=g[r])=>g[r]=(...r)=>e(...r)||t?.(...r),y=(r,e,t,l,i)=>"()"===r[0]&&2==r.length?y(r[1],e,t):"string"==typeof r?t=>e(t,r,t):"."===r[0]?(l=A(r[1]),i=r[2],r=>e(l(r),i,r)):"[]"===r[0]&&3===r.length?(l=A(r[1]),i=A(r[2]),r=>e(l(r),i(r),r)):t?(r=A(r),t=>e([r(t)],0,t)):()=>n("Bad left value"),v=(r,e)=>[,(r=+l((r=>46===r||r>=48&&r<=57||(69===r||101===r?2:0))))!=r?n():r];s[46]=r=>!r&&v();for(let r=48;r<=57;r++)s[r]=r=>r?n():v();const C={n:"\n",r:"\r",t:"\t",b:"\b",f:"\f",v:"\v"},b=t=>(l,a,o="")=>{for(l&&n("Unexpected string"),i();(a=e.charCodeAt(r))-t;)92===a?(i(),a=i(),o+=C[a]||a):o+=i();return i()||n("Bad string"),[,o]};s[34]=b(34),s[39]=b(39),u("()",170),m("()",((r,e,t)=>void 0!==e&&(t=e?","===e[0]?(e=e.slice(1).map((r=>r?A(r):err())),r=>e.map((e=>e(r)))):(e=A(e),r=>[e(r)]):()=>[],y(r,((r,e,n)=>r[e](...t(n))),!0)))),u("[]",170),m("[]",((r,e)=>e?(r=A(r),e=A(e),t=>r(t)[e(t)]):err())),c(".",170),m(".",((r,e)=>(r=A(r),e=e[0]?e:e[1],t=>r(t)[e]))),h("()",170),m("()",((r,e)=>void 0===e&&(!r&&n("Empty ()"),A(r))));const $=(...r)=>(r=r.map(A),e=>r.map((r=>r(e))).pop());function N(r){if(!r)return"";if(Array.isArray(r)){const[e,...t]=r;return e?"[]"==e||"{}"==e||"()"==e?(t.length>1?N(t.shift()):"")+e[0]+N(t[0])+e[1]:1===t.length?e+N(t[0]):2===t.length?N(t[0])+("."===e?e:" "+e+" ")+N(t[1]):t.filter(Boolean).map((r=>N(r))).join(e+"\n"):JSON.stringify(t[0])}return r}f(",",10),m(",",$),f(";",5),m(";",$),c("=",20,!0),m("=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]=e(n)))))),c("*",120),m("*",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)*e(t)))),c("/",120),m("/",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)/e(t)))),c("%",120),m("%",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)%e(t)))),c("*=",20,!0),m("*=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]*=e(n)))))),c("/=",20,!0),m("/=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]/=e(n)))))),c("%=",20,!0),m("%=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]%=e(n)))))),c("+",110),m("+",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)+e(t)))),c("-",110),m("-",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)-e(t)))),d("+",140),m("+",((r,e)=>!e&&(r=A(r),e=>+r(e)))),d("-",140),m("-",((r,e)=>!e&&(r=A(r),e=>-r(e)))),c("+=",20,!0),m("+=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]+=e(n)))))),c("-=",20,!0),m("-=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]-=e(n)))))),p("++",150,(r=>r?["++-",r]:["++",a(149)])),m("++",(r=>y(r,((r,e)=>++r[e])))),m("++-",(r=>y(r,((r,e)=>r[e]++)))),p("--",150,(r=>r?["--+",r]:["--",a(149)])),m("--",(r=>y(r,((r,e)=>--r[e])))),m("--+",(r=>y(r,((r,e)=>r[e]--)))),d("~",140),m("~",((r,e)=>!e&&(r=A(r),e=>~r(e)))),c("|",50),m("|",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)|e(t)))),c("&",70),m("&",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)&e(t)))),c("^",60),m("^",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)^e(t)))),c("==",80),m("==",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)==e(t)))),c("!=",80),m("!=",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)!=e(t)))),c(">",90),m(">",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)>e(t)))),c("<",90),m("<",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)<e(t)))),c(">=",90),m(">=",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)>=e(t)))),c("<=",90),m("<=",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)<=e(t)))),c(">>",100),m(">>",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)>>e(t)))),c("<<",100),m("<<",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)<<e(t)))),c(">>=",20,!0),m(">>=",((r,e)=>(e=A(e),prop(r,((r,t,n)=>r[t]>>=e(n)))))),c("<<=",20,!0),m("<<=",((r,e)=>(e=A(e),prop(r,((r,t,n)=>r[t]<<=e(n)))))),d("!",140),m("!",((r,e)=>!e&&(r=A(r),e=>!r(e)))),c("||",30),m("||",((r,e)=>(r=A(r),e=A(e),t=>r(t)||e(t)))),c("&&",40),m("&&",((r,e)=>(r=A(r),e=A(e),t=>r(t)&&e(t))));var j=r=>A(t(r));p("/*",200,((t,n)=>(l((t=>42!==t&&47!==e.charCodeAt(r+1))),i(2),t||a(n)||[]))),p("//",200,((r,e)=>(l((r=>r>=32)),r||a(e)||[]))),c("**",130,!0),m("**",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)**e(t)))),p("?",20,((r,e,t)=>r&&(e=a(19))&&l((r=>58===r))&&["?",r,e,a(19)])),m("?",((r,e,t)=>(r=A(r),e=A(e),t=A(t),n=>r(n)?e(n):t(n)))),p("true",200,(r=>r?err():[,!0])),p("false",200,(r=>r?err():[,!1])),h("[]",200),m("[]",((r,e)=>void 0===e&&(r=(r=r?","===r[0]?r.slice(1):[r]:[]).map((r=>"..."===r[0]?(r=A(r[1]),e=>r(e)):(r=A(r),e=>[r(e)]))),e=>r.flatMap((r=>r(e)))))),h("{}",200),m("{}",((r,e)=>void 0===e&&(r=(r=r?","!==r[0]?[r]:r.slice(1):[]).map((r=>A("string"==typeof r?[":",r,r]:r))),e=>Object.fromEntries(r.flatMap((r=>r(e))))))),c(":",19,!0),m(":",((r,e)=>(e=A(e),Array.isArray(r)?(r=A(r),t=>[[r(t),e(t)]]):t=>[[r,e(t)]]))),c("=>",20,!0),m("=>",((r,e)=>(r=(r="()"===r[0]?r[1]:r)?r=","===r[0]?r.slice(1):[r]:[],e=A("{}"===e[0]?e[1]:e),(t=null)=>(t=Object.create(t),(...n)=>(r.map(((r,e)=>t[r]=n[e])),e(t)))))),c(""),p("?.",170,(r=>r&&["?.",r])),m("?.",(r=>(r=A(r),e=>r(e)||(()=>{})))),p("?.",170,((r,e)=>r&&!(e=a(170))?.map&&["?.",r,e])),m("?.",((r,e)=>e&&(r=A(r),t=>r(t)?.[e]))),m("()",((r,e,t,n,l,i)=>void 0!==e&&"?."===r[0]&&(r[2]||Array.isArray(r[1]))&&(n=e?","===e[0]?(e=e.slice(1).map(A),r=>e.map((e=>e(r)))):(e=A(e),r=>[e(r)]):()=>[],!r[2]&&(r=r[1]),l="[]"===r[0]&&3===r.length?A(r[2]):()=>r[2],t=A(r[1]),r=>t(r)?.[l(r)]?.(...n(r))))),d("...",140),m("...",(r=>(r=A(r),e=>Object.entries(r(e))))),c("in",90),m("in",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)in e(t)))),c("===",80),c("!==",9),m("===",((r,e)=>(r=A(r),e=A(e),t=>r(t)===e(t)))),m("!==",((r,e)=>(r=A(r),e=A(e),t=>r(t)!==e(t)))),c("??",30),m("??",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)??e(t)))),c("??=",20,!0),m("??=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]??=e(n)))))),c("||=",20,!0),m("||=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]||=e(n)))))),c("&&=",20,!0),m("&&=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]&&=e(n)))))),c(">>>",80),m(">>>",((r,e)=>(r=A(r),e=A(e),t=>r(t)>>>e(t)))),c(">>>=",20,!0),m(">>>=",((r,e)=>(e=A(e),y(r,((r,t,n)=>r[t]>>>=e(n)))))),p("undefined",20,(r=>r?n():[,void 0])),p("NaN",20,(r=>r?n():[,NaN])),p("null",20,(r=>r?n():[,null]));export{u as access,c as binary,A as compile,j as default,h as group,f as nary,m as operator,t as parse,N as stringify,p as token,d as unary};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "subscript",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "9.0.1",
|
|
4
4
|
"description": "Fast and tiny expression evaluator with minimal syntax.",
|
|
5
5
|
"main": "subscript.js",
|
|
6
6
|
"module": "subscript.js",
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"feature",
|
|
22
22
|
"subscript.js",
|
|
23
23
|
"subscript.min.js",
|
|
24
|
+
"subscript.d.ts",
|
|
24
25
|
"justin.js",
|
|
25
26
|
"justin.min.js"
|
|
26
27
|
],
|
package/src/compile.js
CHANGED
|
@@ -10,18 +10,18 @@ export const compile = (node) => !Array.isArray(node) ? compile.id(node) : !node
|
|
|
10
10
|
operators = {},
|
|
11
11
|
|
|
12
12
|
// register an operator
|
|
13
|
-
operator = (op, fn, prev = operators[op]) => (operators[op] = (...args) => fn(...args) || prev
|
|
13
|
+
operator = (op, fn, prev = operators[op]) => (operators[op] = (...args) => fn(...args) || prev?.(...args)),
|
|
14
14
|
|
|
15
15
|
// takes node and returns evaluator depending on the case with passed params (container, path, ctx) =>
|
|
16
16
|
prop = (a, fn, generic, obj, path) => (
|
|
17
17
|
// (((x))) => x
|
|
18
|
-
a[0] === '()' ? prop(a[1], fn, generic) :
|
|
18
|
+
a[0] === '()' && a.length == 2 ? prop(a[1], fn, generic) :
|
|
19
19
|
// (_, name, ctx) => ctx[path]
|
|
20
20
|
typeof a === 'string' ? ctx => fn(ctx, a, ctx) :
|
|
21
21
|
// (container, path, ctx) => container(ctx)[path]
|
|
22
22
|
a[0] === '.' ? (obj = compile(a[1]), path = a[2], ctx => fn(obj(ctx), path, ctx)) :
|
|
23
23
|
// (container, path, ctx) => container(ctx)[path(ctx)]
|
|
24
|
-
a[0] === '[' ? (obj = compile(a[1]), path = compile(a[2]), ctx => fn(obj(ctx), path(ctx), ctx)) :
|
|
24
|
+
a[0] === '[]' && a.length === 3 ? (obj = compile(a[1]), path = compile(a[2]), ctx => fn(obj(ctx), path(ctx), ctx)) :
|
|
25
25
|
// (src, _, ctx) => src(ctx)
|
|
26
26
|
generic ? (a = compile(a), ctx => fn([a(ctx)], 0, ctx)) : () => err('Bad left value')
|
|
27
27
|
)
|
package/src/parse.js
CHANGED
|
@@ -94,8 +94,8 @@ export let idx, cur,
|
|
|
94
94
|
(a, b) => (
|
|
95
95
|
b = expr(prec),
|
|
96
96
|
(
|
|
97
|
-
(a?.[0] !== op) && (a = [op, a]), // if beginning of sequence - init node
|
|
98
|
-
b?.[0] === op ? a.push(...b.slice(1)) : a.push(b), // comments can return same-token expr
|
|
97
|
+
(a?.[0] !== op) && (a = [op, a || null]), // if beginning of sequence - init node
|
|
98
|
+
b?.[0] === op ? a.push(...b.slice(1)) : a.push(b || null), // comments can return same-token expr
|
|
99
99
|
a
|
|
100
100
|
))
|
|
101
101
|
)
|
|
@@ -103,10 +103,11 @@ export let idx, cur,
|
|
|
103
103
|
|
|
104
104
|
// register (a), [b], {c} etc groups
|
|
105
105
|
// FIXME: add "Unclosed paren" error
|
|
106
|
-
group = (op, prec) => token(op[0], prec, a => (!a &&
|
|
106
|
+
group = (op, prec) => token(op[0], prec, a => (!a && [op, expr(0, op.charCodeAt(1))])),
|
|
107
107
|
|
|
108
108
|
// register a(b), a[b], a<b> etc,
|
|
109
|
-
|
|
109
|
+
// NOTE: we make sure `null` indicates placeholder
|
|
110
|
+
access = (op, prec) => token(op[0], prec, a => (a && [op, a, expr(0, op.charCodeAt(1)) || null]))
|
|
110
111
|
|
|
111
112
|
|
|
112
113
|
export default parse
|
package/src/stringify.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// convert ast to code string (codegen)
|
|
2
|
+
|
|
3
|
+
// FIXME: possible enhancements
|
|
4
|
+
// * pairs via options
|
|
5
|
+
// * custom spacing/newlines (`a.b` vs `a + b` vs `a, b` vs `a; b`)
|
|
6
|
+
// * newlines?
|
|
7
|
+
// * custom literals?
|
|
8
|
+
export function stringify(node) {
|
|
9
|
+
if (!node) return ''
|
|
10
|
+
|
|
11
|
+
if (Array.isArray(node)) {
|
|
12
|
+
const [op, ...args] = node;
|
|
13
|
+
|
|
14
|
+
// 1, "x"
|
|
15
|
+
if (!op) return JSON.stringify(args[0])
|
|
16
|
+
|
|
17
|
+
// (a), a(b)
|
|
18
|
+
if (op == '[]' || op == '{}' || op == '()') return (args.length > 1 ? stringify(args.shift()) : '') + op[0] + (stringify(args[0])) + op[1]
|
|
19
|
+
|
|
20
|
+
// +a
|
|
21
|
+
if (args.length === 1) return op + stringify(args[0])
|
|
22
|
+
|
|
23
|
+
// a + b
|
|
24
|
+
if (args.length === 2) return stringify(args[0]) + (op === '.' ? op : (' ' + op + ' ')) + stringify(args[1])
|
|
25
|
+
|
|
26
|
+
// a; b; c
|
|
27
|
+
return args.filter(Boolean).map(a => stringify(a)).join(op + '\n')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return node;
|
|
31
|
+
}
|
package/subscript.d.ts
ADDED
package/subscript.js
CHANGED
|
@@ -19,5 +19,6 @@ import parse from './src/parse.js'
|
|
|
19
19
|
|
|
20
20
|
export { parse, access, binary, unary, nary, group, token } from './src/parse.js'
|
|
21
21
|
export { compile, operator } from './src/compile.js'
|
|
22
|
+
export { stringify } from './src/stringify.js'
|
|
22
23
|
|
|
23
24
|
export default s => compile(parse(s))
|
package/subscript.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
let t,r,e=e=>(t=0,r=e,e=
|
|
1
|
+
let t,r,e=e=>(t=0,r=e,e=i(),r[t]?n():e||""),n=(e="Bad syntax",n=r.slice(0,t).split("\n"),l=n.pop())=>{let o=r.slice(t-108,t).split("\n").pop(),i=r.slice(t,t+108).split("\n").shift();throw EvalError(`${e} at ${n.length}:${l.length} \`${t>=108?"…":""}${o}┃${i}\``,"font-weight: bold")},l=(e,n=t,l)=>{for(;l=e(r.charCodeAt(t));)t+=l;return r.slice(n,t)},o=(e=1,n=t)=>(t+=e,r.slice(n,t)),i=(r=0,o)=>{let i,p,c,h;for(;(i=s())&&(c=((h=a[i])&&h(p,r))??(!p&&l(e.id)));)p=c;return o&&(i==o?t++:n()),p},s=e=>{for(;(e=r.charCodeAt(t))<=32;)t++;return e};e.id=t=>t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122||36==t||95==t||t>=192&&215!=t&&247!=t;let a=[],p=(l,o=32,i,s=l.charCodeAt(0),p=l.length,c=a[s],h=l.toUpperCase()!==l)=>a[s]=(s,a,d,f=t)=>(d?l==d:(p<2||r.substr(t,p)==l)&&(d=l))&&a<o&&!(h&&e.id(r.charCodeAt(t+p)))&&(t+=p,i(s)||(t=f,!c&&n()))||c?.(s,a,d),c=(t,r,e=!1)=>p(t,r,((n,l)=>n&&(l=i(r-(e?.5:0)))&&[t,n,l])),h=(t,r,e)=>p(t,r,(n=>e?n&&[t,n]:!n&&(n=i(r-.5))&&[t,n])),d=(t,r,e)=>{p(t,r,((e,n)=>(n=i(r),e?.[0]!==t&&(e=[t,e||null]),n?.[0]===t?e.push(...n.slice(1)):e.push(n||null),e)))},f=(t,r)=>p(t[0],r,(r=>!r&&[t,i(0,t.charCodeAt(1))])),u=(t,r)=>p(t[0],r,(r=>r&&[t,r,i(0,t.charCodeAt(1))||null]));const g=(t,r)=>[,(t=+l((t=>46===t||t>=48&&t<=57||(69===t||101===t?2:0))))!=t?n():t];a[46]=t=>!t&&g();for(let t=48;t<=57;t++)a[t]=t=>t?n():g();const A={n:"\n",r:"\r",t:"\t",b:"\b",f:"\f",v:"\v"},y=e=>(l,i,s="")=>{for(l&&n("Unexpected string"),o();(i=r.charCodeAt(t))-e;)92===i?(o(),i=o(),s+=A[i]||i):s+=o();return o()||n("Bad string"),[,s]};a[34]=y(34),a[39]=y(39);const C=t=>Array.isArray(t)?t[0]?v[t[0]](...t.slice(1)):()=>t[1]:C.id(t);C.id=t=>r=>r?.[t];const v={},m=(t,r,e=v[t])=>v[t]=(...t)=>r(...t)||e?.(...t),$=(t,r,e,l,o)=>"()"===t[0]&&2==t.length?$(t[1],r,e):"string"==typeof t?e=>r(e,t,e):"."===t[0]?(l=C(t[1]),o=t[2],t=>r(l(t),o,t)):"[]"===t[0]&&3===t.length?(l=C(t[1]),o=C(t[2]),t=>r(l(t),o(t),t)):e?(t=C(t),e=>r([t(e)],0,e)):()=>n("Bad left value");u("()",170),m("()",((t,r,e)=>void 0!==r&&(e=r?","===r[0]?(r=r.slice(1).map((t=>t?C(t):err())),t=>r.map((r=>r(t)))):(r=C(r),t=>[r(t)]):()=>[],$(t,((t,r,n)=>t[r](...e(n))),!0)))),u("[]",170),m("[]",((t,r)=>r?(t=C(t),r=C(r),e=>t(e)[r(e)]):err())),c(".",170),m(".",((t,r)=>(t=C(t),r=r[0]?r:r[1],e=>t(e)[r]))),f("()",170),m("()",((t,r)=>void 0===r&&(!t&&n("Empty ()"),C(t))));const b=(...t)=>(t=t.map(C),r=>t.map((t=>t(r))).pop());function B(t){if(!t)return"";if(Array.isArray(t)){const[r,...e]=t;return r?"[]"==r||"{}"==r||"()"==r?(e.length>1?B(e.shift()):"")+r[0]+B(e[0])+r[1]:1===e.length?r+B(e[0]):2===e.length?B(e[0])+("."===r?r:" "+r+" ")+B(e[1]):e.filter(Boolean).map((t=>B(t))).join(r+"\n"):JSON.stringify(e[0])}return t}d(",",10),m(",",b),d(";",5),m(";",b),c("=",20,!0),m("=",((t,r)=>(r=C(r),$(t,((t,e,n)=>t[e]=r(n)))))),c("*",120),m("*",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)*r(e)))),c("/",120),m("/",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)/r(e)))),c("%",120),m("%",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)%r(e)))),c("*=",20,!0),m("*=",((t,r)=>(r=C(r),$(t,((t,e,n)=>t[e]*=r(n)))))),c("/=",20,!0),m("/=",((t,r)=>(r=C(r),$(t,((t,e,n)=>t[e]/=r(n)))))),c("%=",20,!0),m("%=",((t,r)=>(r=C(r),$(t,((t,e,n)=>t[e]%=r(n)))))),c("+",110),m("+",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)+r(e)))),c("-",110),m("-",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)-r(e)))),h("+",140),m("+",((t,r)=>!r&&(t=C(t),r=>+t(r)))),h("-",140),m("-",((t,r)=>!r&&(t=C(t),r=>-t(r)))),c("+=",20,!0),m("+=",((t,r)=>(r=C(r),$(t,((t,e,n)=>t[e]+=r(n)))))),c("-=",20,!0),m("-=",((t,r)=>(r=C(r),$(t,((t,e,n)=>t[e]-=r(n)))))),p("++",150,(t=>t?["++-",t]:["++",i(149)])),m("++",(t=>$(t,((t,r)=>++t[r])))),m("++-",(t=>$(t,((t,r)=>t[r]++)))),p("--",150,(t=>t?["--+",t]:["--",i(149)])),m("--",(t=>$(t,((t,r)=>--t[r])))),m("--+",(t=>$(t,((t,r)=>t[r]--)))),h("~",140),m("~",((t,r)=>!r&&(t=C(t),r=>~t(r)))),c("|",50),m("|",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)|r(e)))),c("&",70),m("&",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)&r(e)))),c("^",60),m("^",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)^r(e)))),c("==",80),m("==",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)==r(e)))),c("!=",80),m("!=",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)!=r(e)))),c(">",90),m(">",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)>r(e)))),c("<",90),m("<",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)<r(e)))),c(">=",90),m(">=",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)>=r(e)))),c("<=",90),m("<=",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)<=r(e)))),c(">>",100),m(">>",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)>>r(e)))),c("<<",100),m("<<",((t,r)=>r&&(t=C(t),r=C(r),e=>t(e)<<r(e)))),c(">>=",20,!0),m(">>=",((t,r)=>(r=C(r),prop(t,((t,e,n)=>t[e]>>=r(n)))))),c("<<=",20,!0),m("<<=",((t,r)=>(r=C(r),prop(t,((t,e,n)=>t[e]<<=r(n)))))),h("!",140),m("!",((t,r)=>!r&&(t=C(t),r=>!t(r)))),c("||",30),m("||",((t,r)=>(t=C(t),r=C(r),e=>t(e)||r(e)))),c("&&",40),m("&&",((t,r)=>(t=C(t),r=C(r),e=>t(e)&&r(e))));var x=t=>C(e(t));export{u as access,c as binary,C as compile,x as default,f as group,d as nary,m as operator,e as parse,B as stringify,p as token,h as unary};
|