re2-js-generator/lexer.l

257 lines
9.6 KiB
Plaintext
Raw Normal View History

2019-01-24 13:57:54 +00:00
var lex = [
"ERR",
"DELIM",
"LCB",
"RCB",
"INTEGER",
"FLOAT_LITERAL",
"BOOL_LITERAL",
"INTEGER_LITERAL",
];
2019-01-24 15:10:15 +00:00
var searchString = function (_str, _quote, _yylexstart, _yycursor) {
var found_back_slash = false;
_yycursor++;
while(_yycursor < _str.length){
var char = _str[_yycursor];
if(_quote == '"') {
switch (char) {
case "\\":
found_back_slash = true;
break;
case '"':
if(!found_back_slash) {
return { success: true, pos: _yycursor + 1 }
}
found_back_slash = false;
break;
}
} else if(_quote == "'") {
switch (char) {
case "\\":
found_back_slash = true;
break;
case "'":
if(!found_back_slash) {
return { success: true, pos: _yycursor + 1 }
}
found_back_slash = false;
break;
}
}
_yycursor++;
}
return {success: false, pos: _yycursor + 1 }
};
2019-01-24 13:57:54 +00:00
var addLexeme = function(_str, _yylexstart, _yycursor, _lexeme) {
if(_lexeme !== "ERR"){
console.log(print_f("found lex: %s; start: %s; end: %s; result => %s", _lexeme, _yylexstart, _yycursor, _str.substring(_yylexstart, _yycursor)));
} else {
console.log(print_f("search end\n"));
return true;
}
};
var unknownSymbol = function(_str, _yylexstart, _yycursor){
throw print_f("Found unknown symbol on position: %s", _yycursor)
};
2019-01-24 15:18:27 +00:00
var notFoundCloseQuote = function(_str, _yylexstart, _yycursor) {
console.log( print_f("Not found close quote start: %s", _yycursor));
throw print_f("Not found close quote start: %s", _yycursor);
2019-01-24 15:10:15 +00:00
};
2019-01-24 13:57:54 +00:00
var start_search = function(_str) {
console.log("start search", _str);
var id = 1;
var yych = null;
var YYLEXSTART = 0;
var yyaccept = 0;
var YYCURSOR = 0;
var YYMARKER = 0;
var str = _str;
var reset = function(){
yyaccept = 0;
id = 1;
YYLEXSTART = YYCURSOR;
YYMARKER = YYCURSOR;
};
while(true)
{
switch(id) /*!re2c
re2c:define:YYCTYPE = _r2c_var_;
re2c:yyfill:enable = 0;
D = [0-9];
end = "\x00";
L = [A-Za-z_];
RL = [\U00000400-\U00000451];
CR = "\r";
LF = "\n";
CRLF = CR?LF;
INTEGER = "-"?D+;
SP = " ";
TAB = "\t";
DELIM = SP|TAB|CR|LF;
LSB = "[";
RSB = "]";
LCB = "(";
RCB = ")";
COLON = ":";
COMMA = ",";
DOT = ".";
REM = "%";
GT = ">";
GTE = ">=";
LT = "<";
LTE = "<=";
EQ = "==";
NEQ = "!=";
AND = 'AND';
OR = 'OR';
NOT = 'NOT';
LIKE = 'LIKE';
NLIKE = 'NLIKE';
ADDRESS = "Address";
TIME = "Time";
TIMEDIFF = "TimeDiff";
BOOL_LITERAL = 'true'|'false';
FLOAT_LITERAL = "-"? D* "." D+ ("e" "-"? D+)?;
INTEGER_LITERAL = INTEGER;
ID = L(L|D)*;
QU = "\"";
2019-01-24 15:10:15 +00:00
SQU = "'";
2019-01-24 13:57:54 +00:00
end { if(addLexeme(str, YYLEXSTART, YYCURSOR, "ERR")) return; reset(); break; }
LSB { if(addLexeme(str, YYLEXSTART, YYCURSOR, "LSB")) return; reset(); break; }
RSB { if(addLexeme(str, YYLEXSTART, YYCURSOR, "RSB")) return; reset(); break; }
LCB { if(addLexeme(str, YYLEXSTART, YYCURSOR, "LCB")) return; reset(); break; }
RCB { if(addLexeme(str, YYLEXSTART, YYCURSOR, "RCB")) return; reset(); break; }
COLON { if(addLexeme(str, YYLEXSTART, YYCURSOR, "COLON")) return; reset(); break; }
COMMA { if(addLexeme(str, YYLEXSTART, YYCURSOR, "COMMA")) return; reset(); break; }
DOT { if(addLexeme(str, YYLEXSTART, YYCURSOR, "DOT")) return; reset(); break; }
REM { if(addLexeme(str, YYLEXSTART, YYCURSOR, "REM")) return; reset(); break; }
GT { if(addLexeme(str, YYLEXSTART, YYCURSOR, "GT")) return; reset(); break; }
GTE { if(addLexeme(str, YYLEXSTART, YYCURSOR, "GTE")) return; reset(); break; }
LT { if(addLexeme(str, YYLEXSTART, YYCURSOR, "LT")) return; reset(); break; }
LTE { if(addLexeme(str, YYLEXSTART, YYCURSOR, "LTE")) return; reset(); break; }
EQ { if(addLexeme(str, YYLEXSTART, YYCURSOR, "EQ")) return; reset(); break; }
NEQ { if(addLexeme(str, YYLEXSTART, YYCURSOR, "NEQ")) return; reset(); break; }
LIKE { if(addLexeme(str, YYLEXSTART, YYCURSOR, "LIKE")) return; reset(); break; }
NLIKE { if(addLexeme(str, YYLEXSTART, YYCURSOR, "NLIKE")) return; reset(); break; }
AND { if(addLexeme(str, YYLEXSTART, YYCURSOR, "AND")) return; reset(); break; }
OR { if(addLexeme(str, YYLEXSTART, YYCURSOR, "OR")) return; reset(); break; }
NOT { if(addLexeme(str, YYLEXSTART, YYCURSOR, "NOT")) return; reset(); break; }
ADDRESS { if(addLexeme(str, YYLEXSTART, YYCURSOR, "ADDRESS")) return; reset(); break; }
TIME { if(addLexeme(str, YYLEXSTART, YYCURSOR, "TIME")) return; reset(); break; }
TIMEDIFF { if(addLexeme(str, YYLEXSTART, YYCURSOR, "TIMEDIFF")) return; reset(); break; }
INTEGER_LITERAL { if(addLexeme(str, YYLEXSTART, YYCURSOR, "INTEGER_LITERAL")) return; reset(); break; }
FLOAT_LITERAL { if(addLexeme(str, YYLEXSTART, YYCURSOR, "FLOAT_LITERAL")) return; reset(); break; }
BOOL_LITERAL { if(addLexeme(str, YYLEXSTART, YYCURSOR, "BOOL_LITERAL")) return; reset(); break; }
ID { if(addLexeme(str, YYLEXSTART, YYCURSOR, "ID")) return; reset(); break; }
DELIM { reset(); break; }
2019-01-24 15:10:15 +00:00
QU|SQU { id = 100000000; break;}
2019-01-24 13:57:54 +00:00
[^] { if(unknownSymbol(str, YYLEXSTART, YYCURSOR)) return; reset(); break; }
2019-01-24 15:10:15 +00:00
*/ENDER}
};
2019-01-24 13:57:54 +00:00
var print_f = function () {
var r_str = "";
var next = arguments[0];
var rx = /(%[a-zA-Z]{1})/;
var a = 1, match;
while (match = rx.exec(next)) {
var prev = next.substring(0, match.index);
var macro = next.substring(match.index + 1, match.index + 2);
next = next.substring(match.index + 2, next.length);
r_str += prev;
var arg = arguments[a];
if(arg !== undefined) {
switch (macro) {
2019-01-24 15:10:15 +00:00
case "s":
2019-01-24 13:57:54 +00:00
if(arg.to_string && !arg.toString) r_str += arg.to_string();
if(arg.toString && !arg.to_string) r_str += arg.toString();
break;
case "i":
r_str += (arg.to_number && arg.to_number()) || parseInt(arg);
break;
case "f":
r_str += (arg.to_number && arg.to_number()) || parseFloat(arg);
break;
}
} else {
r_str += "%" + macro;
}
a++;
}
r_str += next;
return r_str;
};
console.log("TEST SINGLE")
start_search("[");
start_search("]");
start_search("(");
start_search(")");
start_search(":");
start_search(",");
start_search(".");
start_search("%");
start_search(">");
start_search(">=");
start_search("<");
start_search("<=");
start_search("==");
start_search("!=");
start_search("AND");
start_search("and");
start_search("OR");
start_search("or");
start_search("NOT");
start_search("not");
start_search("LIKE");
start_search("like");
start_search("NLIKE");
start_search("nlike");
start_search("Address");
start_search("Time");
start_search("TimeDiff");
console.log("TEST ALL")
start_search("[ ] ( ) : , . % > >= < <= == != AND and OR or NOT not LIKE like NLIKE nlike Address Time TimeDiff ");
2019-01-24 15:10:15 +00:00
console.log("TEST STRING LITERAL");
start_search(' "111\\\"11\\\"1" "222222" ');
start_search(" '111\\\'11\\\'1' '222222' ");
2019-01-24 15:18:27 +00:00
console.log("TEST FAILS");
start_search(' sdfasdfasdfsdf "fasdf');