浏览代码

HUE-6615 [editor] Fix multiline comment related issues with the statement parser

Johan Ahlen 8 年之前
父节点
当前提交
9de07a4

+ 4 - 2
desktop/core/src/desktop/static/desktop/js/autocomplete/jison/sqlStatementsParser.jison

@@ -20,7 +20,8 @@
 %%
 
 '/*'                                                                  { this.begin("multiLineComment"); return 'PART_OF_STATEMENT'; }
-<multiLineComment>^(?![*][/])*                                        { return 'PART_OF_STATEMENT'; }
+<multiLineComment>[^*]+                                               { return 'PART_OF_STATEMENT'; }
+<multiLineComment>[*][^/]                                             { return 'PART_OF_STATEMENT'; }
 <multiLineComment><<EOF>>                                             { this.popState(); return 'PART_OF_STATEMENT'; }
 <multiLineComment>'*/'                                                { this.popState(); return 'PART_OF_STATEMENT'; }
 
@@ -44,9 +45,10 @@
 <backTick><<EOF>>                                                     { this.popState(); return 'PART_OF_STATEMENT'; }
 <backTick>'`'                                                         { this.popState(); return 'PART_OF_STATEMENT'; }
 
-[^"\\;'`-]+                                                           { return 'PART_OF_STATEMENT'; }
+[^"\/;'`-]+                                                           { return 'PART_OF_STATEMENT'; }
 [-][^;-]                                                              { return 'PART_OF_STATEMENT'; }
 [/][^;*]                                                              { return 'PART_OF_STATEMENT'; }
+
 ';'                                                                   { return ';'; }
 
 <<EOF>>                                                               { return 'EOF'; }

+ 24 - 22
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlStatementsParser.js

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

+ 20 - 0
desktop/core/src/desktop/static/desktop/spec/autocomplete/sqlStatementsParserSpec.js

@@ -201,5 +201,25 @@
         location: { first_line: 1, first_column: 0, last_line: 3, last_column: 16 }
       }]);
     });
+
+    it('should split "SELECT id\\n\\n /* from customers; */ FROM other;" correctly', function () {
+      testParser('SELECT id\n\n /* from customers; */ FROM other;', [{
+        statement: 'SELECT id\n\n /* from customers; */ FROM other;',
+        location: { first_line: 1, first_column: 0, last_line: 3, last_column: 34 }
+      }]);
+    });
+
+    it('should split "SELECT " \\" ;; ", \'"\', \' ;\' from bla; /* \\n\\n"" ; \\n; */ FROM other;" correctly', function () {
+      testParser('USE `db;`;\r\nSELECT " \\" ;; ", \'"\', \' ;\' from bla; /* \n\n"" ; \n;  FROM other;*/', [{
+        statement: 'USE `db;`;',
+        location: { first_line: 1, first_column: 0, last_line: 1, last_column: 10 }
+      }, {
+        statement: '\r\nSELECT " \\" ;; ", \'"\', \' ;\' from bla;',
+        location: { first_line: 1, first_column: 10, last_line: 2, last_column: 37 }
+      }, {
+        statement: ' /* \n\n"" ; \n;  FROM other;*/',
+        location: { first_line: 2, first_column: 37, last_line: 5, last_column: 16 }
+      }]);
+    });
   });
 })();