ohm-js
Version:
1 lines • 95 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).ohm={})}(this,(function(t){"use strict";function e(t){const e=t||"";return function(){throw new Error("this method "+e+" is abstract! (it has no implementation in class "+this.constructor.name+")")}}function r(t,e){if(!t)throw new Error(e||"Assertion failed")}function n(t,e,r){let n;Object.defineProperty(t,e,{get(){return n||(n=r.call(this)),n}})}function i(t,e){const r=[];for(;e-- >0;)r.push(t());return r}function s(t,e){return new Array(e+1).join(t)}function o(t,e){return i((()=>t),e)}function a(t){const e=[];for(let r=0;r<t.length;r++){const n=t[r];t.lastIndexOf(n)!==r&&e.indexOf(n)<0&&e.push(n)}return e}function u(t){const e=[];return t.forEach((t=>{e.indexOf(t)<0&&e.push(t)})),e}function c(t){const e=t[0];return e===e.toUpperCase()}function l(t){return!c(t)}function p(t,e,r){const n=r||" ";return t.length<e?s(n,e-t.length)+t:t}function h(){this.strings=[]}h.prototype.append=function(t){this.strings.push(t)},h.prototype.contents=function(){return this.strings.join("")};const m=t=>String.fromCodePoint(parseInt(t,16));function d(t){if("\\"!==t.charAt(0))return t;switch(t.charAt(1)){case"b":return"\b";case"f":return"\f";case"n":return"\n";case"r":return"\r";case"t":return"\t";case"v":return"\v";case"x":return m(t.slice(2,4));case"u":return"{"===t.charAt(2)?m(t.slice(3,-1)):m(t.slice(2,6));default:return t.charAt(1)}}function f(t){if(null==t)return String(t);const e=Object.prototype.toString.call(t);try{let r;return r=t.constructor&&t.constructor.name?t.constructor.name:0===e.indexOf("[object ")?e.slice(8,-1):typeof t,r+": "+JSON.stringify(String(t))}catch(t){return e}}function g(t,e="unexpected null value"){if(null==t)throw new Error(e);return t}var v=Object.freeze({__proto__:null,abstract:e,assert:r,defineLazyProperty:n,clone:function(t){return t?Object.assign({},t):t},repeatFn:i,repeatStr:s,repeat:o,getDuplicates:a,copyWithoutDuplicates:u,isSyntactic:c,isLexical:l,padLeft:p,StringBuffer:h,unescapeCodePoint:d,unexpectedObjToString:f,checkNotNull:g});const I={Lu:/\p{Lu}/u,Ll:/\p{Ll}/u,Lt:/\p{Lt}/u,Lm:/\p{Lm}/u,Lo:/\p{Lo}/u,Nl:/\p{Nl}/u,Nd:/\p{Nd}/u,Mn:/\p{Mn}/u,Mc:/\p{Mc}/u,Pc:/\p{Pc}/u,Zs:/\p{Zs}/u,L:/\p{Letter}/u,Ltmo:/\p{Lt}|\p{Lm}|\p{Lo}/u};class y{constructor(){if(this.constructor===y)throw new Error("PExpr cannot be instantiated -- it's abstract")}withSource(t){return t&&(this.source=t.trimmed()),this}}const x=Object.create(y.prototype),S=Object.create(y.prototype);class b extends y{constructor(t){super(),this.obj=t}}class _ extends y{constructor(t,e){super(),this.from=t,this.to=e,this.matchCodePoint=t.length>1||e.length>1}}class w extends y{constructor(t){super(),this.index=t}}class A extends y{constructor(t){super(),this.terms=t}}class N extends A{constructor(t,e,r){super([r,t.rules[e].body]),this.superGrammar=t,this.name=e,this.body=r}}class O extends A{constructor(t,e,r,n){const i=t.rules[e].body;super([...r,i,...n]),this.superGrammar=t,this.ruleName=e,this.expansionPos=r.length}}class L extends y{constructor(t){super(),this.factors=t}}class R extends y{constructor(t){super(),this.expr=t}}class F extends R{}class P extends R{}class E extends R{}F.prototype.operator="*",P.prototype.operator="+",E.prototype.operator="?",F.prototype.minNumMatches=0,P.prototype.minNumMatches=1,E.prototype.minNumMatches=0,F.prototype.maxNumMatches=Number.POSITIVE_INFINITY,P.prototype.maxNumMatches=Number.POSITIVE_INFINITY,E.prototype.maxNumMatches=1;class C extends y{constructor(t){super(),this.expr=t}}class T extends y{constructor(t){super(),this.expr=t}}class k extends y{constructor(t){super(),this.expr=t}}class D extends y{constructor(t,e=[]){super(),this.ruleName=t,this.args=e}isSyntactic(){return c(this.ruleName)}toMemoKey(){return this._memoKey||Object.defineProperty(this,"_memoKey",{value:this.toString()}),this._memoKey}}class j extends y{constructor(t){super(),this.category=t,this.pattern=I[t]}}function M(t,e){let r;return e?(r=new Error(e.getLineAndColumnMessage()+t),r.shortMessage=t,r.interval=e):r=new Error(t),r}function G(){return M("Interval sources don't match")}function B(t,e,r){return M("Rule "+t+" is not declared in grammar "+e,r)}function q(t,e,r,n){let i="Duplicate declaration for rule '"+t+"' in grammar '"+e+"'";return e!==r&&(i+=" (originally declared in '"+r+"')"),M(i,n)}function U(t,e,r,n){return M("Wrong number of parameters for rule "+t+" (expected "+e+", got "+r+")",n)}function $(t,e,r){return M("Duplicate parameter names in rule "+t+": "+e.join(", "),r)}function z(t,e){return M("Incorrect argument type: expected "+t,e.source)}function K(t,e){const r=e.length>0?e[e.length-1].args:[];let n="Nullable expression "+t.expr.substituteParams(r)+" is not allowed inside '"+t.operator+"' (possible infinite loop)";if(e.length>0){n+="\nApplication stack (most recent application last):\n"+e.map((t=>new D(t.ruleName,t.args))).join("\n")}return M(n,t.expr.source)}function H(t,e,r,n){return M("Rule "+t+" involves an alternation which has inconsistent arity (expected "+e+", got "+r+")",n.source)}function V(t){if(1===t.length)throw t[0];if(t.length>1)throw function(t){const e=t.map((t=>t.message));return M(["Errors:"].concat(e).join("\n- "),t[0].interval)}(t)}function W(t,e,r){const n=t.length;return(t.slice(0,r)+e+t.slice(r+e.length)).substr(0,n)}function J(...t){const e=this,{offset:n}=e,{repeatStr:i}=v,s=new h;s.append("Line "+e.lineNum+", col "+e.colNum+":\n");const o=function(t){let e=0;return t.map((t=>{const r=t.toString();return e=Math.max(e,r.length),r})).map((t=>p(t,e)))}([null==e.prevLine?0:e.lineNum-1,e.lineNum,null==e.nextLine?0:e.lineNum+1]),a=(t,e,r)=>{s.append(r+o[t]+" | "+e+"\n")};null!=e.prevLine&&a(0,e.prevLine," "),a(1,e.line,"> ");const u=e.line.length;let c=i(" ",u+1);for(let s=0;s<t.length;++s){let o=t[s][0],a=t[s][1];r(o>=0&&o<=a,"range start must be >= 0 and <= end");const l=n-e.colNum+1;o=Math.max(0,o-l),a=Math.min(a-l,u),c=W(c,i("~",a-o),o)}const l=2+o[1].length+3;return s.append(i(" ",l)),c=W(c,"^",e.colNum-1),s.append(c.replace(/ +$/,"")+"\n"),null!=e.nextLine&&a(2,e.nextLine," "),s.contents()}let Q=[];function Z(t){Q.push(t)}function Y(t,e){let r=1,n=1,i=0,s=0,o=null,a=null,u=-1;for(;i<e;){const e=t.charAt(i++);"\n"===e?(r++,n=1,u=s,s=i):"\r"!==e&&n++}let c=t.indexOf("\n",s);if(-1===c)c=t.length;else{const e=t.indexOf("\n",c+1);o=-1===e?t.slice(c):t.slice(c,e),o=o.replace(/^\r?\n/,"").replace(/\r$/,"")}u>=0&&(a=t.slice(u,s).replace(/\r?\n$/,""));return{offset:e,lineNum:r,colNum:n,line:t.slice(s,c).replace(/\r$/,""),prevLine:a,nextLine:o,toString:J}}function X(t,e,...r){return Y(t,e).toString(...r)}const tt=(()=>{let t=0;return e=>""+e+t++})();class et{constructor(t,e,r){this.sourceString=t,this.startIdx=e,this.endIdx=r}get contents(){return void 0===this._contents&&(this._contents=this.sourceString.slice(this.startIdx,this.endIdx)),this._contents}get length(){return this.endIdx-this.startIdx}coverageWith(...t){return et.coverage(...t,this)}collapsedLeft(){return new et(this.sourceString,this.startIdx,this.startIdx)}collapsedRight(){return new et(this.sourceString,this.endIdx,this.endIdx)}getLineAndColumn(){return Y(this.sourceString,this.startIdx)}getLineAndColumnMessage(){const t=[this.startIdx,this.endIdx];return X(this.sourceString,this.startIdx,t)}minus(t){if(this.sourceString!==t.sourceString)throw G();return this.startIdx===t.startIdx&&this.endIdx===t.endIdx?[]:this.startIdx<t.startIdx&&t.endIdx<this.endIdx?[new et(this.sourceString,this.startIdx,t.startIdx),new et(this.sourceString,t.endIdx,this.endIdx)]:this.startIdx<t.endIdx&&t.endIdx<this.endIdx?[new et(this.sourceString,t.endIdx,this.endIdx)]:this.startIdx<t.startIdx&&t.startIdx<this.endIdx?[new et(this.sourceString,this.startIdx,t.startIdx)]:[this]}relativeTo(t){if(this.sourceString!==t.sourceString)throw G();return r(this.startIdx>=t.startIdx&&this.endIdx<=t.endIdx,"other interval does not cover this one"),new et(this.sourceString,this.startIdx-t.startIdx,this.endIdx-t.startIdx)}trimmed(){const{contents:t}=this,e=this.startIdx+t.match(/^\s*/)[0].length,r=this.endIdx-t.match(/\s*$/)[0].length;return new et(this.sourceString,e,r)}subInterval(t,e){const r=this.startIdx+t;return new et(this.sourceString,r,r+e)}}et.coverage=function(t,...e){let{startIdx:r,endIdx:n}=t;for(const i of e){if(i.sourceString!==t.sourceString)throw G();r=Math.min(r,i.startIdx),n=Math.max(n,i.endIdx)}return new et(t.sourceString,r,n)};class rt{constructor(t){this.source=t,this.pos=0,this.examinedLength=0}atEnd(){const t=this.pos>=this.source.length;return this.examinedLength=Math.max(this.examinedLength,this.pos+1),t}next(){const t=this.source[this.pos++];return this.examinedLength=Math.max(this.examinedLength,this.pos),t}nextCharCode(){const t=this.next();return t&&t.charCodeAt(0)}nextCodePoint(){const t=this.source.slice(this.pos++).codePointAt(0);return t>65535&&(this.pos+=1),this.examinedLength=Math.max(this.examinedLength,this.pos),t}matchString(t,e){let r;if(e){for(r=0;r<t.length;r++){const e=this.next(),n=t[r];if(null==e||e.toUpperCase()!==n.toUpperCase())return!1}return!0}for(r=0;r<t.length;r++)if(this.next()!==t[r])return!1;return!0}sourceSlice(t,e){return this.source.slice(t,e)}interval(t,e){return new et(this.source,t,e||this.pos)}}class nt{constructor(t,e,r,i,s,o,a){this.matcher=t,this.input=e,this.startExpr=r,this._cst=i,this._cstOffset=s,this._rightmostFailurePosition=o,this._rightmostFailures=a,this.failed()&&(n(this,"message",(function(){const t="Expected "+this.getExpectedText();return X(this.input,this.getRightmostFailurePosition())+t})),n(this,"shortMessage",(function(){const t="expected "+this.getExpectedText(),e=Y(this.input,this.getRightmostFailurePosition());return"Line "+e.lineNum+", col "+e.colNum+": "+t})))}succeeded(){return!!this._cst}failed(){return!this.succeeded()}getRightmostFailurePosition(){return this._rightmostFailurePosition}getRightmostFailures(){if(!this._rightmostFailures){this.matcher.setInput(this.input);const t=this.matcher._match(this.startExpr,{tracing:!1,positionToRecordFailures:this.getRightmostFailurePosition()});this._rightmostFailures=t.getRightmostFailures()}return this._rightmostFailures}toString(){return this.succeeded()?"[match succeeded]":"[match failed at position "+this.getRightmostFailurePosition()+"]"}getExpectedText(){if(this.succeeded())throw new Error("cannot get expected text of a successful MatchResult");const t=new h;let e=this.getRightmostFailures();e=e.filter((t=>!t.isFluffy()));for(let r=0;r<e.length;r++)r>0&&(r===e.length-1?t.append(e.length>2?", or ":" or "):t.append(", ")),t.append(e[r].toString());return t.contents()}getInterval(){const t=this.getRightmostFailurePosition();return new et(this.input,t,t)}}class it{constructor(){this.applicationMemoKeyStack=[],this.memo={},this.maxExaminedLength=0,this.maxRightmostFailureOffset=-1,this.currentLeftRecursion=void 0}isActive(t){return this.applicationMemoKeyStack.indexOf(t.toMemoKey())>=0}enter(t){this.applicationMemoKeyStack.push(t.toMemoKey())}exit(){this.applicationMemoKeyStack.pop()}startLeftRecursion(t,e){e.isLeftRecursion=!0,e.headApplication=t,e.nextLeftRecursion=this.currentLeftRecursion,this.currentLeftRecursion=e;const{applicationMemoKeyStack:r}=this,n=r.indexOf(t.toMemoKey())+1,i=r.slice(n);e.isInvolved=function(t){return i.indexOf(t)>=0},e.updateInvolvedApplicationMemoKeys=function(){for(let t=n;t<r.length;t++){const e=r[t];this.isInvolved(e)||i.push(e)}}}endLeftRecursion(){this.currentLeftRecursion=this.currentLeftRecursion.nextLeftRecursion}shouldUseMemoizedResult(t){if(!t.isLeftRecursion)return!0;const{applicationMemoKeyStack:e}=this;for(let r=0;r<e.length;r++){const n=e[r];if(t.isInvolved(n))return!1}return!0}memoize(t,e){return this.memo[t]=e,this.maxExaminedLength=Math.max(this.maxExaminedLength,e.examinedLength),this.maxRightmostFailureOffset=Math.max(this.maxRightmostFailureOffset,e.rightmostFailureOffset),e}clearObsoleteEntries(t,e){if(t+this.maxExaminedLength<=e)return;const{memo:r}=this;this.maxExaminedLength=0,this.maxRightmostFailureOffset=-1,Object.keys(r).forEach((n=>{const i=r[n];t+i.examinedLength>e?delete r[n]:(this.maxExaminedLength=Math.max(this.maxExaminedLength,i.examinedLength),this.maxRightmostFailureOffset=Math.max(this.maxRightmostFailureOffset,i.rightmostFailureOffset))}))}}const st={succeeded:1,isRootNode:2,isImplicitSpaces:4,isMemoized:8,isHeadOfLeftRecursion:16,terminatesLR:32};function ot(t){return"string"==typeof t?t.replace(/ /g,"⋅").replace(/\t/g,"␉").replace(/\n/g,"␊").replace(/\r/g,"␍"):String(t)}class at{constructor(t,e,r,n,i,s,o){this.input=t,this.pos=this.pos1=e,this.pos2=r,this.source=new et(t,e,r),this.expr=n,this.bindings=s,this.children=o||[],this.terminatingLREntry=null,this._flags=i?st.succeeded:0}get displayString(){return this.expr.toDisplayString()}clone(){return this.cloneWithExpr(this.expr)}cloneWithExpr(t){const e=new at(this.input,this.pos,this.pos2,t,this.succeeded,this.bindings,this.children);return e.isHeadOfLeftRecursion=this.isHeadOfLeftRecursion,e.isImplicitSpaces=this.isImplicitSpaces,e.isMemoized=this.isMemoized,e.isRootNode=this.isRootNode,e.terminatesLR=this.terminatesLR,e.terminatingLREntry=this.terminatingLREntry,e}recordLRTermination(t,e){this.terminatingLREntry=new at(this.input,this.pos,this.pos2,this.expr,!1,[e],[t]),this.terminatingLREntry.terminatesLR=!0}walk(t,e){let r=t;function n(t,i,s){let o=!0;r.enter&&r.enter.call(e,t,i,s)===at.prototype.SKIP&&(o=!1),o&&(t.children.forEach((e=>{n(e,t,s+1)})),r.exit&&r.exit.call(e,t,i,s))}"function"==typeof r&&(r={enter:r}),this.isRootNode?this.children.forEach((t=>{n(t,null,0)})):n(this,null,0)}toString(){const t=new h;return this.walk(((e,r,n)=>{if(!e)return this.SKIP;if("Alt"!==e.expr.constructor.name){if(t.append(function(t,e,r){const n=ot(t.slice(e,e+r));return n.length<r?n+o(" ",r-n.length).join(""):n}(e.input,e.pos,10)+o(" ",2*n+1).join("")),t.append((e.succeeded?"✓":"✗")+" "+e.displayString),e.isHeadOfLeftRecursion&&t.append(" (LR)"),e.succeeded){const r=ot(e.source.contents);t.append(" ⇒ "),t.append("string"==typeof r?'"'+r+'"':r)}t.append("\n")}})),t.contents()}}let ut,ct;at.prototype.SKIP={},Object.keys(st).forEach((t=>{const e=st[t];Object.defineProperty(at.prototype,t,{get(){return!!(this._flags&e)},set(t){t?this._flags|=e:this._flags&=~e}})})),y.prototype.allowsSkippingPrecedingSpace=e("allowsSkippingPrecedingSpace"),x.allowsSkippingPrecedingSpace=S.allowsSkippingPrecedingSpace=D.prototype.allowsSkippingPrecedingSpace=b.prototype.allowsSkippingPrecedingSpace=_.prototype.allowsSkippingPrecedingSpace=j.prototype.allowsSkippingPrecedingSpace=function(){return!0},A.prototype.allowsSkippingPrecedingSpace=R.prototype.allowsSkippingPrecedingSpace=k.prototype.allowsSkippingPrecedingSpace=T.prototype.allowsSkippingPrecedingSpace=C.prototype.allowsSkippingPrecedingSpace=w.prototype.allowsSkippingPrecedingSpace=L.prototype.allowsSkippingPrecedingSpace=function(){return!1},Z((t=>{ut=t})),y.prototype.assertAllApplicationsAreValid=function(t,e){ct=0,this._assertAllApplicationsAreValid(t,e)},y.prototype._assertAllApplicationsAreValid=e("_assertAllApplicationsAreValid"),x._assertAllApplicationsAreValid=S._assertAllApplicationsAreValid=b.prototype._assertAllApplicationsAreValid=_.prototype._assertAllApplicationsAreValid=w.prototype._assertAllApplicationsAreValid=j.prototype._assertAllApplicationsAreValid=function(t,e){},k.prototype._assertAllApplicationsAreValid=function(t,e){ct++,this.expr._assertAllApplicationsAreValid(t,e),ct--},A.prototype._assertAllApplicationsAreValid=function(t,e){for(let r=0;r<this.terms.length;r++)this.terms[r]._assertAllApplicationsAreValid(t,e)},L.prototype._assertAllApplicationsAreValid=function(t,e){for(let r=0;r<this.factors.length;r++)this.factors[r]._assertAllApplicationsAreValid(t,e)},R.prototype._assertAllApplicationsAreValid=C.prototype._assertAllApplicationsAreValid=T.prototype._assertAllApplicationsAreValid=function(t,e){this.expr._assertAllApplicationsAreValid(t,e)},D.prototype._assertAllApplicationsAreValid=function(t,e,r=!1){const n=e.rules[this.ruleName],i=c(t)&&0===ct;if(!n)throw B(this.ruleName,e.name,this.source);if(!r&&c(this.ruleName)&&!i)throw function(t,e){return M("Cannot apply syntactic rule "+t+" from here (inside a lexical context)",e.source)}(this.ruleName,this);const s=this.args.length,o=n.formals.length;if(s!==o)throw function(t,e,r,n){return M("Wrong number of arguments for rule "+t+" (expected "+e+", got "+r+")",n)}(this.ruleName,o,s,this.source);const a=ut&&n===ut.rules.applySyntactic;if(ut&&n===ut.rules.caseInsensitive&&!(this.args[0]instanceof b))throw z('a Terminal (e.g. "abc")',this.args[0]);if(a){const t=this.args[0];if(!(t instanceof D))throw z("a syntactic rule application",t);if(!c(t.ruleName))throw function(t){const{ruleName:e}=t;return M(`applySyntactic is for syntactic rules, but '${e}' is a lexical rule. NOTE: A _syntactic rule_ is a rule whose name begins with a capital letter. See https://ohmjs.org/d/svl for more details.`,t.source)}(t);if(i)throw M("applySyntactic is not required here (in a syntactic context)",this.source)}this.args.forEach((r=>{if(r._assertAllApplicationsAreValid(t,e,a),1!==r.getArity())throw function(t,e){return M("Invalid parameter to rule "+t+": "+e+" has arity "+e.getArity()+", but parameter expressions must have arity 1",e.source)}(this.ruleName,r)}))},y.prototype.assertChoicesHaveUniformArity=e("assertChoicesHaveUniformArity"),x.assertChoicesHaveUniformArity=S.assertChoicesHaveUniformArity=b.prototype.assertChoicesHaveUniformArity=_.prototype.assertChoicesHaveUniformArity=w.prototype.assertChoicesHaveUniformArity=k.prototype.assertChoicesHaveUniformArity=j.prototype.assertChoicesHaveUniformArity=function(t){},A.prototype.assertChoicesHaveUniformArity=function(t){if(0===this.terms.length)return;const e=this.terms[0].getArity();for(let r=0;r<this.terms.length;r++){const n=this.terms[r];n.assertChoicesHaveUniformArity();const i=n.getArity();if(e!==i)throw H(t,e,i,n)}},N.prototype.assertChoicesHaveUniformArity=function(t){const e=this.terms[0].getArity(),r=this.terms[1].getArity();if(e!==r)throw H(t,r,e,this.terms[0])},L.prototype.assertChoicesHaveUniformArity=function(t){for(let e=0;e<this.factors.length;e++)this.factors[e].assertChoicesHaveUniformArity(t)},R.prototype.assertChoicesHaveUniformArity=function(t){this.expr.assertChoicesHaveUniformArity(t)},C.prototype.assertChoicesHaveUniformArity=function(t){},T.prototype.assertChoicesHaveUniformArity=function(t){this.expr.assertChoicesHaveUniformArity(t)},D.prototype.assertChoicesHaveUniformArity=function(t){},y.prototype.assertIteratedExprsAreNotNullable=e("assertIteratedExprsAreNotNullable"),x.assertIteratedExprsAreNotNullable=S.assertIteratedExprsAreNotNullable=b.prototype.assertIteratedExprsAreNotNullable=_.prototype.assertIteratedExprsAreNotNullable=w.prototype.assertIteratedExprsAreNotNullable=j.prototype.assertIteratedExprsAreNotNullable=function(t){},A.prototype.assertIteratedExprsAreNotNullable=function(t){for(let e=0;e<this.terms.length;e++)this.terms[e].assertIteratedExprsAreNotNullable(t)},L.prototype.assertIteratedExprsAreNotNullable=function(t){for(let e=0;e<this.factors.length;e++)this.factors[e].assertIteratedExprsAreNotNullable(t)},R.prototype.assertIteratedExprsAreNotNullable=function(t){if(this.expr.assertIteratedExprsAreNotNullable(t),this.expr.isNullable(t))throw K(this,[])},E.prototype.assertIteratedExprsAreNotNullable=C.prototype.assertIteratedExprsAreNotNullable=T.prototype.assertIteratedExprsAreNotNullable=k.prototype.assertIteratedExprsAreNotNullable=function(t){this.expr.assertIteratedExprsAreNotNullable(t)},D.prototype.assertIteratedExprsAreNotNullable=function(t){this.args.forEach((e=>{e.assertIteratedExprsAreNotNullable(t)}))};class lt{constructor(t){this.matchLength=t}get ctorName(){throw new Error("subclass responsibility")}numChildren(){return this.children?this.children.length:0}childAt(t){if(this.children)return this.children[t]}indexOfChild(t){return this.children.indexOf(t)}hasChildren(){return this.numChildren()>0}hasNoChildren(){return!this.hasChildren()}onlyChild(){if(1!==this.numChildren())throw new Error("cannot get only child of a node of type "+this.ctorName+" (it has "+this.numChildren()+" children)");return this.firstChild()}firstChild(){if(this.hasNoChildren())throw new Error("cannot get first child of a "+this.ctorName+" node, which has no children");return this.childAt(0)}lastChild(){if(this.hasNoChildren())throw new Error("cannot get last child of a "+this.ctorName+" node, which has no children");return this.childAt(this.numChildren()-1)}childBefore(t){const e=this.indexOfChild(t);if(e<0)throw new Error("Node.childBefore() called w/ an argument that is not a child");if(0===e)throw new Error("cannot get child before first child");return this.childAt(e-1)}childAfter(t){const e=this.indexOfChild(t);if(e<0)throw new Error("Node.childAfter() called w/ an argument that is not a child");if(e===this.numChildren()-1)throw new Error("cannot get child after last child");return this.childAt(e+1)}isTerminal(){return!1}isNonterminal(){return!1}isIteration(){return!1}isOptional(){return!1}}class pt extends lt{get ctorName(){return"_terminal"}isTerminal(){return!0}get primitiveValue(){throw new Error("The `primitiveValue` property was removed in Ohm v17.")}}class ht extends lt{constructor(t,e,r,n){super(n),this.ruleName=t,this.children=e,this.childOffsets=r}get ctorName(){return this.ruleName}isNonterminal(){return!0}isLexical(){return l(this.ctorName)}isSyntactic(){return c(this.ctorName)}}class mt extends lt{constructor(t,e,r,n){super(r),this.children=t,this.childOffsets=e,this.optional=n}get ctorName(){return"_iter"}isIteration(){return!0}isOptional(){return this.optional}}function dt(t,e){const r={};if(t.source&&e){const n=t.source.relativeTo(e);r.sourceInterval=[n.startIdx,n.endIdx]}return r}function ft(t){return/^[a-zA-Z_$][0-9a-zA-Z_$]*$/.test(t)}function gt(t){const e=Object.create(null);t.forEach((t=>{e[t]=(e[t]||0)+1})),Object.keys(e).forEach((r=>{if(e[r]<=1)return;let n=1;t.forEach(((e,i)=>{e===r&&(t[i]=e+"_"+n++)}))}))}y.prototype.eval=e("eval"),x.eval=function(t){const{inputStream:e}=t,r=e.pos,n=e.nextCodePoint();return void 0!==n?(t.pushBinding(new pt(String.fromCodePoint(n).length),r),!0):(t.processFailure(r,this),!1)},S.eval=function(t){const{inputStream:e}=t,r=e.pos;return e.atEnd()?(t.pushBinding(new pt(0),r),!0):(t.processFailure(r,this),!1)},b.prototype.eval=function(t){const{inputStream:e}=t,r=e.pos;return e.matchString(this.obj)?(t.pushBinding(new pt(this.obj.length),r),!0):(t.processFailure(r,this),!1)},_.prototype.eval=function(t){const{inputStream:e}=t,r=e.pos,n=this.matchCodePoint?e.nextCodePoint():e.nextCharCode();return void 0!==n&&this.from.codePointAt(0)<=n&&n<=this.to.codePointAt(0)?(t.pushBinding(new pt(String.fromCodePoint(n).length),r),!0):(t.processFailure(r,this),!1)},w.prototype.eval=function(t){return t.eval(t.currentApplication().args[this.index])},k.prototype.eval=function(t){t.enterLexifiedContext();const e=t.eval(this.expr);return t.exitLexifiedContext(),e},A.prototype.eval=function(t){for(let e=0;e<this.terms.length;e++)if(t.eval(this.terms[e]))return!0;return!1},L.prototype.eval=function(t){for(let e=0;e<this.factors.length;e++){const r=this.factors[e];if(!t.eval(r))return!1}return!0},R.prototype.eval=function(t){const{inputStream:e}=t,r=e.pos,n=this.getArity(),i=[],s=[];for(;i.length<n;)i.push([]),s.push([]);let o,a=0,u=r;for(;a<this.maxNumMatches&&t.eval(this.expr);){if(e.pos===u)throw K(this,t._applicationStack);u=e.pos,a++;const r=t._bindings.splice(t._bindings.length-n,n),c=t._bindingOffsets.splice(t._bindingOffsets.length-n,n);for(o=0;o<r.length;o++)i[o].push(r[o]),s[o].push(c[o])}if(a<this.minNumMatches)return!1;let c=t.posToOffset(r),l=0;if(a>0){const t=i[n-1],e=s[n-1],r=e[e.length-1]+t[t.length-1].matchLength;c=s[0][0],l=r-c}const p=this instanceof E;for(o=0;o<i.length;o++)t._bindings.push(new mt(i[o],s[o],l,p)),t._bindingOffsets.push(c);return!0},C.prototype.eval=function(t){const{inputStream:e}=t,r=e.pos;t.pushFailuresInfo();const n=t.eval(this.expr);return t.popFailuresInfo(),n?(t.processFailure(r,this),!1):(e.pos=r,!0)},T.prototype.eval=function(t){const{inputStream:e}=t,r=e.pos;return!!t.eval(this.expr)&&(e.pos=r,!0)},D.prototype.eval=function(t){const e=t.currentApplication(),r=e?e.args:[],n=this.substituteParams(r),i=t.getCurrentPosInfo();if(i.isActive(n))return n.handleCycle(t);const s=n.toMemoKey(),o=i.memo[s];if(o&&i.shouldUseMemoizedResult(o)){if(t.hasNecessaryInfo(o))return t.useMemoizedResult(t.inputStream.pos,o);delete i.memo[s]}return n.reallyEval(t)},D.prototype.handleCycle=function(t){const e=t.getCurrentPosInfo(),{currentLeftRecursion:r}=e,n=this.toMemoKey();let i=e.memo[n];return r&&r.headApplication.toMemoKey()===n?i.updateInvolvedApplicationMemoKeys():i||(i=e.memoize(n,{matchLength:0,examinedLength:0,value:!1,rightmostFailureOffset:-1}),e.startLeftRecursion(this,i)),t.useMemoizedResult(t.inputStream.pos,i)},D.prototype.reallyEval=function(t){const{inputStream:e}=t,n=e.pos,i=t.getCurrentPosInfo(),s=t.grammar.rules[this.ruleName],{body:o}=s,{description:a}=s;t.enterApplication(i,this),a&&t.pushFailuresInfo();const u=e.examinedLength;e.examinedLength=0;let c=this.evalOnce(o,t);const l=i.currentLeftRecursion,p=this.toMemoKey(),h=l&&l.headApplication.toMemoKey()===p;let m;t.doNotMemoize?t.doNotMemoize=!1:h?(c=this.growSeedResult(o,t,n,l,c),i.endLeftRecursion(),m=l,m.examinedLength=e.examinedLength-n,m.rightmostFailureOffset=t._getRightmostFailureOffset(),i.memoize(p,m)):l&&l.isInvolved(p)||(m=i.memoize(p,{matchLength:e.pos-n,examinedLength:e.examinedLength-n,value:c,failuresAtRightmostPosition:t.cloneRecordedFailures(),rightmostFailureOffset:t._getRightmostFailureOffset()}));const d=!!c;if(a&&(t.popFailuresInfo(),d||t.processFailure(n,this),m&&(m.failuresAtRightmostPosition=t.cloneRecordedFailures())),t.isTracing()&&m){const e=t.getTraceEntry(n,this,d,d?[c]:[]);h&&(r(null!=e.terminatingLREntry||!d),e.isHeadOfLeftRecursion=!0),m.traceEntry=e}return e.examinedLength=Math.max(e.examinedLength,u),t.exitApplication(i,c),d},D.prototype.evalOnce=function(t,e){const{inputStream:r}=e,n=r.pos;if(e.eval(t)){const i=t.getArity(),s=e._bindings.splice(e._bindings.length-i,i),o=e._bindingOffsets.splice(e._bindingOffsets.length-i,i),a=r.pos-n;return new ht(this.ruleName,s,o,a)}return!1},D.prototype.growSeedResult=function(t,e,r,n,i){if(!i)return!1;const{inputStream:s}=e;for(;;){if(n.matchLength=s.pos-r,n.value=i,n.failuresAtRightmostPosition=e.cloneRecordedFailures(),e.isTracing()){const t=e.trace[e.trace.length-1];n.traceEntry=new at(e.input,r,s.pos,this,!0,[i],[t.clone()])}if(s.pos=r,i=this.evalOnce(t,e),s.pos-r<=n.matchLength)break;e.isTracing()&&e.trace.splice(-2,1)}return e.isTracing()&&n.traceEntry.recordLRTermination(e.trace.pop(),i),s.pos=r+n.matchLength,n.value},j.prototype.eval=function(t){const{inputStream:e}=t,r=e.pos,n=e.next();return n&&this.pattern.test(n)?(t.pushBinding(new pt(n.length),r),!0):(t.processFailure(r,this),!1)},y.prototype.getArity=e("getArity"),x.getArity=S.getArity=b.prototype.getArity=_.prototype.getArity=w.prototype.getArity=D.prototype.getArity=j.prototype.getArity=function(){return 1},A.prototype.getArity=function(){return 0===this.terms.length?0:this.terms[0].getArity()},L.prototype.getArity=function(){let t=0;for(let e=0;e<this.factors.length;e++)t+=this.factors[e].getArity();return t},R.prototype.getArity=function(){return this.expr.getArity()},C.prototype.getArity=function(){return 0},T.prototype.getArity=k.prototype.getArity=function(){return this.expr.getArity()},y.prototype.outputRecipe=e("outputRecipe"),x.outputRecipe=function(t,e){return["any",dt(this,e)]},S.outputRecipe=function(t,e){return["end",dt(this,e)]},b.prototype.outputRecipe=function(t,e){return["terminal",dt(this,e),this.obj]},_.prototype.outputRecipe=function(t,e){return["range",dt(this,e),this.from,this.to]},w.prototype.outputRecipe=function(t,e){return["param",dt(this,e),this.index]},A.prototype.outputRecipe=function(t,e){return["alt",dt(this,e)].concat(this.terms.map((r=>r.outputRecipe(t,e))))},N.prototype.outputRecipe=function(t,e){return this.terms[0].outputRecipe(t,e)},O.prototype.outputRecipe=function(t,e){const r=this.terms.slice(0,this.expansionPos),n=this.terms.slice(this.expansionPos+1);return["splice",dt(this,e),r.map((r=>r.outputRecipe(t,e))),n.map((r=>r.outputRecipe(t,e)))]},L.prototype.outputRecipe=function(t,e){return["seq",dt(this,e)].concat(this.factors.map((r=>r.outputRecipe(t,e))))},F.prototype.outputRecipe=P.prototype.outputRecipe=E.prototype.outputRecipe=C.prototype.outputRecipe=T.prototype.outputRecipe=k.prototype.outputRecipe=function(t,e){return[this.constructor.name.toLowerCase(),dt(this,e),this.expr.outputRecipe(t,e)]},D.prototype.outputRecipe=function(t,e){return["app",dt(this,e),this.ruleName,this.args.map((r=>r.outputRecipe(t,e)))]},j.prototype.outputRecipe=function(t,e){return["unicodeChar",dt(this,e),this.category]},y.prototype.introduceParams=e("introduceParams"),x.introduceParams=S.introduceParams=b.prototype.introduceParams=_.prototype.introduceParams=w.prototype.introduceParams=j.prototype.introduceParams=function(t){return this},A.prototype.introduceParams=function(t){return this.terms.forEach(((e,r,n)=>{n[r]=e.introduceParams(t)})),this},L.prototype.introduceParams=function(t){return this.factors.forEach(((e,r,n)=>{n[r]=e.introduceParams(t)})),this},R.prototype.introduceParams=C.prototype.introduceParams=T.prototype.introduceParams=k.prototype.introduceParams=function(t){return this.expr=this.expr.introduceParams(t),this},D.prototype.introduceParams=function(t){const e=t.indexOf(this.ruleName);if(e>=0){if(this.args.length>0)throw new Error("Parameterized rules cannot be passed as arguments to another rule.");return new w(e).withSource(this.source)}return this.args.forEach(((e,r,n)=>{n[r]=e.introduceParams(t)})),this},y.prototype.isNullable=function(t){return this._isNullable(t,Object.create(null))},y.prototype._isNullable=e("_isNullable"),x._isNullable=_.prototype._isNullable=w.prototype._isNullable=P.prototype._isNullable=j.prototype._isNullable=function(t,e){return!1},S._isNullable=function(t,e){return!0},b.prototype._isNullable=function(t,e){return"string"==typeof this.obj&&""===this.obj},A.prototype._isNullable=function(t,e){return 0===this.terms.length||this.terms.some((r=>r._isNullable(t,e)))},L.prototype._isNullable=function(t,e){return this.factors.every((r=>r._isNullable(t,e)))},F.prototype._isNullable=E.prototype._isNullable=C.prototype._isNullable=T.prototype._isNullable=function(t,e){return!0},k.prototype._isNullable=function(t,e){return this.expr._isNullable(t,e)},D.prototype._isNullable=function(t,e){const r=this.toMemoKey();if(!Object.prototype.hasOwnProperty.call(e,r)){const{body:n}=t.rules[this.ruleName],i=n.substituteParams(this.args);e[r]=!1,e[r]=i._isNullable(t,e)}return e[r]},y.prototype.substituteParams=e("substituteParams"),x.substituteParams=S.substituteParams=b.prototype.substituteParams=_.prototype.substituteParams=j.prototype.substituteParams=function(t){return this},w.prototype.substituteParams=function(t){return g(t[this.index])},A.prototype.substituteParams=function(t){return new A(this.terms.map((e=>e.substituteParams(t))))},L.prototype.substituteParams=function(t){return new L(this.factors.map((e=>e.substituteParams(t))))},R.prototype.substituteParams=C.prototype.substituteParams=T.prototype.substituteParams=k.prototype.substituteParams=function(t){return new this.constructor(this.expr.substituteParams(t))},D.prototype.substituteParams=function(t){if(0===this.args.length)return this;{const e=this.args.map((e=>e.substituteParams(t)));return new D(this.ruleName,e)}},y.prototype.toArgumentNameList=e("toArgumentNameList"),x.toArgumentNameList=function(t,e){return["any"]},S.toArgumentNameList=function(t,e){return["end"]},b.prototype.toArgumentNameList=function(t,e){return"string"==typeof this.obj&&/^[_a-zA-Z0-9]+$/.test(this.obj)?["_"+this.obj]:["$"+t]},_.prototype.toArgumentNameList=function(t,e){let r=this.from+"_to_"+this.to;return ft(r)||(r="_"+r),ft(r)||(r="$"+t),[r]},A.prototype.toArgumentNameList=function(t,e){const r=this.terms.map((e=>e.toArgumentNameList(t,!0))),n=[],i=r[0].length;for(let t=0;t<i;t++){const e=[];for(let n=0;n<this.terms.length;n++)e.push(r[n][t]);const i=u(e);n.push(i.join("_or_"))}return e||gt(n),n},L.prototype.toArgumentNameList=function(t,e){let r=[];return this.factors.forEach((e=>{const n=e.toArgumentNameList(t,!0);r=r.concat(n),t+=n.length})),e||gt(r),r},R.prototype.toArgumentNameList=function(t,e){const r=this.expr.toArgumentNameList(t,e).map((t=>"s"===t[t.length-1]?t+"es":t+"s"));return e||gt(r),r},E.prototype.toArgumentNameList=function(t,e){return this.expr.toArgumentNameList(t,e).map((t=>"opt"+t[0].toUpperCase()+t.slice(1)))},C.prototype.toArgumentNameList=function(t,e){return[]},T.prototype.toArgumentNameList=k.prototype.toArgumentNameList=function(t,e){return this.expr.toArgumentNameList(t,e)},D.prototype.toArgumentNameList=function(t,e){return[this.ruleName]},j.prototype.toArgumentNameList=function(t,e){return["$"+t]},w.prototype.toArgumentNameList=function(t,e){return["param"+this.index]},y.prototype.toDisplayString=e("toDisplayString"),A.prototype.toDisplayString=L.prototype.toDisplayString=function(){return this.source?this.source.trimmed().contents:"["+this.constructor.name+"]"},x.toDisplayString=S.toDisplayString=R.prototype.toDisplayString=C.prototype.toDisplayString=T.prototype.toDisplayString=k.prototype.toDisplayString=b.prototype.toDisplayString=_.prototype.toDisplayString=w.prototype.toDisplayString=function(){return this.toString()},D.prototype.toDisplayString=function(){if(this.args.length>0){const t=this.args.map((t=>t.toDisplayString()));return this.ruleName+"<"+t.join(",")+">"}return this.ruleName},j.prototype.toDisplayString=function(){return"Unicode ["+this.category+"] character"};class vt{constructor(t,e,r){if(!function(t){return"description"===t||"string"===t||"code"===t}(r))throw new Error("invalid Failure type: "+r);this.pexpr=t,this.text=e,this.type=r,this.fluffy=!1}getPExpr(){return this.pexpr}getText(){return this.text}getType(){return this.type}isDescription(){return"description"===this.type}isStringTerminal(){return"string"===this.type}isCode(){return"code"===this.type}isFluffy(){return this.fluffy}makeFluffy(){this.fluffy=!0}clearFluffy(){this.fluffy=!1}subsumes(t){return this.getText()===t.getText()&&this.type===t.type&&(!this.isFluffy()||this.isFluffy()&&t.isFluffy())}toString(){return"string"===this.type?JSON.stringify(this.getText()):this.getText()}clone(){const t=new vt(this.pexpr,this.text,this.type);return this.isFluffy()&&t.makeFluffy(),t}toKey(){return this.toString()+"#"+this.type}}y.prototype.toFailure=e("toFailure"),x.toFailure=function(t){return new vt(this,"any object","description")},S.toFailure=function(t){return new vt(this,"end of input","description")},b.prototype.toFailure=function(t){return new vt(this,this.obj,"string")},_.prototype.toFailure=function(t){return new vt(this,JSON.stringify(this.from)+".."+JSON.stringify(this.to),"code")},C.prototype.toFailure=function(t){const e=this.expr===x?"nothing":"not "+this.expr.toFailure(t);return new vt(this,e,"description")},T.prototype.toFailure=function(t){return this.expr.toFailure(t)},D.prototype.toFailure=function(t){let{description:e}=t.rules[this.ruleName];if(!e){e=(/^[aeiouAEIOU]/.test(this.ruleName)?"an":"a")+" "+this.ruleName}return new vt(this,e,"description")},j.prototype.toFailure=function(t){return new vt(this,"a Unicode ["+this.category+"] character","description")},A.prototype.toFailure=function(t){const e="("+this.terms.map((e=>e.toFailure(t))).join(" or ")+")";return new vt(this,e,"description")},L.prototype.toFailure=function(t){const e="("+this.factors.map((e=>e.toFailure(t))).join(" ")+")";return new vt(this,e,"description")},R.prototype.toFailure=function(t){const e="("+this.expr.toFailure(t)+this.operator+")";return new vt(this,e,"description")},y.prototype.toString=e("toString"),x.toString=function(){return"any"},S.toString=function(){return"end"},b.prototype.toString=function(){return JSON.stringify(this.obj)},_.prototype.toString=function(){return JSON.stringify(this.from)+".."+JSON.stringify(this.to)},w.prototype.toString=function(){return"$"+this.index},k.prototype.toString=function(){return"#("+this.expr.toString()+")"},A.prototype.toString=function(){return 1===this.terms.length?this.terms[0].toString():"("+this.terms.map((t=>t.toString())).join(" | ")+")"},L.prototype.toString=function(){return 1===this.factors.length?this.factors[0].toString():"("+this.factors.map((t=>t.toString())).join(" ")+")"},R.prototype.toString=function(){return this.expr+this.operator},C.prototype.toString=function(){return"~"+this.expr},T.prototype.toString=function(){return"&"+this.expr},D.prototype.toString=function(){if(this.args.length>0){const t=this.args.map((t=>t.toString()));return this.ruleName+"<"+t.join(",")+">"}return this.ruleName},j.prototype.toString=function(){return"\\p{"+this.category+"}"};class It extends y{constructor(t){super(),this.obj=t}_getString(t){const e=t.currentApplication().args[this.obj.index];return r(e instanceof b,"expected a Terminal expression"),e.obj}allowsSkippingPrecedingSpace(){return!0}eval(t){const{inputStream:e}=t,r=e.pos,n=this._getString(t);return e.matchString(n,!0)?(t.pushBinding(new pt(n.length),r),!0):(t.processFailure(r,this),!1)}getArity(){return 1}substituteParams(t){return new It(this.obj.substituteParams(t))}toDisplayString(){return this.obj.toDisplayString()+" (case-insensitive)"}toFailure(t){return new vt(this,this.obj.toFailure(t)+" (case-insensitive)","description")}_isNullable(t,e){return this.obj._isNullable(t,e)}}var yt=Object.freeze({__proto__:null,CaseInsensitiveTerminal:It,PExpr:y,any:x,end:S,Terminal:b,Range:_,Param:w,Alt:A,Extend:N,Splice:O,Seq:L,Iter:R,Star:F,Plus:P,Opt:E,Not:C,Lookahead:T,Lex:k,Apply:D,UnicodeChar:j});let xt;Z((t=>{xt=t.rules.applySyntactic.body}));const St=new D("spaces");class bt{constructor(t,e,r){this.matcher=t,this.startExpr=e,this.grammar=t.grammar,this.input=t.getInput(),this.inputStream=new rt(this.input),this.memoTable=t._memoTable,this.userData=void 0,this.doNotMemoize=!1,this._bindings=[],this._bindingOffsets=[],this._applicationStack=[],this._posStack=[0],this.inLexifiedContextStack=[!1],this.rightmostFailurePosition=-1,this._rightmostFailurePositionStack=[],this._recordedFailuresStack=[],void 0!==r&&(this.positionToRecordFailures=r,this.recordedFailures=Object.create(null))}posToOffset(t){return t-this._posStack[this._posStack.length-1]}enterApplication(t,e){this._posStack.push(this.inputStream.pos),this._applicationStack.push(e),this.inLexifiedContextStack.push(!1),t.enter(e),this._rightmostFailurePositionStack.push(this.rightmostFailurePosition),this.rightmostFailurePosition=-1}exitApplication(t,e){const r=this._posStack.pop();this._applicationStack.pop(),this.inLexifiedContextStack.pop(),t.exit(),this.rightmostFailurePosition=Math.max(this.rightmostFailurePosition,this._rightmostFailurePositionStack.pop()),e&&this.pushBinding(e,r)}enterLexifiedContext(){this.inLexifiedContextStack.push(!0)}exitLexifiedContext(){this.inLexifiedContextStack.pop()}currentApplication(){return this._applicationStack[this._applicationStack.length-1]}inSyntacticContext(){const t=this.currentApplication();return t?t.isSyntactic()&&!this.inLexifiedContext():this.startExpr.factors[0].isSyntactic()}inLexifiedContext(){return this.inLexifiedContextStack[this.inLexifiedContextStack.length-1]}skipSpaces(){return this.pushFailuresInfo(),this.eval(St),this.popBinding(),this.popFailuresInfo(),this.inputStream.pos}skipSpacesIfInSyntacticContext(){return this.inSyntacticContext()?this.skipSpaces():this.inputStream.pos}maybeSkipSpacesBefore(t){return t.allowsSkippingPrecedingSpace()&&t!==St?this.skipSpacesIfInSyntacticContext():this.inputStream.pos}pushBinding(t,e){this._bindings.push(t),this._bindingOffsets.push(this.posToOffset(e))}popBinding(){this._bindings.pop(),this._bindingOffsets.pop()}numBindings(){return this._bindings.length}truncateBindings(t){for(;this._bindings.length>t;)this.popBinding()}getCurrentPosInfo(){return this.getPosInfo(this.inputStream.pos)}getPosInfo(t){let e=this.memoTable[t];return e||(e=this.memoTable[t]=new it),e}processFailure(t,e){if(this.rightmostFailurePosition=Math.max(this.rightmostFailurePosition,t),this.recordedFailures&&t===this.positionToRecordFailures){const t=this.currentApplication();t&&(e=e.substituteParams(t.args)),this.recordFailure(e.toFailure(this.grammar),!1)}}recordFailure(t,e){const r=t.toKey();this.recordedFailures[r]?this.recordedFailures[r].isFluffy()&&!t.isFluffy()&&this.recordedFailures[r].clearFluffy():this.recordedFailures[r]=e?t.clone():t}recordFailures(t,e){Object.keys(t).forEach((r=>{this.recordFailure(t[r],e)}))}cloneRecordedFailures(){if(!this.recordedFailures)return;const t=Object.create(null);return Object.keys(this.recordedFailures).forEach((e=>{t[e]=this.recordedFailures[e].clone()})),t}getRightmostFailurePosition(){return this.rightmostFailurePosition}_getRightmostFailureOffset(){return this.rightmostFailurePosition>=0?this.posToOffset(this.rightmostFailurePosition):-1}getMemoizedTraceEntry(t,e){const r=this.memoTable[t];if(r&&e instanceof D){const t=r.memo[e.toMemoKey()];if(t&&t.traceEntry){const r=t.traceEntry.cloneWithExpr(e);return r.isMemoized=!0,r}}return null}getTraceEntry(t,e,r,n){if(e instanceof D){const t=this.currentApplication(),r=t?t.args:[];e=e.substituteParams(r)}return this.getMemoizedTraceEntry(t,e)||new at(this.input,t,this.inputStream.pos,e,r,n,this.trace)}isTracing(){return!!this.trace}hasNecessaryInfo(t){return!(this.trace&&!t.traceEntry)&&(!this.recordedFailures||this.inputStream.pos+t.rightmostFailureOffset!==this.positionToRecordFailures||!!t.failuresAtRightmostPosition)}useMemoizedResult(t,e){this.trace&&this.trace.push(e.traceEntry);const r=this.inputStream.pos+e.rightmostFailureOffset;return this.rightmostFailurePosition=Math.max(this.rightmostFailurePosition,r),this.recordedFailures&&this.positionToRecordFailures===r&&e.failuresAtRightmostPosition&&this.recordFailures(e.failuresAtRightmostPosition,!0),this.inputStream.examinedLength=Math.max(this.inputStream.examinedLength,e.examinedLength+t),!!e.value&&(this.inputStream.pos+=e.matchLength,this.pushBinding(e.value,t),!0)}eval(t){const{inputStream:e}=this,r=this._bindings.length,n=this.userData;let i;this.recordedFailures&&(i=this.recordedFailures,this.recordedFailures=Object.create(null));const s=e.pos,o=this.maybeSkipSpacesBefore(t);let a;this.trace&&(a=this.trace,this.trace=[]);const u=t.eval(this);if(this.trace){const e=this._bindings.slice(r),n=this.getTraceEntry(o,t,u,e);n.isImplicitSpaces=t===St,n.isRootNode=t===this.startExpr,a.push(n),this.trace=a}return u?this.recordedFailures&&e.pos===this.positionToRecordFailures&&Object.keys(this.recordedFailures).forEach((t=>{this.recordedFailures[t].makeFluffy()})):(e.pos=s,this.truncateBindings(r),this.userData=n),this.recordedFailures&&this.recordFailures(i,!1),t===xt&&this.skipSpaces(),u}getMatchResult(){let t;this.grammar._setUpMatchState(this),this.eval(this.startExpr),this.recordedFailures&&(t=Object.keys(this.recordedFailures).map((t=>this.recordedFailures[t])));const e=this._bindings[0];return e&&(e.grammar=this.grammar),new nt(this.matcher,this.input,this.startExpr,e,this._bindingOffsets[0],this.rightmostFailurePosition,t)}getTrace(){this.trace=[];const t=this.getMatchResult(),e=this.trace[this.trace.length-1];return e.result=t,e}pushFailuresInfo(){this._rightmostFailurePositionStack.push(this.rightmostFailurePosition),this._recordedFailuresStack.push(this.recordedFailures)}popFailuresInfo(){this.rightmostFailurePosition=this._rightmostFailurePositionStack.pop(),this.recordedFailures=this._recordedFailuresStack.pop()}}class _t{constructor(t){this.grammar=t,this._memoTable=[],this._input="",this._isMemoTableStale=!1}_resetMemoTable(){this._memoTable=[],this._isMemoTableStale=!1}getInput(){return this._input}setInput(t){return this._input!==t&&this.replaceInputRange(0,this._input.length,t),this}replaceInputRange(t,e,r){const n=this._input,i=this._memoTable;if(t<0||t>n.length||e<0||e>n.length||t>e)throw new Error("Invalid indices: "+t+" and "+e);this._input=n.slice(0,t)+r+n.slice(e),this._input!==n&&i.length>0&&(this._isMemoTableStale=!0);const s=i.slice(e);i.length=t;for(let t=0;t<r.length;t++)i.push(void 0);for(const t of s)i.push(t);for(let e=0;e<t;e++){const r=i[e];r&&r.clearObsoleteEntries(e,t)}return this}match(t,e={incremental:!0}){return this._match(this._getStartExpr(t),{incremental:e.incremental,tracing:!1})}trace(t,e={incremental:!0}){return this._match(this._getStartExpr(t),{incremental:e.incremental,tracing:!0})}_match(t,e={}){const r={tracing:!1,incremental:!0,positionToRecordFailures:void 0,...e};if(r.incremental){if(this._isMemoTableStale&&!this.grammar.supportsIncrementalParsing)throw function(t){return M(`Grammar '${t.name}' does not support incremental parsing`)}(this.grammar)}else this._resetMemoTable();const n=new bt(this,t,r.positionToRecordFailures);return r.tracing?n.getTrace():n.getMatchResult()}_getStartExpr(t){const e=t||this.grammar.defaultStartRule;if(!e)throw new Error("Missing start rule argument -- the grammar has no default start rule.");const r=this.grammar.parseApplication(e);return new L([r,S])}}const wt=[],At=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);class Nt{constructor(t,e,n){this._node=t,this.source=e,this._baseInterval=n,t.isNonterminal()&&r(e===n),this._childWrappers=[]}_forgetMemoizedResultFor(t){delete this._node[this._semantics.attributeKeys[t]],this.children.forEach((e=>{e._forgetMemoizedResultFor(t)}))}child(t){if(!(0<=t&&t<this._node.numChildren()))return;let e=this._childWrappers[t];if(!e){const r=this._node.childAt(t),n=this._node.childOffsets[t],i=this._baseInterval.subInterval(n,r.matchLength),s=r.isNonterminal()?i:this._baseInterval;e=this._childWrappers[t]=this._semantics.wrap(r,i,s)}return e}_children(){for(let t=0;t<this._node.numChildren();t++)this.child(t);return this._childWrappers}isIteration(){return this._node.isIteration()}isTerminal(){return this._node.isTerminal()}isNonterminal(){return this._node.isNonterminal()}isSyntactic(){return this.isNonterminal()&&this._node.isSyntactic()}isLexical(){return this.isNonterminal()&&this._node.isLexical()}isOptional(){return this._node.isOptional()}iteration(t){const e=t||[],r=e.map((t=>t._node)),n=new mt(r,[],-1,!1),i=this._semantics.wrap(n,null,null);return i._childWrappers=e,i}get children(){return this._children()}get ctorName(){return this._node.ctorName}get numChildren(){return this._node.numChildren()}get sourceString(){return this.source.contents}}class Ot{constructor(t,e){const r=this;if(this.grammar=t,this.checkedActionDicts=!1,this.Wrapper=class extends(e?e.Wrapper:Nt){constructor(t,e,n){super(t,e,n),r.checkActionDictsIfHaventAlready(),this._semantics=r}toString(){return"[semantics wrapper for "+r.grammar.name+"]"}},this.super=e,e){if(!t.equals(this.super.grammar)&&!t._inheritsFrom(this.super.grammar))throw new Error("Cannot extend a semantics for grammar '"+this.super.grammar.name+"' for use with grammar '"+t.name+"' (not a sub-grammar)");this.operations=Object.create(this.super.operations),this.attributes=Object.create(this.super.attributes),this.attributeKeys=Object.create(null);for(const t in this.attributes)Object.defineProperty(this.attributeKeys,t,{value:tt(t)})}else this.operations=Object.create(null),this.attributes=Object.create(null),this.attributeKeys=Object.create(null)}toString(){return"[semantics for "+this.grammar.name+"]"}checkActionDictsIfHaventAlready(){this.checkedActionDicts||(this.checkActionDicts(),this.checkedActionDicts=!0)}checkActionDicts(){let t;for(t in this.operations)this.operations[t].checkActionDict(this.grammar);for(t in this.attributes)this.attributes[t].checkActionDict(this.grammar)}toRecipe(t){function e(t){return t.super!==Ot.BuiltInSemantics._getSemantics()}let r="(function(g) {\n";if(e(this)){r+=" var semantics = "+this.super.toRecipe(!0)+"(g";const t=this.super.grammar;let e=this.grammar;for(;e!==t;)r+=".superGrammar",e=e.superGrammar;r+=");\n",r+=" return g.extendSemantics(semantics)"}else r+=" return g.createSemantics()";return["Operation","Attribute"].forEach((t=>{const n=this[t.toLowerCase()+"s"];Object.keys(n).forEach((i=>{const{actionDict:s,formals:o,builtInDefault:a}=n[i];let u,c=i;o.length>0&&(c+="("+o.join(", ")+")"),u=e(this)&&this.super[t.toLowerCase()+"s"][i]?"extend"+t:"add"+t,r+="\n ."+u+"("+JSON.stringify(c)+", {";const l=[];Object.keys(s).forEach((t=>{if(s[t]!==a){let e=s[t].toString().trim();e=e.replace(/^.*\(/,"function("),l.push("\n "+JSON.stringify(t)+": "+e)}})),r+=l.join(",")+"\n })"}))})),r+=";\n })",t||(r="(function() {\n var grammar = this.fromRecipe("+this.grammar.toRecipe()+");\n var semantics = "+r+"(grammar);\n return semantics;\n});\n"),r}addOperationOrAttribute(t,e,r){const n=t+"s",i=Lt(e,t),{name:s}=i,{formals:o}=i;this.assertNewName(s,t);const a=function(t,e,r){return function(...n){const i=(this._semantics.operations[e]||this._semantics.attributes[e]).formals.map((t=>this.args[t]));if(this.isIteration()||1!==n.length)throw function(t,e,r,n){let i=n.slice(0,-1).map((t=>{const e=" "+t[0].name+" > "+t[1];return 3===t.length?e+" for '"+t[2]+"'":e})).join("\n");i+="\n "+e+" > "+t;let s="";"_iter"===t&&(s=["\nNOTE: as of Ohm v16, there is no default action for iteration nodes — see "," https://ohmjs.org/d/dsa for details."].join("\n"));const o=M([`Missing semantic action for '${t}' in ${r} '${e}'.${s}`,"Action stack (most recent call last):",i].join("\n"));return o.name="missingSemanticAction",o}(this.ctorName,e,t,wt);return r.apply(n[0],i