diff --git a/README.md b/README.md index d853fa1..30b204a 100644 --- a/README.md +++ b/README.md @@ -6,30 +6,45 @@ ## Compile lemon-js ```bash gcc -o ./lemon-src/lemon-js -O2 ./lemon-src/lemon-js.c - npm install -``` -or -```bash +# or ./build.sh ``` -## Example +## Example (One minute start) Also you can build example config ```bash ./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 -You need create your config.js from config.template - ```bash node main.js --c=.js --t= ``` -default config -> config.js - ## Test Also you can test your parser ```bash diff --git a/build-example.sh b/build-example.sh index 187e148..b27864a 100755 --- a/build-example.sh +++ b/build-example.sh @@ -4,4 +4,6 @@ gcc -o ./lemon-src/lemon-js -O2 ./lemon-src/lemon-js.c npm install -cat config.example > config.js \ No newline at end of file +cat config.example > config.js + +node main.js \ No newline at end of file diff --git a/config.example b/config.example index 9100e12..1102fa6 100644 --- a/config.example +++ b/config.example @@ -1,6 +1,7 @@ var config = { lemon_bin: "./lemon-src/lemon-js", - out_path: "example/", + input_path: "example/", + output_path: "example/", /** * В папке назначения должен лежать файл parser.y; diff --git a/config.template b/config.template index 6b68bec..5c88bd9 100644 --- a/config.template +++ b/config.template @@ -1,6 +1,7 @@ var config = { lemon_bin: "./lemon-src/lemon-js", - out_path: "output/", + input_path: "parser_template/", + output_path: "out/", /** * В папке назначения должен лежать файл parser.y; diff --git a/example/parser.y b/example/parser.y index f913f7f..b7b866c 100644 --- a/example/parser.y +++ b/example/parser.y @@ -11,7 +11,7 @@ } %code { - &&REPLACER{example/test_code_environment.js}&& + &&REPLACER{test_code_environment.js}&& var _result = { error: false @@ -48,4 +48,4 @@ console.log("Syntax error"); } -&&REPLACER{example/rules.y}&& \ No newline at end of file +&&REPLACER{rules.y}&& \ No newline at end of file diff --git a/main.js b/main.js index f43104c..aac5520 100644 --- a/main.js +++ b/main.js @@ -16,7 +16,8 @@ if(args["c"] !== undefined) { var program_path = config.lemon_bin; 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 fn_y = file_name + ".y"; @@ -29,17 +30,17 @@ var temp_fn_out = "temp_" + "file_name" + ".out"; 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) { - 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 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) { 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}); - 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(parser_path + fn_out, temp_parser_out); + fs.writeFileSync(output_path + fn_js, out_js); + + var temp_parser_out = fs.readFileSync(input_path + temp_fn_out, "utf8"); + fs.writeFileSync(output_path + fn_out, temp_parser_out); }; var start = function () { 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 && process.exit(1); post_process_parser(); - fs.unlinkSync(parser_path + temp_fn_y); - fs.unlinkSync(parser_path + temp_fn_js); - fs.unlinkSync(parser_path + temp_fn_out); + fs.unlinkSync(input_path + temp_fn_y); + fs.unlinkSync(input_path + temp_fn_js); + fs.unlinkSync(input_path + temp_fn_out); }); }; diff --git a/output/parser.y b/parser_template/parser.y similarity index 88% rename from output/parser.y rename to parser_template/parser.y index 5d3940a..91de1e8 100644 --- a/output/parser.y +++ b/parser_template/parser.y @@ -27,6 +27,14 @@ parser.parse(parser["TOKEN_" + token.lexeme], token); lexemes.push(token); break; + case -2: + // end of search + // do nothing + break; + case -1: + // empty string or not found any lexemes + // do nothing + break; case 1: return { success: false, @@ -83,4 +91,4 @@ console.log("Syntax error"); } -&&REPLACER{output/rules.y}&& \ No newline at end of file +&&REPLACER{rules.y}&& \ No newline at end of file diff --git a/output/rules.y b/parser_template/rules.y similarity index 100% rename from output/rules.y rename to parser_template/rules.y diff --git a/test.js b/test.js index f034eab..0d2dade 100644 --- a/test.js +++ b/test.js @@ -23,7 +23,7 @@ exec("node main.js -o=" + cfg_path + " -t=node", function(err, stdout, stderr) { }); 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")) { fs.mkdirSync("tests");