subscript 8.1.3 → 8.2.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 CHANGED
@@ -45,16 +45,20 @@ _Subscript_ supports [common syntax](https://en.wikipedia.org/wiki/Comparison_of
45
45
 
46
46
  ### Justin
47
47
 
48
- _Justin_ is minimal JS subset, _JSON_ + _Expressions_ (see [thread](https://github.com/endojs/Jessie/issues/66)). It extends _subscript_ with:
48
+ _Just-in_ is no-keywords JS subset, _JSON_ + _expressions_ (see [thread](https://github.com/endojs/Jessie/issues/66)).<br/> It extends _subscript_ with:
49
49
 
50
- + `a ** b` (right-assoc)
51
- + `a ? b : c`
52
- + `a?.b`
50
+ + `a === b`, `a !== b`
51
+ + `a ** b`, `a **= b`
52
+ + `a ?? b`, `a ??= b`
53
+ + `a ||= b`, `a &&= b`
54
+ + `a ? b : c`, `a?.b`
53
55
  + `[a, b]` Array
54
56
  + `{a: b}` Object
55
- + `a in b`
57
+ + `(a, b) => c` Function
56
58
  + `// foo`, `/* bar */`
57
- + `true`, `false`, `null`
59
+ + `a ||= b`
60
+ + `true`, `false`, `null`, `NaN`, `undefined`
61
+ + `a in b`
58
62
  <!-- + `...x` unary operator -->
59
63
  <!-- + strings interpolation -->
60
64
 
@@ -1,11 +1,11 @@
1
- import { PREC_EQ } from '../src/const.js'
1
+ import { PREC_EQ, PREC_COMP } from '../src/const.js'
2
2
  import { unary, binary } from "../src/parse.js"
3
3
  import { operator, compile } from "../src/compile.js"
4
4
 
5
5
 
6
6
  binary('==', PREC_EQ), operator('==', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) == b(ctx)))
7
7
  binary('!=', PREC_EQ), operator('!=', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) != b(ctx)))
8
- binary('>', PREC_EQ), operator('>', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) > b(ctx)))
9
- binary('<', PREC_EQ), operator('<', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) < b(ctx)))
10
- binary('>=', PREC_EQ), operator('>=', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) >= b(ctx)))
11
- binary('<=', PREC_EQ), operator('<=', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) <= b(ctx)))
8
+ binary('>', PREC_COMP), operator('>', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) > b(ctx)))
9
+ binary('<', PREC_COMP), operator('<', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) < b(ctx)))
10
+ binary('>=', PREC_COMP), operator('>=', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) >= b(ctx)))
11
+ binary('<=', PREC_COMP), operator('<=', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) <= b(ctx)))
package/justin.js CHANGED
@@ -1,19 +1,40 @@
1
- // justin lang https://github.com/endojs/Jessie/issues/66
2
- import { skip, cur, idx, err, expr, lookup, token, binary, unary } from './src/parse.js'
1
+ // no-keywords js, just in https://github.com/endojs/Jessie/issues/66
2
+ import { err, token, binary } from './src/parse.js'
3
3
  import compile, { operator } from './src/compile.js'
4
- import { CPAREN, COLON, PREC_ASSIGN, PREC_PREFIX, PREC_OR, PREC_ACCESS, PREC_COMP, PREC_EXP, PREC_GROUP } from './src/const.js'
5
4
 
6
- // register subscript operators set
7
5
  import subscript from './subscript.js'
8
6
  import './feature/comment.js'
9
7
  import './feature/pow.js'
10
- import './feature/in.js'
11
8
  import './feature/ternary.js'
12
9
  import './feature/bool.js'
13
10
  import './feature/array.js'
14
11
  import './feature/object.js'
12
+ import './feature/arrow.js'
15
13
  import './feature/optional.js'
14
+ import { PREC_ASSIGN, PREC_EQ, PREC_LOR, PREC_COMP } from './src/const.js'
16
15
 
16
+ binary('in', PREC_COMP), operator('in', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) in b(ctx)))
17
+
18
+ // register !==, ===
19
+ binary('===', PREC_EQ), binary('!==', 9)
20
+ operator('===', (a, b) => (a = compile(a), b = compile(b), ctx => a(ctx) === b(ctx)))
21
+ operator('===', (a, b) => (a = compile(a), b = compile(b), ctx => a(ctx) !== b(ctx)))
22
+
23
+ // add nullish coalescing
24
+ binary('??', PREC_LOR)
25
+ operator('??', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) ?? b(ctx)))
26
+ binary('??=', PREC_ASSIGN, true)
27
+ operator('??=', (a, b) => (b = compile(b), prop(a, (obj, path, ctx) => (obj[path] ??= b(ctx)))))
28
+
29
+ // complete logical assignments
30
+ binary('||=', PREC_ASSIGN, true)
31
+ operator('||=', (a, b) => (b = compile(b), prop(a, (obj, path, ctx) => (obj[path] ||= b(ctx)))))
32
+ binary('&&=', PREC_ASSIGN, true)
33
+ operator('&&=', (a, b) => (b = compile(b), prop(a, (obj, path, ctx) => (obj[path] &&= b(ctx)))))
34
+
35
+ // add JS literals
36
+ token('undefined', 20, a => a ? err() : [, undefined])
37
+ token('NaN', 20, a => a ? err() : [, NaN])
17
38
  token('null', 20, a => a ? err() : [, null])
