Liferay.Loader.define("data-engine-js-components-web@1.0.87/js/utils/Tokenizer",["module","exports","require","./Token"],(function(e,t,n){window;{Object.defineProperty(t,"__esModule",{value:!0}),t.Tokenizer=void 0;var i=n("./Token");const e=["*","/","+","-"];class Tokenizer{static stringifyTokens(e){return e.reduce(((e,t)=>{let{value:n}=t;return t.type===i.Token.VARIABLE&&(n="[".concat(n,"]")),e+n}),"")}static tokenize(e){const t=[];e=e.replace(/\s/g,"");let n=[],s=[],o=[];const emptyNumberBuffer=()=>{s.length&&(t.push(new i.Token(i.Token.LITERAL,s.join(""))),s=[])},emptyVariableBuffer=()=>{t.push(new i.Token(i.Token.VARIABLE,o.join(""))),o=[]},r=e.split("");for(;r.length;){let e=r.shift();if(this.isDigit(e))s.push(e);else if(this.isDot(e))s.includes(".")||s.push(e);else if(this.isLeftBracket(e)){s.length&&(emptyNumberBuffer(),t.push(new i.Token(i.Token.OPERATOR,"*")));do{if(e=r.shift(),this.isRightBracket(e)){emptyVariableBuffer();break}o.push(e)}while(r.length)}else if(this.isLetter(e)){s.length&&(emptyNumberBuffer(),t.push(new i.Token(i.Token.OPERATOR,"*")));do{n.push(e),e=r.shift()}while(this.isLetter(e));void 0!==e&&r.unshift(e),t.push(new i.Token(i.Token.FUNCTION,n.join(""))),n=[]}else if(this.isOperator(e))emptyNumberBuffer(),t.push(new i.Token(i.Token.OPERATOR,e));else if(this.isLeftParenthesis(e))s.length&&(emptyNumberBuffer(),t.push(new i.Token(i.Token.OPERATOR,"*"))),t.push(new i.Token(i.Token.LEFT_PARENTHESIS,e));else{if(!this.isRightParenthesis(e))throw new Error("Unsupported character ".concat(e));emptyNumberBuffer(),t.push(new i.Token(i.Token.RIGHT_PARENTHESIS,e))}}return s.length&&emptyNumberBuffer(),o.length&&emptyVariableBuffer(),t}static isDot(e){return"."===e}static isDigit(e){return void 0!==e&&/[0-9]/.test(e)}static isLetter(e){return void 0!==e&&/[a-zA-Z]/.test(e)}static isLeftBracket(e){return"["===e}static isRightBracket(e){return"]"===e}static isLeftParenthesis(e){return"("===e}static isRightParenthesis(e){return")"===e}static isOperator(t){return e.includes(t)}static isValid(e){const t=Tokenizer.tokenize(e),n=t.filter((({type:e})=>e===i.Token.LEFT_PARENTHESIS)),s=t.filter((({type:e})=>e===i.Token.RIGHT_PARENTHESIS)),o=t.map((({type:e},n)=>e===i.Token.OPERATOR?Boolean(t[n-1])&&Boolean(t[n+1]):e!==i.Token.FUNCTION||Boolean(t[n+2]))).every((e=>!0===e));return n.length===s.length&&o}}t.Tokenizer=Tokenizer}}));
//# sourceMappingURL=Tokenizer.js.map