UNPKG

@masala/parser

Version:
3 lines (2 loc) 28.6 kB
const empty={__MASALA_EMPTY__:"empty"};class Option{constructor(value){this.value=value}isPresent(){return this.value!==empty}map(bindCall){if(this.isPresent()){return someOrNone(bindCall(this.value))}else{return this}}flatMap(bindCall){if(this.isPresent()){return bindCall(this.value)}else{return this}}filter(f){if(this.isPresent()&&f(this.value)){return this}return new Option(empty)}get(){return this.value}orElse(value){if(this.isPresent()){return this.value}else{return value}}orLazyElse(value){if(this.isPresent()){return this.value}else{return value()}}}function someOrNone(value){return new Option(value)}function none(){return new Option(empty)}var option={some:someOrNone,none:none};class Try{constructor(value,error){this.value=value;this.error=error}isSuccess(){return this.error===null}isFailure(){return!this.isSuccess()}onSuccess(bindCall){if(this.isSuccess()){bindCall(this.value)}return this}onFailure(bindCall){if(this.isFailure()){bindCall(this.error)}return this}map(bindCall){if(this.isSuccess()){try{return success(bindCall(this.value))}catch(e){return failure(e)}}else{return this}}flatMap(bindCall){if(this.isSuccess()){try{return bindCall(this.value)}catch(e){return failure(e)}}else{return this}}success(){return this.value}failure(){return this.error}recoverWith(value){if(this.isSuccess()){return this.value}else{return value}}lazyRecoverWith(value){if(this.isSuccess()){return this.value}else{return value(this.error)}}filter(f){if(this.isSuccess()){if(f(this.value)){return this}else{return failure(new Error("invalid filter"))}}return this}}function success(value){return new Try(value,null)}function failure(error){return new Try(null,error)}var atry={success:success,failure:failure};class Unit{}var unit=new Unit;var index$1={option:option,atry:atry,unit:unit};class ParserResponse{constructor(input,offset,consumed){this.input=input;this.offset=offset;this.consumed=consumed}isAccepted(){return this.fold((function(){return true}),(function(){return false}))}toTry(){return this.fold((accept=>atry.success(accept.value)),(reject=>atry.failure(new Error("parser error at "+reject.offset))))}isEos(){return false}getOffset(){return this.offset}location(){return this.input.location(this.getOffset())}}class Reject extends ParserResponse{constructor(input,offset,consumed){super(input,offset,consumed)}fold(_,reject){return reject(this)}map(){return this}flatMap(){return this}filter(){return new Reject(this.input,this.offset,false)}}class Accept extends ParserResponse{constructor(value,input,offset,consumed){super(input,offset,consumed);this.value=value}isEos(){return this.input.endOfStream(this.offset)}fold(accept){return accept(this)}map(callback){return new Accept(callback(this.value,this),this.input,this.offset,this.consumed)}flatMap(callback){return callback(this.value,this)}filter(predicate){if(predicate(this.value)){return this}else{return new Reject(this.input,this.offset,false)}}}const accept=(value,input,offset,consumed)=>new Accept(value,input,offset,consumed);const reject=(input,offset,consumed)=>new Reject(input,offset,consumed);const response={accept:accept,reject:reject};class Stream{constructor(){}location(index){return index}get(index){try{if(this.endOfStream(index)){return atry.failure(new Error("End of stream reached"))}else{return atry.success(this.unsafeGet(index))}}catch(e){return atry.failure(e)}}subStreamAt(s,index){for(var i=0;i<s.length;i++){var value=this.get(i+index);if(!value.isSuccess()||value.success()!==s[i]){return false}}return true}}class StringStream extends Stream{constructor(source){super();this.source=source}endOfStream(index){return this.source.length<=index}unsafeGet(index){return this.source.charAt(index)}}function factory$3(source){return new StringStream(source)}class ArrayStream extends Stream{constructor(source){super();this.source=source}endOfStream(index){return index>=this.source.length}unsafeGet(index){return this.source[index]}}function factory$2(source){return new ArrayStream(source)}class ParserStream extends Stream{constructor(parser,lowerStream){super();this.source=parser;this.input=lowerStream;this.offsets=[0]}getOffset(index){if(index<this.offsets.length){return option.some(this.offsets[index])}return option.none()}location(index){if(index===0){return 0}const option=this.getOffset(index);if(option.isPresent()){return this.input.location(option.get())}else{throw"No location has been found yet for index "+index}}endOfStream(index){const option=this.getOffset(index);if(option.isPresent()){return this.input.endOfStream(option.get())}else{const last=this.offsets[this.offsets.length-1];return this.input.endOfStream(last+1)}}unsafeGet(index){let sourceIndex;let option=this.getOffset(index);if(option.isPresent()){sourceIndex=option.get()}else{throw new Error}const result=this.source.parse(this.input,sourceIndex);if(result.isAccepted()){this.offsets.push(result.offset);return result.value}else{throw new Error}}}function factory$1(parser,lowerStream){return new ParserStream(parser,lowerStream)}class BufferedStream extends Stream{constructor(source){super();this.source=source;this.cache={}}location(index){return this.source.location(index)}endOfStream(index){return this.source.endOfStream(index)}get(index){var self=this;if(!self.cache[index]){self.cache[index]=self.source.get(index)}return self.cache[index]}}function factory(source){return new BufferedStream(source)}var stream={ofString:factory$3,ofArray:factory$2,ofParser:factory$1,buffered:factory};const NEUTRAL=Symbol("MASALA_NEUTRAL");class Tuple{constructor(array){if(array===undefined){array=[]}this.value=array}at(index){return this.value[index]}array(){return[...this.value]}single(){return this.value[0]}first(){return this.value[0]}last(){return this.value[this.size()-1]}join(j=""){return this.value.join(j)}append(element){if(element===NEUTRAL){return new Tuple([...this.value])}if(isTuple(element)){return new Tuple([...this.value,...element.value])}return new Tuple([...this.value,element])}isEmpty(){return this.size()===0}size(){return this.value.length}}function isTuple(object){return object instanceof Tuple}function tuple(array){if(array===undefined){return new Tuple([])}else{return new Tuple(array)}}class Parser{constructor(parse){this.parse=parse.bind(this)}val(text){return this.parse(stream.ofString(text)).value}flatMap(f){return bind(this,f)}map(f){var self=this;return new Parser(((input,index=0)=>self.parse(input,index).map(f)))}filter(p){var self=this;return new Parser(((input,index=0)=>self.parse(input,index).filter(p)))}match(v){return this.filter((a=>a===v))}then(p){return this.flatMap((a=>p.map((b=>new Tuple([]).append(a).append(b)))))}single(){return this.map((tuple=>tuple.single()))}last(){return this.map((tuple=>tuple.last()))}first(){return this.map((tuple=>tuple.first()))}array(){return this.map((value=>{if(!isTuple(value)){throw"array() is called only on TupleParser"}return value.array()}))}join(j=""){return this.map((value=>{if(!isTuple(value)){throw"Error: join() is called only on TupleParser"}return value.join(j)}))}thenEos(){return this.then(eos().drop())}eos(){return new Parser(((input,index=0)=>this.parse(input,index).fold((accept=>input.endOfStream(accept.offset)?response.accept(accept.value,accept.input,accept.offset,true):response.reject(accept.input,accept.offset,accept.consumed)),(reject=>response.reject(reject.input,reject.offset,reject.consumed)))))}concat(p){return this.then(p)}drop(){return this.map((()=>NEUTRAL))}thenLeft(p){return this.then(p.drop())}thenRight(p){return this.drop().then(p)}returns(v){return this.drop().map((()=>v))}or(p){return choice(this,p)}and(p){return both(this,p)}opt(){return this.map(option.some).or(returns$1(option.none()))}rep(){return repeatable(this,(()=>true),(l=>l!==0))}occurrence(occurrence){return repeatable(this,(l=>l<occurrence),(l=>l===occurrence))}optrep(){return repeatable(this,(()=>true),(()=>true))}chain(p){var self=this;return new Parser(((input,index=0)=>p.parse(stream.buffered(stream.ofParser(self,input)),index)))}debug(hint,details=true){var f=p=>{if(details){console.log("[debug] : ",hint,p)}else{console.log("[debug] : ",hint)}return p};return this.map(f)}}function bindAccepted(accept_a,f){return f(accept_a.value).parse(accept_a.input,accept_a.offset).fold((accept_b=>response.accept(accept_b.value,accept_b.input,accept_b.offset,accept_a.consumed||accept_b.consumed)),(reject_b=>response.reject(reject_b.input,reject_b.offset,accept_a.consumed||reject_b.consumed)))}function bind(self,f){return new Parser(((input,index=0)=>self.parse(input,index).fold((accept_a=>bindAccepted(accept_a,f)),(reject_a=>reject_a))))}function choice(self,f){return new Parser(((input,index=0)=>self.parse(input,index).fold((accept=>accept),(reject=>reject.consumed?reject:f.parse(input,index)))))}function both(self,f){return new Parser(((input,index=0)=>self.parse(input,index).fold((accept=>f.parse(input,index).map((r=>accept.value.append(r)))),(reject=>reject))))}function repeatable(self,occurrences,accept){return new Parser(((input,index=0)=>{var consumed=false,value=new Tuple([]),offset=index,current=self.parse(input,index),occurrence=0;while(current.isAccepted()&&occurrences(occurrence)){occurrence+=1;value=value.append(current.value);consumed=consumed||current.consumed;offset=current.offset;current=self.parse(input,current.offset)}if(accept(occurrence)){return response.accept(value,input,offset,consumed)}return response.reject(input,offset,consumed)}))}function returns$1(v){return new Parser(((input,index=0)=>response.accept(v,input,index,false)))}function eos(){return new Parser(((input,index=0)=>{if(input.endOfStream(index)){return response.accept(unit,input,index,false)}else{return response.reject(input,index,false)}}))}function parse(p){return new Parser(p)}function lazy(p,parameters,self={}){if(parameters&&!Array.isArray(parameters)){throw"Lazy(parser, [params]) function expect parser parameters to be packed into an array"}return new Parser(((input,index=0)=>p.apply(self,parameters).parse(input,index)))}function returns(v){return new Parser(((input,index=0)=>response.accept(v,input,index,false)))}function error(){return new Parser(((input,index=0)=>response.reject(input,index,false)))}function satisfy(predicate){return new Parser(((input,index=0)=>input.get(index).filter(predicate).map((value=>response.accept(value,input,index+1,true))).lazyRecoverWith((()=>response.reject(input,index,false)))))}function doTry(p){return new Parser(((input,index=0)=>p.parse(input,index).fold((accept=>accept),(_=>response.reject(input,index,false)))))}function layer(p){return new Parser(((input,index=0)=>p.parse(input,index).fold((accept=>response.accept((new Tuple).append(accept.value),input,index,false)),(reject=>reject))))}function any(){return satisfy((()=>true))}function nop(){return new Parser(((input,index=0)=>response.accept(NEUTRAL,input,index,true)))}function not(p){return doTry(p).then(error()).or(any())}function subStream(length){return any().occurrence(length)}function startWith(value){return nop().returns(value)}function moveUntil(stop,include=false){if(typeof stop==="string"){return searchStringStart(stop,include)}if(Array.isArray(stop)){return searchArrayStringStart(stop,include)}let findParser=not(stop).rep().join();if(include){return findParser.then(stop).join()}const foundEos=Symbol("found-eos");return doTry(findParser.then(eos()).returns(foundEos)).or(findParser).filter((v=>v!==foundEos))}function dropTo(stop){if(typeof stop==="string"){return moveUntil(stop).then(string$1(stop)).drop()}else{return moveUntil(stop).then(stop).drop()}}function regex(rg){const flags=rg.flags.includes("y")?rg.flags:rg.flags+"y";const sticky=new RegExp(rg.source,flags);return new Parser(((input,index=0)=>{sticky.lastIndex=index;const matches=sticky.exec(input.source);if(!matches){return response.reject(input,index,false)}else{const text=matches[0];return response.accept(text,input,sticky.lastIndex,true)}}))}function tryAll(parsers){if(parsers.length===0){return nop()}let combinator=doTry(parsers[0]);for(let i=1;i<parsers.length;i++){combinator=doTry(combinator.or(doTry(parsers[i])))}return combinator}var flow={parse:parse,nop:nop,try:doTry,any:any,subStream:subStream,not:not,layer:layer,lazy:lazy,returns:returns,error:error,eos:eos,satisfy:satisfy,startWith:startWith,moveUntil:moveUntil,dropTo:dropTo,regex:regex,tryAll:tryAll};function searchStringStart(string,include=false){return new Parser(((input,index=0)=>{if(typeof input.source!=="string"){throw"Input source must be a String"}const sourceIndex=input.source.indexOf(string,index);let offset=sourceIndex;let result=input.source.substring(index,sourceIndex);if(include){result+=string;offset+=string.length}if(sourceIndex>0){return response.accept(result,input,offset,true)}else{return response.reject(input,index,false)}}))}function searchArrayStringStart(array,include=false){return new Parser(((input,index=0)=>{if(typeof input.source!=="string"){throw"Input source must be a String"}let sourceIndex=-1;let offset,result;let i=0;while(sourceIndex<0&&i<array.length){const needle=array[i];sourceIndex=input.source.indexOf(needle,index);i++;if(sourceIndex>0){offset=sourceIndex;result=input.source.substring(index,sourceIndex);if(include){result+=needle;offset+=needle.length}break}}if(sourceIndex>0){return response.accept(result,input,offset,true)}else{return response.reject(input,index,false)}}))}function string$1(s){return new Parser(((input,index=0)=>{if(input.subStreamAt(s.split(""),index)){return response.accept(s,input,index+s.length,true)}else{return response.reject(input,index,false)}}))}const ASCII_LETTER=Symbol("ASCII");const OCCIDENTAL_LETTER=Symbol("OCCIDENTAL");const UTF8_LETTER=Symbol("UTF8");function isUtf8Letter(char){return char.toLowerCase()!==char.toUpperCase()}function isExtendedOccidental(v){return/(?![\u00F7\u00D7])[\u00C0-\u017F^\u00F7]/.test(v)}function letter(symbol=null){if(symbol===null||symbol===OCCIDENTAL_LETTER){return flow.satisfy((c=>"a"<=c&&c<="z"||"A"<=c&&c<="Z"||isExtendedOccidental(c)))}if(symbol===ASCII_LETTER){return flow.satisfy((c=>"a"<=c&&c<="z"||"A"<=c&&c<="Z"))}if(symbol===UTF8_LETTER){return flow.satisfy((c=>"a"<=c&&c<="z"||"A"<=c&&c<="Z"||isUtf8Letter(c)))}throw"Parameter "+symbol.toString()+" has wrong type : Should be C.OCCIDENTAL_LETTER, C.ASCII_LETTER or C.UTF8_LETTER"}function utf8Letter(){return flow.satisfy(isUtf8Letter)}function emoji(){return flow.satisfy((c=>!isUtf8Letter(c)&&isEmojiRegex(c))).rep()}function letters(symbolOrTestFunctionOrRegex){return letter(symbolOrTestFunctionOrRegex).rep().map((values=>values.join("")))}function char(c){if(c.length!==1){throw new Error("Char parser must contains one character")}return flow.satisfy((v=>c===v))}function notChar(c){if(c.length!==1){throw new Error("Char parser must contains one character")}return flow.satisfy((v=>c!==v))}function charIn(c){return flow.satisfy((v=>c.indexOf(v)!==-1))}function charNotIn(c){return flow.satisfy((v=>c.indexOf(v)===-1))}function subString(length){return flow.subStream(length).map((s=>s.join("")))}function stringIn(array){const tryString=s=>flow.try(string$1(s));if(array.length===0){return flow.nop()}if(array.length===1){return tryString(array[0])}const initial=tryString(array[0]);const workArray=array.slice(1);return workArray.reduce(((accu,next)=>accu.or(tryString(next))),initial)}function notString(s){return flow.not(string$1(s))}function stringLiteral(){const anyChar=string$1('\\"').or(notChar('"'));return char('"').thenRight(anyChar.optrep()).thenLeft(char('"')).map((r=>r.join("")))}function charLiteral(){const anyChar=string$1("\\'").or(notChar("'"));return char("'").thenRight(anyChar).thenLeft(char("'")).single()}function lowerCase(){return flow.satisfy((v=>"a"<=v&&v<="z"))}function upperCase(){return flow.satisfy((v=>"A"<=v&&v<="Z"))}function inRegexRange(regexRange){const regExp=new RegExp(`^[${regexRange}]$`);return flow.satisfy((c=>regExp.test(c)))}var chars={utf8Letter:utf8Letter,letter:letter,letterAs:letter,letters:letters,lettersAs:letters,emoji:emoji,notChar:notChar,char:char,charIn:charIn,inRegexRange:inRegexRange,charNotIn:charNotIn,subString:subString,string:string$1,stringIn:stringIn,notString:notString,charLiteral:charLiteral,stringLiteral:stringLiteral,lowerCase:lowerCase,upperCase:upperCase,UTF8_LETTER:UTF8_LETTER,OCCIDENTAL_LETTER:OCCIDENTAL_LETTER,ASCII_LETTER:ASCII_LETTER};function isEmojiRegex(){return/\uD83C\uDFF4\uDB40\uDC67\uDB40\uDC62(?:\uDB40\uDC65\uDB40\uDC6E\uDB40\uDC67|\uDB40\uDC77\uDB40\uDC6C\uDB40\uDC73|\uDB40\uDC73\uDB40\uDC63\uDB40\uDC74)\uDB40\uDC7F|\uD83D\uDC69\u200D\uD83D\uDC69\u200D(?:\uD83D\uDC66\u200D\uD83D\uDC66|\uD83D\uDC67\u200D(?:\uD83D[\uDC66\uDC67]))|\uD83D\uDC68(?:\u200D(?:\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D)?\uD83D\uDC68|(?:\uD83D[\uDC68\uDC69])\u200D(?:\uD83D\uDC66\u200D\uD83D\uDC66|\uD83D\uDC67\u200D(?:\uD83D[\uDC66\uDC67]))|\uD83D\uDC66\u200D\uD83D\uDC66|\uD83D\uDC67\u200D(?:\uD83D[\uDC66\uDC67])|[\u2695\u2696\u2708]\uFE0F|\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92])|(?:\uD83C[\uDFFB-\uDFFF])\u200D[\u2695\u2696\u2708]\uFE0F|(?:\uD83C[\uDFFB-\uDFFF])\u200D(?:\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92]))|\uD83D\uDC69\u200D(?:\u2764\uFE0F\u200D(?:\uD83D\uDC8B\u200D(?:\uD83D[\uDC68\uDC69])|\uD83D[\uDC68\uDC69])|\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92])|\uD83D\uDC69\u200D\uD83D\uDC66\u200D\uD83D\uDC66|(?:\uD83D\uDC41\uFE0F\u200D\uD83D\uDDE8|\uD83D\uDC69(?:\uD83C[\uDFFB-\uDFFF])\u200D[\u2695\u2696\u2708]|(?:(?:\u26F9|\uD83C[\uDFCB\uDFCC]|\uD83D\uDD75)\uFE0F|\uD83D\uDC6F|\uD83E[\uDD3C\uDDDE\uDDDF])\u200D[\u2640\u2642]|(?:\u26F9|\uD83C[\uDFCB\uDFCC]|\uD83D\uDD75)(?:\uD83C[\uDFFB-\uDFFF])\u200D[\u2640\u2642]|(?:\uD83C[\uDFC3\uDFC4\uDFCA]|\uD83D[\uDC6E\uDC71\uDC73\uDC77\uDC81\uDC82\uDC86\uDC87\uDE45-\uDE47\uDE4B\uDE4D\uDE4E\uDEA3\uDEB4-\uDEB6]|\uD83E[\uDD26\uDD37-\uDD39\uDD3D\uDD3E\uDDD6-\uDDDD])(?:(?:\uD83C[\uDFFB-\uDFFF])\u200D[\u2640\u2642]|\u200D[\u2640\u2642])|\uD83D\uDC69\u200D[\u2695\u2696\u2708])\uFE0F|\uD83D\uDC69\u200D\uD83D\uDC67\u200D(?:\uD83D[\uDC66\uDC67])|\uD83D\uDC69\u200D\uD83D\uDC69\u200D(?:\uD83D[\uDC66\uDC67])|\uD83D\uDC68(?:\u200D(?:(?:\uD83D[\uDC68\uDC69])\u200D(?:\uD83D[\uDC66\uDC67])|\uD83D[\uDC66\uDC67])|\uD83C[\uDFFB-\uDFFF])|\uD83C\uDFF3\uFE0F\u200D\uD83C\uDF08|\uD83D\uDC69\u200D\uD83D\uDC67|\uD83D\uDC69(?:\uD83C[\uDFFB-\uDFFF])\u200D(?:\uD83C[\uDF3E\uDF73\uDF93\uDFA4\uDFA8\uDFEB\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92])|\uD83D\uDC69\u200D\uD83D\uDC66|\uD83C\uDDF4\uD83C\uDDF2|\uD83C\uDDFD\uD83C\uDDF0|\uD83C\uDDF6\uD83C\uDDE6|\uD83D\uDC69(?:\uD83C[\uDFFB-\uDFFF])|\uD83C\uDDFC(?:\uD83C[\uDDEB\uDDF8])|\uD83C\uDDEB(?:\uD83C[\uDDEE-\uDDF0\uDDF2\uDDF4\uDDF7])|\uD83C\uDDE9(?:\uD83C[\uDDEA\uDDEC\uDDEF\uDDF0\uDDF2\uDDF4\uDDFF])|\uD83C\uDDE7(?:\uD83C[\uDDE6\uDDE7\uDDE9-\uDDEF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uDD7A\uDD90\uDD95\uDD96\uDE45-\uDE47\uDE4B-\uDE4F\uDEA3\uDEB4-\uDEB6\uDEC0\uDECC]|\uD83E[\uDD18-\uDD1C\uDD1E\uDD1F\uDD26\uDD30-\uDD39\uDD3D\uDD3E\uDDD1-\uDDDD])(?:\uD83C[\uDFFB-\uDFFF])?|(?:[\u231A\u231B\u23E9-\u23EC\u23F0\u23F3\u25FD\u25FE\u2614\u2615\u2648-\u2653\u267F\u2693\u26A1\u26AA\u26AB\u26BD\u26BE\u26C4\u26C5\u26CE\u26D4\u26EA\u26F2\u26F3\u26F5\u26FA\u26FD\u2705\u270A\u270B\u2728\u274C\u274E\u2753-\u2755\u2757\u2795-\u2797\u27B0\u27BF\u2B1B\u2B1C\u2B50\u2B55]|\uD83C[\uDC04\uDCCF\uDD8E\uDD91-\uDD9A\uDDE6-\uDDFF\uDE01\uDE1A\uDE2F\uDE32-\uDE36\uDE38-\uDE3A\uDE50\uDE51\uDF00-\uDF20\uDF2D-\uDF35\uDF37-\uDF7C\uDF7E-\uDF93\uDFA0-\uDFCA\uDFCF-\uDFD3\uDFE0-\uDFF0\uDFF4\uDFF8-\uDFFF]|\uD83D[\uDC00-\uDC3E\uDC40\uDC42-\uDCFC\uDCFF-\uDD3D\uDD4B-\uDD4E\uDD50-\uDD67\uDD7A\uDD95\uDD96\uDDA4\uDDFB-\uDE4F\uDE80-\uDEC5\uDECC\uDED0-\uDED2\uDEEB\uDEEC\uDEF4-\uDEF8]|\uD83E[\uDD10-\uDD3A\uDD3C-\uDD3E\uDD40-\uDD45\uDD47-\uDD4C\uDD50-\uDD6B\uDD80-\uDD97\uDDC0\uDDD0-\uDDE6])|(?:[#\*0-9\xA9\xAE\u203C\u2049\u2122\u2139\u2194-\u2199\u21A9\u21AA\u231A\u231B\u2328\u23CF\u23E9-\u23F3\u23F8-\u23FA\u24C2\u25AA\u25AB\u25B6\u25C0\u25FB-\u25FE\u2600-\u2604\u260E\u2611\u2614\u2615\u2618\u261D\u2620\u2622\u2623\u2626\u262A\u262E\u262F\u2638-\u263A\u2640\u2642\u2648-\u2653\u2660\u2663\u2665\u2666\u2668\u267B\u267F\u2692-\u2697\u2699\u269B\u269C\u26A0\u26A1\u26AA\u26AB\u26B0\u26B1\u26BD\u26BE\u26C4\u26C5\u26C8\u26CE\u26CF\u26D1\u26D3\u26D4\u26E9\u26EA\u26F0-\u26F5\u26F7-\u26FA\u26FD\u2702\u2705\u2708-\u270D\u270F\u2712\u2714\u2716\u271D\u2721\u2728\u2733\u2734\u2744\u2747\u274C\u274E\u2753-\u2755\u2757\u2763\u2764\u2795-\u2797\u27A1\u27B0\u27BF\u2934\u2935\u2B05-\u2B07\u2B1B\u2B1C\u2B50\u2B55\u3030\u303D\u3297\u3299]|\uD83C[\uDC04\uDCCF\uDD70\uDD71\uDD7E\uDD7F\uDD8E\uDD91-\uDD9A\uDDE6-\uDDFF\uDE01\uDE02\uDE1A\uDE2F\uDE32-\uDE3A\uDE50\uDE51\uDF00-\uDF21\uDF24-\uDF93\uDF96\uDF97\uDF99-\uDF9B\uDF9E-\uDFF0\uDFF3-\uDFF5\uDFF7-\uDFFF]|\uD83D[\uDC00-\uDCFD\uDCFF-\uDD3D\uDD49-\uDD4E\uDD50-\uDD67\uDD6F\uDD70\uDD73-\uDD7A\uDD87\uDD8A-\uDD8D\uDD90\uDD95\uDD96\uDDA4\uDDA5\uDDA8\uDDB1\uDDB2\uDDBC\uDDC2-\uDDC4\uDDD1-\uDDD3\uDDDC-\uDDDE\uDDE1\uDDE3\uDDE8\uDDEF\uDDF3\uDDFA-\uDE4F\uDE80-\uDEC5\uDECB-\uDED2\uDEE0-\uDEE5\uDEE9\uDEEB\uDEEC\uDEF0\uDEF3-\uDEF8]|\uD83E[\uDD10-\uDD3A\uDD3C-\uDD3E\uDD40-\uDD45\uDD47-\uDD4C\uDD50-\uDD6B\uDD80-\uDD97\uDDC0\uDDD0-\uDDE6])\uFE0F/}function number$1(){var join=r=>r.join(""),joinOrEmpty=r=>r.map(join).orElse(""),digits=digit().rep().map(join),integer=chars.charIn("+-").opt().then(digits).array().map((r=>r[0].orElse("")+r[1])),float=integer.then(chars.char(".").then(digits).opt().map(joinOrEmpty)).then(chars.charIn("eE").then(integer).opt().map(joinOrEmpty)).array().map((r=>r[0]+r[1]+r[2]));return float.map((r=>parseFloat(r,10)))}function digit(){return flow.satisfy((v=>"0"<=v&&v<="9")).map((c=>parseInt(c)))}function digits(){return digit().rep().map((v=>parseInt(v.join(""))))}function integer(){var join=r=>r.join(""),digits=digit().rep().map(join),integer=chars.charIn("+-").opt().then(digits).array().map((r=>r[0].orElse("")+r[1]));return integer.map((i=>parseInt(i,10)))}var numbers={number:number$1,digit:digit,digits:digits,integer:integer};const C=chars;const F=flow;const N=numbers;var index={parser:Parser,response:response};class TokenDefinition{constructor(parser,name,precedence){this.parser=parser;this.name=name;this.precedence=precedence}}class Token{constructor(name,value){this.name=name;this.value=value}accept(name){return this.name===name?option.some(this.value):option.none()}}class GenLex{constructor(){this.spaces=defaultSpaces();this.definitions=[];this.tokensMap={}}tokenize(parser,name,precedence=1e3){if(typeof parser==="string"){if(name===undefined){name=parser}return this.tokenize(C.string(parser),name,precedence)}const definition=new TokenDefinition(parser,name,precedence);this.definitions.push(definition);const token=literal((token=>token.accept(name)),name);this.tokensMap[name]=token;return token}keywords(keys,precedence=1e3){return keys.reduce(((acc,key)=>acc.concat(this.tokenize(key,key,precedence))),[])}setSeparators(spacesCharacters){if(typeof spacesCharacters!=="string"){throw"setSeparators needs a string as separators, such as ' \r\n\f\t' ;"+" use setSeparatorsParser to declare a parser"}this.spaces=C.charIn(spacesCharacters).map((()=>unit))}setSeparatorsParser(spacesParser){this.spaces=spacesParser.map((()=>unit))}updatePrecedence(tokenName,precedence){this.definitions.find((def=>def.name===tokenName)).precedence=precedence}buildTokenizer(){const token=this.findTokenByPrecedence();return this.spaces.optrep().drop().then(token).then(this.spaces.optrep().drop()).single()}use(grammar){return this.buildTokenizer().chain(grammar)}findTokenByPrecedence(){const sortedDefinitions=this.definitions.sort(((d1,d2)=>d2.precedence-d1.precedence));return sortedDefinitions.reduce(((combinator,definition)=>F.try(getTokenParser(definition)).or(combinator)),F.error())}remove(tokenName){this.definitions=this.definitions.filter((d=>d.name!==tokenName));delete this.tokensMap[tokenName]}tokens(){return this.tokensMap}get(tokenName){return this.tokensMap[tokenName]}}function getTokenParser(def){return def.parser.map((value=>new Token(def.name,value)))}function literal(tokenize,name){return F.parse(((input,index)=>input.get(index).map((value=>tokenize(value).map((token=>response.accept(token,input,index+1,true))).orLazyElse((()=>response.reject(input,index,false))))).lazyRecoverWith((()=>response.reject(input,index,false)))))}function defaultSpaces(){return C.charIn(" \r\n\f\t").map((()=>unit))}function getMathGenLex(){const basicGenlex=new GenLex;basicGenlex.tokenize(N.number(),"number",1100);basicGenlex.tokenize(C.char("+"),"plus",1e3);basicGenlex.tokenize(C.char("-"),"minus",1e3);basicGenlex.tokenize(C.char("*"),"mult",800);basicGenlex.tokenize(C.char("/"),"div",800);basicGenlex.tokenize(C.char("("),"open",1e3);basicGenlex.tokenize(C.char(")"),"close",1e3);return basicGenlex}let genlex=new GenLex;genlex.keywords(["null","false","true","{","}","[","]",":",","]);let number=genlex.tokenize(N.number(),"number",1100);let string=genlex.tokenize(C.stringLiteral(),"string",800);function tkKey(s){return genlex.get(s)}function arrayOrNothing(){var value=[],addValue=e=>{value=value.concat(e)},getValue=()=>value,item=F.lazy(expr).map(addValue);return item.then(tkKey(",").thenRight(item).optrep().array()).opt().map(getValue)}function objectOrNothing(){var value={},addValue=e=>{value[e[0]]=e[1]},getValue=()=>value,attribute=string.thenLeft(tkKey(":")).then(F.lazy(expr)).array().map(addValue);return attribute.thenLeft(tkKey(",").then(attribute).optrep()).array().opt().map(getValue)}function expr(){return number.or(string).or(tkKey("null").returns(null)).or(tkKey("true").returns(true)).or(tkKey("false").returns(false)).or(tkKey("[").thenRight(F.lazy(arrayOrNothing)).thenLeft(tkKey("]")).single()).or(tkKey("{").thenRight(F.lazy(objectOrNothing)).thenLeft(tkKey("}")).single())}var jsonParser={parse:function(source){const parser=genlex.use(expr().thenLeft(F.eos()).single());return parser.parse(source,0)}};var standard={jsonParser:jsonParser};const JSON=standard.jsonParser;const MD=standard.markdownBundle;export{C,F,GenLex,JSON,MD,N,NEUTRAL,Parser,stream as Streams,Tuple,accept,index$1 as data,getMathGenLex,isTuple,index as parsec,reject,standard,tuple}; //# sourceMappingURL=masala-parser.js.map