OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ParseDDL.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <exception>
19 #include <list>
20 #include <memory>
21 #include <string>
22 
23 #include "Parser/ParserNode.h"
24 #include "Parser/parser.h"
25 
26 namespace Parser {
27 
28 template <typename DDLSTMT>
29 auto parseDDL(const std::string& stmt_type, const std::string& stmt_str) {
30  std::list<std::unique_ptr<Parser::Stmt>> parse_trees;
31  std::string last_parsed;
32  SQLParser parser;
33  if (parser.parse(stmt_str, parse_trees, last_parsed) > 0) {
34  throw std::runtime_error("Syntax error in " + stmt_type + " \"" + stmt_str +
35  "\" at " + last_parsed);
36  }
37  if (auto ddl_stmt = dynamic_cast<DDLSTMT*>(parse_trees.front().get())) {
38  parse_trees.front().release();
39  return std::unique_ptr<DDLSTMT>(ddl_stmt);
40  }
41  throw std::runtime_error("Expected " + stmt_type + " is not found in \"" + stmt_str +
42  "\"");
43 }
44 
45 } // namespace Parser
Classes representing a parse tree.
auto parseDDL(const std::string &stmt_type, const std::string &stmt_str)
Definition: ParseDDL.h:29