| 35 | namespace zvec::sqlengine { |
| 36 | |
| 37 | SQLInfo::Ptr ZVecSQLParser::parse(const std::string &query, |
| 38 | bool need_formatted_tree) { |
| 39 | try { |
| 40 | ANTLRInputStream input(query); |
| 41 | CaseChangingCharStream in(&input, true); |
| 42 | |
| 43 | SQLLexer lexer(&in); |
| 44 | |
| 45 | CommonTokenStream tokens(&lexer); |
| 46 | |
| 47 | SQLParser parser(&tokens); |
| 48 | |
| 49 | // remove and add new error listeners |
| 50 | ErrorVerboseListener lexer_error_listener; |
| 51 | lexer.removeErrorListeners(); // remove all error listeners |
| 52 | lexer.addErrorListener((ANTLRErrorListener *)&lexer_error_listener); // add |
| 53 | ErrorVerboseListener parser_error_listener; |
| 54 | parser.removeErrorListeners(); // remove all error listeners |
| 55 | parser.addErrorListener( |
| 56 | (ANTLRErrorListener *)&parser_error_listener); // add |
| 57 | |
| 58 | // int64_t curtime = Util::cur_micro_second_time(); |
| 59 | ParseTree *tree = parser.compilation_unit(); |
| 60 | |
| 61 | if (lexer.getNumberOfSyntaxErrors() > 0 || |
| 62 | parser.getNumberOfSyntaxErrors() > 0) { |
| 63 | LOG_INFO("SLL failed. using LL"); |
| 64 | tokens.reset(); |
| 65 | parser.reset(); |
| 66 | parser.getInterpreter<ParserATNSimulator>()->setPredictionMode( |
| 67 | PredictionMode::LL); |
| 68 | tree = parser.compilation_unit(); |
| 69 | } |
| 70 | |
| 71 | // int64_t duration = Util::cur_micro_second_time() - curtime; |
| 72 | // printf("parsing time %ld\n", duration); |
| 73 | // LOG_DEBUG("antlr parsing time: [%ld]", duration); |
| 74 | |
| 75 | if (lexer.getNumberOfSyntaxErrors() > 0) { |
| 76 | err_msg_ = "lexer error [" + lexer_error_listener.err_msg() + "]"; |
| 77 | return nullptr; |
| 78 | } |
| 79 | if (parser.getNumberOfSyntaxErrors() > 0) { |
| 80 | err_msg_ = "syntax error [" + parser_error_listener.err_msg() + "]"; |
| 81 | return nullptr; |
| 82 | } |
| 83 | |
| 84 | if (need_formatted_tree) { |
| 85 | formatted_tree_ = to_formatted_string_tree(tree, &parser); |
| 86 | } |
| 87 | |
| 88 | SQLInfo::Ptr info = sql_info(tree); |
| 89 | return info; |
| 90 | } catch (std::exception &e) { |
| 91 | err_msg_ = "parse error [" + std::string(e.what()) + "]"; |
| 92 | return nullptr; |
| 93 | } |
| 94 | } |
nothing calls this directly
no test coverage detected