@malagu/core
Version:
171 lines • 6.25 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExpressionCompilerImpl = void 0;
const expression_protocol_1 = require("./expression-protocol");
const annotation_1 = require("../annotation");
let ExpressionCompilerImpl = class ExpressionCompilerImpl {
constructor() {
this.ESCAPE_CHAR = '\\';
this.SPECIAL_CHAR = '$';
this.BRACKET_BEGIN = '{';
this.BRACKET_END = '}';
}
compileSections(text) {
if (!text || text.indexOf(this.SPECIAL_CHAR) < 0) {
return [];
}
const sections = [];
let middleText = text;
while (middleText) {
const me = this.middleCompile(middleText);
if (!me) {
sections.push(middleText);
middleText = undefined;
}
else {
sections.push(me.expression);
middleText = me.nextText;
}
}
return sections;
}
middleCompile(text) {
let me;
if (text.startsWith('${{')) {
me = this.nextMiddleExpression(text.substring(3), 2);
}
else if (text.startsWith('${')) {
me = this.nextMiddleExpression(text.substring(2));
}
else {
me = this.nextString(text);
}
return me;
}
nextMiddleExpression(text, bracketBeginCharNum = 1) {
let stringed = false;
let escaped = false;
let bracketBeginCharFound = 0;
const section = [];
for (let i = 0; i < text.length; i++) {
const c = text[i];
if (!escaped) {
if ('\'' === c || '\"' === c) {
stringed = !stringed;
section.push(c);
continue;
}
else if (c === this.ESCAPE_CHAR) {
escaped = true;
continue;
}
}
if (stringed) {
section.push(c);
escaped = false;
}
else if (escaped) {
if (this.SPECIAL_CHAR === c || this.BRACKET_BEGIN === c || this.BRACKET_END === c) {
section.push(c);
}
else {
section.push(this.ESCAPE_CHAR);
section.push(c);
}
escaped = false;
}
else if (this.BRACKET_BEGIN === c) {
bracketBeginCharFound++;
section.push(c);
}
else if (this.BRACKET_END === c) {
if (bracketBeginCharFound === 0 && bracketBeginCharNum === 1) {
const jexlEngine = this.jexlEngineProvider.provide();
const expression = jexlEngine.createExpression(section.join(''));
let nextText;
if (i !== text.length - 1) {
nextText = text.substring(i + 1);
}
return { expression, nextText };
}
else if (bracketBeginCharFound > 0) {
bracketBeginCharFound--;
section.push(c);
}
else {
bracketBeginCharNum--;
}
}
else {
section.push(c);
}
}
}
nextString(text) {
let escaped = false;
let specialCharFound = false;
const section = [];
for (let i = 0; i < text.length; i++) {
const c = text[i];
if (!escaped) {
if ('\'' === c || '\"' === c) {
section.push(c);
continue;
}
else if (c === this.ESCAPE_CHAR) {
escaped = true;
continue;
}
}
if (escaped) {
if (this.SPECIAL_CHAR === c || this.BRACKET_BEGIN === c || this.BRACKET_END === c) {
section.push(c);
}
else {
section.push(this.ESCAPE_CHAR);
section.push(c);
}
escaped = false;
}
else if (specialCharFound) {
if (this.BRACKET_BEGIN === c) {
const expression = section.join('');
const nextText = text.substring(i - 1);
return { expression, nextText };
}
else {
specialCharFound = false;
section.push(this.SPECIAL_CHAR);
section.push(c);
}
}
else {
if (this.SPECIAL_CHAR === c) {
specialCharFound = true;
}
else {
section.push(c);
}
}
}
return { expression: section.join('') };
}
};
__decorate([
(0, annotation_1.Autowired)(expression_protocol_1.JexlEngineProvider),
__metadata("design:type", Object)
], ExpressionCompilerImpl.prototype, "jexlEngineProvider", void 0);
ExpressionCompilerImpl = __decorate([
(0, annotation_1.Component)(expression_protocol_1.ExpressionCompiler)
], ExpressionCompilerImpl);
exports.ExpressionCompilerImpl = ExpressionCompilerImpl;
//# sourceMappingURL=expression-compiler.js.map