fix readme and add input config
This commit is contained in:
parent
11948ecb73
commit
a7873cc203
35
README.md
35
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=<your_config>.js --t=<web|node>
|
||||
```
|
||||
|
||||
default config -> config.js
|
||||
|
||||
## Test
|
||||
Also you can test your parser
|
||||
```bash
|
||||
|
@ -5,3 +5,5 @@ gcc -o ./lemon-src/lemon-js -O2 ./lemon-src/lemon-js.c
|
||||
npm install
|
||||
|
||||
cat config.example > config.js
|
||||
|
||||
node main.js
|
@ -1,6 +1,7 @@
|
||||
var config = {
|
||||
lemon_bin: "./lemon-src/lemon-js",
|
||||
out_path: "example/",
|
||||
input_path: "example/",
|
||||
output_path: "example/",
|
||||
|
||||
/**
|
||||
* В папке назначения должен лежать файл parser.y;
|
||||
|
@ -1,6 +1,7 @@
|
||||
var config = {
|
||||
lemon_bin: "./lemon-src/lemon-js",
|
||||
out_path: "output/",
|
||||
input_path: "parser_template/",
|
||||
output_path: "out/",
|
||||
|
||||
/**
|
||||
* В папке назначения должен лежать файл 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}&&
|
||||
&&REPLACER{rules.y}&&
|
29
main.js
29
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);
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -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}&&
|
||||
&&REPLACER{rules.y}&&
|
2
test.js
2
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");
|
||||
|
Loading…
Reference in New Issue
Block a user