UNPKG

cbon

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