UNPKG

cbon

Version:
2 lines (1 loc) 15.5 kB
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self).cbon={})}(this,(function(e){"use strict";class n{}class t extends n{constructor(e){super(),this.range=e}}class r extends n{}class s extends r{constructor(e,n){super(),this.items=n,this.range=e}}class o extends r{constructor(e,n){super(),this.items=n,this.range=e}}class i extends n{constructor(e,n){super(),this.val=n,this.range=e}}class l extends n{constructor(e,n,t){super(),this.val=n,this.col=t,this.range=e}}class a extends n{constructor(e,n){super(),this.val=n,this.range=e}}class c extends a{}class u extends a{}class f extends a{}class h extends a{}class d extends a{}class g extends a{}function p(e){return e.map(e=>e instanceof s?new y(e.range,e.items):e instanceof o?new m(e.range,e.items):e)}class v{}class w extends v{}class y extends w{constructor(e,n){super(),this.items=p(n),this.range=e}}class m extends w{constructor(e,n){super(),this.items=p(n),this.range=e}}class x extends v{constructor(e){super(),this.range=e}}class _ extends v{constructor(e){super(),this.range=e}}class b extends v{constructor(e,n,t){super(),this.val=n,this.col=t,this.range=e}}class F extends v{constructor(e,n){super(),this.val=n,this.range=e}}class k extends v{constructor(e,n){super(),this.val=n,this.range=e}}class N{constructor(e,n){this.key=n,this.range=e}}class j{constructor(e,n){this.type=n,this.range=e}}class ${constructor(e,n,t){this.key=e,this.val=n,this.split=t}}class S extends v{constructor(e,n,t){super(),this.items=t,this.begin=e,this.end=n}}class E extends v{constructor(e,n,t){super(),this.items=t,this.begin=e,this.end=n}}class q extends v{constructor(e){super(),this.items=e}}function B(){return new Promise(e=>e())}class C{constructor(e,n,t){this.count=e,this.char=n,this.line=t}}class A{constructor(e,n){this.from=e,this.to=n}}const O=Symbol("ReDo");class P extends Error{constructor(e){super(),this.err=e}}class T{constructor(e){this.count=0,this.char=0,this.line=0,this.states=[],this.lines=[],this.errors=[],this.queue=[],this.show_all_err=e}push(e){this.states.push(e)}pop(){return this.states.pop()}call(e){const n=this.states[this.states.length-1](e);if(null!=n)if(n===O)this.queue.push(()=>this.call(e));else{const{fn:t,ignoreFirst:r}=n;this.push(t),r||this.queue.push(()=>this.call(e))}}get pos(){return new C(this.count,this.char,this.line)}get lastPos(){const e=new C(this.count,this.char,this.line);return e.count--,0===e.char?(e.line--,e.char=this.lines[e.line]):e.char--,e}}class U{constructor(e){this.state=e}end(){this.state.pop()}call(e,...n){return{fn:e(new U(this.state),...n),ignoreFirst:!1}}callNoFirst(e,...n){return{fn:e(new U(this.state),...n),ignoreFirst:!0}}flag(){this.last_flag=this.state.pos}range(e=!1){var n;const t=e?this.state.lastPos:this.state.pos;return new A(null!=(n=this.last_flag)?n:t,t)}error(e,n){if(!this.state.show_all_err)throw new P({range:e,msg:n});this.state.errors.push({range:e,msg:n})}}const D=Symbol("continue"),I=Symbol("break");function M(){return!1}function J(e,n={show_all_err:!1,async:!1,cancel:M}){var r,i,l,a,c,u;const h=null!=(i=null===(r=n)||void 0===r?void 0:r.cancel)?i:M,g=new T(null!=(a=null===(l=n)||void 0===l?void 0:l.show_all_err)&&a);let p;g.push(function(e,n){const r=[];return i=>{if(i instanceof t)n(new q(r));else{if(i instanceof d)return e.callNoFirst(L,i,e=>r.push(e));if(i instanceof f)return e.callNoFirst(R,i,e=>r.push(e));i instanceof s?r.push(new y(i.range,i.items)):i instanceof o?r.push(new m(i.range,i.items)):e.error(i.range,"File root must have no content other than a document")}}}(new U(g),e=>{p=e}));let v=!1;const w=function(e){return null!=e[Symbol.asyncIterator]?e[Symbol.asyncIterator]():e[Symbol.iterator]()}(e);function*x(){if(0!=g.queue.length)return g.queue.pop()(),D;if(v)return I;const e=yield w.next();if(!0===e.done)return v=!0,D;g.call(e.value)}return(null!=(u=null===(c=n)||void 0===c?void 0:c.async)&&u?async function(){for(;!await h();){await B();const e=x();let n=e.next();n.done||(n=e.next(await n.value));const t=n.value;if(t!==D&&t===I)break}return g.errors.length,{err:g.errors,val:p}}:function(){for(;!h();){const e=x();let n=e.next();n.done||(n=e.next(n.value));const t=n.value;if(t!==D&&t===I)break}return g.errors.length,{err:g.errors,val:p}})()}function L(e,n,r){const s=[];return o=>{if(o instanceof t)return e.error(o.range,"Block is not closed"),e.end(),r(new S(n.range,o.range,s)),O;if(o instanceof c)s.push(new x(o.range));else if(o instanceof g)e.end(),r(new S(n.range,o.range,s));else{if(o instanceof d)return e.error(o.range,"Block content must start with a key"),e.callNoFirst(L,o,e=>{});if(o instanceof f)return e.error(o.range,"Block content must start with a key"),e.callNoFirst(R,o,e=>{});if(o instanceof l||o instanceof i)return e.callNoFirst(z,o,e=>s.push(e));o instanceof h?e.error(o.range,"No Array here"):e.error(o.range,"Block content must start with a key")}}}function R(e,n,r){const s=[];return o=>{if(o instanceof t)return e.error(o.range,"Array is not closed"),e.end(),r(new E(n.range,o.range,s)),O;if(o instanceof c)s.push(new x(o.range));else if(o instanceof h)e.end(),r(new E(n.range,o.range,s));else if(o instanceof g)e.error(o.range,"No Block here");else{if(!(o instanceof u))return e.call(G,e=>s.push(e));e.error(o.range,"Array cant have key")}}}function z(e,n,r){return s=>s instanceof t?(e.error(s.range,"There should be a key value here"),e.end(),O):s instanceof c?(e.error(s.range,"There should be a key value here"),e.end(),O):s instanceof u?(e.end(),e.callNoFirst(G,e=>{const t=new j(s.range,s.val),o=new $(new N(n.range,n instanceof l?new b(n.range,n.val,n.col):n.val),e,t);r(o)})):(e.end(),e.call(G,e=>{const t=new $(new N(n.range,n instanceof l?new b(n.range,n.val,n.col):n.val),e);r(t)}))}function G(e,n){return r=>{if(r instanceof t)return e.error(r.range,"There should be a value here"),e.end(),O;if(r instanceof l)e.end(),n(new b(r.range,r.val,r.col));else{if(r instanceof i)return e.end(),e.call(K,r,e=>{n(e)});if(r instanceof c||r instanceof u||r instanceof h||r instanceof g)return e.error(r.range,"There should be a value here"),e.end(),O;if(r instanceof d)return e.end(),e.callNoFirst(L,r,e=>{n(e)});if(r instanceof f)return e.end(),e.callNoFirst(R,r,e=>{n(e)});e.end()}}}const H=/(0x[\da-fA-F_]+)|(([\-]?([\d\_])+)\.([\-]?([\d\_])+([eE]([\-]?)\d+)?))|(([\-]?([\d\_])+)\.([eE]([\-]?)\d+)?)|([\-]?\.(([\d\_])+([eE]([\-]?)\d+)?))|(([\-]?([\d\_])+([eE]([\-]?)\d+)?))/i;function K(e,n,t){return r=>{e.end(),"true"===n.val?t(new k(n.range,!0)):"false"===n.val?t(new k(n.range,!1)):"null"===n.val?t(new _(n.range)):H.test(n.val)?t(new F(n.range,Number(n.val.replace("_","")))):t(new b(n.range,n.val,null))}}class Q{get(e,n){return n}call(e,...n){return()=>e(this,...n)}}function V(e,n={async:!1}){var t;return(null===(t=n)||void 0===t?void 0:t.async)?async function(e,...n){var t,r;const s=[e(new Q,...n)];let o=void 0;do{const e=s[s.length-1],n=await e.next(o);if(o=void 0,n.done)s.pop(),o=n.value;else{const e=null===(r=(t=n).value)||void 0===r?void 0:r.call(t);null!=e&&s.push(e)}}while(s.length>0&&(await B(),1));return o}(W,e):function(e,...n){var t,r;const s=[e(new Q,...n)];let o=void 0;for(;s.length>0;){const e=s[s.length-1].next(o);if(o=void 0,e.done)s.pop(),o=e.value;else{const n=null===(r=(t=e).value)||void 0===r?void 0:r.call(t);null!=n&&s.push(n)}}return o}(W,e)}function*W(e,n){const t=[];for(const r of n.items)r instanceof S?t.push(yield e.call(Y,r)):r instanceof E&&t.push(yield e.call(Z,r));return t}function*X(e,n){return n instanceof S?yield e.call(Y,n):n instanceof E?yield e.call(Z,n):n instanceof F?n.val:n instanceof b?n.val:n instanceof k?n.val:n instanceof _?null:void 0}function*Y(e,n){const t=[];for(const s of n.items){if(s instanceof x)continue;const n=yield e.call(X,s.val);void 0!==n&&t.push([(r=s.key,r.key instanceof b?r.key.val:r.key),n])}var r;return Object.fromEntries(t)}function*Z(e,n){const t=[];for(const r of n.items){const n=yield e.call(X,r);void 0!==n&&t.push(n)}return t}const ee=Symbol("EOF"),ne=/\s/;function te(e,n={show_all_err:!1,iterable:!1,async:!1,cancel:M}){var r,s,o,i,l,a,p,v,w,y;const m=null!=(s=null===(r=n)||void 0===r?void 0:r.cancel)?s:M,x=new T(null!=(i=null===(o=n)||void 0===o?void 0:o.show_all_err)&&i),_=[];var b,F;x.push((b=new U(x),F=e=>{_.push(e)},e=>{if(e===ee)b.end();else{if(ne.test(e))return;if(","!==e&&":"!==e&&"="!==e&&"["!==e&&"]"!==e&&"{"!==e&&"}"!==e)return'"'===e||"'"===e?b.callNoFirst(re,e,F):"/"===e||"#"===e?b.callNoFirst(ae,e,F):b.call(ie,F);b.flag(),F(function(e,n){switch(n){case",":return new c(e,n);case":":case"=":return new u(e,n);case"[":return new f(e,n);case"]":return new h(e,n);case"{":return new d(e,n);case"}":return new g(e,n)}}(b.range(),e))}}));let k=null,N=!1;const j=e[Symbol.iterator]();function $(){if(0!=x.queue.length)return x.queue.pop()(),D;let e;if(N)e=ee;else{const n=j.next();if(!0===n.done)return N=!0,D;e=n.value}if(x.call(e),N)return I;x.count++,"\n"===e?("\r"!==k&&(x.lines[x.line]=x.char,x.line++,x.char=0),k=null):"\r"===e?(x.lines[x.line]=x.char,x.line++,x.char=0,k="\r"):(x.char++,k=null)}return(null!=(a=null===(l=n)||void 0===l?void 0:l.iterable)&&a?null!=(v=null===(p=n)||void 0===p?void 0:p.async)&&v?async function*(){let e=!1;for(;!await m()&&(await B(),0!==_.length&&(yield _.shift()),!e);){const n=$();n!==D&&(n!==I||(e=!0))}if(yield new t(new A(x.pos,x.pos)),0!==x.errors.length)return x.errors}:function*(){let e=!1;for(;!m()&&(0!==_.length&&(yield _.shift()),!e);){const n=$();n!==D&&(n!==I||(e=!0))}if(yield new t(new A(x.pos,x.pos)),0!==x.errors.length)return x.errors}:null!=(y=null===(w=n)||void 0===w?void 0:w.async)&&y?async function(){for(;!await m();){await B();const e=$();if(e!==D&&e===I)break}return _.push(new t(new A(x.pos,x.pos))),0!==x.errors.length?{err:x.errors,val:_}:{val:_}}:function(){for(;!m();){const e=$();if(e!==D&&e===I)break}return _.push(new t(new A(x.pos,x.pos))),0!==x.errors.length?{err:x.errors,val:_}:{val:_}})()}function re(e,n,t){const r=[];return e.flag(),s=>{if(s===ee)return e.error(e.range(),"String is not closed"),e.end(),O;if('"'===s||"'"===s)if(s===n){const s=new l(e.range(),r.join(""),n);e.end(),t(s)}else r.push(s);else{if("\\"===s)return e.callNoFirst(oe,e=>r.push(e));r.push(s)}}}const se=/[0-9a-fA-F]/i;function oe(e,n){let t=!1,r=[],s=!1;return e.flag(),o=>{if(t){if(o===ee)return e.error(e.range(),"Unicode escape is not finish"),e.end(),O;if(se.test(o))r.push(o),s||4!==r.length||(e.end(),n(String.fromCodePoint(Number(`0x${r.join("")}`))));else if("{"===o){if(0!==r.length||s)return e.error(e.range(),"Unicode escape is not finish"),e.end(),O;s=!0}else{if("}"!==o)return e.error(e.range(),"Unicode escape is not finish"),e.end(),O;if(e.end(),!s)return void e.error(e.range(),"Not in Unicode escape block");if(0===r.length||r.length>6)return void e.error(e.range(),"Invalid Unicode escape sequence");n(String.fromCodePoint(Number(`0x${r.join("")}`)))}}else if("u"===o)t=!0;else{if(e.end(),o===ee)return O;n("n"===o?"\n":"r"===o?"\r":"t"===o?"\t":"\\"===o?"\\":'"'===o?'"':"'"===o?"'":"0"===o?"\0":"b"===o?"\b":"f"===o?"\f":"v"===o?"\v":o)}}}function ie(e,n){const t=[];return e.flag(),r=>r===ee||ne.test(r)||'"'===r||"'"===r||","===r||":"===r||"="===r||"["===r||"]"===r||"{"===r||"}"===r?(n(new i(e.range(!0),t.join(""))),e.end(),O):"/"===r||"#"===r?(n(new i(e.range(!0),t.join(""))),e.end(),e.callNoFirst(ae,r,n)):void t.push(r)}const le=ae;function ae(e,n,t,r=!1){e.flag();const s=e.last_flag;return o=>(e.end(),"/"===n?"/"===o?e.callNoFirst(ce,n,s,t):"*"===o?e.callNoFirst(ue,n,s,t):r?(t(o),O):(e.error(e.range(),"Line Comment need two /"),e.call(ce,n,s,t)):"*"===o?e.callNoFirst(ue,n,s,t):r?(t(o),O):e.call(ce,n,s,t))}function ce(e,n,t,r){const o=[],i=[];return e.last_flag=t,n=>n===ee||"\n"===n||"\r"===n?(o.length>0&&i.push(o.join("")),e.end(),r(new s(e.range(),i)),O):"/"===n||"#"===n?(o.length>0&&i.push(o.join("")),o.length=0,e.callNoFirst(ae,n,e=>{i.push(e)})):void o.push(n)}function ue(e,n,t,r){const i=[],l=[];let a=!1;e.last_flag=t;let c=!1;return t=>{if(c)return e.end(),O;if(t===ee)return a&&i.push("*"),i.length>0&&l.push(i.join("")),e.end(),r(new o(e.range(),l)),e.flag(),e.error(e.range(),"Block Comment is not close"),O;if("*"===t)a?i.push("*"):a=!0;else if("/"===t||"#"===t){if(!a)return e.callNoFirst(le,t,e=>{e===ee?i.push(t):e instanceof s||e instanceof o?(i.length>0&&l.push(i.join("")),i.length=0,l.push(e)):(i.push(t),i.push(e))},!0);if(a=!1,t===n)return e.callNoFirst(le,t,n=>{n instanceof s||n instanceof o?(i.length>0&&l.push(i.join("")),i.length=0,l.push(n)):(i.length>0&&l.push(i.join("")),i.length=0,c=!0,r(new o(e.range(),l)))},!0);i.push("*"),i.push(t)}else a&&i.push("*"),a=!1,i.push(t)}}const fe={string:"'",splitter:null,comma:!1,comma_when_only_one_line:!0,split_before_brackets:!1,strict_string:!1,strict_key:!1,tab:" ",tab_count:2},he={...fe,comma_when_only_one_line:!1},de={...fe,string:'"',splitter:":",comma:!0,split_before_brackets:!0,strict_string:!0,strict_key:!0};function ge(e){return"min"===e?he:fe}const pe=/'/gu,ve=/d/gu,we=/["'\s]|true|false|null/u,ye=/[\d\-\+]/u;function me(e){return e.length>0&&!we.test(e)}e.DefaultFormat=fe,e.JsonFormat=de,e.MinFormat=he,e.parser=function(e,n){var t,r,s,o,i,l,a;const c=null!=(r=null===(t=n)||void 0===t?void 0:t.cancel)?r:M,u=null!=(o=null===(s=n)||void 0===s?void 0:s.show_all_err)&&o,f=null!=(l=null===(i=n)||void 0===i?void 0:i.async)&&l;let h=!1;const d=[];let g;function*p(t){var r,s,o;try{const n=yield J(yield te(e,{show_all_err:u,iterable:!0,async:f,cancel:t}),{show_all_err:u,async:f,cancel:t});null!=n.err&&d.push(...(o=n.err,o.flatMap(e=>[`${e.msg}`,` at ${e.range.from.line+1}:${e.range.from.char+1} to ${e.range.to.line+1}:${e.range.to.char+1}`]))),g=n.val}catch(e){if(!(e instanceof P))throw e;d.push(e.err.msg)}if(0!==d.length)throw new SyntaxError(`\n ${d.join("\n ")}\n`);if(yield)return null;const i=yield V(g,{async:f});return null!=(s=null===(r=n)||void 0===r?void 0:r.alwaysDocs)&&s?i:1==i.length?i[0]:i}return((null===(a=n)||void 0===a?void 0:a.async)?async function(){async function e(){return h||(h=await c()),h}const n=p(e);return n.next(await n.next(await n.next().value).value),n.next(await n.next(await e()).value).value}:function(){function e(){return h||(h=c()),h}const n=p(e);return n.next(n.next(n.next().value).value),n.next(n.next(e()).value).value})()},e.stringify=function(e,n){var t,r,s,o;const i=null!=(r=null===(t=n)||void 0===t?void 0:t.style)?r:null;return function e(n,t,r,s){var o;const{string:i,comma:l,splitter:a}=r;if("string"==typeof n){if(function(e){return me(e)&&!ye.test(e[0])}(n))return n;const e=n.replace('"'===i?ve:pe,`\\${i}`);return`${i}${e}${i}`}if("number"==typeof n||"bigint"==typeof n||"boolean"==typeof n)return`${n}`;if(null==n)return"null";if("object"==typeof n){if(s.has(n))throw TypeError("Converting circular structure to CBON");s.add(n);const c=null!=(o=n.toCBON)?o:n.toJSON;if("function"==typeof c)return c();if(n instanceof Array){const o=n.map(n=>{const o=e(n,t,r,s);return void 0===o?"null":o}).join(l?",":" ");return s.delete(n),`[${o}]`}{const o=[];for(const l in n){const a=n[l],c=e(a,t,r,s);void 0!==c&&o.push([me(l)?l:`${i}${l}${i}`,c])}const c=o.map(e=>e.join(null!=a?a:" ")).join(l?",":" ");return s.delete(n),`{${c}}`}}return}(e,"beautify"===i,{...ge(i),...(o=null===(s=n)||void 0===s?void 0:s.format,null!=o?o:{})},new Set)},e.styleFormat=ge,Object.defineProperty(e,"__esModule",{value:!0})}));