Browse Source

HUE-4031 [editor] Create initial structure for the syntax parser response

Johan Ahlen 8 years ago
parent
commit
59277755f0

+ 3 - 1
desktop/core/src/desktop/static/desktop/js/autocomplete/jison/syntax_footer.jison

@@ -14,4 +14,6 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-%%
+%%
+
+SqlParseSupport.initSyntaxParser(parser);

+ 27 - 0
desktop/core/src/desktop/static/desktop/js/autocomplete/spec/sqlSyntaxParserSpec.js

@@ -0,0 +1,27 @@
+// Licensed to Cloudera, Inc. under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  Cloudera, Inc. licenses this file
+// to you under the Apache License, Version 2.0 (the
+// 'License'); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+(function () {
+  describe('sqlSyntaxParser.js', function() {
+
+    it('should find errors for "SLELECT"', function() {
+      var result = sqlSyntaxParser.parseSyntax('SLELECT');
+      expect(result).toBeTruthy();
+      expect(result.text).toEqual('SLELECT');
+      expect(result.expected.length).toBeGreaterThan(0);
+    });
+  });
+})();

+ 46 - 1
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlParseSupport.js

@@ -1483,7 +1483,52 @@ var SqlParseSupport = (function () {
     };
   };
 
+  var initSyntaxParser = function (parser) {
+    parser.prepareNewStatement = function () {
+      // Empty for compatibility with the autocomplete parser
+    };
+
+    var lexerModified = false;
+
+    parser.yy.parseError = function (str, hash) {
+      parser.yy.error = hash;
+    };
+
+    parser.parseSyntax = function (sql, dialect, debug) {
+      parser.yy.error = undefined;
+
+      parser.yy.activeDialect = (dialect !== 'hive' && dialect !== 'impala') ? undefined : dialect;
+
+      // Hack to set the inital state of the lexer without first having to hit a token
+      // has to be done as the first token found can be dependant on dialect
+      if (!lexerModified) {
+        var originalSetInput = parser.lexer.setInput;
+        parser.lexer.setInput = function (input, yy) {
+          var lexer = originalSetInput.bind(parser.lexer)(input, yy);
+          if (typeof parser.yy.activeDialect !== 'undefined') {
+            lexer.begin(parser.yy.activeDialect);
+          }
+          return lexer;
+        };
+        lexerModified = true;
+      }
+
+      // TODO: Find a way around throwing an exception when the parser finds a syntax error
+      try {
+        parser.yy.error = false;
+        parser.parse(sql);
+      } catch (err) {
+        if (debug) {
+          console.log(err);
+          console.error(err.stack);
+        }
+      }
+      return parser.yy.error || false;
+    }
+  };
+
   return {
-    initSqlParser: initSqlParser
+    initSqlParser: initSqlParser,
+    initSyntaxParser: initSyntaxParser
   };
 })();

+ 3 - 1
desktop/core/src/desktop/static/desktop/js/autocomplete/sqlSyntaxParser.js

@@ -4701,7 +4701,9 @@ _handle_error:
 
     return true;
 }};
-/* generated by jison-lex 0.3.4 */
+
+
+SqlParseSupport.initSyntaxParser(parser);/* generated by jison-lex 0.3.4 */
 var lexer = (function(){
 var lexer = ({
 

+ 3 - 0
desktop/core/src/desktop/templates/jasmineRunner.html

@@ -123,6 +123,9 @@
   <script type="text/javascript" src="../static/desktop/js/autocomplete/spec/sqlSpecUpdate.js"></script>
   <script type="text/javascript" src="../static/desktop/js/autocomplete/spec/sqlSpecUse.js"></script>
 
+  <script type="text/javascript" src="../static/desktop/js/autocomplete/sqlSyntaxParser.js"></script>
+  <script type="text/javascript" src="../static/desktop/js/autocomplete/spec/sqlSyntaxParserSpec.js"></script>
+
   <script type="text/javascript" charset="utf-8">
     if (/PhantomJS/.test(window.navigator.userAgent)) {
       console.log("Running Jasmine tests with JUnitXmlReporter and TerminalReporter");