Преглед изворни кода

HUE-9139 [editor] Add statement parser support for escaped \ in quoted values

Johan Ahlen пре 5 година
родитељ
комит
2e7dc9c3f0

+ 2 - 2
desktop/core/src/desktop/js/parse/jison/sqlStatementsParser.jison

@@ -31,12 +31,12 @@
 <inLineComment>[\n]                                                   { this.popState(); return 'PART_OF_STATEMENT'; }
 
 '"'                                                                   { this.begin("doubleQuote"); return 'PART_OF_STATEMENT'; }
-<doubleQuote>(?:\\["]|[^"])+                                          { return 'PART_OF_STATEMENT'; }
+<doubleQuote>(?:\\\\|\\["]|[^"])+                                     { return 'PART_OF_STATEMENT'; }
 <doubleQuote><<EOF>>                                                  { this.popState(); return 'EOF'; }
 <doubleQuote>'"'                                                      { this.popState(); return 'PART_OF_STATEMENT'; }
 
 '\''                                                                  { this.begin("singleQuote"); return 'PART_OF_STATEMENT'; }
-<singleQuote>(?:\\[']|[^'])+                                          { return 'PART_OF_STATEMENT'; }
+<singleQuote>(?:\\\\|\\[']|[^'])+                                     { return 'PART_OF_STATEMENT'; }
 <singleQuote><<EOF>>                                                  { this.popState(); return 'EOF'; }
 <singleQuote>'\''                                                     { this.popState(); return 'PART_OF_STATEMENT'; }
 

+ 1 - 1
desktop/core/src/desktop/js/parse/sqlStatementsParser.js

@@ -775,7 +775,7 @@ case 27:console.log(yy_.yytext);
 break;
 }
 },
-rules: [/^(?:\/\*)/,/^(?:[^*]+)/,/^(?:[*][^\/])/,/^(?:$)/,/^(?:\*\/)/,/^(?:--)/,/^(?:[^\n]+)/,/^(?:$)/,/^(?:[\n])/,/^(?:")/,/^(?:(?:\\["]|[^"])+)/,/^(?:$)/,/^(?:")/,/^(?:')/,/^(?:(?:\\[']|[^'])+)/,/^(?:$)/,/^(?:')/,/^(?:`)/,/^(?:[^`]+)/,/^(?:$)/,/^(?:`)/,/^(?:[^"\/;'`-]+)/,/^(?:[-][^;-]?)/,/^(?:[\/][^;*]?)/,/^(?:;)/,/^(?:$)/,/^(?:.)/,/^(?:.)/],
+rules: [/^(?:\/\*)/,/^(?:[^*]+)/,/^(?:[*][^\/])/,/^(?:$)/,/^(?:\*\/)/,/^(?:--)/,/^(?:[^\n]+)/,/^(?:$)/,/^(?:[\n])/,/^(?:")/,/^(?:(?:\\\\|\\["]|[^"])+)/,/^(?:$)/,/^(?:")/,/^(?:')/,/^(?:(?:\\\\|\\[']|[^'])+)/,/^(?:$)/,/^(?:')/,/^(?:`)/,/^(?:[^`]+)/,/^(?:$)/,/^(?:`)/,/^(?:[^"\/;'`-]+)/,/^(?:[-][^;-]?)/,/^(?:[\/][^;*]?)/,/^(?:;)/,/^(?:$)/,/^(?:.)/,/^(?:.)/],
 conditions: {"multiLineComment":{"rules":[1,2,3,4],"inclusive":false},"inLineComment":{"rules":[6,7,8],"inclusive":false},"singleQuote":{"rules":[14,15,16],"inclusive":false},"doubleQuote":{"rules":[10,11,12],"inclusive":false},"backTick":{"rules":[18,19,20],"inclusive":false},"INITIAL":{"rules":[0,5,9,13,17,21,22,23,24,25,26,27],"inclusive":true}}
 });
 return lexer;

+ 34 - 0
desktop/core/src/desktop/js/parse/sqlStatementsParser.test.js

@@ -103,6 +103,40 @@ describe('sqlStatementsParser.js', () => {
     ]);
   });
 
+  it('should split escaped \\ correctly in single quotes', () => {
+    testParser("SELECT '\\\\';\n SELECT 1;", [
+      {
+        type: 'statement',
+        statement: "SELECT '\\\\';",
+        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 12 },
+        firstToken: 'SELECT'
+      },
+      {
+        type: 'statement',
+        statement: '\n SELECT 1;',
+        location: { first_line: 1, first_column: 12, last_line: 2, last_column: 10 },
+        firstToken: 'SELECT'
+      }
+    ]);
+  });
+
+  it('should split escaped \\ correctly in double quotes', () => {
+    testParser('SELECT "\\\\";\n SELECT 1;', [
+      {
+        type: 'statement',
+        statement: 'SELECT "\\\\";',
+        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 12 },
+        firstToken: 'SELECT'
+      },
+      {
+        type: 'statement',
+        statement: '\n SELECT 1;',
+        location: { first_line: 1, first_column: 12, last_line: 2, last_column: 10 },
+        firstToken: 'SELECT'
+      }
+    ]);
+  });
+
   it('should split ";   \\n;   \\r\\n;" correctly', () => {
     testParser(';   \n;   \r\n;', [
       {