LemonJS/main.js

82 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-01-28 12:08:59 +00:00
/**
* Created by Aleksey Chichenkov <a.chichenkov@initi.ru> on 1/28/19.
*/
var js_beautify = require("js-beautify");
var args = require("args-parser")(process.argv);
var fs = require("fs");
var exec = require('child_process').exec;
2019-01-29 08:02:47 +00:00
var config;
if(args["c"] !== undefined) {
config = require("./" + args["c"]);
} else {
config = require("./config.js");
}
var program_path = config.lemon_bin;
var lemon_flags = config.flags;
var parser_path = config.out_path;
var file_name = config.file_name;
var fn_y = file_name + ".y";
var fn_js = file_name + ".js";
var fn_out = file_name + ".out";
var temp_fn_y = "temp_" + "file_name" + ".y";
var temp_fn_js = "temp_" + "file_name" + ".js";
var temp_fn_out = "temp_" + "file_name" + ".out";
2019-01-28 12:08:59 +00:00
var update_parser_y = function () {
2019-01-29 08:02:47 +00:00
var source_parser_y = fs.readFileSync(parser_path + fn_y, "utf8");
2019-01-28 12:08:59 +00:00
var result = /&&.*?REPLACER\{(.*?)\}&&/gm.exec(source_parser_y);
if(result) {
var file_path = result[1];
var process_code = fs.readFileSync(file_path, "utf8");
source_parser_y = source_parser_y.replace(/&&.*?REPLACER\{(.*?)\}&&/gm, process_code);
2019-01-29 08:02:47 +00:00
fs.writeFileSync(parser_path + temp_fn_y, source_parser_y);
2019-01-28 12:08:59 +00:00
}
};
var post_process_parser = function () {
2019-01-29 08:02:47 +00:00
var out_js = fs.readFileSync(parser_path + temp_fn_js, "utf8");
if(args["t"] !== undefined) {
switch (args["t"]) {
case "web":
out_js = "(function(){\n" + out_js + "return Lexer; \n})()";
break;
case "node":
out_js += "\n\n module.exports = LemonJS;";
break;
}
}
2019-01-28 12:08:59 +00:00
out_js = js_beautify(out_js, {indent_size: 4, space_in_empty_paren: true});
2019-01-29 08:02:47 +00:00
fs.writeFileSync(parser_path + fn_js, out_js);
var temp_parser_out = fs.readFileSync(parser_path + temp_fn_out, "utf8");
fs.writeFileSync(parser_path + fn_out, temp_parser_out);
2019-01-28 12:08:59 +00:00
};
var start = function () {
update_parser_y();
2019-01-29 08:02:47 +00:00
exec(program_path + " " + parser_path + temp_fn_y + " " + lemon_flags, function(err, stdout, stderr) {
2019-01-28 12:08:59 +00:00
err && console.log("ERROR: ", err);
err && process.exit(1);
post_process_parser();
2019-01-29 08:02:47 +00:00
fs.unlinkSync(parser_path + temp_fn_y);
fs.unlinkSync(parser_path + temp_fn_js);
fs.unlinkSync(parser_path + temp_fn_out);
2019-01-28 12:08:59 +00:00
});
};
start();