This commit is contained in:
Aleksey Chichenkov 2019-01-31 13:11:50 +03:00
parent d32375c1f1
commit 8fb254b925
4 changed files with 28 additions and 12 deletions

View File

@ -10,6 +10,7 @@
```bash ```bash
git clone -- http://git.macaw.me:3000/chichenkov/re2-js-generator.git git clone -- http://git.macaw.me:3000/chichenkov/re2-js-generator.git
cd re2-js-generator cd re2-js-generator
./build.sh
``` ```
## One Minute Guide ## One Minute Guide
You can fast use example: You can fast use example:
@ -22,14 +23,17 @@ node lexme.js
## Start ## Start
```bash ```bash
node main.js -inp=<input_file> -o=<output_path> -t=<web|node> <-nb|--no-beautify> -logs -web-template=<web_template_file>
# or
./run.sh ./run.sh
### or
node main -c=<config_path>
### or
node main.js -inp=<input_file> -o=<output_path> -t=<web|node> <-nb|--no-beautify> -logs -web-template=<web_template_file>
``` ```
Result fill write in <lexer.js> file. Result fill write in <lexer.js> file.
## Flags ## Flags
```bash ```bash
-c=<config_path>
-inp=<input_file.l> -inp=<input_file.l>
-o=<output_path> -o=<output_path>
-t=<web|node> -t=<web|node>

2
build.sh Executable file
View File

@ -0,0 +1,2 @@
#!/usr/bin/env bash
cat config.template > config.js

30
main.js
View File

@ -7,8 +7,15 @@ var args = require("./libs/args-parser")(process.argv);
var fs = require("fs"); var fs = require("fs");
var exec = require('child_process').exec; var exec = require('child_process').exec;
var inp = args["in"] ? args["in"] : "lexer.l"; var config;
var enable_logs = args["logs"] ? args["logs"] : false; 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) { exec("re2c -i " + inp, function(err, stdout, stderr) {
err && console.log("ERROR: ", err); err && console.log("ERROR: ", err);
@ -18,7 +25,7 @@ exec("re2c -i " + inp, function(err, stdout, stderr) {
}); });
var post_process_lexer = function (_string) { var post_process_lexer = function (_string) {
var output = args["o"] || "lexer.js"; var output = args["o"] || config.output;
// replace start and end fbrackets // replace start and end fbrackets
_string = _string.replace(/START\n\{/gm, ""); _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(/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 _string = _string.replace(/yych/gm, "this._yy_char"); // replace yych to this._yy_char
if(args["t"] !== undefined) { var type = args["t"] || config.type;
switch (args["t"]) {
if(type !== undefined) {
switch (type) {
case "web": case "web":
if(args["web-template"]){ var wt = args["web-template"] || config["web-template"];
if(!fs.existsSync(args["web-template"])){ if(wt){
throw "Not exist file: " + args["web-template"]; 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); _string = template.replace(/<%%LEXER%%>/gm, _string);
} else { } else {
_string = "(function () {var deps = [];define(deps, function(){\n" + _string + "return Lexer; \n});})();"; _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); _string = process_metatags(_string);
if( !(args["no-beautify"] || args["nb"]) ) { if( !(args["no-beautify"] || args["nb"] || config["no-beautify"]) ) {
_string = js_beautify(_string, { _string = js_beautify(_string, {
indent_size: 4, indent_size: 4,
indent_char: ' ', indent_char: ' ',