subscript 9.2.0 → 10.0.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/README.md +115 -169
- package/feature/access.js +67 -7
- package/feature/accessor.js +49 -0
- package/feature/asi.js +15 -0
- package/feature/async.js +45 -0
- package/feature/block.js +41 -0
- package/feature/class.js +69 -0
- package/feature/collection.js +40 -0
- package/feature/comment.js +25 -5
- package/feature/destruct.js +33 -0
- package/feature/function.js +44 -0
- package/feature/group.js +39 -9
- package/feature/if.js +23 -38
- package/feature/literal.js +13 -0
- package/feature/loop.js +107 -106
- package/feature/module.js +42 -0
- package/feature/number.js +41 -38
- package/feature/op/arithmetic.js +29 -0
- package/feature/op/arrow.js +33 -0
- package/feature/op/assign-logical.js +33 -0
- package/feature/op/assignment.js +47 -0
- package/feature/op/bitwise-unsigned.js +17 -0
- package/feature/op/bitwise.js +29 -0
- package/feature/op/comparison.js +19 -0
- package/feature/op/defer.js +15 -0
- package/feature/op/equality.js +16 -0
- package/feature/op/identity.js +15 -0
- package/feature/op/increment.js +23 -0
- package/feature/op/logical.js +21 -0
- package/feature/op/membership.js +17 -0
- package/feature/op/nullish.js +13 -0
- package/feature/op/optional.js +61 -0
- package/feature/op/pow.js +19 -0
- package/feature/op/range.js +26 -0
- package/feature/op/spread.js +15 -0
- package/feature/op/ternary.js +15 -0
- package/feature/op/type.js +18 -0
- package/feature/op/unary.js +41 -0
- package/feature/prop.js +34 -0
- package/feature/regex.js +31 -0
- package/feature/seq.js +21 -0
- package/feature/string.js +24 -17
- package/feature/switch.js +48 -0
- package/feature/template.js +39 -0
- package/feature/try.js +57 -0
- package/feature/unit.js +35 -0
- package/feature/var.js +51 -41
- package/jessie.js +31 -0
- package/jessie.min.js +8 -0
- package/justin.js +39 -48
- package/justin.min.js +8 -4
- package/package.json +15 -16
- package/parse.js +153 -0
- package/subscript.d.ts +45 -5
- package/subscript.js +62 -22
- package/subscript.min.js +5 -4
- package/util/bundle.js +507 -0
- package/util/stringify.js +172 -0
- package/feature/add.js +0 -22
- package/feature/array.js +0 -11
- package/feature/arrow.js +0 -23
- package/feature/assign.js +0 -11
- package/feature/bitwise.js +0 -11
- package/feature/bool.js +0 -5
- package/feature/call.js +0 -15
- package/feature/compare.js +0 -11
- package/feature/control.js +0 -142
- package/feature/increment.js +0 -11
- package/feature/logic.js +0 -11
- package/feature/mult.js +0 -25
- package/feature/object.js +0 -17
- package/feature/optional.js +0 -23
- package/feature/pow.js +0 -5
- package/feature/shift.js +0 -12
- package/feature/spread.js +0 -6
- package/feature/ternary.js +0 -10
- package/src/compile.d.ts +0 -17
- package/src/compile.js +0 -28
- package/src/const.js +0 -45
- package/src/parse.d.ts +0 -22
- package/src/parse.js +0 -113
- package/src/stringify.js +0 -27
- /package/{LICENSE → license} +0 -0
package/package.json
CHANGED
|
@@ -1,37 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "subscript",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "10.0.0",
|
|
4
|
+
"description": "Tiny expression parser & evaluator",
|
|
5
5
|
"main": "subscript.js",
|
|
6
6
|
"module": "subscript.js",
|
|
7
7
|
"browser": "subscript.js",
|
|
8
8
|
"types": "./subscript.d.ts",
|
|
9
9
|
"exports": {
|
|
10
10
|
".": "./subscript.js",
|
|
11
|
-
"./parse": "./
|
|
12
|
-
"./compile": "./src/compile.js",
|
|
13
|
-
"./const": "./src/const.js",
|
|
11
|
+
"./parse": "./parse.js",
|
|
14
12
|
"./justin": "./justin.js",
|
|
15
|
-
"./
|
|
16
|
-
"./feature/*": "./feature/*"
|
|
13
|
+
"./jessie": "./jessie.js",
|
|
14
|
+
"./feature/*": "./feature/*",
|
|
15
|
+
"./util/*": "./util/*"
|
|
17
16
|
},
|
|
18
17
|
"type": "module",
|
|
19
18
|
"files": [
|
|
20
|
-
"src",
|
|
21
19
|
"feature",
|
|
20
|
+
"util",
|
|
22
21
|
"subscript.js",
|
|
23
22
|
"subscript.min.js",
|
|
24
23
|
"subscript.d.ts",
|
|
24
|
+
"parse.js",
|
|
25
25
|
"justin.js",
|
|
26
|
-
"justin.min.js"
|
|
26
|
+
"justin.min.js",
|
|
27
|
+
"jessie.js",
|
|
28
|
+
"jessie.min.js"
|
|
27
29
|
],
|
|
28
|
-
"directories": {
|
|
29
|
-
"lib": "lib",
|
|
30
|
-
"src": "src",
|
|
31
|
-
"test": "test"
|
|
32
|
-
},
|
|
33
30
|
"scripts": {
|
|
34
|
-
"build": "esbuild subscript.js --bundle --minify --format=esm --outfile=subscript.min.js && esbuild justin.js --bundle --minify --format=esm --outfile=justin.min.js",
|
|
31
|
+
"build": "esbuild subscript.js --bundle --minify --format=esm --outfile=subscript.min.js && esbuild justin.js --bundle --minify --format=esm --outfile=justin.min.js && esbuild jessie.js --bundle --minify --format=esm --outfile=jessie.min.js",
|
|
35
32
|
"prepublishOnly": "npm run build",
|
|
36
33
|
"test": "node test/test.js",
|
|
37
34
|
"check-types": "tsc --noEmit subscript.d.ts"
|
|
@@ -79,6 +76,8 @@
|
|
|
79
76
|
"devDependencies": {
|
|
80
77
|
"@playwright/test": "^1.57.0",
|
|
81
78
|
"esbuild": "^0.27.2",
|
|
82
|
-
"
|
|
79
|
+
"jsep": "^1.4.0",
|
|
80
|
+
"terser": "^5.44.1",
|
|
81
|
+
"tst": "^9.2.0"
|
|
83
82
|
}
|
|
84
83
|
}
|
package/parse.js
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
// Pratt parser core + operator registry + compile
|
|
2
|
+
// Character codes
|
|
3
|
+
const SPACE = 32;
|
|
4
|
+
|
|
5
|
+
// current string, index
|
|
6
|
+
export let idx, cur,
|
|
7
|
+
|
|
8
|
+
// parse input string to AST
|
|
9
|
+
parse = s => (idx = 0, cur = s, parse.newline = false, s = expr(), cur[idx] ? err() : s || ''),
|
|
10
|
+
|
|
11
|
+
// display error with context
|
|
12
|
+
err = (msg = 'Unexpected token', at = idx,
|
|
13
|
+
lines = cur.slice(0, at).split('\n'),
|
|
14
|
+
last = lines.pop(),
|
|
15
|
+
before = cur.slice(Math.max(0, at - 40), at),
|
|
16
|
+
ptr = '\u032D',
|
|
17
|
+
chr = (cur[at] || '∅') + ptr,
|
|
18
|
+
after = cur.slice(at + 1, at + 20)
|
|
19
|
+
) => {
|
|
20
|
+
throw SyntaxError(`${msg} at ${lines.length + 1}:${last.length + 1}\n${(cur[at-41]!=='\n' ? '...' : '') +before}${chr}${after}`)
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
// attach location to node (returns node for chaining)
|
|
24
|
+
loc = (node, at = idx) => (Array.isArray(node) && (node.loc = at), node),
|
|
25
|
+
|
|
26
|
+
// advance until condition meets
|
|
27
|
+
next = (is, from = idx, l) => {
|
|
28
|
+
while (l = is(cur.charCodeAt(idx))) idx += l;
|
|
29
|
+
return cur.slice(from, idx);
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
// advance n characters
|
|
33
|
+
skip = (n=1) => cur[idx+=n],
|
|
34
|
+
|
|
35
|
+
// set position (for backtracking)
|
|
36
|
+
seek = n => idx = n,
|
|
37
|
+
|
|
38
|
+
// a + b - c
|
|
39
|
+
expr = (prec = 0, end) => {
|
|
40
|
+
let cc, token, newNode, fn, prevReserved = parse.reserved, nl;
|
|
41
|
+
if (end) parse.asi && (parse.newline = false);
|
|
42
|
+
parse.reserved = 0;
|
|
43
|
+
|
|
44
|
+
while (
|
|
45
|
+
(cc = parse.space()) &&
|
|
46
|
+
(nl = parse.newline, 1) &&
|
|
47
|
+
cc !== end &&
|
|
48
|
+
(newNode =
|
|
49
|
+
((fn = lookup[cc]) && fn(token, prec)) ??
|
|
50
|
+
(parse.asi && token && nl && (newNode = parse.asi(token, prec, expr))) ??
|
|
51
|
+
(!token && !parse.reserved && next(parse.id))
|
|
52
|
+
)
|
|
53
|
+
) token = newNode, parse.reserved = 0;
|
|
54
|
+
parse.reserved = prevReserved;
|
|
55
|
+
|
|
56
|
+
if (end) cc == end ? idx++ : err('Unclosed ' + String.fromCharCode(end - (end > 42 ? 2 : 1)));
|
|
57
|
+
|
|
58
|
+
return token;
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
// skip space chars, return first non-space character
|
|
62
|
+
space = parse.space = (cc, from = idx) => {
|
|
63
|
+
while ((cc = cur.charCodeAt(idx)) <= SPACE) {
|
|
64
|
+
if (parse.asi && cc === 10) parse.newline = true
|
|
65
|
+
idx++
|
|
66
|
+
}
|
|
67
|
+
return cc
|
|
68
|
+
},
|
|
69
|
+
|
|
70
|
+
// is char an id?
|
|
71
|
+
id = parse.id = c =>
|
|
72
|
+
(c >= 48 && c <= 57) ||
|
|
73
|
+
(c >= 65 && c <= 90) ||
|
|
74
|
+
(c >= 97 && c <= 122) ||
|
|
75
|
+
c == 36 || c == 95 ||
|
|
76
|
+
(c >= 192 && c != 215 && c != 247),
|
|
77
|
+
|
|
78
|
+
// check if word matches at current position
|
|
79
|
+
word = (w, l = w.length) => cur.substr(idx, l) === w && !parse.id(cur.charCodeAt(idx + l)),
|
|
80
|
+
|
|
81
|
+
// parse (...) group
|
|
82
|
+
parens = () => (skip(), expr(0, 41)),
|
|
83
|
+
|
|
84
|
+
// operator lookup table
|
|
85
|
+
lookup = [],
|
|
86
|
+
|
|
87
|
+
// create operator checker/mapper
|
|
88
|
+
token = (
|
|
89
|
+
op,
|
|
90
|
+
prec = SPACE,
|
|
91
|
+
map,
|
|
92
|
+
c = op.charCodeAt(0),
|
|
93
|
+
l = op.length,
|
|
94
|
+
prev = lookup[c],
|
|
95
|
+
word = op.toUpperCase() !== op,
|
|
96
|
+
matched, r
|
|
97
|
+
) => lookup[c] = (a, curPrec, curOp, from = idx) =>
|
|
98
|
+
(matched = curOp,
|
|
99
|
+
(curOp ?
|
|
100
|
+
op == curOp :
|
|
101
|
+
(l < 2 || (op.charCodeAt(1) === cur.charCodeAt(idx + 1) && (l < 3 || cur.substr(idx, l) == op))) && (!word || !parse.id(cur.charCodeAt(idx + l))) && (matched = curOp = op)
|
|
102
|
+
) &&
|
|
103
|
+
curPrec < prec &&
|
|
104
|
+
(idx += l, (r = map(a)) ? loc(r, from) : (idx = from, matched = 0, word && r !== false && (parse.reserved = 1), !word && !prev && err()), r)
|
|
105
|
+
) ||
|
|
106
|
+
prev?.(a, curPrec, matched),
|
|
107
|
+
|
|
108
|
+
binary = (op, prec, right = false) => token(op, prec, (a, b) => a && (b = expr(prec - (right ? .5 : 0))) && [op, a, b]),
|
|
109
|
+
|
|
110
|
+
unary = (op, prec, post) => token(op, prec, a => post ? (a && [op, a]) : (!a && (a = expr(prec - .5)) && [op, a])),
|
|
111
|
+
|
|
112
|
+
literal = (op, val) => token(op, 200, a => !a && [, val]),
|
|
113
|
+
|
|
114
|
+
nary = (op, prec, right) => {
|
|
115
|
+
token(op, prec,
|
|
116
|
+
(a, b) => (
|
|
117
|
+
b = expr(prec - (right ? .5 : 0)),
|
|
118
|
+
(
|
|
119
|
+
(a?.[0] !== op) && (a = [op, a || null]),
|
|
120
|
+
b?.[0] === op ? a.push(...b.slice(1)) : a.push(b || null),
|
|
121
|
+
a
|
|
122
|
+
))
|
|
123
|
+
)
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
group = (op, prec) => token(op[0], prec, a => (!a && [op, expr(0, op.charCodeAt(1)) || null])),
|
|
127
|
+
|
|
128
|
+
access = (op, prec) => token(op[0], prec, a => (a && [op, a, expr(0, op.charCodeAt(1)) || null]));
|
|
129
|
+
|
|
130
|
+
// === Compile: AST → Evaluator ===
|
|
131
|
+
|
|
132
|
+
// Current node being compiled (for error location)
|
|
133
|
+
let curNode;
|
|
134
|
+
|
|
135
|
+
// Compile error with source location
|
|
136
|
+
const compileErr = (msg = 'Compile error', node = curNode) => err(msg, node?.loc);
|
|
137
|
+
|
|
138
|
+
// Operator registry
|
|
139
|
+
export const operators = {};
|
|
140
|
+
|
|
141
|
+
// Register an operator (chainable for overrides)
|
|
142
|
+
export const operator = (op, fn, prev = operators[op]) =>
|
|
143
|
+
(operators[op] = (...args) => fn(...args) || prev?.(...args));
|
|
144
|
+
|
|
145
|
+
// Compile AST to evaluator function
|
|
146
|
+
export const compile = node => (
|
|
147
|
+
curNode = node,
|
|
148
|
+
!Array.isArray(node) ? (node === undefined ? () => undefined : ctx => ctx?.[node]) :
|
|
149
|
+
node[0] === undefined ? (v => () => v)(node[1]) :
|
|
150
|
+
operators[node[0]]?.(...node.slice(1)) ?? compileErr(`Unknown operator: ${node[0]}`)
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
export default parse;
|
package/subscript.d.ts
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
|
-
|
|
1
|
+
// AST node types
|
|
2
|
+
export type Identifier = string;
|
|
3
|
+
export type Literal = [undefined, any];
|
|
4
|
+
export type Operation = [string, ...AST[]];
|
|
5
|
+
export type AST = Identifier | Literal | Operation;
|
|
2
6
|
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
|
|
7
|
+
// Evaluator function
|
|
8
|
+
export type Evaluator = (ctx?: any) => any;
|
|
9
|
+
|
|
10
|
+
// Operator compiler: receives args, returns evaluator
|
|
11
|
+
export type Operator = (...args: AST[]) => Evaluator | undefined;
|
|
12
|
+
|
|
13
|
+
// Parse exports
|
|
14
|
+
export let idx: number;
|
|
15
|
+
export let cur: string;
|
|
16
|
+
export function parse(s: string): AST;
|
|
17
|
+
export function err(msg?: string, at?: number): never;
|
|
18
|
+
export function loc<T>(node: T, at?: number): T;
|
|
19
|
+
export function next(is: (c: number) => number, from?: number): string;
|
|
20
|
+
export function skip(n?: number): string;
|
|
21
|
+
export function seek(n: number): number;
|
|
22
|
+
export function expr(prec?: number, end?: number): AST;
|
|
23
|
+
export const lookup: ((a: AST, prec: number, op?: string) => AST)[];
|
|
24
|
+
export function token(op: string, prec?: number, map?: (a: AST) => AST): void;
|
|
25
|
+
export function binary(op: string, prec: number, right?: boolean): void;
|
|
26
|
+
export function unary(op: string, prec: number, post?: boolean): void;
|
|
27
|
+
export function literal(op: string, val: any): void;
|
|
28
|
+
export function nary(op: string, prec: number, right?: boolean): void;
|
|
29
|
+
export function group(op: string, prec: number): void;
|
|
30
|
+
export function access(op: string, prec: number): void;
|
|
31
|
+
|
|
32
|
+
// Compile exports
|
|
33
|
+
export const operators: Record<string, Operator>;
|
|
34
|
+
export function operator(op: string, fn: Operator): void;
|
|
35
|
+
export function compile(node: AST): Evaluator;
|
|
36
|
+
|
|
37
|
+
// Default export
|
|
38
|
+
export default parse;
|
|
39
|
+
|
|
40
|
+
// subscript template tag
|
|
41
|
+
interface Subscript {
|
|
42
|
+
(strings: TemplateStringsArray, ...values: any[]): Evaluator;
|
|
43
|
+
(s: string): Evaluator;
|
|
44
|
+
}
|
|
45
|
+
declare const subscript: Subscript;
|
|
46
|
+
export { subscript };
|
package/subscript.js
CHANGED
|
@@ -1,24 +1,64 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* subscript: Minimal expression parser + compiler
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* subscript`a + b`(ctx) - template tag, returns compiled evaluator
|
|
6
|
+
* subscript`a + ${x}`(ctx) - interpolations: primitives, objects, AST nodes
|
|
7
|
+
* subscript('a + b')(ctx) - direct call (no caching)
|
|
8
|
+
*
|
|
9
|
+
* For more features:
|
|
10
|
+
* import { parse, compile } from 'subscript/justin.js' // + JSON, arrows, templates
|
|
11
|
+
* import { parse, compile } from 'subscript/jessie.js' // + statements, functions
|
|
3
12
|
*/
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import './feature/
|
|
7
|
-
import './feature/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import './feature/
|
|
11
|
-
import './feature/
|
|
12
|
-
import './feature/
|
|
13
|
-
import './feature/
|
|
14
|
-
import './feature/
|
|
15
|
-
import './feature/
|
|
16
|
-
import './feature/
|
|
17
|
-
|
|
18
|
-
import
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
|
|
13
|
+
|
|
14
|
+
// Expression features
|
|
15
|
+
import './feature/number.js'; // Decimal numbers: 123, 1.5, 1e3
|
|
16
|
+
import './feature/string.js'; // Double-quoted strings with escapes
|
|
17
|
+
|
|
18
|
+
// Operators (C-family common set) - order matters for token chain performance
|
|
19
|
+
import './feature/op/assignment.js'; // = += -= *= /= %= |= &= ^= >>= <<=
|
|
20
|
+
import './feature/op/logical.js'; // ! && ||
|
|
21
|
+
import './feature/op/bitwise.js'; // ~ | & ^ >> <<
|
|
22
|
+
import './feature/op/comparison.js'; // < > <= >=
|
|
23
|
+
import './feature/op/equality.js'; // == !=
|
|
24
|
+
import './feature/op/arithmetic.js'; // + - * / %
|
|
25
|
+
import './feature/op/increment.js'; // ++ --
|
|
26
|
+
|
|
27
|
+
import './feature/group.js'; // Grouping: (a), sequences: a, b; a; b
|
|
28
|
+
import './feature/access.js'; // Property access: a.b, a[b], f(), [a,b]
|
|
29
|
+
|
|
30
|
+
import { parse, compile } from './parse.js';
|
|
31
|
+
export * from './parse.js';
|
|
32
|
+
|
|
33
|
+
// Cache for compiled templates (keyed by template strings array reference)
|
|
34
|
+
const cache = new WeakMap();
|
|
35
|
+
|
|
36
|
+
// Template tag: subscript`a + b` or subscript`a + ${x}`
|
|
37
|
+
const subscript = (strings, ...values) =>
|
|
38
|
+
// Direct call subscript('code') - strings is just a string
|
|
39
|
+
typeof strings === 'string' ? compile(parse(strings)) :
|
|
40
|
+
// Template literal - use cache
|
|
41
|
+
cache.get(strings) || cache.set(strings, compileTemplate(strings, values)).get(strings);
|
|
42
|
+
|
|
43
|
+
// Compile template with placeholders (using Private Use Area chars)
|
|
44
|
+
const PUA = 0xE000;
|
|
45
|
+
const compileTemplate = (strings, values) => {
|
|
46
|
+
const code = strings.reduce((acc, s, i) => acc + (i ? String.fromCharCode(PUA + i - 1) : '') + s, '');
|
|
47
|
+
const ast = parse(code);
|
|
48
|
+
const inject = node => {
|
|
49
|
+
if (typeof node === 'string' && node.length === 1) {
|
|
50
|
+
let i = node.charCodeAt(0) - PUA, v;
|
|
51
|
+
if (i >= 0 && i < values.length) return v = values[i], isAST(v) ? v : [, v];
|
|
52
|
+
}
|
|
53
|
+
return Array.isArray(node) ? node.map(inject) : node;
|
|
54
|
+
};
|
|
55
|
+
return compile(inject(ast));
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// Detect AST node vs regular value
|
|
59
|
+
// AST: string (identifier), or array with string/undefined first element
|
|
60
|
+
const isAST = v =>
|
|
61
|
+
typeof v === 'string' ||
|
|
62
|
+
(Array.isArray(v) && (typeof v[0] === 'string' || v[0] === undefined));
|
|
63
|
+
|
|
64
|
+
export default subscript;
|
package/subscript.min.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
var
|
|
2
|
-
`),t
|
|
3
|
-
|
|
4
|
-
`)
|
|
1
|
+
var f,A,m=r=>(f=0,A=r,m.newline=!1,r=h(),A[f]?S():r||""),S=(r="Unexpected token",e=f,t=A.slice(0,e).split(`
|
|
2
|
+
`),n=t.pop(),o=A.slice(Math.max(0,e-40),e),s="\u032D",u=(A[e]||"\u2205")+s,c=A.slice(e+1,e+20))=>{throw SyntaxError(`${r} at ${t.length+1}:${n.length+1}
|
|
3
|
+
${(A[e-41]!==`
|
|
4
|
+
`?"...":"")+o}${u}${c}`)},W=(r,e=f)=>(Array.isArray(r)&&(r.loc=e),r),N=(r,e=f,t)=>{for(;t=r(A.charCodeAt(f));)f+=t;return A.slice(e,f)},g=(r=1)=>A[f+=r],z=r=>f=r,h=(r=0,e)=>{let t,n,o,s,u=m.reserved,c;for(e&&m.asi&&(m.newline=!1),m.reserved=0;(t=m.space())&&(c=m.newline,1)&&t!==e&&(o=((s=C[t])&&s(n,r))??(m.asi&&n&&c&&(o=m.asi(n,r,h)))??(!n&&!m.reserved&&N(m.id)));)n=o,m.reserved=0;return m.reserved=u,e&&(t==e?f++:S("Unclosed "+String.fromCharCode(e-(e>42?2:1)))),n},k=m.space=(r,e=f)=>{for(;(r=A.charCodeAt(f))<=32;)m.asi&&r===10&&(m.newline=!0),f++;return r},Zr=m.id=r=>r>=48&&r<=57||r>=65&&r<=90||r>=97&&r<=122||r==36||r==95||r>=192&&r!=215&&r!=247,b=(r,e=r.length)=>A.substr(f,e)===r&&!m.id(A.charCodeAt(f+e)),M=()=>(g(),h(0,41)),C=[],v=(r,e=32,t,n=r.charCodeAt(0),o=r.length,s=C[n],u=r.toUpperCase()!==r,c,d)=>C[n]=(y,U,L,x=f)=>(c=L,(L?r==L:(o<2||r.charCodeAt(1)===A.charCodeAt(f+1)&&(o<3||A.substr(f,o)==r))&&(!u||!m.id(A.charCodeAt(f+o)))&&(c=L=r))&&U<e&&(f+=o,(d=t(y))?W(d,x):(f=x,c=0,u&&d!==!1&&(m.reserved=1),!u&&!s&&S()),d)||s?.(y,U,c)),p=(r,e,t=!1)=>v(r,e,(n,o)=>n&&(o=h(e-(t?.5:0)))&&[r,n,o]),T=(r,e,t)=>v(r,e,n=>t?n&&[r,n]:!n&&(n=h(e-.5))&&[r,n]),qr=(r,e)=>v(r,200,t=>!t&&[,e]),J=(r,e,t)=>{v(r,e,(n,o)=>(o=h(e-(t?.5:0)),n?.[0]!==r&&(n=[r,n||null]),o?.[0]===r?n.push(...o.slice(1)):n.push(o||null),n))},rr=(r,e)=>v(r[0],e,t=>!t&&[r,h(0,r.charCodeAt(1))||null]),V=(r,e)=>v(r[0],e,t=>t&&[r,t,h(0,r.charCodeAt(1))||null]),er,Cr=(r="Compile error",e=er)=>S(r,e?.loc),G={},l=(r,e,t=G[r])=>G[r]=(...n)=>e(...n)||t?.(...n),i=r=>(er=r,Array.isArray(r)?r[0]===void 0?(e=>()=>e)(r[1]):G[r[0]]?.(...r.slice(1))??Cr(`Unknown operator: ${r[0]}`):r===void 0?()=>{}:e=>e?.[r]);var $=46,_=48,F=57,Er=69,gr=101,wr=43,Sr=45,kr=97,vr=102,Tr=65,Ir=70,Y=r=>[,(r=+N(e=>e===$&&A.charCodeAt(f+1)!==$||e>=_&&e<=F||((e===Er||e===gr)&&((e=A.charCodeAt(f+1))>=_&&e<=F||e===wr||e===Sr)?2:0)))!=r?S():r],Rr={2:r=>r===48||r===49,8:r=>r>=48&&r<=55,16:r=>r>=_&&r<=F||r>=kr&&r<=vr||r>=Tr&&r<=Ir};m.number=null;C[$]=r=>!r&&A.charCodeAt(f+1)!==$&&Y();for(let r=_;r<=F;r++)C[r]=e=>e?void 0:Y();C[_]=r=>{if(r)return;let e=m.number;if(e){for(let[t,n]of Object.entries(e))if(t[0]==="0"&&A[f+1]?.toLowerCase()===t[1])return g(2),[,parseInt(N(Rr[n]),n)]}return Y()};var Nr=92,tr=34,nr=39,Ur={n:`
|
|
5
|
+
`,r:"\r",t:" ",b:"\b",f:"\f",v:"\v"},or=r=>(e,t,n="")=>{if(!(e||!m.string?.[String.fromCharCode(r)]))return g(),N(o=>o-r&&(o===Nr?(n+=Ur[A[f+1]]||A[f+1],2):(n+=A[f],1))),A[f]===String.fromCharCode(r)?g():S("Bad string"),[,n]};C[tr]=or(tr);C[nr]=or(nr);m.string={'"':!0};var w=20;p("=",w,!0);p("+=",w,!0);p("-=",w,!0);p("*=",w,!0);p("/=",w,!0);p("%=",w,!0);p("|=",w,!0);p("&=",w,!0);p("^=",w,!0);p(">>=",w,!0);p("<<=",w,!0);var E=(r,e,t,n)=>typeof r=="string"?o=>e(o,r,o):r[0]==="."?(t=i(r[1]),n=r[2],o=>e(t(o),n,o)):r[0]==="[]"&&r.length===3?(t=i(r[1]),n=i(r[2]),o=>e(t(o),n(o),o)):r[0]==="()"&&r.length===2?E(r[1],e):(()=>{throw Error("Invalid assignment target")})();l("=",(r,e)=>(e=i(e),E(r,(t,n,o)=>t[n]=e(o))));l("+=",(r,e)=>(e=i(e),E(r,(t,n,o)=>t[n]+=e(o))));l("-=",(r,e)=>(e=i(e),E(r,(t,n,o)=>t[n]-=e(o))));l("*=",(r,e)=>(e=i(e),E(r,(t,n,o)=>t[n]*=e(o))));l("/=",(r,e)=>(e=i(e),E(r,(t,n,o)=>t[n]/=e(o))));l("%=",(r,e)=>(e=i(e),E(r,(t,n,o)=>t[n]%=e(o))));l("|=",(r,e)=>(e=i(e),E(r,(t,n,o)=>t[n]|=e(o))));l("&=",(r,e)=>(e=i(e),E(r,(t,n,o)=>t[n]&=e(o))));l("^=",(r,e)=>(e=i(e),E(r,(t,n,o)=>t[n]^=e(o))));l(">>=",(r,e)=>(e=i(e),E(r,(t,n,o)=>t[n]>>=e(o))));l("<<=",(r,e)=>(e=i(e),E(r,(t,n,o)=>t[n]<<=e(o))));var Mr=30,_r=40,ir=140;p("!",ir);T("!",ir);p("||",Mr);p("&&",_r);l("!",r=>(r=i(r),e=>!r(e)));l("||",(r,e)=>(r=i(r),e=i(e),t=>r(t)||e(t)));l("&&",(r,e)=>(r=i(r),e=i(e),t=>r(t)&&e(t)));var Pr=50,Br=60,Dr=70,sr=100,Lr=140;p("|",Pr);p("&",Dr);p("^",Br);p(">>",sr);p("<<",sr);T("~",Lr);l("~",r=>(r=i(r),e=>~r(e)));l("|",(r,e)=>(r=i(r),e=i(e),t=>r(t)|e(t)));l("&",(r,e)=>(r=i(r),e=i(e),t=>r(t)&e(t)));l("^",(r,e)=>(r=i(r),e=i(e),t=>r(t)^e(t)));l(">>",(r,e)=>(r=i(r),e=i(e),t=>r(t)>>e(t)));l("<<",(r,e)=>(r=i(r),e=i(e),t=>r(t)<<e(t)));var X=90;p("<",X);p(">",X);p("<=",X);p(">=",X);l(">",(r,e)=>(r=i(r),e=i(e),t=>r(t)>e(t)));l("<",(r,e)=>(r=i(r),e=i(e),t=>r(t)<e(t)));l(">=",(r,e)=>(r=i(r),e=i(e),t=>r(t)>=e(t)));l("<=",(r,e)=>(r=i(r),e=i(e),t=>r(t)<=e(t)));var lr=80;p("==",lr);p("!=",lr);l("==",(r,e)=>(r=i(r),e=i(e),t=>r(t)==e(t)));l("!=",(r,e)=>(r=i(r),e=i(e),t=>r(t)!=e(t)));var ur=110,Z=120,fr=140;p("+",ur);p("-",ur);p("*",Z);p("/",Z);p("%",Z);T("+",fr);T("-",fr);l("+",(r,e)=>e!==void 0?(r=i(r),e=i(e),t=>r(t)+e(t)):(r=i(r),t=>+r(t)));l("-",(r,e)=>e!==void 0?(r=i(r),e=i(e),t=>r(t)-e(t)):(r=i(r),t=>-r(t)));l("*",(r,e)=>(r=i(r),e=i(e),t=>r(t)*e(t)));l("/",(r,e)=>(r=i(r),e=i(e),t=>r(t)/e(t)));l("%",(r,e)=>(r=i(r),e=i(e),t=>r(t)%e(t)));var O=150;v("++",O,r=>r?["++",r,null]:["++",h(O-1)]);v("--",O,r=>r?["--",r,null]:["--",h(O-1)]);var q=(r,e,t,n)=>typeof r=="string"?o=>e(o,r):r[0]==="."?(t=i(r[1]),n=r[2],o=>e(t(o),n)):r[0]==="[]"&&r.length===3?(t=i(r[1]),n=i(r[2]),o=>e(t(o),n(o))):r[0]==="()"&&r.length===2?q(r[1],e):(()=>{throw Error("Invalid increment target")})();l("++",(r,e)=>q(r,e===null?(t,n)=>t[n]++:(t,n)=>++t[n]));l("--",(r,e)=>q(r,e===null?(t,n)=>t[n]--:(t,n)=>--t[n]));var pr=5,$r=123,Fr=125,I=(r,e,t,n=r.charCodeAt(0),o=r.length,s=C[n],u)=>C[n]=(c,d,y,U=f)=>!c&&(y?r==y:(o<2||A.substr(f,o)==r)&&(y=r))&&d<e&&!m.id(A.charCodeAt(f+o))&&(z(f+o),(u=t())?W(u,U):(z(U),!s&&S()),u)||s?.(c,d,y);var P=()=>k()!==$r?h(pr+.5):(g(),["block",h(pr-.5,Fr)||null]);l("block",r=>r===void 0?()=>{}:(r=i(r),e=>r(e)));var B=(r,e,t)=>{if(typeof r=="string"){t[r]=e;return}let[n,...o]=r;if(n==="{}")for(let s of o){let u,c,d;s[0]==="="?[,[,u,c],d]=s:[,u,c]=s;let y=e[u];y===void 0&&d&&(y=i(d)(t)),B(c,y,t)}else if(n==="[]"){let s=0;for(let u of o){if(u===null){s++;continue}if(Array.isArray(u)&&u[0]==="..."){t[u[1]]=e.slice(s);break}let c=u,d;Array.isArray(u)&&u[0]==="="&&([,c,d]=u);let y=e[s++];y===void 0&&d&&(y=i(d)(t)),B(c,y,t)}}};var Q=Symbol("break"),H=Symbol("continue"),mr=Symbol("return"),D=(r,e)=>{try{return{v:r(e)}}catch(t){if(t?.type===Q)return{b:1};if(t?.type===H)return{c:1};if(t?.type===mr)return{r:1,v:t.value};throw t}},R=5,Xr=125,Or=59;I("while",R+1,()=>(k(),["while",M(),P()]));I("do",R+1,()=>(r=>(k(),g(5),k(),["do",r,M()]))(P()));I("for",R+1,()=>(k(),b("await")?(g(5),k(),["for await",M(),P()]):["for",M(),P()]));I("break",R+1,()=>["break"]);I("continue",R+1,()=>["continue"]);I("return",R+1,()=>{m.asi&&(m.newline=!1),k();let r=A.charCodeAt(f);return!r||r===Xr||r===Or||m.newline?["return"]:["return",h(R)]});l("while",(r,e)=>(r=i(r),e=i(e),t=>{let n,o;for(;r(t)&&!(n=D(e,t)).b;){if(n.r)return n.v;n.c||(o=n.v)}return o}));l("do",(r,e)=>(r=i(r),e=i(e),t=>{let n,o;do{if((n=D(r,t)).b)break;if(n.r)return n.v;n.c||(o=n.v)}while(e(t));return o}));l("for",(r,e)=>{if(Array.isArray(r)&&r[0]===";"){let[,t,n,o]=r;return t=t?i(t):null,n=n?i(n):()=>!0,o=o?i(o):null,e=i(e),s=>{let u,c;for(t?.(s);n(s)&&!(u=D(e,s)).b;o?.(s)){if(u.r)return u.v;u.c||(c=u.v)}return c}}if(Array.isArray(r)&&(r[0]==="in"||r[0]==="of")){let[t,n,o]=r;if(Array.isArray(n)&&(n[0]==="let"||n[0]==="const"||n[0]==="var")&&(n=n[1]),t==="in")return Hr(n,o,e);if(t==="of")return Qr(n,o,e)}});var Qr=(r,e,t)=>{e=i(e),t=i(t);let n=Array.isArray(r);return o=>{let s,u,c=n?null:o[r];for(let d of e(o)){if(n?B(r,d,o):o[r]=d,(s=D(t,o)).b)break;if(s.r)return s.v;s.c||(u=s.v)}return n||(o[r]=c),u}},Hr=(r,e,t)=>{e=i(e),t=i(t);let n=Array.isArray(r);return o=>{let s,u,c=n?null:o[r];for(let d in e(o)){if(n?B(r,d,o):o[r]=d,(s=D(t,o)).b)break;if(s.r)return s.v;s.c||(u=s.v)}return n||(o[r]=c),u}};l("break",()=>()=>{throw{type:Q}});l("continue",()=>()=>{throw{type:H}});l("return",r=>(r=r!==void 0?i(r):null,e=>{throw{type:mr,value:r?.(e)}}));var cr=r=>r?.[0]==="_"&&r[1]==="_"||r==="constructor"||r==="prototype",j=170;V("[]",j);p(".",j);V("()",j);var a=r=>{throw Error(r)};l("[]",(r,e)=>e===void 0?(r=r?r[0]===","?r.slice(1):[r]:[],r=r.map(t=>t==null?(()=>{}):t[0]==="..."?(t=i(t[1]),n=>t(n)):(t=i(t),n=>[t(n)])),t=>r.flatMap(n=>n(t))):(e==null&&a("Missing index"),r=i(r),e=i(e),t=>{let n=e(t);return cr(n)?void 0:r(t)[n]}));l(".",(r,e)=>(r=i(r),e=e[0]?e:e[1],cr(e)?()=>{}:t=>r(t)[e]));l("()",(r,e)=>{if(e===void 0)return r==null?a("Empty ()"):i(r);let t=o=>o?.[0]===","&&o.slice(1).some(s=>s==null||t(s));t(e)&&a("Empty argument");let n=e?e[0]===","?(e=e.slice(1).map(i),o=>e.map(s=>s(o))):(e=i(e),o=>[e(o)]):()=>[];return K(r,(o,s,u)=>o[s](...n(u)),!0)});var Kr=r=>{throw Error(r)},K=(r,e,t,n,o)=>r==null?Kr("Empty ()"):r[0]==="()"&&r.length==2?K(r[1],e,t):typeof r=="string"?s=>e(s,r,s):r[0]==="."?(n=i(r[1]),o=r[2],s=>e(n(s),o,s)):r[0]==="?."?(n=i(r[1]),o=r[2],s=>{let u=n(s);return u==null?void 0:e(u,o,s)}):r[0]==="[]"&&r.length===3?(n=i(r[1]),o=i(r[2]),s=>e(n(s),o(s),s)):r[0]==="?.[]"?(n=i(r[1]),o=i(r[2]),s=>{let u=n(s);return u==null?void 0:e(u,o(s),s)}):(r=i(r),s=>e([r(s)],0,s));var Gr=5,Wr=10,zr=170;rr("()",zr);J(",",Wr);J(";",Gr,!0);var Ar=r=>{throw Error(r)};l("()",(r,e)=>{if(e===void 0)return r==null?Ar("Empty ()"):i(r);let t=o=>o?.[0]===","&&o.slice(1).some(s=>s==null||t(s));t(e)&&Ar("Empty argument");let n=e?e[0]===","?(e=e.slice(1).map(i),o=>e.map(s=>s(o))):(e=i(e),o=>[e(o)]):()=>[];return K(r,(o,s,u)=>o[s](...n(u)),!0)});var dr=(...r)=>(r=r.map(i),e=>{let t;for(let n of r)try{t=n(e)}catch(o){throw(o?.type===Q||o?.type===H)&&(o.value=t),o}return t});l(",",dr);l(";",dr);var hr=new WeakMap,Jr=(r,...e)=>typeof r=="string"?i(m(r)):hr.get(r)||hr.set(r,Vr(r,e)).get(r),yr=57344,Vr=(r,e)=>{let t=r.reduce((s,u,c)=>s+(c?String.fromCharCode(yr+c-1):"")+u,""),n=m(t),o=s=>{if(typeof s=="string"&&s.length===1){let u=s.charCodeAt(0)-yr,c;if(u>=0&&u<e.length)return c=e[u],Yr(c)?c:[,c]}return Array.isArray(s)?s.map(o):s};return i(o(n))},Yr=r=>typeof r=="string"||Array.isArray(r)&&(typeof r[0]=="string"||r[0]===void 0),De=Jr;export{V as access,p as binary,i as compile,A as cur,De as default,S as err,h as expr,rr as group,Zr as id,f as idx,qr as literal,W as loc,C as lookup,J as nary,N as next,l as operator,G as operators,M as parens,m as parse,z as seek,g as skip,k as space,v as token,T as unary,b as word};
|