UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 7.8 kB
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */ import{MatchFail as t,MatchSuccess as e}from"../primitive/MatchResult.js";import{padStart as n}from"../primitive/xtString.js";import{utf as i}from"../primitive/Utf.js";import{sLen as r,sNum as s}from"../safe/safe.js";import{nextPow2 as o}from"../primitive/xtBit.js";const a=Symbol.for("nodejs.util.inspect.custom"),h=" / ";function l(t,e){return t?`%x${e.map((t=>t.chrHex)).join(".")}`:`"${e.map((t=>t.chr)).join("")}"`}export class BnfChar{constructor(t,e,n){this.name=void 0,"number"==typeof t?this.ord=t:t instanceof BnfChar?(this.ord=t.ord,null==e&&(e=t.caseInsensitive)):(r("value",t).exactly(1).throwNot(),this.ord=t.charCodeAt(0)),this.caseInsensitive=BnfChar.smartSensitive(this.ord,e),this.name=n}static smartSensitive(t,e){if(i.asciiCased(t))return null!=e&&e}get nonPrintable(){return!i.printable(this.ord)}get length(){return[1,1]}get chrHex(){const t=this.ord.toString(16).toUpperCase(),e=t.length<=2?2:o(t.length);return n(t,e,"0")}get chr(){return this.nonPrintable?"%x"+this.chrHex:String.fromCharCode(this.ord)}descr(){return this.nonPrintable?"%x"+this.chrHex:`"${this.chr}"`}atStartOf(n){if(n.empty)return new t(0);let i=!1;if(this.caseInsensitive){i=(32|n.codePointAt(0))===(32|this.ord)}else i=n.codePointAt(0)===this.ord;return i?new e(n.span(1),{name:this.name,value:n.left(1)}):new t(0)}[Symbol.toPrimitive](){return this.descr()+(this.caseInsensitive?"/i":"")}[a](){return this[Symbol.toPrimitive]()}toString(){return this[Symbol.toPrimitive]()}}export class BnfRange{constructor(t,e,n){if(this.name=void 0,this.start=new BnfChar(t),this.end=new BnfChar(e),this.start.caseInsensitive||this.end.caseInsensitive)throw new RangeError("You can only specify case sensitive characters.");this.nonPrintable=this.start.nonPrintable||this.end.nonPrintable,s("end.ord",this.end.ord).gt(this.start.ord).throwNot(),this.name=n}get length(){return[1,1]}get rangeLength(){return this.end.ord-this.start.ord+1}[Symbol.iterator](){let t=this.start.ord;const e=this.end.ord;return{next:function(){return t<=e?{done:!1,value:new BnfChar(t++)}:{done:!0,value:void 0}}}}descr(){return this.nonPrintable?`%x${this.start.chrHex}-${this.end.chrHex}`:`${this.start.descr()}-${this.end.descr()}`}atStartOf(n){if(0===n.length)return new t(0);const i=n.codePointAt(0);return i<this.start.ord||i>this.end.ord?new t(0):new e(n.span(1),{name:this.name,value:n.left(1)})}[Symbol.toPrimitive](){return`${this.start[Symbol.toPrimitive]()}-${this.end[Symbol.toPrimitive]()}`}[a](){return this[Symbol.toPrimitive]()}toString(){return this[Symbol.toPrimitive]()}}export class BnfString{constructor(t,e=!0,n){this.name=void 0,this.nonPrintable=!1;let i=e;if(Array.isArray(t)){if(!t.every((t=>t instanceof BnfChar)))return void(this._chars=[]);this._chars=t;let n=!1,r=!0,s=!1;t.forEach((t=>{n||(n=t.nonPrintable),r&&(r=null==t.caseInsensitive),s||(s=t.caseInsensitive===!e)})),this.nonPrintable=n,i=r?void 0:s?"mix":e}else{r("value",t).atLeast(2).throwNot();let n=!1,s=!0;this._chars=t.split("").map((t=>{const i=new BnfChar(t,e);return n||(n=i.nonPrintable),s&&(s=null==i.caseInsensitive),i})),this.nonPrintable=n,i=s?void 0:e}this.caseInsensitive=i,this.name=n}get length(){return[this._chars.length,this._chars.length]}[Symbol.iterator](){return this._chars[Symbol.iterator]()}get str(){return this._chars.map((t=>t.chr)).join("")}descr(){return!1===this.caseInsensitive||"mix"===this.caseInsensitive?new BnfConcat(...this._chars).descr():l(this.nonPrintable,this._chars)}atStartOf(n){let i=n.span(0),r=0;for(;r<this._chars.length;r++){const e=this._chars[r].atStartOf(i);if(e.fail||null==e.remain)return new t(n.length-i.length);i=e.remain}const s=n.left(n.length-i.length);return new e(i,{name:this.name,value:s})}[Symbol.toPrimitive](){switch(this.caseInsensitive){case!0:return l(this.nonPrintable,this._chars)+"/i";case"mix":return new BnfConcat(...this._chars)[Symbol.toPrimitive]();default:return l(this.nonPrintable,this._chars)}}[a](){return this[Symbol.toPrimitive]()}toString(){return this[Symbol.toPrimitive]()}}export class BnfConcat{constructor(...t){this.name=void 0,this.suppressComponents=!1,this.items=t,this.nonPrintable=t.some((t=>t.nonPrintable))}get length(){let t=0,e=0;for(const n of this.items){const[i,r]=n.length;t+=i,e+=r}return[t,e]}[Symbol.iterator](){return this.items[Symbol.iterator]()}descr(t=!1){const e=new Array(this.items.length),n=[];let i=!1,r=0;for(const s of this.items)s instanceof BnfChar&&s.caseInsensitive?(n.push(s),i||(i=s.nonPrintable)):(n.length>0&&(e[r++]=l(t||i,n),n.length=0),e[r++]=s.descr(t));return n.length>0&&(e[r++]=t||i?`%x${n.map((t=>t.chrHex)).join(".")}`:`"${n.map((t=>t.chr)).join("")}"`,n.length=0),r<=1?e.slice(0,r).join(" "):`(${e.slice(0,r).join(" ")})`}atStartOf(n){const i=[];let r=n.span(0);for(const e of this.items){const s=e.atStartOf(r);if(s.fail||null==s.remain)return new t(n.length-r.length);r=s.remain,i.push(s.result)}const s=n.left(n.length-r.length);return this.suppressComponents?new e(r,{name:this.name,value:s}):new e(r,{name:this.name,value:s,components:i})}[Symbol.toPrimitive](){return`(${this.items.map((t=>t[Symbol.toPrimitive]())).join(" ")})`}[a](){return this[Symbol.toPrimitive]()}toString(){return this[Symbol.toPrimitive]()}}export class BnfAlt{constructor(...t){this.name=void 0,this.suppressComponents=!1,this.items=[],this.nonPrintable=!1;for(const e of t)e instanceof BnfAlt?this.items.push(...e.items):this.items.push(e)}get length(){let t=Number.MAX_SAFE_INTEGER,e=0;for(const n of this.items){const[i,r]=n.length;i<t&&(t=i),r>e&&(e=r)}return[t,e]}[Symbol.iterator](){return this.items[Symbol.iterator]()}descr(t=!1){const e=new Array(this.items.length);let n=0;for(const i of this.items)e[n++]=i.descr(t);return n<=1?e.join(h):`(${e.join(h)})`}atStartOf(n){for(const t of this.items){const i=t.atStartOf(n);if(!i.fail&&null!=i.remain){const t={name:this.name,value:i.result.value};return this.suppressComponents||(t.components=[i.result]),new e(i.remain,t)}}return new t(0)}[Symbol.toPrimitive](){return`(${this.items.map((t=>t[Symbol.toPrimitive]())).join(h)})`}[a](){return this[Symbol.toPrimitive]()}toString(){return this[Symbol.toPrimitive]()}static Split(t,e=void 0,n){const i=new BnfAlt(...t.split("").map((t=>new BnfChar(t,e))));return i.name=n,i}}export class BnfRepeat{constructor(t,e,n,i){this.name=void 0,this.suppressComponents=!1,s("min",e).unsigned().throwNot(),s("max",n).atLeast(e).throwNot(),this.rule=t,this.min=e,this.max=n,this.name=i}get nonPrintable(){return this.rule.nonPrintable}get length(){const[t,e]=this.rule.length;return[t*this.min,this.max===Number.MAX_SAFE_INTEGER?Number.MAX_SAFE_INTEGER:e*this.max]}descr(t=!1){let e=(0==this.min?"":this.min)+"*";return this.max!==Number.MAX_SAFE_INTEGER&&(e+=this.max),this.min===this.max&&(e=this.min.toString()),e+this.rule.descr(t)}atStartOf(n){const i=[];let r=n.span(0);for(let e=0;e<this.min;e++){const e=this.rule.atStartOf(r);if(e.fail||null==e.remain)return new t(n.length-r.length);r=e.remain,i.push(e.result)}for(let t=this.min;t<this.max;t++){const t=this.rule.atStartOf(r);if(t.fail||null==t.remain)break;r=t.remain,i.push(t.result)}const s=n.left(n.length-r.length);return new e(r,{name:this.name,value:s,components:this.suppressComponents?[]:i})}[Symbol.toPrimitive](){const t=this.max===Number.MAX_SAFE_INTEGER?"∞":this.max;return this.min+"*"+t+this.rule[Symbol.toPrimitive]()}[a](){return this[Symbol.toPrimitive]()}toString(){return this[Symbol.toPrimitive]()}static ZeroPlus(t,e){return new BnfRepeat(t,0,Number.MAX_SAFE_INTEGER,e)}static OnePlus(t,e){return new BnfRepeat(t,1,Number.MAX_SAFE_INTEGER,e)}static Optional(t,e){return new BnfRepeat(t,0,1,e)}static Between(t,e,n,i){return new BnfRepeat(n,t,e,i)}static Exactly(t,e){return new BnfRepeat(e,t,t)}}