This commit is contained in:
Aleksey Chichenkov 2019-01-30 12:49:21 +03:00
parent bcb12599ac
commit 1bec222dec
3 changed files with 267 additions and 84 deletions

168
lexer.l
View File

@ -1,4 +1,5 @@
var types = [
"DSEQ",
"SLASH",
"LSB",
"RSB",
@ -20,7 +21,7 @@ var types = [
"OR",
"NOT",
"ADDRESS",
"OID",
"OID_LITERAL",
"TIME",
"TIMEDIFF",
"INTEGER_LITERAL",
@ -72,6 +73,16 @@ Lexer.prototype = {
console.log( print_f("Found unknown symbol on position: %s", this._yy_cursor));
},
_oidUnexpectedSymbol: function(){
this._error = true;
this._last_found_lexeme = {
error: 3,
start: this._yy_lex_start,
end: this._yy_cursor
};
console.log( print_f("Found unknown symbol in Oid on position: %s", this._yy_cursor));
},
_foundLexeme: function(_lexeme) {
console.log(print_f("found lex: %s; start: %s; end: %s; result => %s", _lexeme, this._yy_lex_start, this._yy_cursor, this._string.substring(this._yy_lex_start, this._yy_cursor)));
this._last_found_lexeme = {
@ -82,6 +93,18 @@ Lexer.prototype = {
end: this._yy_cursor
};
},
_foundOidLexeme: function(_lexeme, _lsb, _rsb) {
console.log(print_f("found lex: %s; start: %s; end: %s; result => %s", _lexeme, this._yy_lex_start, this._yy_cursor, this._string.substring(this._yy_lex_start, this._yy_cursor)));
this._last_found_lexeme = {
error: 0,
lexeme: _lexeme,
value: this._string.substring(this._yy_lex_start, this._yy_cursor),
start: this._yy_lex_start,
end: this._yy_cursor,
lsb: _lsb,
rsb: _rsb
};
},
_endOfString: function(){
console.log(print_f("search end\n"));
this._end = true;
@ -128,6 +151,116 @@ Lexer.prototype = {
this._notFoundCloseQuote();
},
_searchOid: function (){
var lsb, rsb;
var state = 0;
while(this._yy_cursor < this._string.length){
switch(state){
case 0:
this._yy_char = this._string[this._yy_cursor];
(function(){
switch(this._yy_char){
case " ":
state = 0;
this._yy_cursor++;
break;
case "[":
lsb = {start: this._yy_cursor, end: this._yy_cursor + 1};
state = 1;
break;
default:
state = 5;
}
}.bind(this))();
break;
case 1:
this._yy_char = this._string[++this._yy_cursor];
(function(){
switch(this._yy_char){
case " ":
state = 1;
break;
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
state = 2;
break;
case "]":
state = 4;
break;
default:
state = 5;
}
}.bind(this))();
break;
case 2:
this._yy_char = this._string[++this._yy_cursor];
(function(){
switch(this._yy_char){
case " ":
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
state = 2;
break;
case ".":
state = 3;
break;
case "]":
state = 4;
break;
default:
state = 5;
}
}.bind(this))();
break;
case 3:
this._yy_char = this._string[++this._yy_cursor];
(function(){
switch(this._yy_char){
case " ":
case "0":
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
state = 2;
break;
default:
state = 5;
}
}.bind(this))();
break;
case 4:
rsb = {start: this._yy_cursor, end: this._yy_cursor + 1};
++this._yy_cursor;
this._foundOidLexeme("OID_LITERAL", lsb, rsb);
return;
case 5:
this._oidUnexpectedSymbol();
return;
}
}
},
_set_next: function(){
this._yy_accept = 0;
this._state = 1;
@ -147,13 +280,17 @@ Lexer.prototype = {
if(this._end) return false;
while(true){
switch(id) /*!re2c
switch(id) {
START/*!re2c
re2c:define:YYCTYPE = _r2c_var_;
re2c:define:YYCURSOR = this._yy_cursor;
re2c:define:YYMARKER = this._yy_marker;
re2c:yyfill:enable = 0;
D = [0-9];
DSEQ = D+;
end = "\x00";
L = [A-Za-z_];
RL = [\U00000400-\U00000451];
@ -194,10 +331,10 @@ Lexer.prototype = {
OID = "Oid";
TIMEDIFF = "TimeDiff";
BOOL_LITERAL = 'true'|'false';
FLOAT_LITERAL = "-"? D* "." D+ ("e" "-"? D+)?;
INTEGER_LITERAL = INTEGER;
ID = L(L|D)*;
BOOL_LITERAL = 'true'|'false';
INTEGER_LITERAL = INTEGER;
FLOAT_LITERAL = "-"? D* "." D+ ("e" "-"? D+)?;
QU = "\"";
SQU = "'";
@ -206,6 +343,7 @@ Lexer.prototype = {
end { this._endOfString(); return; }
DSEQ { this._foundLexeme("DSEQ"); this._set_next(); return; }
SLASH { this._foundLexeme("SLASH"); this._set_next(); return; }
LSB { this._foundLexeme("LSB"); this._set_next(); return; }
@ -233,7 +371,7 @@ Lexer.prototype = {
ADDRESS { this._foundLexeme("ADDRESS"); this._set_next(); return; }
TIME { this._foundLexeme("TIME"); this._set_next(); return; }
TIMEDIFF { this._foundLexeme("TIMEDIFF"); this._set_next(); return; }
OID { this._foundLexeme("OID"); this._set_next(); return; }
OID { id = 100000001; break; }
INTEGER_LITERAL { this._foundLexeme("INTEGER_LITERAL"); this._set_next(); return; }
FLOAT_LITERAL { this._foundLexeme("FLOAT_LITERAL"); this._set_next(); return; }
@ -245,10 +383,26 @@ Lexer.prototype = {
QU|SQU { id = 100000000; break;}
[^] { this._unknownSymbol(); this._set_next(); return; }
*/ENDER}
*/END &&<STRING&&
&&<OID&&
}
}
}
};
&&>STRING
case 100000000:
this._searchString();
this._set_next();
return;
&&
&&>OID
case 100000001:
this._searchOid();
this._set_next();
return;
&&
var print_f = function() {
var r_str = "";

32
main.js
View File

@ -16,8 +16,9 @@ exec("re2c -i lexer.l", function(err, stdout, stderr) {
var post_process_lexer = function (_string) {
// insert last case for string detect
_string = _string.replace(/\}\nENDER}/gm, "yy100000000: { this._searchString(); this._set_next(); return; }}}");
// replace start and end fbrackets
_string = _string.replace(/START\n\{/gm, "");
_string = _string.replace(/\}\nEND/gm, "");
_string = _string.replace(/^.*(_r2c_var_.*;|unsigned int yyaccept = 0;)\n/gm, ""); // replace var yych;
_string = _string.replace(/(yych = \*this._yy_cursor);\n/gm, "\tcase 1:\n yych = this._string[this._yy_cursor];\n"); // insert "case 1:" before;
@ -52,9 +53,36 @@ var post_process_lexer = function (_string) {
var output = args["o"] || "lexer.js";
_string = process_metatags(_string);
if( !(args["no-beautify"] || args["nb"]) ) {
_string = js_beautify(_string, {indent_size: 4, space_in_empty_paren: true});
}
fs.writeFileSync(output, _string);
};
var process_metatags = function (_string) {
var metatags = {};
_string = _string.replace(/&&>([A-Z_][A-Z0-9_]+)([\s\S]*?)&&/gm, function (_full, _metatag, _text) {
if(!metatags[_metatag]){
metatags[_metatag] = {
includes: []
};
}
metatags[_metatag].includes.push(_text);
return "";
});
_string = _string.replace(/&&<([A-Z_][A-Z0-9_]+)&&/gm, function (_full, _metatag) {
var metatag = metatags[_metatag];
if(!metatag){
throw "not found metatag for include"
}
return metatag.includes.join("");
});
return _string;
};

View File

@ -42,11 +42,12 @@ var test = function() {
(new Lexer("NLIKE")).search();
(new Lexer("nlike")).search();
(new Lexer("Address")).search();
(new Lexer("Oid")).search();
(new Lexer("Oid[1.2.3]")).search();
(new Lexer("Oid [ 2431.2.3 ]")).search();
(new Lexer("Time")).search();
(new Lexer("TimeDiff")).search();
var lex_test_all = new Lexer("[ ] ( ) : , . % > >= < <= == != AND and OR or NOT not LIKE like NLIKE nlike Address Time TimeDiff 'sdfadfasdf' \"asdfasfd\" ")
var lex_test_all = new Lexer("[ ] ( ) : , . % > >= < <= == != AND and OR or NOT not LIKE like NLIKE nlike Address Oid[1.2.3] Time TimeDiff 'sdfadfasdf' \"asdfasfd\" ")
var _lex;
console.log("start search");