add search string literal lexeme

This commit is contained in:
Aleksey Chichenkov 2019-01-24 18:10:15 +03:00
parent 2203caddc6
commit 76ddc4083a
3 changed files with 63 additions and 27 deletions

78
lexer.l
View File

@ -11,6 +11,41 @@ var lex = [
"INTEGER_LITERAL", "INTEGER_LITERAL",
]; ];
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 }
};
var addLexeme = function(_str, _yylexstart, _yycursor, _lexeme) { var addLexeme = function(_str, _yylexstart, _yycursor, _lexeme) {
if(_lexeme !== "ERR"){ if(_lexeme !== "ERR"){
@ -25,6 +60,10 @@ var unknownSymbol = function(_str, _yylexstart, _yycursor){
throw print_f("Found unknown symbol on position: %s", _yycursor) throw print_f("Found unknown symbol on position: %s", _yycursor)
}; };
var notFoundCloseQuote = function (_str, _yylexstart, _yycursor) {
throw print_f("Not found close quote start: %s", _yycursor)
};
var start_search = function(_str) { var start_search = function(_str) {
console.log("start search", _str); console.log("start search", _str);
var id = 1; var id = 1;
@ -93,7 +132,7 @@ while(true)
ID = L(L|D)*; ID = L(L|D)*;
QU = "\""; QU = "\"";
EQU = "\\\""; SQU = "'";
@ -133,9 +172,11 @@ while(true)
ID { if(addLexeme(str, YYLEXSTART, YYCURSOR, "ID")) return; reset(); break; } ID { if(addLexeme(str, YYLEXSTART, YYCURSOR, "ID")) return; reset(); break; }
DELIM { reset(); break; } DELIM { reset(); break; }
QU|SQU { id = 100000000; break;}
[^] { if(unknownSymbol(str, YYLEXSTART, YYCURSOR)) return; reset(); break; } [^] { if(unknownSymbol(str, YYLEXSTART, YYCURSOR)) return; reset(); break; }
*/} */ENDER}
} };
var print_f = function () { var print_f = function () {
var r_str = ""; var r_str = "";
@ -153,24 +194,9 @@ var print_f = function () {
if(arg !== undefined) { if(arg !== undefined) {
switch (macro) { switch (macro) {
case "s": // v2.string or string case "s":
// var v_1 = arg.to_string && arg.to_string();
// r_str += (arg.to_string && arg.to_string()) || arg.toString();
// var has_to_string = arg.to_string !== undefined;
// var has_toString = arg.toString !== undefined;
if(arg.to_string && !arg.toString) r_str += arg.to_string(); if(arg.to_string && !arg.toString) r_str += arg.to_string();
if(arg.toString && !arg.to_string) r_str += arg.toString(); if(arg.toString && !arg.to_string) r_str += arg.toString();
break;
case "T": // v2.type
case "M": // v2.model_type
case "A": // v2.address
case "P": // all printable
r_str += v2.tools.print(arg);
break; break;
case "i": case "i":
r_str += (arg.to_number && arg.to_number()) || parseInt(arg); r_str += (arg.to_number && arg.to_number()) || parseInt(arg);
@ -178,12 +204,6 @@ var print_f = function () {
case "f": case "f":
r_str += (arg.to_number && arg.to_number()) || parseFloat(arg); r_str += (arg.to_number && arg.to_number()) || parseFloat(arg);
break; break;
case "b":
r_str += arg.toString();
break;
default:
r_str += "%" + macro;
break;
} }
} else { } else {
r_str += "%" + macro; r_str += "%" + macro;
@ -228,5 +248,9 @@ start_search("TimeDiff");
console.log("TEST ALL") console.log("TEST ALL")
start_search("[ ] ( ) : , . % > >= < <= == != AND and OR or NOT not LIKE like NLIKE nlike Address Time TimeDiff "); start_search("[ ] ( ) : , . % > >= < <= == != AND and OR or NOT not LIKE like NLIKE nlike Address Time TimeDiff ");
console.log("TEST FAILS"); console.log("TEST STRING LITERAL");
start_search(' sdfasdfasdfsdf "fasdf'); start_search(' "111\\\"11\\\"1" "222222" ');
start_search(" '111\\\'11\\\'1' '222222' ");
//console.log("TEST FAILS");
//start_search(' sdfasdfasdfsdf "fasdf');

View File

@ -13,6 +13,11 @@ exec("re2c -i lexer.l", function(err, stdout, stderr) {
}); });
var post_process_lexer = function (_string) { var post_process_lexer = function (_string) {
var search_string = fs.readFileSync("search_string.js", "utf8");
// insert last case for string detect
_string = _string.replace(/\}\nENDER}/gm, "yy100000000: { " + search_string + " reset(); break; }}}");
_string = _string.replace(/^.*(_r2c_var_.*;|unsigned int yyaccept = 0;)\n/gm, ""); // replace var yych; _string = _string.replace(/^.*(_r2c_var_.*;|unsigned int yyaccept = 0;)\n/gm, ""); // replace var yych;
_string = _string.replace(/(yych = \*YYCURSOR);\n/gm, "\tcase 1:\n yych = str[YYCURSOR];\n"); // insert "case 1:" before; _string = _string.replace(/(yych = \*YYCURSOR);\n/gm, "\tcase 1:\n yych = str[YYCURSOR];\n"); // insert "case 1:" before;
_string = _string.replace(/\*(.*?);/gm, "str[$1];"); // замена разыменовываний _string = _string.replace(/\*(.*?);/gm, "str[$1];"); // замена разыменовываний

7
search_string.js Normal file
View File

@ -0,0 +1,7 @@
var info = searchString(str, yych, YYLEXSTART, YYCURSOR);
if(info.success) {
YYCURSOR = info.pos;
addLexeme(str, YYLEXSTART, YYCURSOR, "STRING");
} else {
notFoundCloseQuote(str, YYLEXSTART, YYCURSOR);
}