UNPKG

mframejs

Version:
226 lines 10.6 kB
Object.defineProperty(exports, "__esModule", { value: true }); var charcode_1 = require("./charcode"); var charCode = new charcode_1.CharCodes(); var Tokenizer = (function () { function Tokenizer(expression) { this.baseTokens = []; this.tokens = []; this.chars = []; this.curChar = null; this.curCharNo = 0; this.curtype = null; this.expressionOriginal = expression; this.isMix = expression.indexOf('${') !== -1; this.isMix = this.isMix ? true : expression.indexOf('@{') !== -1; this.isOutsideExpression = this.isMix; this.expression = this.setStrings(this.expressionOriginal); this.expression = this.removeWhitespaceExpressions(this.expression); this.curChar = this.expression.charCodeAt(this.curCharNo); this.expressionLength = this.expression.length; } Tokenizer.prototype.start = function () { this.generateBaseTokens(); this.combineBaseTokens(); return this.tokens; }; Tokenizer.prototype.setStrings = function (expression) { var text = this.isOutsideExpression; var trimmed = ''; if (text) { trimmed = '"'; } var count = 0; for (var i = 0; i < expression.length; i++) { switch (true) { case text && expression.charCodeAt(i) === '$'.charCodeAt(0) && expression.charCodeAt(i + 1) === '{'.charCodeAt(0): count++; trimmed = trimmed + '"' + expression[i]; text = false; break; case !text && expression.charCodeAt(i) === '$'.charCodeAt(0) && expression.charCodeAt(i + 1) === '{'.charCodeAt(0): count++; trimmed = trimmed + expression[i]; break; case text && expression.charCodeAt(i) === '@'.charCodeAt(0) && expression.charCodeAt(i + 1) === '{'.charCodeAt(0): count++; trimmed = trimmed + '"' + expression[i]; text = false; break; case !text && expression.charCodeAt(i) === '@'.charCodeAt(0) && expression.charCodeAt(i + 1) === '{'.charCodeAt(0): count++; trimmed = trimmed + expression[i]; break; case !text && expression.charCodeAt(i) === '}'.charCodeAt(0): count--; if (!count) { trimmed = trimmed + expression[i] + '"'; text = true; } else { trimmed = trimmed + expression[i]; } break; default: trimmed = trimmed + expression[i]; } } if (text) { trimmed = trimmed + '"'; } return trimmed; }; Tokenizer.prototype.removeWhitespaceExpressions = function (expression) { var text = false; var trimmed = ''; var stringcharType; for (var i = 0; i < expression.length; i++) { switch (true) { case !text && charCode.STRING_START_END.has(expression.charCodeAt(i)): trimmed = trimmed + expression[i]; stringcharType = expression.charCodeAt(i); text = true; break; case text && (stringcharType === expression.charCodeAt(i)): trimmed = trimmed + expression[i]; text = false; break; case !text && charCode.WHITESPACE.has(expression.charCodeAt(i)): break; default: trimmed = trimmed + expression[i]; break; } } return trimmed; }; Tokenizer.prototype.parsedAllChars = function () { var done = this.curCharNo < this.expressionLength; return !done; }; Tokenizer.prototype.advanceChar = function () { this.curCharNo++; this.curChar = this.expression.charCodeAt(this.curCharNo); }; Tokenizer.prototype.addToken = function () { var val = this.chars.map(function (a) { return String.fromCharCode(a); }).join(''); this.baseTokens.push({ type: this.curtype, value: this.curtype === 'number' ? parseFloat(val) : val }); this.chars = []; this.curtype = null; }; Tokenizer.prototype.generateBaseTokens = function () { var done = this.parsedAllChars(); while (!done) { switch (true) { case this.curtype === null && charCode.NUMBER.has(this.curChar): this.curtype = 'number'; while (charCode.NUMBER.has(this.curChar) && !this.parsedAllChars()) { this.chars.push(this.curChar); this.advanceChar(); } this.addToken(); break; case this.curtype === null && charCode.STRING_START_END.has(this.curChar): this.curtype = 'string'; var stringcharType = this.curChar; this.advanceChar(); while ((stringcharType !== this.curChar) && !this.parsedAllChars()) { this.chars.push(this.curChar); this.advanceChar(); } this.advanceChar(); this.addToken(); break; case this.curtype === null && charCode.OPERATOR.has(this.curChar): this.curtype = 'operator'; this.chars.push(this.curChar); this.advanceChar(); this.addToken(); break; default: this.curtype = 'variable'; while (!charCode.OPERATOR.has(this.curChar) && !this.parsedAllChars()) { this.chars.push(this.curChar); this.advanceChar(); } this.addToken(); } done = this.parsedAllChars(); } }; Tokenizer.prototype.advanceNextBaseToken = function () { this.baseTokenNo++; this.token = this.baseTokens[this.baseTokenNo]; }; Tokenizer.prototype.combineBaseTokens = function () { this.tokens = []; this.baseTokenNo = 0; while (this.baseTokenNo < this.baseTokens.length) { this.token = this.baseTokens[this.baseTokenNo]; switch (true) { case this.token.type === 'variable': var root = this.tokens[this.tokens.length - 1]; if (!root || root && root.value !== '.' && root.value !== '[') { this.token.root = true; } this.tokens.push(this.token); this.advanceNextBaseToken(); break; case this.token.type === 'string': this.tokens.push(this.token); this.advanceNextBaseToken(); break; case this.token.type === 'operator': var next1 = this.baseTokens[this.baseTokenNo + 1] ? this.baseTokens[this.baseTokenNo + 1] : undefined; var next2 = this.baseTokens[this.baseTokenNo + 2] ? this.baseTokens[this.baseTokenNo + 2] : undefined; if (next1 && next1.type === 'operator' && charCode.OPERATOR_COMBO.has(this.token.value + next1.value)) { if (next2 && next2.type === 'operator' && charCode.OPERATOR_COMBO.has(this.token.value + next1.value + next2.value)) { this.token.value = this.token.value + next1.value + next2.value; this.tokens.push(this.token); this.baseTokens.splice(this.baseTokenNo, 1); this.baseTokens.splice(this.baseTokenNo, 1); this.advanceNextBaseToken(); } else { this.token.value = this.token.value + next1.value; this.tokens.push(this.token); this.baseTokens.splice(this.baseTokenNo, 1); this.advanceNextBaseToken(); } } else { if ((this.token.value === '-' && next1.type === 'number') && this.tokens.length > 0 && this.tokens[this.tokens.length - 1].type === 'operator') { this.token.type = 'number'; } else { if ((this.token.value === '$' || this.token.value === '_') && next1.type === 'variable') { next1.value = this.token.value + next1.value; this.baseTokens.splice(this.baseTokenNo, 1); } else { this.tokens.push(this.token); this.advanceNextBaseToken(); } } } break; case this.token.type === 'number': var check = this.baseTokens[this.baseTokenNo + 1] ? this.baseTokens[this.baseTokenNo + 1] : undefined; while (check && check.type === 'number') { this.token.value = this.token.value + check.value; this.token.value = this.token.value * 1; this.baseTokens.splice(this.baseTokenNo, 1); check = this.baseTokens[this.baseTokenNo + 1] ? this.baseTokens[this.baseTokenNo + 1] : undefined; } this.tokens.push(this.token); this.advanceNextBaseToken(); } } }; return Tokenizer; }()); exports.Tokenizer = Tokenizer; //# sourceMappingURL=tokenizer.js.map