fix readme and add input config

This commit is contained in:
Aleksey Chichenkov 2019-01-30 14:50:02 +03:00
parent 11948ecb73
commit a7873cc203
9 changed files with 61 additions and 29 deletions

View File

@ -6,30 +6,45 @@
## Compile lemon-js ## Compile lemon-js
```bash ```bash
gcc -o ./lemon-src/lemon-js -O2 ./lemon-src/lemon-js.c gcc -o ./lemon-src/lemon-js -O2 ./lemon-src/lemon-js.c
npm install npm install
``` # or
or
```bash
./build.sh ./build.sh
``` ```
## Example ## Example (One minute start)
Also you can build example config Also you can build example config
```bash ```bash
./build-example.sh ./build-example.sh
node test.js -c=config.js node main.js
#or test if want check work
node test.js
```
## Configurate
You need create your config.js from config.template or config.example
```javascript
var config = {
lemon_bin: "./lemon-src/lemon-js", // папка с исходниками лемона
input_path: "parser_template/", // папка с шаблоном парсера
output_path: "out/", // папка куда будет выгружен файл
file_name: "parser", // имя файла шаблона
flags: "-l", // флаги для ./lemon-js
require_templates: "require_js/" // Папка для шаблона require_js/
};
```
```bash
cat config.template > config.js
# or
cat config.example > config.js
``` ```
## Start ## Start
You need create your config.js from config.template
```bash ```bash
node main.js --c=<your_config>.js --t=<web|node> node main.js --c=<your_config>.js --t=<web|node>
``` ```
default config -> config.js
## Test ## Test
Also you can test your parser Also you can test your parser
```bash ```bash

View File

@ -4,4 +4,6 @@ gcc -o ./lemon-src/lemon-js -O2 ./lemon-src/lemon-js.c
npm install npm install
cat config.example > config.js cat config.example > config.js
node main.js

View File

@ -1,6 +1,7 @@
var config = { var config = {
lemon_bin: "./lemon-src/lemon-js", lemon_bin: "./lemon-src/lemon-js",
out_path: "example/", input_path: "example/",
output_path: "example/",
/** /**
* В папке назначения должен лежать файл parser.y; * В папке назначения должен лежать файл parser.y;

View File

@ -1,6 +1,7 @@
var config = { var config = {
lemon_bin: "./lemon-src/lemon-js", lemon_bin: "./lemon-src/lemon-js",
out_path: "output/", input_path: "parser_template/",
output_path: "out/",
/** /**
* В папке назначения должен лежать файл parser.y; * В папке назначения должен лежать файл parser.y;

View File

@ -11,7 +11,7 @@
} }
%code { %code {
&&REPLACER{example/test_code_environment.js}&& &&REPLACER{test_code_environment.js}&&
var _result = { var _result = {
error: false error: false
@ -48,4 +48,4 @@
console.log("Syntax error"); console.log("Syntax error");
} }
&&REPLACER{example/rules.y}&& &&REPLACER{rules.y}&&

29
main.js
View File

@ -16,7 +16,8 @@ if(args["c"] !== undefined) {
var program_path = config.lemon_bin; var program_path = config.lemon_bin;
var lemon_flags = config.flags; var lemon_flags = config.flags;
var parser_path = config.out_path; var input_path = config.input_path;
var output_path = config.output_path;
var file_name = config.file_name; var file_name = config.file_name;
var fn_y = file_name + ".y"; var fn_y = file_name + ".y";
@ -29,17 +30,17 @@ var temp_fn_out = "temp_" + "file_name" + ".out";
var update_parser_y = function () { var update_parser_y = function () {
var source_parser_y = fs.readFileSync(parser_path + fn_y, "utf8"); var source_parser_y = fs.readFileSync(input_path + fn_y, "utf8");
source_parser_y = source_parser_y.replace(/&&.*?REPLACER\{(.*?)\}&&/gm, function (match, file_path, offset, string) { source_parser_y = source_parser_y.replace(/&&.*?REPLACER\{(.*?)\}&&/gm, function (match, file_path, offset, string) {
return fs.readFileSync(file_path, "utf8"); return fs.readFileSync("./" + input_path + file_path, "utf8");
}); });
fs.writeFileSync(parser_path + temp_fn_y, source_parser_y); fs.writeFileSync(input_path + temp_fn_y, source_parser_y);
}; };
var post_process_parser = function () { var post_process_parser = function () {
var out_js = fs.readFileSync(parser_path + temp_fn_js, "utf8"); var out_js = fs.readFileSync(input_path + temp_fn_js, "utf8");
if (args["t"] !== undefined) { if (args["t"] !== undefined) {
switch (args["t"]) { switch (args["t"]) {
@ -56,24 +57,28 @@ var post_process_parser = function () {
out_js = js_beautify(out_js, {indent_size: 4, space_in_empty_paren: true}); out_js = js_beautify(out_js, {indent_size: 4, space_in_empty_paren: true});
fs.writeFileSync(parser_path + fn_js, out_js); if(!fs.existsSync(output_path)){
fs.mkdirSync(output_path);
}
var temp_parser_out = fs.readFileSync(parser_path + temp_fn_out, "utf8"); fs.writeFileSync(output_path + fn_js, out_js);
fs.writeFileSync(parser_path + fn_out, temp_parser_out);
var temp_parser_out = fs.readFileSync(input_path + temp_fn_out, "utf8");
fs.writeFileSync(output_path + fn_out, temp_parser_out);
}; };
var start = function () { var start = function () {
update_parser_y(); update_parser_y();
exec(program_path + " " + parser_path + temp_fn_y + " " + lemon_flags, function(err, stdout, stderr) { exec(program_path + " " + input_path + temp_fn_y + " " + lemon_flags, function(err, stdout, stderr) {
err && console.log("ERROR: ", err); err && console.log("ERROR: ", err);
err && process.exit(1); err && process.exit(1);
post_process_parser(); post_process_parser();
fs.unlinkSync(parser_path + temp_fn_y); fs.unlinkSync(input_path + temp_fn_y);
fs.unlinkSync(parser_path + temp_fn_js); fs.unlinkSync(input_path + temp_fn_js);
fs.unlinkSync(parser_path + temp_fn_out); fs.unlinkSync(input_path + temp_fn_out);
}); });
}; };

View File

@ -27,6 +27,14 @@
parser.parse(parser["TOKEN_" + token.lexeme], token); parser.parse(parser["TOKEN_" + token.lexeme], token);
lexemes.push(token); lexemes.push(token);
break; break;
case -2:
// end of search
// do nothing
break;
case -1:
// empty string or not found any lexemes
// do nothing
break;
case 1: case 1:
return { return {
success: false, success: false,
@ -83,4 +91,4 @@
console.log("Syntax error"); console.log("Syntax error");
} }
&&REPLACER{output/rules.y}&& &&REPLACER{rules.y}&&

View File

@ -23,7 +23,7 @@ exec("node main.js -o=" + cfg_path + " -t=node", function(err, stdout, stderr) {
}); });
var test = function() { var test = function() {
var LemonJS = require("./" + config.out_path + "/" + config.file_name + ".js"); var LemonJS = require("./" + config.input_path + "/" + config.file_name + ".js");
if (!fs.existsSync("tests")) { if (!fs.existsSync("tests")) {
fs.mkdirSync("tests"); fs.mkdirSync("tests");