uascript
Version:
Javascript in Ukrainian
44 lines (43 loc) • 1.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.compile = void 0;
const keyword_map_1 = require("./keyword-map");
const parser_1 = require("./parser");
const STRING_PATTERN = '$';
const COMMENT_PATTERN = '%';
function compile(source) {
return new Promise((resolve, reject) => {
try {
const { source: sourceWithoutStrings, fragments: strings } = parseString(source);
const { source: sourceWithoutStringsAndComments, fragments: comments } = parseComments(sourceWithoutStrings);
source = sourceWithoutStringsAndComments;
Object.entries(keyword_map_1.dataMap).map(map => source = source.replace(new RegExp(map[1], 'g'), map[0]));
source = parser_1.restore(source, strings, STRING_PATTERN);
source = parser_1.restore(source, comments, COMMENT_PATTERN);
resolve(source);
}
catch (e) {
reject(e);
}
});
}
exports.compile = compile;
function parseString(source) {
const doubleQuoteStrings = source.match(/"(.*?)"/g) || [];
const singleQuoteStrings = source.match(/'(.*?)'/g) || [];
const templateQuoteStrings = source.match(/`((.|\n)*)`/g) || [];
const strings = [...doubleQuoteStrings, ...singleQuoteStrings, ...templateQuoteStrings];
return {
source: parser_1.parse(source, strings, STRING_PATTERN),
fragments: strings
};
}
function parseComments(source) {
const singleLineComment = source.match(/\/\/(.*?)\n/g) || [];
const multiLineStrings = source.match(/\/\*((.|\n)*)\*\//g) || [];
const comments = [...singleLineComment, ...multiLineStrings];
return {
source: parser_1.parse(source, comments, COMMENT_PATTERN),
fragments: comments
};
}