From 76ddc4083aa71a5a303e127a327770db2ce46611 Mon Sep 17 00:00:00 2001 From: Aleksey Chichenkov Date: Thu, 24 Jan 2019 18:10:15 +0300 Subject: [PATCH] add search string literal lexeme --- lexer.l | 78 +++++++++++++++++++++++++++--------------- parse_source_lexeme.js | 5 +++ search_string.js | 7 ++++ 3 files changed, 63 insertions(+), 27 deletions(-) create mode 100644 search_string.js diff --git a/lexer.l b/lexer.l index b1d146f..86f157a 100644 --- a/lexer.l +++ b/lexer.l @@ -11,6 +11,41 @@ var lex = [ "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) { if(_lexeme !== "ERR"){ @@ -25,6 +60,10 @@ var unknownSymbol = function(_str, _yylexstart, _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) { console.log("start search", _str); var id = 1; @@ -93,7 +132,7 @@ while(true) ID = L(L|D)*; QU = "\""; - EQU = "\\\""; + SQU = "'"; @@ -133,9 +172,11 @@ while(true) ID { if(addLexeme(str, YYLEXSTART, YYCURSOR, "ID")) return; reset(); break; } DELIM { reset(); break; } + QU|SQU { id = 100000000; break;} + [^] { if(unknownSymbol(str, YYLEXSTART, YYCURSOR)) return; reset(); break; } - */} -} + */ENDER} +}; var print_f = function () { var r_str = ""; @@ -153,24 +194,9 @@ var print_f = function () { if(arg !== undefined) { switch (macro) { - case "s": // v2.string or string - // 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; - + case "s": if(arg.to_string && !arg.toString) r_str += arg.to_string(); 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; case "i": r_str += (arg.to_number && arg.to_number()) || parseInt(arg); @@ -178,12 +204,6 @@ var print_f = function () { case "f": r_str += (arg.to_number && arg.to_number()) || parseFloat(arg); break; - case "b": - r_str += arg.toString(); - break; - default: - r_str += "%" + macro; - break; } } else { r_str += "%" + macro; @@ -228,5 +248,9 @@ start_search("TimeDiff"); console.log("TEST ALL") start_search("[ ] ( ) : , . % > >= < <= == != AND and OR or NOT not LIKE like NLIKE nlike Address Time TimeDiff "); -console.log("TEST FAILS"); -start_search(' sdfasdfasdfsdf "fasdf'); \ No newline at end of file +console.log("TEST STRING LITERAL"); +start_search(' "111\\\"11\\\"1" "222222" '); +start_search(" '111\\\'11\\\'1' '222222' "); + +//console.log("TEST FAILS"); +//start_search(' sdfasdfasdfsdf "fasdf'); \ No newline at end of file diff --git a/parse_source_lexeme.js b/parse_source_lexeme.js index 8e48bc7..55e3183 100644 --- a/parse_source_lexeme.js +++ b/parse_source_lexeme.js @@ -13,6 +13,11 @@ exec("re2c -i lexer.l", function(err, stdout, stderr) { }); 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(/(yych = \*YYCURSOR);\n/gm, "\tcase 1:\n yych = str[YYCURSOR];\n"); // insert "case 1:" before; _string = _string.replace(/\*(.*?);/gm, "str[$1];"); // замена разыменовываний diff --git a/search_string.js b/search_string.js new file mode 100644 index 0000000..1f8545f --- /dev/null +++ b/search_string.js @@ -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); +} \ No newline at end of file