/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/llparse-frontend@3.0.0/lib/frontend.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Frontend=exports.DEFAULT_MAX_TABLE_WIDTH=exports.DEFAULT_MIN_TABLE_SIZE=exports.ContainerWrap=exports.Container=exports.SpanField=exports.Identifier=exports.source=exports.transform=exports.node=exports.code=void 0;const assert=require("assert"),debugAPI=require("debug"),source=require("llparse-builder");exports.source=source;const frontend=require("./namespace/frontend"),container_1=require("./container");Object.defineProperty(exports,"Container",{enumerable:!0,get:function(){return container_1.Container}}),Object.defineProperty(exports,"ContainerWrap",{enumerable:!0,get:function(){return container_1.ContainerWrap}});const span_field_1=require("./span-field");Object.defineProperty(exports,"SpanField",{enumerable:!0,get:function(){return span_field_1.SpanField}});const trie_1=require("./trie"),utils_1=require("./utils");Object.defineProperty(exports,"Identifier",{enumerable:!0,get:function(){return utils_1.Identifier}});const enumerator_1=require("./enumerator"),peephole_1=require("./peephole"),debug=debugAPI("llparse:translator");var frontend_1=require("./namespace/frontend");Object.defineProperty(exports,"code",{enumerable:!0,get:function(){return frontend_1.code}}),Object.defineProperty(exports,"node",{enumerable:!0,get:function(){return frontend_1.node}}),Object.defineProperty(exports,"transform",{enumerable:!0,get:function(){return frontend_1.transform}}),exports.DEFAULT_MIN_TABLE_SIZE=32,exports.DEFAULT_MAX_TABLE_WIDTH=4;class Frontend{constructor(e,n,t={}){this.prefix=e,this.implementation=n,this.id=new utils_1.Identifier(this.prefix+"__n_"),this.codeId=new utils_1.Identifier(this.prefix+"__c_"),this.map=new Map,this.spanMap=new Map,this.codeCache=new Map,this.resumptionTargets=new Set,this.options={maxTableElemWidth:void 0===t.maxTableElemWidth?exports.DEFAULT_MAX_TABLE_WIDTH:t.maxTableElemWidth,minTableSize:void 0===t.minTableSize?exports.DEFAULT_MIN_TABLE_SIZE:t.minTableSize},assert(0<this.options.maxTableElemWidth,"Invalid `options.maxTableElemWidth`, must be positive")}compile(e,n){debug("checking loops");(new source.LoopChecker).check(e),debug("allocating spans");const t=(new source.SpanAllocator).allocate(e).concurrency.map(((e,n)=>{const t=new span_field_1.SpanField(n,e.map((e=>this.translateSpanCode(e.callback))));for(const n of e)this.spanMap.set(n,t);return t}));debug("translating");let o=this.translate(e);debug("enumerating");const r=new enumerator_1.Enumerator;let s=r.getAllNodes(o);debug("peephole optimization");o=(new peephole_1.Peephole).optimize(o,s),debug("re-enumerating"),s=r.getAllNodes(o),debug("registering resumption targets"),this.resumptionTargets.add(o);for(const e of s)this.registerNode(e);return{prefix:this.prefix,properties:n,resumptionTargets:this.resumptionTargets,root:o,spans:t}}getResumptionTargets(){return this.resumptionTargets}translate(e){if(this.map.has(e))return this.map.get(e);const n=()=>this.id.id(e.name),t=this.implementation.node;let o;if(e instanceof source.node.Error)o=new t.Error(new frontend.node.Error(n(),e.code,e.reason));else if(e instanceof source.node.Pause)o=new t.Pause(new frontend.node.Pause(n(),e.code,e.reason));else if(e instanceof source.node.Consume)o=new t.Consume(new frontend.node.Consume(n(),e.field));else if(e instanceof source.node.SpanStart)o=new t.SpanStart(new frontend.node.SpanStart(n(),this.spanMap.get(e.span),this.translateSpanCode(e.span.callback)));else if(e instanceof source.node.SpanEnd)o=new t.SpanEnd(new frontend.node.SpanEnd(n(),this.spanMap.get(e.span),this.translateSpanCode(e.span.callback)));else if(e instanceof source.node.Invoke)assert("match"===e.code.signature||"value"===e.code.signature,"Passing `span` callback to `invoke` is not allowed"),o=new t.Invoke(new frontend.node.Invoke(n(),this.translateCode(e.code)));else{if(!(e instanceof source.node.Match))throw new Error(`Unknown node type for "${e.name}" ${e.constructor.toString()}`);o=this.translateMatch(e)}const r=e.getOtherwiseEdge();if(Array.isArray(o)){assert(e instanceof source.node.Match);const n=e;if(assert.notStrictEqual(r,void 0,`Node "${e.name}" has no \`.otherwise()\``),void 0!==r)for(const e of o)e.ref.otherwise||e.ref.setOtherwise(this.translate(r.node),r.noAdvance);const t=this.translateTransform(n.getTransform());for(const e of o)e.ref.setTransform(t);return assert(o.length>=1),o[0]}{const n=o;if(assert(n.ref instanceof frontend.node.Node),this.map.set(e,n),void 0!==r?n.ref.setOtherwise(this.translate(r.node),r.noAdvance):assert(e instanceof source.node.Error,`Node "${e.name}" has no \`.otherwise()\``),n.ref instanceof frontend.node.Invoke)for(const t of e)n.ref.addEdge(t.key,this.translate(t.node));else assert.strictEqual(Array.from(e).length,0);return n}}registerNode(e){const n=this.implementation.node;e instanceof n.Consume||e instanceof n.Empty||e instanceof n.Sequence||e instanceof n.Single||e instanceof n.SpanStart||e instanceof n.TableLookup?this.resumptionTargets.add(e):(e instanceof n.Pause||e instanceof n.SpanEnd)&&this.resumptionTargets.add(e.ref.otherwise.node)}translateMatch(e){const n=new trie_1.Trie(e.name),t=(e.getOtherwiseEdge(),n.build(Array.from(e)));if(void 0===t)return new this.implementation.node.Empty(new frontend.node.Empty(this.id.id(e.name)));const o=[];return this.translateTrie(e,t,o),assert(o.length>=1),o}translateTrie(e,n,t){if(n instanceof trie_1.TrieEmpty)return assert(this.map.has(e)),this.translate(n.node);if(n instanceof trie_1.TrieSingle)return this.translateSingle(e,n,t);if(n instanceof trie_1.TrieSequence)return this.translateSequence(e,n,t);throw new Error("Unknown trie node")}translateSingle(e,n,t){const o=this.maybeTableLookup(e,n,t);if(void 0!==o)return o;const r=new this.implementation.node.Single(new frontend.node.Single(this.id.id(e.name)));t.push(r),this.map.has(e)||this.map.set(e,r);for(const o of n.children){const n=this.translateTrie(e,o.node,t);r.ref.addEdge({key:o.key,noAdvance:o.noAdvance,node:n,value:o.node instanceof trie_1.TrieEmpty?o.node.value:void 0})}const s=n.otherwise;return s&&r.ref.setOtherwise(this.translateTrie(e,s,t),!0,s.value),r}maybeTableLookup(e,n,t){if(n.children.length<this.options.minTableSize)return void debug('not enough children of "%s" to allocate table, got %d need %d',e.name,n.children.length,this.options.minTableSize);const o=new Map;if(!n.children.every((n=>{if(!(n.node instanceof trie_1.TrieEmpty))return debug('non-leaf trie child of "%s" prevents table allocation',e.name),!1;const t=n.node;if(void 0!==t.value)return debug('value passing trie leaf of "%s" prevents table allocation',e.name),!1;const r=t.node;if(!o.has(r))return o.set(r,{keys:[n.key],noAdvance:n.noAdvance,trie:t}),!0;const s=o.get(r);return s.noAdvance!==n.noAdvance?(debug('noAdvance mismatch in a trie leaf of "%s" prevents table allocation',e.name),!1):(s.keys.push(n.key),!0)})))return;if(o.size>=1<<this.options.maxTableElemWidth)return void debug('too many different trie targets of "%s" for a table allocation',e.name);const r=new this.implementation.node.TableLookup(new frontend.node.TableLookup(this.id.id(e.name)));return t.push(r),this.map.has(e)||this.map.set(e,r),o.forEach((n=>{const o=this.translateTrie(e,n.trie,t);r.ref.addEdge({keys:n.keys,noAdvance:n.noAdvance,node:o})})),debug('optimized "%s" to a table lookup node',e.name),r}translateSequence(e,n,t){const o=new this.implementation.node.Sequence(new frontend.node.Sequence(this.id.id(e.name),n.select));t.push(o),this.map.has(e)||this.map.set(e,o);const r=this.translateTrie(e,n.child,t),s=n.child instanceof trie_1.TrieEmpty?n.child.value:void 0;return o.ref.setEdge(r,s),o}translateCode(e){const n=this.codeId.id(e.name).name,t=this.implementation.code;let o;if(e instanceof source.code.IsEqual)o=new t.IsEqual(new frontend.code.IsEqual(n,e.field,e.value));else if(e instanceof source.code.Load)o=new t.Load(new frontend.code.Load(n,e.field));else if(e instanceof source.code.MulAdd){const r=new frontend.code.MulAdd(n,e.field,{base:e.options.base,max:e.options.max,signed:void 0===e.options.signed||e.options.signed});o=new t.MulAdd(r)}else if(e instanceof source.code.And)o=new t.And(new frontend.code.Or(n,e.field,e.value));else if(e instanceof source.code.Or)o=new t.Or(new frontend.code.Or(n,e.field,e.value));else if(e instanceof source.code.Store)o=new t.Store(new frontend.code.Store(n,e.field));else if(e instanceof source.code.Test)o=new t.Test(new frontend.code.Test(n,e.field,e.value));else if(e instanceof source.code.Update)o=new t.Update(new frontend.code.Update(n,e.field,e.value));else if(e instanceof source.code.Span)o=new t.Span(new frontend.code.Span(e.name));else if(e instanceof source.code.Match)o=new t.Match(new frontend.code.Match(e.name));else{if(!(e instanceof source.code.Value))throw new Error(`Unsupported code: "${e.name}"`);o=new t.Value(new frontend.code.Value(e.name))}return this.codeCache.has(o.ref.cacheKey)?this.codeCache.get(o.ref.cacheKey):(this.codeCache.set(o.ref.cacheKey,o),o)}translateSpanCode(e){return this.translateCode(e)}translateTransform(e){const n=this.implementation.transform;if(void 0===e)return new n.ID(new frontend.transform.ID);if("to_lower"===e.name)return new n.ToLower(new frontend.transform.ToLower);if("to_lower_unsafe"===e.name)return new n.ToLowerUnsafe(new frontend.transform.ToLowerUnsafe);throw new Error(`Unsupported transform: "${e.name}"`)}}exports.Frontend=Frontend;
//# sourceMappingURL=/sm/357a70fd2d4e7590e661f8c9c94552f75f33c06cc66006bbb15802a7799f2fa4.map