18
39
 
19
40
  export default subscript
package/justin.min.js CHANGED
@@ -1 +1 @@
1
- let r,e,t=t=>(r=0,e=t,t=i(),e[r]?a():t||""),a=(t="Bad syntax",a=e.slice(0,r).split("\n"),s=a.pop())=>{let i=e.slice(r-108,r).split("\n").pop(),n=e.slice(r,r+108).split("\n").shift();throw EvalError(`${t} at ${a.length}:${s.length} \`${r>=108?"…":""}${i}▶${n}\``,"font-weight: bold")},s=(t=1,a=r,s)=>{if("number"==typeof t)r+=t;else for(;s=t(e.charCodeAt(r));)r+=s;return e.slice(a,r)},i=(e=0,s,i,n,l,p)=>{for(;(i=t.space())&&(l=((p=o[i])&&p(n,e))??(!n&&t.id()));)n=l;return s&&(i==s?r++:a()),n},n=r=>r>=48&&r<=57||r>=65&&r<=90||r>=97&&r<=122||36==r||95==r||r>=192&&215!=r&&247!=r;t.id=()=>s(n);let l=t.space=t=>{for(;(t=e.charCodeAt(r))<=32;)r++;return t},o=[],p=(t,a=32,s,i=t.charCodeAt(0),l=t.length,p=o[i],c=t.toUpperCase()!==t)=>o[i]=(i,o,d=r)=>o<a&&(l<2||e.substr(r,l)==t)&&(!c||!n(e.charCodeAt(r+l)))&&(r+=l,s(i,o))||(r=d,p?.(i,o)),c=(r,e,t=!1)=>p(r,e,((a,s)=>a&&(s=i(e-(t?.5:0)))&&[r,a,s])),d=(r,e,t)=>p(r,e,(a=>t?a&&[r,a]:!a&&(a=i(e-.5))&&[r,a])),f=(r,e)=>{p(r,e,((t,a)=>(a=i(e),(!t||t[0]!==r)&&(t=[r,t]),t.push(a),t)))},h=(r,e)=>p(r[0],e,(e=>!e&&[r,i(0,r.charCodeAt(1))])),m=(r,e)=>p(r[0],e,(e=>e&&[r[0],e,i(0,r.charCodeAt(1))]));const A=r=>Array.isArray(r)?r[0]?u[r[0]](...r.slice(1)):()=>r[1]:A.id(r);A.id=r=>e=>e?.[r];const u={},y=(r,e,t=u[r])=>u[r]=(...r)=>e(...r)||t&&t(...r),C=(r,e,t,s,i)=>"()"===r[0]?C(r[1],e,t):"string"==typeof r?t=>e(t,r,t):"."===r[0]?(s=A(r[1]),i=r[2],r=>e(s(r),i,r)):"["===r[0]?(s=A(r[1]),i=A(r[2]),r=>e(s(r),i(r),r)):t?(r=A(r),t=>e([r(t)],0,t)):()=>a("Bad left value"),b=r=>r?a():[,(r=+s((r=>46===r||r>=48&&r<=57||(69===r||101===r?2:0))))!=r?a():r];o[46]=r=>!r&&b();for(let r=48;r<=57;r++)o[r]=b;const g={n:"\n",r:"\r",t:"\t",b:"\b",f:"\f",v:"\v"},$=t=>(i,n,l="")=>{for(i&&a("Unexpected string"),s();(n=e.charCodeAt(r))-t;)92===n?(s(),n=s(),l+=g[n]||n):l+=s();return s()||a("Bad string"),[,l]};o[34]=$(34),o[39]=$(39),m("()",17),y("(",((r,e,t)=>(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)]):()=>[],C(r,((r,e,a)=>r[e](...t(a))),!0)))),m("[]",17),y("[",((r,e)=>e?(r=A(r),e=A(e),t=>r(t)[e(t)]):err())),c(".",17),y(".",((r,e)=>(r=A(r),e=e[0]?e:e[1],t=>r(t)[e]))),h("()",17),y("()",(r=>(!r&&a("Empty ()"),A(r))));const v=(...r)=>(r=r.map(A),e=>r.map((r=>r(e))).pop());f(",",1),y(",",v),f(";",1),y(";",v),c("*",12),y("*",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)*e(t)))),c("/",12),y("/",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)/e(t)))),c("%",12),y("%",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)%e(t)))),c("*=",2,!0),y("*=",((r,e)=>(e=A(e),C(r,((r,t,a)=>r[t]*=e(a)))))),c("/=",2,!0),y("/=",((r,e)=>(e=A(e),C(r,((r,t,a)=>r[t]/=e(a)))))),c("%=",2,!0),y("%=",((r,e)=>(e=A(e),C(r,((r,t,a)=>r[t]%=e(a)))))),d("+",14),y("+",((r,e)=>!e&&(r=A(r),e=>+r(e)))),d("-",14),y("-",((r,e)=>!e&&(r=A(r),e=>-r(e)))),c("+",11),y("+",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)+e(t)))),c("-",11),y("-",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)-e(t)))),c("+=",2,!0),y("+=",((r,e)=>(e=A(e),C(r,((r,t,a)=>r[t]+=e(a)))))),c("-=",2,!0),y("-=",((r,e)=>(e=A(e),C(r,((r,t,a)=>r[t]-=e(a)))))),p("++",15,(r=>r?["++-",r]:["++",i(14)])),y("++",(r=>C(r,((r,e,t)=>++r[e])))),y("++-",(r=>C(r,((r,e,t)=>r[e]++)))),p("--",15,(r=>r?["--+",r]:["--",i(14)])),y("--",(r=>C(r,((r,e,t)=>--r[e])))),y("--+",(r=>C(r,((r,e,t)=>r[e]--)))),d("~",14),y("~",((r,e)=>!e&&(r=A(r),e=>~r(e)))),c("|",5),y("|",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)|e(t)))),c("&",7),y("&",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)&e(t)))),c("^",6),y("^",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)^e(t)))),c(">>",10),y(">>",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)>>e(t)))),c("<<",10),y("<<",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)<<e(t)))),c("==",8),y("==",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)==e(t)))),c("!=",8),y("!=",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)!=e(t)))),c(">",8),y(">",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)>e(t)))),c("<",8),y("<",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)<e(t)))),c(">=",8),y(">=",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)>=e(t)))),c("<=",8),y("<=",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)<=e(t)))),d("!",14),y("!",((r,e)=>!e&&(r=A(r),e=>!r(e)))),c("||",3),y("||",((r,e)=>(r=A(r),e=A(e),t=>r(t)||e(t)))),c("&&",4),y("&&",((r,e)=>(r=A(r),e=A(e),t=>r(t)&&e(t)))),c("=",2,!0),y("=",((r,e)=>(e=A(e),C(r,((r,t,a)=>r[t]=e(a))))));var E=r=>A(t(r));p("/*",20,((t,a)=>(s((t=>42!==t&&47!==e.charCodeAt(r+1))),s(2),t||i(a)||[]))),p("//",20,((r,e)=>(s((r=>r>=32)),r||i(e)||[""]))),c("**",13,!0),y("**",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)**e(t)))),c("in",9),y("in",((r,e)=>e&&(r=A(r),e=A(e),t=>r(t)in e(t)))),p("?",2,((r,e,t)=>r&&(e=i(2,58))&&["?",r,e,i(3)])),y("?",((r,e,t)=>(r=A(r),e=A(e),t=A(t),a=>r(a)?e(a):t(a)))),p("true",20,(r=>r?err():[,!0])),p("false",20,(r=>r?err():[,!1])),h("[]",20),y("[]",((r,e)=>r?","===r[0]?(r=r.slice(1).map(A),e=>r.map((r=>r(e)))):(r=A(r),e=>[r(e)]):()=>[])),h("{}",20),y("{}",((r,e)=>r?","===r[0]?(r=r.slice(1).map(A),e=>Object.fromEntries(r.map((r=>r(e))))):":"===r[0]?(r=A(r),e=>Object.fromEntries([r(e)])):(e=A(r),t=>({[r]:e(t)})):()=>({}))),c(":",2,!0),y(":",((r,e)=>(e=A(e),r=Array.isArray(r)?A(r):(r=>r).bind(0,r),t=>[r(t),e(t)]))),p("?.",17,(r=>r&&["?.",r])),y("?.",(r=>(r=A(r),e=>r(e)||(()=>{})))),p("?.",17,((r,e)=>r&&!(e=i(17))?.map&&["?.",r,e])),y("?.",((r,e)=>e&&(r=A(r),t=>r(t)?.[e]))),y("(",((r,e,t,a,s,i)=>"?."===r[0]&&(r[2]||Array.isArray(r[1]))&&(a=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]),s="["===r[0]?A(r[2]):()=>r[2],t=A(r[1]),r=>t(r)?.[s(r)]?.(...a(r))))),p("null",20,(r=>r?a():[,null]));export{m as access,c as binary,A as compile,e as cur,E as default,a as err,i as expr,h as group,r as idx,n as isId,o as lookup,f as nary,y as operator,u as operators,t as parse,C as prop,s as skip,l as space,p as token,d as unary};
1
+ let r,e,t=t=>(r=0,e=t,t=s(),e[r]?a():t||""),a=(t="Bad syntax",a=e.slice(0,r).split("\n"),p=a.pop())=>{let s=e.slice(r-108,r).split("\n").pop(),o=e.slice(r,r+108).split("\n").shift();throw EvalError(`${t} at ${a.length}:${p.length} \`${r>=108?"…":""}${s}▶${o}\``,"font-weight: bold")},p=(t=1,a=r,p)=>{if("number"==typeof t)r+=t;else for(;p=t(e.charCodeAt(r));)r+=p;return e.slice(a,r)},s=(e=0,p,s,o,n,l)=>{for(;(s=t.space())&&(n=((l=i[s])&&l(o,e))??(!o&&t.id()));)o=n;return p&&(s==p?r++:a()),o},o=r=>r>=48&&r<=57||r>=65&&r<=90||r>=97&&r<=122||36==r||95==r||r>=192&&215!=r&&247!=r;t.id=()=>p(o),t.space=t=>{for(;(t=e.charCodeAt(r))<=32;)r++;return t};let i=[],n=(t,a=32,p,s=t.charCodeAt(0),n=t.length,l=i[s],c=t.toUpperCase()!==t)=>i[s]=(s,i,d=r)=>i<a&&(n<2||e.substr(r,n)==t)&&(!c||!o(e.charCodeAt(r+n)))&&(r+=n,p(s,i))||(r=d,l?.(s,i)),l=(r,e,t=!1)=>n(r,e,((a,p)=>a&&(p=s(e-(t?.5:0)))&&[r,a,p])),c=(r,e,t)=>n(r,e,(a=>t?a&&[r,a]:!a&&(a=s(e-.5))&&[r,a])),d=(r,e)=>{n(r,e,((t,a)=>(a=s(e),(!t||t[0]!==r)&&(t=[r,t]),t.push(a),t)))},f=(r,e)=>n(r[0],e,(e=>!e&&[r,s(0,r.charCodeAt(1))])),m=(r,e)=>n(r[0],e,(e=>e&&[r[0],e,s(0,r.charCodeAt(1))]));const h=r=>Array.isArray(r)?r[0]?u[r[0]](...r.slice(1)):()=>r[1]:h.id(r);h.id=r=>e=>e?.[r];const u={},A=(r,e,t=u[r])=>u[r]=(...r)=>e(...r)||t&&t(...r),y=(r,e,t,p,s)=>"()"===r[0]?y(r[1],e,t):"string"==typeof r?t=>e(t,r,t):"."===r[0]?(p=h(r[1]),s=r[2],r=>e(p(r),s,r)):"["===r[0]?(p=h(r[1]),s=h(r[2]),r=>e(p(r),s(r),r)):t?(r=h(r),t=>e([r(t)],0,t)):()=>a("Bad left value"),b=r=>r?a():[,(r=+p((r=>46===r||r>=48&&r<=57||(69===r||101===r?2:0))))!=r?a():r];i[46]=r=>!r&&b();for(let r=48;r<=57;r++)i[r]=b;const C={n:"\n",r:"\r",t:"\t",b:"\b",f:"\f",v:"\v"},g=t=>(s,o,i="")=>{for(s&&a("Unexpected string"),p();(o=e.charCodeAt(r))-t;)92===o?(p(),o=p(),i+=C[o]||o):i+=p();return p()||a("Bad string"),[,i]};i[34]=g(34),i[39]=g(39),m("()",17),A("(",((r,e,t)=>(t=e?","===e[0]?(e=e.slice(1).map((r=>r?h(r):err())),r=>e.map((e=>e(r)))):(e=h(e),r=>[e(r)]):()=>[],y(r,((r,e,a)=>r[e](...t(a))),!0)))),m("[]",17),A("[",((r,e)=>e?(r=h(r),e=h(e),t=>r(t)[e(t)]):err())),l(".",17),A(".",((r,e)=>(r=h(r),e=e[0]?e:e[1],t=>r(t)[e]))),f("()",17),A("()",(r=>(!r&&a("Empty ()"),h(r))));const v=(...r)=>(r=r.map(h),e=>r.map((r=>r(e))).pop());d(",",1),A(",",v),d(";",1),A(";",v),l("*",12),A("*",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)*e(t)))),l("/",12),A("/",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)/e(t)))),l("%",12),A("%",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)%e(t)))),l("*=",2,!0),A("*=",((r,e)=>(e=h(e),y(r,((r,t,a)=>r[t]*=e(a)))))),l("/=",2,!0),A("/=",((r,e)=>(e=h(e),y(r,((r,t,a)=>r[t]/=e(a)))))),l("%=",2,!0),A("%=",((r,e)=>(e=h(e),y(r,((r,t,a)=>r[t]%=e(a)))))),c("+",14),A("+",((r,e)=>!e&&(r=h(r),e=>+r(e)))),c("-",14),A("-",((r,e)=>!e&&(r=h(r),e=>-r(e)))),l("+",11),A("+",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)+e(t)))),l("-",11),A("-",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)-e(t)))),l("+=",2,!0),A("+=",((r,e)=>(e=h(e),y(r,((r,t,a)=>r[t]+=e(a)))))),l("-=",2,!0),A("-=",((r,e)=>(e=h(e),y(r,((r,t,a)=>r[t]-=e(a)))))),n("++",15,(r=>r?["++-",r]:["++",s(14)])),A("++",(r=>y(r,((r,e,t)=>++r[e])))),A("++-",(r=>y(r,((r,e,t)=>r[e]++)))),n("--",15,(r=>r?["--+",r]:["--",s(14)])),A("--",(r=>y(r,((r,e,t)=>--r[e])))),A("--+",(r=>y(r,((r,e,t)=>r[e]--)))),c("~",14),A("~",((r,e)=>!e&&(r=h(r),e=>~r(e)))),l("|",5),A("|",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)|e(t)))),l("&",7),A("&",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)&e(t)))),l("^",6),A("^",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)^e(t)))),l(">>",10),A(">>",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)>>e(t)))),l("<<",10),A("<<",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)<<e(t)))),l("==",8),A("==",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)==e(t)))),l("!=",8),A("!=",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)!=e(t)))),l(">",9),A(">",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)>e(t)))),l("<",9),A("<",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)<e(t)))),l(">=",9),A(">=",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)>=e(t)))),l("<=",9),A("<=",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)<=e(t)))),c("!",14),A("!",((r,e)=>!e&&(r=h(r),e=>!r(e)))),l("||",3),A("||",((r,e)=>(r=h(r),e=h(e),t=>r(t)||e(t)))),l("&&",4),A("&&",((r,e)=>(r=h(r),e=h(e),t=>r(t)&&e(t)))),l("=",2,!0),A("=",((r,e)=>(e=h(e),y(r,((r,t,a)=>r[t]=e(a))))));var $=r=>h(t(r));n("/*",20,((t,a)=>(p((t=>42!==t&&47!==e.charCodeAt(r+1))),p(2),t||s(a)||[]))),n("//",20,((r,e)=>(p((r=>r>=32)),r||s(e)||[""]))),l("**",13,!0),A("**",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)**e(t)))),n("?",2,((r,e,t)=>r&&(e=s(2,58))&&["?",r,e,s(3)])),A("?",((r,e,t)=>(r=h(r),e=h(e),t=h(t),a=>r(a)?e(a):t(a)))),n("true",20,(r=>r?err():[,!0])),n("false",20,(r=>r?err():[,!1])),f("[]",20),A("[]",((r,e)=>r?","===r[0]?(r=r.slice(1).map(h),e=>r.map((r=>r(e)))):(r=h(r),e=>[r(e)]):()=>[])),f("{}",20),A("{}",((r,e)=>r?","===r[0]?(r=r.slice(1).map(h),e=>Object.fromEntries(r.map((r=>r(e))))):":"===r[0]?(r=h(r),e=>Object.fromEntries([r(e)])):(e=h(r),t=>({[r]:e(t)})):()=>({}))),l(":",2,!0),A(":",((r,e)=>(e=h(e),r=Array.isArray(r)?h(r):(r=>r).bind(0,r),t=>[r(t),e(t)]))),l("=>",2,!0),A("=>",((r,e)=>(r=(r="()"===r[0]?r[1]:r)?r=","===r[0]?r.slice(1):[r]:[],e=h("{}"===e[0]?e[1]:e),(t=null)=>(t=Object.create(t),(...a)=>(r.map(((r,e)=>t[r]=a[e])),e(t)))))),l(""),n("?.",17,(r=>r&&["?.",r])),A("?.",(r=>(r=h(r),e=>r(e)||(()=>{})))),n("?.",17,((r,e)=>r&&!(e=s(17))?.map&&["?.",r,e])),A("?.",((r,e)=>e&&(r=h(r),t=>r(t)?.[e]))),A("(",((r,e,t,a,p,s)=>"?."===r[0]&&(r[2]||Array.isArray(r[1]))&&(a=e?","===e[0]?(e=e.slice(1).map(h),r=>e.map((e=>e(r)))):(e=h(e),r=>[e(r)]):()=>[],!r[2]&&(r=r[1]),p="["===r[0]?h(r[2]):()=>r[2],t=h(r[1]),r=>t(r)?.[p(r)]?.(...a(r))))),l("in",9),A("in",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)in e(t)))),l("===",8),l("!==",9),A("===",((r,e)=>(r=h(r),e=h(e),t=>r(t)===e(t)))),A("===",((r,e)=>(r=h(r),e=h(e),t=>r(t)!==e(t)))),l("??",3),A("??",((r,e)=>e&&(r=h(r),e=h(e),t=>r(t)??e(t)))),l("??=",2,!0),A("??=",((r,e)=>(e=h(e),prop(r,((r,t,a)=>r[t]??=e(a)))))),l("||=",2,!0),A("||=",((r,e)=>(e=h(e),prop(r,((r,t,a)=>r[t]||=e(a)))))),l("&&=",2,!0),A("&&=",((r,e)=>(e=h(e),prop(r,((r,t,a)=>r[t]&&=e(a)))))),n("undefined",20,(r=>r?a():[,void 0])),n("NaN",20,(r=>r?a():[,NaN])),n("null",20,(r=>r?a():[,null]));export{m as access,l as binary,h as compile,$ as default,f as group,i as lookup,d as nary,A as operator,t as parse,y as prop,n as token,c as unary};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "subscript",
3
- "version": "8.1.3",
3
+ "version": "8.2.1",
4
4
  "description": "Fast and tiny expression evaluator with minimal syntax.",
