Aleksey Chichenkov 3 years ago
parent
commit
8fb254b925
  1. 8
      README.md
  2. 2
      build.sh
  3. 0
      example/lexer.l
  4. 30
      main.js

8
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=<input_file> -o=<output_path> -t=<web|node> <-nb|--no-beautify> -logs -web-template=<web_template_file>
# or
./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.
## Flags
```bash
-c=<config_path>
-inp=<input_file.l>
-o=<output_path>
-t=<web|node>

2
build.sh

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

0
lexer.l → example/lexer.l

30
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: ' ',

Loading…
Cancel
Save