From 8fb254b925d4f7eb20ffea92c83ef390d581665d Mon Sep 17 00:00:00 2001 From: Aleksey Chichenkov Date: Thu, 31 Jan 2019 13:11:50 +0300 Subject: [PATCH] fix --- README.md | 8 ++++++-- build.sh | 2 ++ lexer.l => example/lexer.l | 0 main.js | 30 ++++++++++++++++++++---------- 4 files changed, 28 insertions(+), 12 deletions(-) create mode 100755 build.sh rename lexer.l => example/lexer.l (100%) diff --git a/README.md b/README.md index 2b93388..f001532 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ ```bash git clone -- http://git.macaw.me:3000/chichenkov/re2-js-generator.git cd re2-js-generator +./build.sh ``` ## One Minute Guide You can fast use example: @@ -22,14 +23,17 @@ node lexme.js ## Start ```bash -node main.js -inp= -o= -t= <-nb|--no-beautify> -logs -web-template= -# or ./run.sh +### or +node main -c= +### or +node main.js -inp= -o= -t= <-nb|--no-beautify> -logs -web-template= ``` Result fill write in file. ## Flags ```bash +-c= -inp= -o= -t= diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..d4cc715 --- /dev/null +++ b/build.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +cat config.template > config.js diff --git a/lexer.l b/example/lexer.l similarity index 100% rename from lexer.l rename to example/lexer.l diff --git a/main.js b/main.js index b90438b..bc25fe7 100644 --- a/main.js +++ b/main.js @@ -7,8 +7,15 @@ var args = require("./libs/args-parser")(process.argv); var fs = require("fs"); var exec = require('child_process').exec; -var inp = args["in"] ? args["in"] : "lexer.l"; -var enable_logs = args["logs"] ? args["logs"] : false; +var config; +if(args["c"] !== undefined) { + config = require("./" + args["c"]); +} else { + config = require("./config.js"); +} + +var inp = args["in"] ? args["in"] : config.input; +var enable_logs = args["logs"] ? args["logs"] : config.logs; exec("re2c -i " + inp, function(err, stdout, stderr) { err && console.log("ERROR: ", err); @@ -18,7 +25,7 @@ exec("re2c -i " + inp, function(err, stdout, stderr) { }); var post_process_lexer = function (_string) { - var output = args["o"] || "lexer.js"; + var output = args["o"] || config.output; // replace start and end fbrackets _string = _string.replace(/START\n\{/gm, ""); @@ -47,15 +54,18 @@ var post_process_lexer = function (_string) { _string = _string.replace(/yyaccept/gm, "this._yy_accept"); // replace yyaccept to this._yy_accept _string = _string.replace(/yych/gm, "this._yy_char"); // replace yych to this._yy_char - if(args["t"] !== undefined) { - switch (args["t"]) { + var type = args["t"] || config.type; + + if(type !== undefined) { + switch (type) { case "web": - if(args["web-template"]){ - if(!fs.existsSync(args["web-template"])){ - throw "Not exist file: " + args["web-template"]; + var wt = args["web-template"] || config["web-template"]; + if(wt){ + if(!fs.existsSync(wt)){ + throw "Not exist file: " + wt; } - var template = fs.readFileSync(args["web-template"], "utf8"); + var template = fs.readFileSync(wt, "utf8"); _string = template.replace(/<%%LEXER%%>/gm, _string); } else { _string = "(function () {var deps = [];define(deps, function(){\n" + _string + "return Lexer; \n});})();"; @@ -69,7 +79,7 @@ var post_process_lexer = function (_string) { _string = process_metatags(_string); - if( !(args["no-beautify"] || args["nb"]) ) { + if( !(args["no-beautify"] || args["nb"] || config["no-beautify"]) ) { _string = js_beautify(_string, { indent_size: 4, indent_char: ' ',