add time and tests

This commit is contained in:
Aleksey Chichenkov 2019-01-29 11:02:47 +03:00
parent ee37f260a3
commit edcbb72742
9 changed files with 958 additions and 3952 deletions

17
config.template Normal file
View File

@ -0,0 +1,17 @@
var config = {
lemon_bin: "./lemon-src/lemon-js",
out_path: "",
/**
* В папке назначения должен лежать файл parser.y;
* Или другой файл парсера с именем <file_name>.y
*/
file_name: "",
/**
* Флаги для лемона, можно посмотреть в README.md
*/
flags: "-l",
};
module.exports = config;

58
main.js
View File

@ -7,14 +7,29 @@ var args = require("args-parser")(process.argv);
var fs = require("fs");
var exec = require('child_process').exec;
var program_path = "./lemon-src/lemon-js";
var parser_path = "parsers/filters/";
var file_name = "parser.y";
var temp_file_name = "temp_parser.y";
var config;
if(args["c"] !== undefined) {
config = require("./" + args["c"]);
} else {
config = require("./config.js");
}
var program_path = config.lemon_bin;
var lemon_flags = config.flags;
var parser_path = config.out_path;
var file_name = config.file_name;
var fn_y = file_name + ".y";
var fn_js = file_name + ".js";
var fn_out = file_name + ".out";
var temp_fn_y = "temp_" + "file_name" + ".y";
var temp_fn_js = "temp_" + "file_name" + ".js";
var temp_fn_out = "temp_" + "file_name" + ".out";
var update_parser_y = function () {
var source_parser_y = fs.readFileSync(parser_path + file_name, "utf8");
var source_parser_y = fs.readFileSync(parser_path + fn_y, "utf8");
var result = /&&.*?REPLACER\{(.*?)\}&&/gm.exec(source_parser_y);
if(result) {
@ -23,31 +38,44 @@ var update_parser_y = function () {
source_parser_y = source_parser_y.replace(/&&.*?REPLACER\{(.*?)\}&&/gm, process_code);
fs.writeFileSync(parser_path + temp_file_name, source_parser_y);
fs.writeFileSync(parser_path + temp_fn_y, source_parser_y);
}
};
var post_process_parser = function () {
var out_js = fs.readFileSync(parser_path + "temp_parser.js", "utf8");
out_js = js_beautify(out_js, {indent_size: 4, space_in_empty_paren: true});
fs.writeFileSync(parser_path + "parser.js", out_js);
var out_js = fs.readFileSync(parser_path + temp_fn_js, "utf8");
var temp_parser_out = fs.readFileSync(parser_path + "temp_parser.out", "utf8");
fs.writeFileSync(parser_path + "parser.out", temp_parser_out);
if(args["t"] !== undefined) {
switch (args["t"]) {
case "web":
out_js = "(function(){\n" + out_js + "return Lexer; \n})()";
break;
case "node":
out_js += "\n\n module.exports = LemonJS;";
break;
}
}
out_js = js_beautify(out_js, {indent_size: 4, space_in_empty_paren: true});
fs.writeFileSync(parser_path + fn_js, out_js);
var temp_parser_out = fs.readFileSync(parser_path + temp_fn_out, "utf8");
fs.writeFileSync(parser_path + fn_out, temp_parser_out);
};
var start = function () {
update_parser_y();
exec(program_path + " " + parser_path + temp_file_name + " -l", function(err, stdout, stderr) {
exec(program_path + " " + parser_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_file_name);
fs.unlinkSync(parser_path + "temp_parser.js");
fs.unlinkSync(parser_path + "temp_parser.out");
fs.unlinkSync(parser_path + temp_fn_y);
fs.unlinkSync(parser_path + temp_fn_js);
fs.unlinkSync(parser_path + temp_fn_out);
});
};

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,784 +0,0 @@
State 0:
main ::= * expr
main ::= * literal
integer_literal ::= * INTEGER_LITERAL
literal ::= * integer_literal
float_literal ::= * FLOAT_LITERAL
literal ::= * float_literal
bool_literal ::= * BOOL_LITERAL
literal ::= * bool_literal
string_literal ::= * STRING_LITERAL
literal ::= * string_literal
id ::= * string_literal
id ::= * ID
and ::= * expr AND expr
or ::= * expr OR expr
not ::= * NOT expr
eq ::= * id EQ literal
neq ::= * id NEQ literal
gt ::= * id GT literal
gte ::= * id GTE literal
lt ::= * id LT literal
lte ::= * id LTE literal
like ::= * id LIKE literal
nlike ::= * id NLIKE literal
expr ::= * and
expr ::= * or
expr ::= * not
expr ::= * eq
expr ::= * neq
expr ::= * gt
expr ::= * gte
expr ::= * lt
expr ::= * lte
expr ::= * like
expr ::= * nlike
expr ::= * LCB expr RCB
address_literal ::= * ADDRESS LSB address_literal_content_or_empty RSB
literal ::= * address_literal
oid_literal ::= * OID LSB oid_literal_content_or_empty RSB
literal ::= * oid_literal
time_diff_literal ::= * TIMEDIFF LSB integer_literal integer_literal COLON integer_literal COLON integer_literal integer_literal RSB
literal ::= * time_diff_literal
NOT shift 2
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
FLOAT_LITERAL shift-reduce 4 float_literal ::= FLOAT_LITERAL
BOOL_LITERAL shift-reduce 6 bool_literal ::= BOOL_LITERAL
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ID shift-reduce 11 id ::= ID
LCB shift 1
ADDRESS shift 36
OID shift 33
TIMEDIFF shift 30
main accept
expr shift 24
literal shift 26
integer_literal shift-reduce 3 literal ::= integer_literal
float_literal shift-reduce 5 literal ::= float_literal
bool_literal shift-reduce 7 literal ::= bool_literal
string_literal shift 25
id shift 22
and shift-reduce 23 expr ::= and
or shift-reduce 24 expr ::= or
not shift-reduce 25 expr ::= not
eq shift-reduce 26 expr ::= eq
neq shift-reduce 27 expr ::= neq
gt shift-reduce 28 expr ::= gt
gte shift-reduce 29 expr ::= gte
lt shift-reduce 30 expr ::= lt
lte shift-reduce 31 expr ::= lte
like shift-reduce 32 expr ::= like
nlike shift-reduce 33 expr ::= nlike
address_literal shift-reduce 40 literal ::= address_literal
oid_literal shift-reduce 46 literal ::= oid_literal
time_diff_literal shift-reduce 48 literal ::= time_diff_literal
State 1:
string_literal ::= * STRING_LITERAL
id ::= * string_literal
id ::= * ID
and ::= * expr AND expr
or ::= * expr OR expr
not ::= * NOT expr
eq ::= * id EQ literal
neq ::= * id NEQ literal
gt ::= * id GT literal
gte ::= * id GTE literal
lt ::= * id LT literal
lte ::= * id LTE literal
like ::= * id LIKE literal
nlike ::= * id NLIKE literal
expr ::= * and
expr ::= * or
expr ::= * not
expr ::= * eq
expr ::= * neq
expr ::= * gt
expr ::= * gte
expr ::= * lt
expr ::= * lte
expr ::= * like
expr ::= * nlike
expr ::= * LCB expr RCB
expr ::= LCB * expr RCB
NOT shift 2
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ID shift-reduce 11 id ::= ID
LCB shift 1
expr shift 23
string_literal shift-reduce 10 id ::= string_literal
id shift 22
and shift-reduce 23 expr ::= and
or shift-reduce 24 expr ::= or
not shift-reduce 25 expr ::= not
eq shift-reduce 26 expr ::= eq
neq shift-reduce 27 expr ::= neq
gt shift-reduce 28 expr ::= gt
gte shift-reduce 29 expr ::= gte
lt shift-reduce 30 expr ::= lt
lte shift-reduce 31 expr ::= lte
like shift-reduce 32 expr ::= like
nlike shift-reduce 33 expr ::= nlike
State 2:
string_literal ::= * STRING_LITERAL
id ::= * string_literal
id ::= * ID
and ::= * expr AND expr
or ::= * expr OR expr
not ::= * NOT expr
not ::= NOT * expr
eq ::= * id EQ literal
neq ::= * id NEQ literal
gt ::= * id GT literal
gte ::= * id GTE literal
lt ::= * id LT literal
lte ::= * id LTE literal
like ::= * id LIKE literal
nlike ::= * id NLIKE literal
expr ::= * and
expr ::= * or
expr ::= * not
expr ::= * eq
expr ::= * neq
expr ::= * gt
expr ::= * gte
expr ::= * lt
expr ::= * lte
expr ::= * like
expr ::= * nlike
expr ::= * LCB expr RCB
NOT shift 2
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ID shift-reduce 11 id ::= ID
LCB shift 1
expr shift-reduce 14 not ::= NOT expr
string_literal shift-reduce 10 id ::= string_literal
id shift 22
and shift-reduce 23 expr ::= and
or shift-reduce 24 expr ::= or
not shift-reduce 25 expr ::= not
eq shift-reduce 26 expr ::= eq
neq shift-reduce 27 expr ::= neq
gt shift-reduce 28 expr ::= gt
gte shift-reduce 29 expr ::= gte
lt shift-reduce 30 expr ::= lt
lte shift-reduce 31 expr ::= lte
like shift-reduce 32 expr ::= like
nlike shift-reduce 33 expr ::= nlike
State 3:
string_literal ::= * STRING_LITERAL
id ::= * string_literal
id ::= * ID
and ::= * expr AND expr
or ::= * expr OR expr
or ::= expr OR * expr
not ::= * NOT expr
eq ::= * id EQ literal
neq ::= * id NEQ literal
gt ::= * id GT literal
gte ::= * id GTE literal
lt ::= * id LT literal
lte ::= * id LTE literal
like ::= * id LIKE literal
nlike ::= * id NLIKE literal
expr ::= * and
expr ::= * or
expr ::= * not
expr ::= * eq
expr ::= * neq
expr ::= * gt
expr ::= * gte
expr ::= * lt
expr ::= * lte
expr ::= * like
expr ::= * nlike
expr ::= * LCB expr RCB
NOT shift 2
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ID shift-reduce 11 id ::= ID
LCB shift 1
expr shift 37
string_literal shift-reduce 10 id ::= string_literal
id shift 22
and shift-reduce 23 expr ::= and
or shift-reduce 24 expr ::= or
not shift-reduce 25 expr ::= not
eq shift-reduce 26 expr ::= eq
neq shift-reduce 27 expr ::= neq
gt shift-reduce 28 expr ::= gt
gte shift-reduce 29 expr ::= gte
lt shift-reduce 30 expr ::= lt
lte shift-reduce 31 expr ::= lte
like shift-reduce 32 expr ::= like
nlike shift-reduce 33 expr ::= nlike
State 4:
string_literal ::= * STRING_LITERAL
id ::= * string_literal
id ::= * ID
and ::= * expr AND expr
and ::= expr AND * expr
or ::= * expr OR expr
not ::= * NOT expr
eq ::= * id EQ literal
neq ::= * id NEQ literal
gt ::= * id GT literal
gte ::= * id GTE literal
lt ::= * id LT literal
lte ::= * id LTE literal
like ::= * id LIKE literal
nlike ::= * id NLIKE literal
expr ::= * and
expr ::= * or
expr ::= * not
expr ::= * eq
expr ::= * neq
expr ::= * gt
expr ::= * gte
expr ::= * lt
expr ::= * lte
expr ::= * like
expr ::= * nlike
expr ::= * LCB expr RCB
NOT shift 2
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ID shift-reduce 11 id ::= ID
LCB shift 1
expr shift-reduce 12 and ::= expr AND expr
string_literal shift-reduce 10 id ::= string_literal
id shift 22
and shift-reduce 23 expr ::= and
or shift-reduce 24 expr ::= or
not shift-reduce 25 expr ::= not
eq shift-reduce 26 expr ::= eq
neq shift-reduce 27 expr ::= neq
gt shift-reduce 28 expr ::= gt
gte shift-reduce 29 expr ::= gte
lt shift-reduce 30 expr ::= lt
lte shift-reduce 31 expr ::= lte
like shift-reduce 32 expr ::= like
nlike shift-reduce 33 expr ::= nlike
State 5:
integer_literal ::= * INTEGER_LITERAL
literal ::= * integer_literal
float_literal ::= * FLOAT_LITERAL
literal ::= * float_literal
bool_literal ::= * BOOL_LITERAL
literal ::= * bool_literal
string_literal ::= * STRING_LITERAL
literal ::= * string_literal
nlike ::= id NLIKE * literal
address_literal ::= * ADDRESS LSB address_literal_content_or_empty RSB
literal ::= * address_literal
oid_literal ::= * OID LSB oid_literal_content_or_empty RSB
literal ::= * oid_literal
time_diff_literal ::= * TIMEDIFF LSB integer_literal integer_literal COLON integer_literal COLON integer_literal integer_literal RSB
literal ::= * time_diff_literal
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
FLOAT_LITERAL shift-reduce 4 float_literal ::= FLOAT_LITERAL
BOOL_LITERAL shift-reduce 6 bool_literal ::= BOOL_LITERAL
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ADDRESS shift 36
OID shift 33
TIMEDIFF shift 30
literal shift-reduce 22 nlike ::= id NLIKE literal
integer_literal shift-reduce 3 literal ::= integer_literal
float_literal shift-reduce 5 literal ::= float_literal
bool_literal shift-reduce 7 literal ::= bool_literal
string_literal shift-reduce 9 literal ::= string_literal
address_literal shift-reduce 40 literal ::= address_literal
oid_literal shift-reduce 46 literal ::= oid_literal
time_diff_literal shift-reduce 48 literal ::= time_diff_literal
State 6:
integer_literal ::= * INTEGER_LITERAL
literal ::= * integer_literal
float_literal ::= * FLOAT_LITERAL
literal ::= * float_literal
bool_literal ::= * BOOL_LITERAL
literal ::= * bool_literal
string_literal ::= * STRING_LITERAL
literal ::= * string_literal
like ::= id LIKE * literal
address_literal ::= * ADDRESS LSB address_literal_content_or_empty RSB
literal ::= * address_literal
oid_literal ::= * OID LSB oid_literal_content_or_empty RSB
literal ::= * oid_literal
time_diff_literal ::= * TIMEDIFF LSB integer_literal integer_literal COLON integer_literal COLON integer_literal integer_literal RSB
literal ::= * time_diff_literal
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
FLOAT_LITERAL shift-reduce 4 float_literal ::= FLOAT_LITERAL
BOOL_LITERAL shift-reduce 6 bool_literal ::= BOOL_LITERAL
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ADDRESS shift 36
OID shift 33
TIMEDIFF shift 30
literal shift-reduce 21 like ::= id LIKE literal
integer_literal shift-reduce 3 literal ::= integer_literal
float_literal shift-reduce 5 literal ::= float_literal
bool_literal shift-reduce 7 literal ::= bool_literal
string_literal shift-reduce 9 literal ::= string_literal
address_literal shift-reduce 40 literal ::= address_literal
oid_literal shift-reduce 46 literal ::= oid_literal
time_diff_literal shift-reduce 48 literal ::= time_diff_literal
State 7:
integer_literal ::= * INTEGER_LITERAL
literal ::= * integer_literal
float_literal ::= * FLOAT_LITERAL
literal ::= * float_literal
bool_literal ::= * BOOL_LITERAL
literal ::= * bool_literal
string_literal ::= * STRING_LITERAL
literal ::= * string_literal
lte ::= id LTE * literal
address_literal ::= * ADDRESS LSB address_literal_content_or_empty RSB
literal ::= * address_literal
oid_literal ::= * OID LSB oid_literal_content_or_empty RSB
literal ::= * oid_literal
time_diff_literal ::= * TIMEDIFF LSB integer_literal integer_literal COLON integer_literal COLON integer_literal integer_literal RSB
literal ::= * time_diff_literal
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
FLOAT_LITERAL shift-reduce 4 float_literal ::= FLOAT_LITERAL
BOOL_LITERAL shift-reduce 6 bool_literal ::= BOOL_LITERAL
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ADDRESS shift 36
OID shift 33
TIMEDIFF shift 30
literal shift-reduce 20 lte ::= id LTE literal
integer_literal shift-reduce 3 literal ::= integer_literal
float_literal shift-reduce 5 literal ::= float_literal
bool_literal shift-reduce 7 literal ::= bool_literal
string_literal shift-reduce 9 literal ::= string_literal
address_literal shift-reduce 40 literal ::= address_literal
oid_literal shift-reduce 46 literal ::= oid_literal
time_diff_literal shift-reduce 48 literal ::= time_diff_literal
State 8:
integer_literal ::= * INTEGER_LITERAL
literal ::= * integer_literal
float_literal ::= * FLOAT_LITERAL
literal ::= * float_literal
bool_literal ::= * BOOL_LITERAL
literal ::= * bool_literal
string_literal ::= * STRING_LITERAL
literal ::= * string_literal
lt ::= id LT * literal
address_literal ::= * ADDRESS LSB address_literal_content_or_empty RSB
literal ::= * address_literal
oid_literal ::= * OID LSB oid_literal_content_or_empty RSB
literal ::= * oid_literal
time_diff_literal ::= * TIMEDIFF LSB integer_literal integer_literal COLON integer_literal COLON integer_literal integer_literal RSB
literal ::= * time_diff_literal
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
FLOAT_LITERAL shift-reduce 4 float_literal ::= FLOAT_LITERAL
BOOL_LITERAL shift-reduce 6 bool_literal ::= BOOL_LITERAL
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ADDRESS shift 36
OID shift 33
TIMEDIFF shift 30
literal shift-reduce 19 lt ::= id LT literal
integer_literal shift-reduce 3 literal ::= integer_literal
float_literal shift-reduce 5 literal ::= float_literal
bool_literal shift-reduce 7 literal ::= bool_literal
string_literal shift-reduce 9 literal ::= string_literal
address_literal shift-reduce 40 literal ::= address_literal
oid_literal shift-reduce 46 literal ::= oid_literal
time_diff_literal shift-reduce 48 literal ::= time_diff_literal
State 9:
integer_literal ::= * INTEGER_LITERAL
literal ::= * integer_literal
float_literal ::= * FLOAT_LITERAL
literal ::= * float_literal
bool_literal ::= * BOOL_LITERAL
literal ::= * bool_literal
string_literal ::= * STRING_LITERAL
literal ::= * string_literal
gte ::= id GTE * literal
address_literal ::= * ADDRESS LSB address_literal_content_or_empty RSB
literal ::= * address_literal
oid_literal ::= * OID LSB oid_literal_content_or_empty RSB
literal ::= * oid_literal
time_diff_literal ::= * TIMEDIFF LSB integer_literal integer_literal COLON integer_literal COLON integer_literal integer_literal RSB
literal ::= * time_diff_literal
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
FLOAT_LITERAL shift-reduce 4 float_literal ::= FLOAT_LITERAL
BOOL_LITERAL shift-reduce 6 bool_literal ::= BOOL_LITERAL
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ADDRESS shift 36
OID shift 33
TIMEDIFF shift 30
literal shift-reduce 18 gte ::= id GTE literal
integer_literal shift-reduce 3 literal ::= integer_literal
float_literal shift-reduce 5 literal ::= float_literal
bool_literal shift-reduce 7 literal ::= bool_literal
string_literal shift-reduce 9 literal ::= string_literal
address_literal shift-reduce 40 literal ::= address_literal
oid_literal shift-reduce 46 literal ::= oid_literal
time_diff_literal shift-reduce 48 literal ::= time_diff_literal
State 10:
integer_literal ::= * INTEGER_LITERAL
literal ::= * integer_literal
float_literal ::= * FLOAT_LITERAL
literal ::= * float_literal
bool_literal ::= * BOOL_LITERAL
literal ::= * bool_literal
string_literal ::= * STRING_LITERAL
literal ::= * string_literal
gt ::= id GT * literal
address_literal ::= * ADDRESS LSB address_literal_content_or_empty RSB
literal ::= * address_literal
oid_literal ::= * OID LSB oid_literal_content_or_empty RSB
literal ::= * oid_literal
time_diff_literal ::= * TIMEDIFF LSB integer_literal integer_literal COLON integer_literal COLON integer_literal integer_literal RSB
literal ::= * time_diff_literal
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
FLOAT_LITERAL shift-reduce 4 float_literal ::= FLOAT_LITERAL
BOOL_LITERAL shift-reduce 6 bool_literal ::= BOOL_LITERAL
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ADDRESS shift 36
OID shift 33
TIMEDIFF shift 30
literal shift-reduce 17 gt ::= id GT literal
integer_literal shift-reduce 3 literal ::= integer_literal
float_literal shift-reduce 5 literal ::= float_literal
bool_literal shift-reduce 7 literal ::= bool_literal
string_literal shift-reduce 9 literal ::= string_literal
address_literal shift-reduce 40 literal ::= address_literal
oid_literal shift-reduce 46 literal ::= oid_literal
time_diff_literal shift-reduce 48 literal ::= time_diff_literal
State 11:
integer_literal ::= * INTEGER_LITERAL
literal ::= * integer_literal
float_literal ::= * FLOAT_LITERAL
literal ::= * float_literal
bool_literal ::= * BOOL_LITERAL
literal ::= * bool_literal
string_literal ::= * STRING_LITERAL
literal ::= * string_literal
neq ::= id NEQ * literal
address_literal ::= * ADDRESS LSB address_literal_content_or_empty RSB
literal ::= * address_literal
oid_literal ::= * OID LSB oid_literal_content_or_empty RSB
literal ::= * oid_literal
time_diff_literal ::= * TIMEDIFF LSB integer_literal integer_literal COLON integer_literal COLON integer_literal integer_literal RSB
literal ::= * time_diff_literal
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
FLOAT_LITERAL shift-reduce 4 float_literal ::= FLOAT_LITERAL
BOOL_LITERAL shift-reduce 6 bool_literal ::= BOOL_LITERAL
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ADDRESS shift 36
OID shift 33
TIMEDIFF shift 30
literal shift-reduce 16 neq ::= id NEQ literal
integer_literal shift-reduce 3 literal ::= integer_literal
float_literal shift-reduce 5 literal ::= float_literal
bool_literal shift-reduce 7 literal ::= bool_literal
string_literal shift-reduce 9 literal ::= string_literal
address_literal shift-reduce 40 literal ::= address_literal
oid_literal shift-reduce 46 literal ::= oid_literal
time_diff_literal shift-reduce 48 literal ::= time_diff_literal
State 12:
integer_literal ::= * INTEGER_LITERAL
literal ::= * integer_literal
float_literal ::= * FLOAT_LITERAL
literal ::= * float_literal
bool_literal ::= * BOOL_LITERAL
literal ::= * bool_literal
string_literal ::= * STRING_LITERAL
literal ::= * string_literal
eq ::= id EQ * literal
address_literal ::= * ADDRESS LSB address_literal_content_or_empty RSB
literal ::= * address_literal
oid_literal ::= * OID LSB oid_literal_content_or_empty RSB
literal ::= * oid_literal
time_diff_literal ::= * TIMEDIFF LSB integer_literal integer_literal COLON integer_literal COLON integer_literal integer_literal RSB
literal ::= * time_diff_literal
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
FLOAT_LITERAL shift-reduce 4 float_literal ::= FLOAT_LITERAL
BOOL_LITERAL shift-reduce 6 bool_literal ::= BOOL_LITERAL
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ADDRESS shift 36
OID shift 33
TIMEDIFF shift 30
literal shift-reduce 15 eq ::= id EQ literal
integer_literal shift-reduce 3 literal ::= integer_literal
float_literal shift-reduce 5 literal ::= float_literal
bool_literal shift-reduce 7 literal ::= bool_literal
string_literal shift-reduce 9 literal ::= string_literal
address_literal shift-reduce 40 literal ::= address_literal
oid_literal shift-reduce 46 literal ::= oid_literal
time_diff_literal shift-reduce 48 literal ::= time_diff_literal
State 13:
string_literal ::= * STRING_LITERAL
id ::= * string_literal
id ::= * ID
oid_literal_content ::= * id
oid_literal_content ::= * oid_literal_content DOT id
oid_literal_content_or_empty ::= * oid_literal_content
(44) oid_literal_content_or_empty ::= *
oid_literal ::= OID LSB * oid_literal_content_or_empty RSB
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ID shift-reduce 11 id ::= ID
string_literal shift-reduce 10 id ::= string_literal
id shift-reduce 41 oid_literal_content ::= id
oid_literal_content shift 32
oid_literal_content_or_empty shift 31
{default} reduce 44 oid_literal_content_or_empty ::=
State 14:
string_literal ::= * STRING_LITERAL
address_literal_content ::= * string_literal
address_literal_content ::= * address_literal_content COMMA string_literal
address_literal_content_or_empty ::= * address_literal_content
(38) address_literal_content_or_empty ::= *
address_literal ::= ADDRESS LSB * address_literal_content_or_empty RSB
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
string_literal shift-reduce 35 address_literal_content ::= string_literal
address_literal_content shift 35
address_literal_content_or_empty shift 34
{default} reduce 38 address_literal_content_or_empty ::=
State 15:
string_literal ::= * STRING_LITERAL
id ::= * string_literal
id ::= * ID
oid_literal_content ::= oid_literal_content DOT * id
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
ID shift-reduce 11 id ::= ID
string_literal shift-reduce 10 id ::= string_literal
id shift-reduce 42 oid_literal_content ::= oid_literal_content DOT id
State 16:
integer_literal ::= * INTEGER_LITERAL
time_diff_literal ::= TIMEDIFF LSB integer_literal integer_literal COLON integer_literal COLON integer_literal * integer_literal RSB
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
integer_literal shift 27
State 17:
integer_literal ::= * INTEGER_LITERAL
time_diff_literal ::= TIMEDIFF LSB integer_literal integer_literal COLON integer_literal COLON * integer_literal integer_literal RSB
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
integer_literal shift 16
State 18:
integer_literal ::= * INTEGER_LITERAL
time_diff_literal ::= TIMEDIFF LSB integer_literal integer_literal COLON * integer_literal COLON integer_literal integer_literal RSB
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
integer_literal shift 28
State 19:
integer_literal ::= * INTEGER_LITERAL
time_diff_literal ::= TIMEDIFF LSB integer_literal * integer_literal COLON integer_literal COLON integer_literal integer_literal RSB
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
integer_literal shift 29
State 20:
integer_literal ::= * INTEGER_LITERAL
time_diff_literal ::= TIMEDIFF LSB * integer_literal integer_literal COLON integer_literal COLON integer_literal integer_literal RSB
INTEGER_LITERAL shift-reduce 2 integer_literal ::= INTEGER_LITERAL
integer_literal shift 19
State 21:
string_literal ::= * STRING_LITERAL
address_literal_content ::= address_literal_content COMMA * string_literal
STRING_LITERAL shift-reduce 8 string_literal ::= STRING_LITERAL
string_literal shift-reduce 36 address_literal_content ::= address_literal_content COMMA string_literal
State 22:
eq ::= id * EQ literal
neq ::= id * NEQ literal
gt ::= id * GT literal
gte ::= id * GTE literal
lt ::= id * LT literal
lte ::= id * LTE literal
like ::= id * LIKE literal
nlike ::= id * NLIKE literal
EQ shift 12
NEQ shift 11
GT shift 10
GTE shift 9
LT shift 8
LTE shift 7
LIKE shift 6
NLIKE shift 5
State 23:
and ::= expr * AND expr
or ::= expr * OR expr
expr ::= LCB expr * RCB
OR shift 3
AND shift 4
RCB shift-reduce 34 expr ::= LCB expr RCB
State 24:
(0) main ::= expr *
and ::= expr * AND expr
or ::= expr * OR expr
$ reduce 0 main ::= expr
OR shift 3
AND shift 4
State 25:
(9) literal ::= string_literal *
(10) id ::= string_literal *
$ reduce 9 literal ::= string_literal
{default} reduce 10 id ::= string_literal
State 26:
(1) main ::= literal *
$ reduce 1 main ::= literal
State 27:
time_diff_literal ::= TIMEDIFF LSB integer_literal integer_literal COLON integer_literal COLON integer_literal integer_literal * RSB
RSB shift-reduce 47 time_diff_literal ::= TIMEDIFF LSB integer_literal integer_literal COLON integer_literal COLON integer_literal integer_literal RSB
State 28:
time_diff_literal ::= TIMEDIFF LSB integer_literal integer_literal COLON integer_literal * COLON integer_literal integer_literal RSB
COLON shift 17
State 29:
time_diff_literal ::= TIMEDIFF LSB integer_literal integer_literal * COLON integer_literal COLON integer_literal integer_literal RSB
COLON shift 18
State 30:
time_diff_literal ::= TIMEDIFF * LSB integer_literal integer_literal COLON integer_literal COLON integer_literal integer_literal RSB
LSB shift 20
State 31:
oid_literal ::= OID LSB oid_literal_content_or_empty * RSB
RSB shift-reduce 45 oid_literal ::= OID LSB oid_literal_content_or_empty RSB
State 32:
oid_literal_content ::= oid_literal_content * DOT id
(43) oid_literal_content_or_empty ::= oid_literal_content *
DOT shift 15
{default} reduce 43 oid_literal_content_or_empty ::= oid_literal_content
State 33:
oid_literal ::= OID * LSB oid_literal_content_or_empty RSB
LSB shift 13
State 34:
address_literal ::= ADDRESS LSB address_literal_content_or_empty * RSB
RSB shift-reduce 39 address_literal ::= ADDRESS LSB address_literal_content_or_empty RSB
State 35:
address_literal_content ::= address_literal_content * COMMA string_literal
(37) address_literal_content_or_empty ::= address_literal_content *
COMMA shift 21
{default} reduce 37 address_literal_content_or_empty ::= address_literal_content
State 36:
address_literal ::= ADDRESS * LSB address_literal_content_or_empty RSB
LSB shift 14
State 37:
and ::= expr * AND expr
or ::= expr * OR expr
(13) or ::= expr OR expr *
AND shift 4
{default} reduce 13 or ::= expr OR expr
----------------------------------------------------
Symbols:
0: $:
1: OR
2: AND
3: NOT
4: INTEGER_LITERAL
5: FLOAT_LITERAL
6: BOOL_LITERAL
7: STRING_LITERAL
8: ID
9: EQ
10: NEQ
11: GT
12: GTE
13: LT
14: LTE
15: LIKE
16: NLIKE
17: LCB
18: RCB
19: COMMA
20: ADDRESS
21: LSB
22: RSB
23: DOT
24: OID
25: TIMEDIFF
26: COLON
27: error:
28: main: NOT INTEGER_LITERAL FLOAT_LITERAL BOOL_LITERAL STRING_LITERAL ID LCB ADDRESS OID TIMEDIFF
29: expr: NOT STRING_LITERAL ID LCB
30: literal: INTEGER_LITERAL FLOAT_LITERAL BOOL_LITERAL STRING_LITERAL ADDRESS OID TIMEDIFF
31: integer_literal: INTEGER_LITERAL
32: float_literal: FLOAT_LITERAL
33: bool_literal: BOOL_LITERAL
34: string_literal: STRING_LITERAL
35: id: STRING_LITERAL ID
36: and: NOT STRING_LITERAL ID LCB
37: or: NOT STRING_LITERAL ID LCB
38: not: NOT
39: eq: STRING_LITERAL ID
40: neq: STRING_LITERAL ID
41: gt: STRING_LITERAL ID
42: gte: STRING_LITERAL ID
43: lt: STRING_LITERAL ID
44: lte: STRING_LITERAL ID
45: like: STRING_LITERAL ID
46: nlike: STRING_LITERAL ID
47: address_literal_content: STRING_LITERAL
48: address_literal_content_or_empty: <lambda> STRING_LITERAL
49: address_literal: ADDRESS
50: oid_literal_content: STRING_LITERAL ID
51: oid_literal_content_or_empty: <lambda> STRING_LITERAL ID
52: oid_literal: OID
53: time_diff_literal: TIMEDIFF

View File

@ -11,7 +11,29 @@
}
%code {
&&REPLACER{process.js}&&
&&REPLACER{parsers/filters/test_code_environment.js}&&
var _result = {};
var LemonJS = function(_input) {
_result = Object.create(null);
var parser = new Parser();
var lexer = new Lexer(_input);
var token;
var lexemes = [];
while (token = lexer.next()) {
if (token.error === 0) {
console.log("PARSE", token.lexeme);
parser.parse(parser["TOKEN_" + token.lexeme], token);
lexemes.push(token);
}
}
parser.parse();
return {
tree: _result.root_node,
lexemes: lexemes
};
};
}
%syntax_error {
@ -447,3 +469,36 @@ literal(A) ::= time_diff_literal(B) . {
A = B;
}
time_literal(A) ::= TIME(KWD) LSB(B) integer_literal(DAY) SLASH integer_literal(MONTH) SLASH integer_literal(YEAR) integer_literal(HH) COLON integer_literal(MM) COLON integer_literal(SS) integer_literal(MS) RSB(C) . {
A = new tokens.time_literal({
keyword: new tokens.LEXEME({
type: KWD.lexeme,
value: KWD.value,
start: KWD.start,
end: KWD.end
}),
LSB: new tokens.LEXEME({
type: B.lexeme,
value: B.value,
start: B.start,
end: B.end
}),
RSB: new tokens.LEXEME({
type: C.lexeme,
value: C.value,
start: C.start,
end: C.end
}),
day: DAY,
month: MONTH,
year: YEAR,
hours: HH,
minutes: MM,
seconds: SS,
microseconds: MS,
});
}
literal(A) ::= time_literal(B) . {
A = B;
}

View File

@ -5,29 +5,38 @@
var fs = require("fs");
var Lexer = require('./lexer.js');
var tokens = (function () {
var std = (function () {
var protos = "__protos__";
var keys = "__keys__";
var std = (function () {
var protos = "__protos__";
var keys = "__keys__";
/**
* Return unique data
*
* @param {Object[]} _arr - prototypes of inheritance classes
* @param {Object} _main - prototype of resulting class
*
* @return {Object}
* */
var unique = function (_arr, _main) {
var result = Object.create(null);
var to_remove = [];
/**
* Return unique data
*
* @param {Object[]} _arr - prototypes of inheritance classes
* @param {Object} _main - prototype of resulting class
*
* @return {Object}
* */
var unique = function (_arr, _main) {
var result = Object.create(null);
var to_remove = [];
for (var i = 0, e = _arr.length; i != e; ++i) {
var item = _arr[i];
for (var i = 0, e = _arr.length; i != e; ++i) {
var item = _arr[i];
for (var key in item) {
for (var key in item) {
if (key in result) {
to_remove.push(key);
continue;
}
result[key] = item[key];
}
if (keys in item) {
for (var ii = 0, ee = item[keys].length; ii != ee; ++ii) {
var key = item[keys][ii];
if (key in result) {
to_remove.push(key);
continue;
@ -35,169 +44,159 @@ var tokens = (function () {
result[key] = item[key];
}
}
}
if (keys in item) {
for (var ii = 0, ee = item[keys].length; ii != ee; ++ii) {
var key = item[keys][ii];
if (key in result) {
to_remove.push(key);
continue;
}
for (var i = 0; i != to_remove.length; ++i) {
delete result[to_remove[i]];
}
result[key] = item[key];
}
for (var key in _main) {
result[key] = _main[key];
}
return result;
};
/**
* Create OOP class
*
* @param {Function[]} _constrs - inheritance classes
* @param {Object} _proto - prototype of resulting class
* @param {Object?} _static - static data
*
* @return {Function}
* */
var class_creator = function (_constrs, _proto, _static) {
_constrs = _constrs || [];
_proto = _proto || [];
_static = _static || [];
var constr;
if (_proto && _proto.hasOwnProperty("constructor")) {
constr = _proto.constructor;
delete _proto.constructor;
} else {
constr = function () {
for (var i = 0; i != _constrs.length; ++i) {
_constrs[i].apply(this, arguments);
}
}
};
}
for (var i = 0; i != to_remove.length; ++i) {
delete result[to_remove[i]];
}
var proto = Object.create(null);
Object.defineProperty(proto, protos, {
"value": []
});
Object.defineProperty(proto, keys, {
"value": []
});
for (var key in _main) {
result[key] = _main[key];
}
/************************FOR MEMBERS*******************************/
for (var i = 0, e = _constrs.length; i != e; ++i) {
proto[protos].push(_constrs[i].prototype);
}
return result;
};
var m_un = unique(proto[protos], _proto);
for (var key in m_un) {
proto[keys].push(key);
/**
* Create OOP class
*
* @param {Function[]} _constrs - inheritance classes
* @param {Object} _proto - prototype of resulting class
* @param {Object?} _static - static data
*
* @return {Function}
* */
var class_creator = function (_constrs, _proto, _static) {
_constrs = _constrs || [];
_proto = _proto || [];
_static = _static || [];
var constr;
if (_proto && _proto.hasOwnProperty("constructor")) {
constr = _proto.constructor;
delete _proto.constructor;
} else {
constr = function () {
for (var i = 0; i != _constrs.length; ++i) {
_constrs[i].apply(this, arguments);
}
};
}
var proto = Object.create(null);
Object.defineProperty(proto, protos, {
"value": []
Object.defineProperty(proto, key, {
"value": m_un[key]
});
Object.defineProperty(proto, keys, {
"value": []
}
/************************FOR MEMBERS END***************************/
/************************FOR STATICS*******************************/
var s_un = unique(_constrs, _static);
for (var key in s_un) {
Object.defineProperty(constr, key, {
"value": s_un[key],
"enumerable": true
});
}
/************************FOR STATICS END***************************/
/************************FOR MEMBERS*******************************/
for (var i = 0, e = _constrs.length; i != e; ++i) {
proto[protos].push(_constrs[i].prototype);
Object.defineProperties(constr, {
"pr": {
"value": proto
},
"prototype": {
"value": proto
}
});
var m_un = unique(proto[protos], _proto);
for (var key in m_un) {
proto[keys].push(key);
Object.freeze(proto);
Object.freeze(constr);
Object.defineProperty(proto, key, {
"value": m_un[key]
});
}
/************************FOR MEMBERS END***************************/
return constr;
};
/************************FOR STATICS*******************************/
var s_un = unique(_constrs, _static);
for (var key in s_un) {
Object.defineProperty(constr, key, {
"value": s_un[key],
"enumerable": true
});
}
/************************FOR STATICS END***************************/
Object.defineProperties(constr, {
"pr": {
"value": proto
},
"prototype": {
"value": proto
}
});
Object.freeze(proto);
Object.freeze(constr);
return constr;
};
/**
* Check if target has prototype
*
* @param {Object} _target - checkable instance
* @param {Object} _proto - posible prototype
*
* */
var check = function (_target, _proto) {
for (var i = 0; i != _target[protos].length; ++i) {
var t_proto = _target[protos][i];
if (t_proto == _proto) {
return true;
}
if (t_proto[protos]) {
if (check(t_proto, _proto))
return true;
}
}
return false;
};
/**
* Check if target is instance of class
*
* @param {Object} _target - checkable instance
* @param {Function} _constr - posible constructor
*
* */
var class_check = function (_target, _constr) {
if (_target instanceof _constr) {
/**
* Check if target has prototype
*
* @param {Object} _target - checkable instance
* @param {Object} _proto - posible prototype
*
* */
var check = function (_target, _proto) {
for (var i = 0; i != _target[protos].length; ++i) {
var t_proto = _target[protos][i];
if (t_proto == _proto) {
return true;
}
return check(_target, _constr.prototype);
};
return {
class: class_creator,
class_check: class_check
};
})();
var tools = {
merge: function (_obj) {
var target = Object.create(null);
var i = 0, e = arguments.length;
for (; i != e; ++i) {
var options = arguments[i];
for (var key in options) {
if (options[key] === undefined && target === options[key])
continue;
target[key] = options[key];
}
if (t_proto[protos]) {
if (check(t_proto, _proto))
return true;
}
return target;
}
return false;
};
/**
* Check if target is instance of class
*
* @param {Object} _target - checkable instance
* @param {Function} _constr - posible constructor
*
* */
var class_check = function (_target, _constr) {
if (_target instanceof _constr) {
return true;
}
return check(_target, _constr.prototype);
};
return {
class: class_creator,
class_check: class_check
};
})();
var tools = {
merge: function (_obj) {
var target = Object.create(null);
var i = 0, e = arguments.length;
for (; i != e; ++i) {
var options = arguments[i];
for (var key in options) {
if (options[key] === undefined && target === options[key])
continue;
target[key] = options[key];
}
}
return target;
}
};
var tokens = (function () {
var Node = std.class([], {
constructor: function Node(_options) {
var base = tools.merge({
@ -496,7 +495,7 @@ var tokens = (function () {
},
position: function () {
return {
start: this.ADDRESS.start,
start: this.keyword.start,
end: this.RSB.end
}
}
@ -564,7 +563,41 @@ var tokens = (function () {
}
});
var time_literal = std.class([Rule], {
constructor: function time_literal(_options) {
var base = tools.merge({
keyword: null,
LSB: null,
RSB: null,
day: -1,
month: -1,
year: -1,
hours: -1,
minutes: -1,
seconds: -1,
microseconds: -1
}, _options);
Rule.call(this, base);
this.keyword = base.keyword;
this.LSB = base.LSB;
this.RSB = base.RSB;
this.day = base.day;
this.month = base.month;
this.year = base.year;
this.hours = base.hours;
this.minutes = base.minutes;
this.seconds = base.seconds;
this.microseconds = base.microseconds;
},
position: function () {
return {
start: this.keyword.start,
end: this.RSB.end
}
}
});
return {
// terminal
@ -579,6 +612,7 @@ var tokens = (function () {
address_literal: address_literal,
oid_literal: oid_literal,
time_diff_literal: time_diff_literal,
time_literal: time_literal,
or: or,
and: and,
@ -599,55 +633,4 @@ var tokens = (function () {
oid_literal_content: oid_literal_content,
}
})();
var _result = {};
var LemonJS = function (_input) {
_result = Object.create(null);
var parser = new Parser();
var lexer = new Lexer(_input);
var token;
while (token = lexer.next()) {
if(token.error === 0) {
console.log("PARSE", token.lexeme);
parser.parse(parser["TOKEN_" + token.lexeme], token);
}
}
parser.parse();
return _result;
};
if(!fs.existsSync("tests")) {
fs.mkdirSync("tests");
}
var test_and = LemonJS("abc == 1 and abc1 == 2 and (bbc == 5)");
fs.writeFileSync("tests/test_and.json", JSON.stringify(test_and, true, 3));
var test_address = LemonJS('abc == Address ["a", "b", "c"]');
fs.writeFileSync("tests/test_address.json", JSON.stringify(test_address, true, 3));
var test_float = LemonJS('abc == 23.2');
fs.writeFileSync("tests/test_float.json", JSON.stringify(test_float, true, 3));
var test_string = LemonJS('abc == "sadfasdf"');
fs.writeFileSync("tests/test_string.json", JSON.stringify(test_string, true, 3));
var test_bool = LemonJS('abc == true or cab == false');
fs.writeFileSync("tests/test_bool.json", JSON.stringify(test_bool, true, 3));
var test_not = LemonJS('not cab == false');
fs.writeFileSync("tests/test_not.json", JSON.stringify(test_not, true, 3));
var test_oid = LemonJS('abc == Oid [a.b.d]');
fs.writeFileSync("tests/test_oid.json", JSON.stringify(test_oid, true, 3));
var test_timediff = LemonJS('add == TimeDiff [17924 15:01:24 441000]');
fs.writeFileSync("tests/test_timediff.json", JSON.stringify(test_timediff, true, 3));
var test_timediff_single = LemonJS('TimeDiff [17924 15:01:24 441000]');
fs.writeFileSync("tests/test_timediff_single.json", JSON.stringify(test_timediff_single, true, 3));
})();

View File

52
test.js Normal file
View File

@ -0,0 +1,52 @@
/**
* Created by Aleksey Chichenkov <a.chichenkov@initi.ru> on 1/29/19.
*/
var fs = require("fs");
var exec = require('child_process').exec;
exec("node main.js -o=config.js -t=node", function(err, stdout, stderr) {
err && console.log("ERROR: ", err);
err && process.exit(1);
test();
});
var test = function() {
var LemonJS = require("./parsers/filters/parser.js");
if(!fs.existsSync("tests")) {
fs.mkdirSync("tests");
}
var test_and = LemonJS("abc == 1 and abc1 == 2 and (bbc == 5)").tree;
fs.writeFileSync("tests/test_and.json", JSON.stringify(test_and, true, 3));
var test_address = LemonJS('abc == Address ["a", "b", "c"]').tree;
fs.writeFileSync("tests/test_address.json", JSON.stringify(test_address, true, 3));
var test_float = LemonJS('abc == 23.2').tree;
fs.writeFileSync("tests/test_float.json", JSON.stringify(test_float, true, 3));
var test_string = LemonJS('abc == "sadfasdf"').tree;
fs.writeFileSync("tests/test_string.json", JSON.stringify(test_string, true, 3));
var test_bool = LemonJS('abc == true or cab == false').tree;
fs.writeFileSync("tests/test_bool.json", JSON.stringify(test_bool, true, 3));
var test_not = LemonJS('not cab == false').tree;
fs.writeFileSync("tests/test_not.json", JSON.stringify(test_not, true, 3));
var test_oid = LemonJS('abc == Oid [a.b.d]').tree;
fs.writeFileSync("tests/test_oid.json", JSON.stringify(test_oid, true, 3));
var test_timediff = LemonJS('add == TimeDiff [17924 15:01:24 441000]').tree;
fs.writeFileSync("tests/test_timediff.json", JSON.stringify(test_timediff, true, 3));
var test_time = LemonJS('add == Time [29/12/2019 15:01:24 441000]').tree;
fs.writeFileSync("tests/test_time.json", JSON.stringify(test_time, true, 3));
};