UNPKG

@effectful/transducers-loose

Version:

@effectful/transducers built with faster generators

495 lines (494 loc) 14.1 kB
"use strict"; exports.__esModule = true; exports.run = exports.inject = exports.commit = exports.clean = exports.Root = exports.Placeholder = void 0; var Kit = require("./kit"); var _core = require("./core"); var M = require("@effectful/es-rt/opts/loose"); const Root = exports.Root = (0, _core.symbol)("match.root", "ctrl"); const Placeholder = exports.Placeholder = (0, _core.symbol)("match.placeholder", "ctrl"); const commit = exports.commit = Kit.pipe(Array.from, function (s) { var i, ctx = M.generator(), loop, r, p; ctx.step = f_1; ctx.$step = 3; return ctx; function f_1(p) { for (;;) { switch (ctx.$cur = ctx.$step) { case 3: loop = M.iterator(s); ctx.$step = 4; ctx.$step = 4; continue; case 4: if (!(loop = loop.step()).done) { i = loop.value; switch (i.type) { case Root: if (!i.value.v.match) { ctx.$step = 4; ctx.$step = 4; continue; } else { i = i.value.s ? (0, _core.enter)(i.pos, i.type, i.value.v) : (0, _core.leave)(i.pos, i.type, i.value.v); ctx.$step = 5; ctx.$step = 5; continue; } case Placeholder: if (!i.value.v.match) { ctx.$step = 4; ctx.$step = 4; continue; } else { ctx.$step = 5; ctx.$step = 5; continue; } default: ctx.$step = 5; ctx.$step = 5; continue; } } else { ctx.$step = 0; ctx.value = void 0; ctx.done = true; return ctx; } case 5: ctx.$step = 4; ctx.value = i; return ctx; case 0: ctx.$step = 0; ctx.value = p; ctx.done = true; return ctx; case 1: ctx.$step = 0; throw p; default: throw new Error("invalid state"); } } } }); const clean = function (s) { var i, ctx = M.generator(), loop, r, p; ctx.step = f_1; ctx.$step = 3; return ctx; function f_1(p) { for (;;) { switch (ctx.$cur = ctx.$step) { case 3: loop = M.iterator(s); ctx.$step = 4; ctx.$step = 4; continue; case 4: if (!(loop = loop.step()).done) { i = loop.value; switch (i.type) { case Root: case Placeholder: ctx.$step = 4; ctx.$step = 4; continue; default: ctx.$step = 4; ctx.value = i; return ctx; } } else { ctx.$step = 0; ctx.value = void 0; ctx.done = true; return ctx; } case 0: ctx.$step = 0; ctx.value = p; ctx.done = true; return ctx; case 1: ctx.$step = 0; throw p; default: throw new Error("invalid state"); } } } }; exports.clean = clean; const inject = exports.inject = Kit.curry(function matchInject(pattern, si) { var pats, starts, plen, plens, s, activePos, activeTok, activePh, activePat, level, i, aplen, p, x, pat, _plen, v, j, k, ph, name, _ph, _s, _v, _aplen, _p, ph1, _x, v1, _matchInject = M.generator(), loop, r, p1; _matchInject.step = matchInject_1; _matchInject.$step = 3; return _matchInject; function matchInject_1(p1) { var a, b; for (;;) { switch (_matchInject.$cur = _matchInject.$step) { case 3: pats = (Array.isArray(pattern) ? pattern : [pattern]).map(i => { return Kit.toArray(Kit.toks(_core.Tag.top, i)); }); starts = pats.map(i => i[0].type); plen = pats.length; plens = pats.map(i => i.length); s = Kit.auto(si); activePos = []; activeTok = []; activePh = []; activePat = []; level = 0; loop = M.iterator(s); _matchInject.$step = 4; _matchInject.$step = 4; continue; case 4: if (!(loop = loop.step()).done) { i = loop.value; if (i.enter) { level++; } aplen = activePos.length; p = 0; _matchInject.$step = 5; _matchInject.$step = 5; continue; } else { _matchInject.$step = 0; _matchInject.value = void 0; _matchInject.done = true; return _matchInject; } case 5: if (p < aplen) { x = activePos[p]; pat = activePat[p]; _plen = pat.length; v = activeTok[p]; if (x !== _plen && x !== -1 && activePh[p] == null) { j = pat[x++]; //during generation we may neglect emitting empty arrays if (j.pos !== i.pos && j.type === _core.Tag.Array && j.enter && !j.leave && pat[x].value === j.value) { x++; j = pat[x++]; } if (x < _plen && j.pos != i.pos && j.enter === i.enter) { activePos[p] = -1; v.match = false; _matchInject.$step = 10; _matchInject.$step = 10; continue; } else { if (j.enter) { switch (j.type) { case _core.Tag.ExpressionStatement: k = pat[x]; if (k.type === _core.Tag.Identifier) { if (k.value.node.name[0] === "$") { ph = activePh[p] = { v, level, name: k.value.node.name.substr(1) }; a = (0, _core.enter)(i.pos, Placeholder, ph); _matchInject.$step = 6; _matchInject.value = a; return _matchInject; } else { _matchInject.$step = 8; _matchInject.$step = 8; continue; } } else { _matchInject.$step = 8; _matchInject.$step = 8; continue; } case _core.Tag.Identifier: case _core.Tag.StringLiteral: name = j.value.node.name || j.value.node.value; if (name[0] === "$") { _ph = activePh[p] = { v, level, name: name.substr(1) }; b = (0, _core.enter)(i.pos, Placeholder, _ph); _matchInject.$step = 7; _matchInject.value = b; return _matchInject; } else { _matchInject.$step = 8; _matchInject.$step = 8; continue; } default: _matchInject.$step = 8; _matchInject.$step = 8; continue; } } else { _matchInject.$step = 8; _matchInject.$step = 8; continue; } } } else { _matchInject.$step = 10; _matchInject.$step = 10; continue; } } else { if (i.enter) { _s = 0; _matchInject.$step = 11; _matchInject.$step = 11; continue; } else { _matchInject.$step = 14; _matchInject.$step = 14; continue; } } case 6: x++; x++; x++; activePos[p] = x; _matchInject.$step = 10; _matchInject.$step = 10; continue; case 7: if (!j.leave) { x++; } activePos[p] = x; _matchInject.$step = 10; _matchInject.$step = 10; continue; case 8: if (j.type !== i.type || j.value.node.name !== i.value.node.name || j.value.node.value !== i.value.node.value) { activePos[p] = -1; v.match = false; _matchInject.$step = 10; _matchInject.$step = 10; continue; } else { if (i.enter) { if (i.leave && !j.leave) { if (pat[x++].value !== j.value) { activePos[p] = -1; v.match = false; _matchInject.$step = 10; _matchInject.$step = 10; continue; } else { _matchInject.$step = 9; _matchInject.$step = 9; continue; } } else { if (j.leave) { if (s.cur().value === i.value) { s.read(); } } _matchInject.$step = 9; _matchInject.$step = 9; continue; } } else { _matchInject.$step = 9; _matchInject.$step = 9; continue; } } case 9: activePos[p] = x; _matchInject.$step = 10; _matchInject.$step = 10; continue; case 10: ++p; _matchInject.$step = 5; _matchInject.$step = 5; continue; case 11: if (_s < plen) { if (starts[_s] == i.type) { _v = { match: null, index: _s }; a = (0, _core.tok)(i.pos, Root, { s: true, v: _v }); _matchInject.$step = 12; _matchInject.value = a; return _matchInject; } else { _matchInject.$step = 13; _matchInject.$step = 13; continue; } } else { _matchInject.$step = 14; _matchInject.$step = 14; continue; } case 12: activePos.push(1); activeTok.push(_v); activePat.push(pats[_s]); _matchInject.$step = 13; _matchInject.$step = 13; continue; case 13: ++_s; _matchInject.$step = 11; _matchInject.$step = 11; continue; case 14: _matchInject.$step = 15; _matchInject.value = i; return _matchInject; case 15: if (i.leave) { _aplen = activePos.length; _p = _aplen - 1; _matchInject.$step = 16; _matchInject.$step = 16; continue; } else { _matchInject.$step = 4; _matchInject.$step = 4; continue; } case 16: if (_p >= 0) { ph1 = activePh[_p]; if (ph1 != null) { if (ph1.level === level) { a = (0, _core.leave)(i.pos, Placeholder, ph1); _matchInject.$step = 17; _matchInject.value = a; return _matchInject; } else { _matchInject.$step = 18; _matchInject.$step = 18; continue; } } else { _matchInject.$step = 18; _matchInject.$step = 18; continue; } } else { level--; _matchInject.$step = 4; _matchInject.$step = 4; continue; } case 17: ph1 = activePh[_p] = null; _matchInject.$step = 18; _matchInject.$step = 18; continue; case 18: if (ph1 == null) { _x = activePos[_p]; if (_x >= 0 && _x === activePat[_p].length) { activePos[_p] = -1; v1 = activeTok[_p]; v1.match = true; while (activePos[0] === -1) { activePos.shift(); activePh.shift(); activeTok.shift(); activePat.shift(); } a = (0, _core.tok)(i.pos, Root, { s: false, v: v1 }); _matchInject.$step = 19; _matchInject.value = a; return _matchInject; } else { _matchInject.$step = 19; _matchInject.$step = 19; continue; } } else { _matchInject.$step = 19; _matchInject.$step = 19; continue; } case 19: --_p; _matchInject.$step = 16; _matchInject.$step = 16; continue; case 0: _matchInject.$step = 0; _matchInject.value = p1; _matchInject.done = true; return _matchInject; case 1: _matchInject.$step = 0; throw p1; default: throw new Error("invalid state"); } } } }); const run = exports.run = Kit.curry(function (pat, si) { return commit(inject(pat, si)); }, true);