UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 4.86 kB
const t=/[\w|/]/;class e{constructor(t){this._symbols=t,this._index=0,this._last=0,this._cur=this._symbols.length>0?this._symbols[0]:null,this._buf=[],this._mode="text",this._error=null}read(){let t=this._read();for(;8===t;)t=this._read();return 0!==t&&1!==t&&(this._last=this._index),t}buf(){return this._buf}last(){return this._last}error(){return this._error}debugPrint(){const t=["EOF","ERROR","TEXT","OPEN_BRACKET","CLOSE_BRACKET","EQUALS","STRING","IDENTIFIER","WHITESPACE"];let e=this.read(),r="";for(;r+=`${(r.length>0?"\n":"")+t[e]} '${this.buf().join("")}'`,0!==e&&1!==e;)e=this.read();return r}_read(){return this._buf=[],this._eof()?0:"text"===this._mode?this._text():this._tag()}_text(){for(;;)switch(this._cur){case null:return this._buf.length>0?2:0;case"[":return this._mode="tag",this._buf.length>0?2:this._tag();case"\\":if(this._next(),"["===this._cur)this._store();else this._output("\\");break;default:this._store()}}_tag(){switch(this._cur){case null:return this._error="unexpected end of input reading tag",1;case"[":return this._store(),3;case"]":return this._store(),this._mode="text",4;case"=":return this._store(),5;case" ":case"\t":case"\n":case"\r":case"\v":case"\f":return this._whitespace();case'"':return this._string();default:return this._isIdentifierSymbol(this._cur)?this._identifier():(this._error="unrecognized character",1)}}_whitespace(){for(this._store();-1!==" \t\n\r\v\f".indexOf(this._cur);)this._store();return 8}_string(){for(this._next();;)switch(this._cur){case null:return this._error="unexpected end of input reading string",1;case'"':return this._next(),6;default:this._store()}}_identifier(){for(this._store();null!==this._cur&&this._isIdentifierSymbol(this._cur);)this._store();return 7}_isIdentifierSymbol(e){return 1===e.length&&null!==e.match(t)}_eof(){return null===this._cur}_next(){return this._eof()||(this._index++,this._cur=this._index<this._symbols.length?this._symbols[this._index]:null),this._cur}_store(){return this._buf.push(this._cur),this._next()}_output(t){this._buf.push(t)}}class r{constructor(t){this._scanner=new e(t),this._error=null}parse(t,e){for(;;){switch(this._scanner.read()){case 0:return!0;case 1:default:return!1;case 2:Array.prototype.push.apply(t,this._scanner.buf());break;case 3:if(!this._parseTag(t,e))return!1}}}error(){return`Error evaluating markup at #${this._scanner.last().toString()} (${this._scanner.error()||this._error})`}_parseTag(t,e){let r=this._scanner.read();if(7!==r)return this._error="expected identifier",!1;const s=this._scanner.buf().join("");if("/"===s[0]){for(let n=e.length-1;n>=0;--n)if(s===`/${e[n].name}`&&null===e[n].end)return e[n].end=t.length,r=this._scanner.read(),4===r||(this._error="expected close bracket",!1);return this._error="failed to find matching tag",!1}const n={name:s,value:null,attributes:{},start:t.length,end:null};if(r=this._scanner.read(),5===r){if(r=this._scanner.read(),6!==r)return this._error="expected string",!1;n.value=this._scanner.buf().join(""),r=this._scanner.read()}for(;;){switch(r){case 4:return e.push(n),!0;case 7:{const t=this._scanner.buf().join("");if(r=this._scanner.read(),5!==r)return this._error="expected equals",!1;if(r=this._scanner.read(),6!==r)return this._error="expected string",!1;const e=this._scanner.buf().join("");n.attributes[t]=e;break}default:return this._error="expected close bracket or identifier",!1}r=this._scanner.read()}}}function s(t,e){for(const r in e){if(!e.hasOwnProperty(r))continue;const n=e[r];n instanceof Object?(t.hasOwnProperty(r)||(t[r]={}),s(t[r],e[r])):t[r]=n}}function n(t){if(0===t.length)return null;const e={};for(let r=0;r<t.length;++r){const n=t[r],i={};i[n.name]={value:n.value,attributes:n.attributes},s(e,i)}return e}function i(t){const e=new r(t),s=[],i=[];if(!e.parse(s,i))return console.warn(e.error()),{symbols:t,tags:null};const u=i.find((t=>null===t.end));if(u)return console.warn(`Markup error: found unclosed tag='${u.name}'`),{symbols:t,tags:null};const h=function(t,e){if(0===t.length)return null;const r={};for(let e=0;e<t.length;++e){const s=t[e];r.hasOwnProperty(s.start)?null===r[s.start].open?r[s.start].open=[s]:r[s.start].open.push(s):r[s.start]={open:[s],close:null},r.hasOwnProperty(s.end)?null===r[s.end].close?r[s.end].close=[s]:r[s.end].close.push(s):r[s.end]={open:null,close:[s]}}let s=[];function i(t){s=s.filter((e=>void 0===t.find((t=>t===e))))}function u(t){for(let e=0;e<t.length;++e)s.push(t[e])}const h=Object.keys(r).sort(((t,e)=>t-e)),o=[];for(let t=0;t<h.length;++t){const e=r[h[t]];null!==e.close&&i(e.close),null!==e.open&&u(e.open),o.push({start:h[t],tags:n(s)})}const a=[];let l=null;for(let t=0;t<o.length;++t){const e=o[t];for(;a.length<e.start;)a.push(l?l.tags:null);l=e}for(;a.length<e;)a.push(null);return a}(i,s.length);return{symbols:s,tags:h}}class u{static evaluate(t){return i(t)}}export{u as Markup};