@easyquery/core
Version:
EasyQuery.JS core modules
70 lines • 2.74 kB
JavaScript
var FormatParser = /** @class */ (function () {
function FormatParser(format) {
this.start(format);
}
FormatParser.prototype.start = function (format) {
this.formatStr = format;
this.pos = 0;
this.exprNum = 0;
this.tokenText = '';
};
FormatParser.prototype.skipSpaces = function () {
while (this.pos < this.formatStr.length && this.formatStr.charAt(this.pos) === ' ')
this.pos++;
};
FormatParser.prototype.next = function () {
this.skipSpaces();
if (this.pos >= this.formatStr.length)
return false;
var npos = 0;
if (this.formatStr.charAt(this.pos) === '{') {
npos = this.formatStr.indexOf('}', this.pos);
if (npos < 0)
return false;
this.tokenText = this.formatStr.substring(this.pos, npos + 1);
if (this.tokenText.indexOf('{expr') === 0) {
this.token = 'expression';
this.exprNum = parseInt(this.tokenText.substring(5, this.tokenText.length));
}
this.pos = npos + 1;
}
else if (this.formatStr.charAt(this.pos) === '[' && this.pos < this.formatStr.length - 1 && this.formatStr.charAt(this.pos + 1) === '[') {
this.pos += 2;
npos = this.formatStr.indexOf(']]', this.pos);
this.token = 'operator';
this.tokenText = this.formatStr.substring(this.pos, npos);
this.pos = npos + 2;
}
else {
this.token = 'text';
var npos1 = this.formatStr.indexOf('{', this.pos);
if (npos1 < 0)
npos1 = this.formatStr.length;
var npos2 = this.formatStr.indexOf('[[', this.pos);
if (npos2 < 0)
npos2 = this.formatStr.length;
npos = Math.min(npos1, npos2);
this.tokenText = this.formatStr.substring(this.pos, npos).trim();
this.pos = npos;
}
return true;
};
FormatParser.prototype.parse = function () {
var result = [];
while (this.next()) {
if (this.token === 'operator') {
result.push({ type: 'operator', text: this.tokenText });
}
else if (this.token === 'expression') {
result.push({ type: 'expression', index: this.exprNum - 1 });
}
else if (this.token === 'text') {
result.push({ type: 'text', text: this.tokenText });
}
}
return result;
};
return FormatParser;
}());
export { FormatParser };
//# sourceMappingURL=format_parser.js.map