5
5
  "main": "subscript.js",
6
6
  "module": "subscript.js",
package/subscript.js CHANGED
@@ -16,7 +16,7 @@ import './feature/assign.js'
16
16
  import compile from './src/compile.js'
17
17
  import parse from './src/parse.js'
18
18
 
19
- export * from './src/parse.js'
20
- export * from './src/compile.js'
19
+ export { parse, access, binary, unary, nary, group, lookup, token } from './src/parse.js'
20
+ export { compile, operator, prop } from './src/compile.js'
21
21
 
22
22
  export default s => compile(parse(s))
package/subscript.min.js CHANGED
@@ -1 +1 @@
1
- let t,e,r=r=>(t=0,e=r,r=a(),e[t]?s():r||""),s=(r="Bad syntax",s=e.slice(0,t).split("\n"),o=s.pop())=>{let a=e.slice(t-108,t).split("\n").pop(),n=e.slice(t,t+108).split("\n").shift();throw EvalError(`${r} at ${s.length}:${o.length} \`${t>=108?"…":""}${a}▶${n}\``,"font-weight: bold")},o=(r=1,s=t,o)=>{if("number"==typeof r)t+=r;else for(;o=r(e.charCodeAt(t));)t+=o;return e.slice(s,t)},a=(e=0,o,a,n,p,c)=>{for(;(a=r.space())&&(p=((c=l[a])&&c(n,e))??(!n&&r.id()));)n=p;return o&&(a==o?t++:s()),n},n=t=>t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122||36==t||95==t||t>=192&&215!=t&&247!=t;r.id=()=>o(n);let p=r.space=r=>{for(;(r=e.charCodeAt(t))<=32;)t++;return r},l=[],c=(r,s=32,o,a=r.charCodeAt(0),p=r.length,c=l[a],i=r.toUpperCase()!==r)=>l[a]=(a,l,d=t)=>l<s&&(p<2||e.substr(t,p)==r)&&(!i||!n(e.charCodeAt(t+p)))&&(t+=p,o(a,l))||(t=d,c?.(a,l)),i=(t,e,r=!1)=>c(t,e,((s,o)=>s&&(o=a(e-(r?.5:0)))&&[t,s,o])),d=(t,e,r)=>c(t,e,(s=>r?s&&[t,s]:!s&&(s=a(e-.5))&&[t,s])),f=(t,e)=>{c(t,e,((r,s)=>(s=a(e),(!r||r[0]!==t)&&(r=[t,r]),r.push(s),r)))},h=(t,e)=>c(t[0],e,(e=>!e&&[t,a(0,t.charCodeAt(1))])),u=(t,e)=>c(t[0],e,(e=>e&&[t[0],e,a(0,t.charCodeAt(1))]));const A=t=>t?s():[,(t=+o((t=>46===t||t>=48&&t<=57||(69===t||101===t?2:0))))!=t?s():t];l[46]=t=>!t&&A();for(let t=48;t<=57;t++)l[t]=A;const C={n:"\n",r:"\r",t:"\t",b:"\b",f:"\f",v:"\v"},g=r=>(a,n,p="")=>{for(a&&s("Unexpected string"),o();(n=e.charCodeAt(t))-r;)92===n?(o(),n=o(),p+=C[n]||n):p+=o();return o()||s("Bad string"),[,p]};l[34]=g(34),l[39]=g(39);const m=t=>Array.isArray(t)?t[0]?y[t[0]](...t.slice(1)):()=>t[1]:m.id(t);m.id=t=>e=>e?.[t];const y={},$=(t,e,r=y[t])=>y[t]=(...t)=>e(...t)||r&&r(...t),b=(t,e,r,o,a)=>"()"===t[0]?b(t[1],e,r):"string"==typeof t?r=>e(r,t,r):"."===t[0]?(o=m(t[1]),a=t[2],t=>e(o(t),a,t)):"["===t[0]?(o=m(t[1]),a=m(t[2]),t=>e(o(t),a(t),t)):r?(t=m(t),r=>e([t(r)],0,r)):()=>s("Bad left value");u("()",17),$("(",((t,e,r)=>(r=e?","===e[0]?(e=e.slice(1).map((t=>t?m(t):err())),t=>e.map((e=>e(t)))):(e=m(e),t=>[e(t)]):()=>[],b(t,((t,e,s)=>t[e](...r(s))),!0)))),u("[]",17),$("[",((t,e)=>e?(t=m(t),e=m(e),r=>t(r)[e(r)]):err())),i(".",17),$(".",((t,e)=>(t=m(t),e=e[0]?e:e[1],r=>t(r)[e]))),h("()",17),$("()",(t=>(!t&&s("Empty ()"),m(t))));const v=(...t)=>(t=t.map(m),e=>t.map((t=>t(e))).pop());f(",",1),$(",",v),f(";",1),$(";",v),i("*",12),$("*",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)*e(r)))),i("/",12),$("/",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)/e(r)))),i("%",12),$("%",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)%e(r)))),i("*=",2,!0),$("*=",((t,e)=>(e=m(e),b(t,((t,r,s)=>t[r]*=e(s)))))),i("/=",2,!0),$("/=",((t,e)=>(e=m(e),b(t,((t,r,s)=>t[r]/=e(s)))))),i("%=",2,!0),$("%=",((t,e)=>(e=m(e),b(t,((t,r,s)=>t[r]%=e(s)))))),d("+",14),$("+",((t,e)=>!e&&(t=m(t),e=>+t(e)))),d("-",14),$("-",((t,e)=>!e&&(t=m(t),e=>-t(e)))),i("+",11),$("+",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)+e(r)))),i("-",11),$("-",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)-e(r)))),i("+=",2,!0),$("+=",((t,e)=>(e=m(e),b(t,((t,r,s)=>t[r]+=e(s)))))),i("-=",2,!0),$("-=",((t,e)=>(e=m(e),b(t,((t,r,s)=>t[r]-=e(s)))))),c("++",15,(t=>t?["++-",t]:["++",a(14)])),$("++",(t=>b(t,((t,e,r)=>++t[e])))),$("++-",(t=>b(t,((t,e,r)=>t[e]++)))),c("--",15,(t=>t?["--+",t]:["--",a(14)])),$("--",(t=>b(t,((t,e,r)=>--t[e])))),$("--+",(t=>b(t,((t,e,r)=>t[e]--)))),d("~",14),$("~",((t,e)=>!e&&(t=m(t),e=>~t(e)))),i("|",5),$("|",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)|e(r)))),i("&",7),$("&",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)&e(r)))),i("^",6),$("^",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)^e(r)))),i(">>",10),$(">>",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)>>e(r)))),i("<<",10),$("<<",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)<<e(r)))),i("==",8),$("==",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)==e(r)))),i("!=",8),$("!=",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)!=e(r)))),i(">",8),$(">",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)>e(r)))),i("<",8),$("<",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)<e(r)))),i(">=",8),$(">=",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)>=e(r)))),i("<=",8),$("<=",((t,e)=>e&&(t=m(t),e=m(e),r=>t(r)<=e(r)))),d("!",14),$("!",((t,e)=>!e&&(t=m(t),e=>!t(e)))),i("||",3),$("||",((t,e)=>(t=m(t),e=m(e),r=>t(r)||e(r)))),i("&&",4),$("&&",((t,e)=>(t=m(t),e=m(e),r=>t(r)&&e(r)))),i("=",2,!0),$("=",((t,e)=>(e=m(e),b(t,((t,r,s)=>t[r]=e(s))))));var x=t=>m(r(t));export{u as access,i as binary,m as compile,e as cur,x as default,s as err,a as expr,h as group,t as idx,n as isId,l as lookup,f as nary,$ as operator,y as operators,r as parse,b as prop,o as skip,p as space,c as token,d as unary};
1
+ let t,e,r=r=>(t=0,e=r,r=a(),e[t]?o():r||""),o=(r="Bad syntax",o=e.slice(0,t).split("\n"),s=o.pop())=>{let a=e.slice(t-108,t).split("\n").pop(),n=e.slice(t,t+108).split("\n").shift();throw EvalError(`${r} at ${o.length}:${s.length} \`${t>=108?"…":""}${a}▶${n}\``,"font-weight: bold")},s=(r=1,o=t,s)=>{if("number"==typeof r)t+=r;else for(;s=r(e.charCodeAt(t));)t+=s;return e.slice(o,t)},a=(e=0,s,a,n,l,c)=>{for(;(a=r.space())&&(l=((c=p[a])&&c(n,e))??(!n&&r.id()));)n=l;return s&&(a==s?t++:o()),n},n=t=>t>=48&&t<=57||t>=65&&t<=90||t>=97&&t<=122||36==t||95==t||t>=192&&215!=t&&247!=t;r.id=()=>s(n),r.space=r=>{for(;(r=e.charCodeAt(t))<=32;)t++;return r};let p=[],l=(r,o=32,s,a=r.charCodeAt(0),l=r.length,c=p[a],i=r.toUpperCase()!==r)=>p[a]=(a,p,d=t)=>p<o&&(l<2||e.substr(t,l)==r)&&(!i||!n(e.charCodeAt(t+l)))&&(t+=l,s(a,p))||(t=d,c?.(a,p)),c=(t,e,r=!1)=>l(t,e,((o,s)=>o&&(s=a(e-(r?.5:0)))&&[t,o,s])),i=(t,e,r)=>l(t,e,(o=>r?o&&[t,o]:!o&&(o=a(e-.5))&&[t,o])),d=(t,e)=>{l(t,e,((r,o)=>(o=a(e),(!r||r[0]!==t)&&(r=[t,r]),r.push(o),r)))},f=(t,e)=>l(t[0],e,(e=>!e&&[t,a(0,t.charCodeAt(1))])),h=(t,e)=>l(t[0],e,(e=>e&&[t[0],e,a(0,t.charCodeAt(1))]));const u=t=>t?o():[,(t=+s((t=>46===t||t>=48&&t<=57||(69===t||101===t?2:0))))!=t?o():t];p[46]=t=>!t&&u();for(let t=48;t<=57;t++)p[t]=u;const A={n:"\n",r:"\r",t:"\t",b:"\b",f:"\f",v:"\v"},C=r=>(a,n,p="")=>{for(a&&o("Unexpected string"),s();(n=e.charCodeAt(t))-r;)92===n?(s(),n=s(),p+=A[n]||n):p+=s();return s()||o("Bad string"),[,p]};p[34]=C(34),p[39]=C(39);const g=t=>Array.isArray(t)?t[0]?m[t[0]](...t.slice(1)):()=>t[1]:g.id(t);g.id=t=>e=>e?.[t];const m={},y=(t,e,r=m[t])=>m[t]=(...t)=>e(...t)||r&&r(...t),$=(t,e,r,s,a)=>"()"===t[0]?$(t[1],e,r):"string"==typeof t?r=>e(r,t,r):"."===t[0]?(s=g(t[1]),a=t[2],t=>e(s(t),a,t)):"["===t[0]?(s=g(t[1]),a=g(t[2]),t=>e(s(t),a(t),t)):r?(t=g(t),r=>e([t(r)],0,r)):()=>o("Bad left value");h("()",17),y("(",((t,e,r)=>(r=e?","===e[0]?(e=e.slice(1).map((t=>t?g(t):err())),t=>e.map((e=>e(t)))):(e=g(e),t=>[e(t)]):()=>[],$(t,((t,e,o)=>t[e](...r(o))),!0)))),h("[]",17),y("[",((t,e)=>e?(t=g(t),e=g(e),r=>t(r)[e(r)]):err())),c(".",17),y(".",((t,e)=>(t=g(t),e=e[0]?e:e[1],r=>t(r)[e]))),f("()",17),y("()",(t=>(!t&&o("Empty ()"),g(t))));const b=(...t)=>(t=t.map(g),e=>t.map((t=>t(e))).pop());d(",",1),y(",",b),d(";",1),y(";",b),c("*",12),y("*",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)*e(r)))),c("/",12),y("/",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)/e(r)))),c("%",12),y("%",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)%e(r)))),c("*=",2,!0),y("*=",((t,e)=>(e=g(e),$(t,((t,r,o)=>t[r]*=e(o)))))),c("/=",2,!0),y("/=",((t,e)=>(e=g(e),$(t,((t,r,o)=>t[r]/=e(o)))))),c("%=",2,!0),y("%=",((t,e)=>(e=g(e),$(t,((t,r,o)=>t[r]%=e(o)))))),i("+",14),y("+",((t,e)=>!e&&(t=g(t),e=>+t(e)))),i("-",14),y("-",((t,e)=>!e&&(t=g(t),e=>-t(e)))),c("+",11),y("+",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)+e(r)))),c("-",11),y("-",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)-e(r)))),c("+=",2,!0),y("+=",((t,e)=>(e=g(e),$(t,((t,r,o)=>t[r]+=e(o)))))),c("-=",2,!0),y("-=",((t,e)=>(e=g(e),$(t,((t,r,o)=>t[r]-=e(o)))))),l("++",15,(t=>t?["++-",t]:["++",a(14)])),y("++",(t=>$(t,((t,e,r)=>++t[e])))),y("++-",(t=>$(t,((t,e,r)=>t[e]++)))),l("--",15,(t=>t?["--+",t]:["--",a(14)])),y("--",(t=>$(t,((t,e,r)=>--t[e])))),y("--+",(t=>$(t,((t,e,r)=>t[e]--)))),i("~",14),y("~",((t,e)=>!e&&(t=g(t),e=>~t(e)))),c("|",5),y("|",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)|e(r)))),c("&",7),y("&",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)&e(r)))),c("^",6),y("^",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)^e(r)))),c(">>",10),y(">>",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)>>e(r)))),c("<<",10),y("<<",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)<<e(r)))),c("==",8),y("==",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)==e(r)))),c("!=",8),y("!=",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)!=e(r)))),c(">",9),y(">",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)>e(r)))),c("<",9),y("<",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)<e(r)))),c(">=",9),y(">=",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)>=e(r)))),c("<=",9),y("<=",((t,e)=>e&&(t=g(t),e=g(e),r=>t(r)<=e(r)))),i("!",14),y("!",((t,e)=>!e&&(t=g(t),e=>!t(e)))),c("||",3),y("||",((t,e)=>(t=g(t),e=g(e),r=>t(r)||e(r)))),c("&&",4),y("&&",((t,e)=>(t=g(t),e=g(e),r=>t(r)&&e(r)))),c("=",2,!0),y("=",((t,e)=>(e=g(e),$(t,((t,r,o)=>t[r]=e(o))))));var v=t=>g(r(t));export{h as access,c as binary,g as compile,v as default,f as group,p as lookup,d as nary,y as operator,r as parse,$ as prop,l as token,i as unary};
package/feature/in.js DELETED
@@ -1,6 +0,0 @@
1
- import { binary } from "../src/parse.js";
2
- import { compile, operator } from "../src/compile.js";
3
- import { PREC_COMP } from "../src/const.js";
4
-
5
- // a in b
6
- binary('in', PREC_COMP), operator('in', (a, b) => b && (a = compile(a), b = compile(b), ctx => a(ctx) in b(ctx)))