/Users/deen/code/yugabyte-db/src/yb/yql/cql/ql/parser/parser.h
Line | Count | Source (jump to first uncovered line) |
1 | | //-------------------------------------------------------------------------------------------------- |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | // |
15 | | // Entry point for the parsing process. Conducting the whole scanning and parsing of SQL statement. |
16 | | //-------------------------------------------------------------------------------------------------- |
17 | | |
18 | | #ifndef YB_YQL_CQL_QL_PARSER_PARSER_H_ |
19 | | #define YB_YQL_CQL_QL_PARSER_PARSER_H_ |
20 | | |
21 | | #include <cstddef> |
22 | | |
23 | | #include "yb/yql/cql/ql/parser/parse_context.h" |
24 | | #include "yb/yql/cql/ql/parser/scanner.h" |
25 | | #include "yb/yql/cql/ql/util/errcodes.h" |
26 | | #include "yb/util/memory/arena.h" |
27 | | |
28 | | namespace yb { |
29 | | namespace ql { |
30 | | |
31 | | class Parser { |
32 | | public: |
33 | | //------------------------------------------------------------------------------------------------ |
34 | | // Public types. |
35 | | typedef std::unique_ptr<Parser> UniPtr; |
36 | | typedef std::unique_ptr<const Parser> UniPtrConst; |
37 | | |
38 | | //------------------------------------------------------------------------------------------------ |
39 | | // Constructor & destructor. |
40 | | Parser(); |
41 | | virtual ~Parser(); |
42 | | |
43 | | // Returns 0 if Bison successfully parses SQL statements, and the compiler can continue on to |
44 | | // semantic analysis. Otherwise, it returns one of the errcodes that are defined in file |
45 | | // "yb/yql/cql/ql/errcodes.h", and the caller (QL API) should stop the compiling process. |
46 | | CHECKED_STATUS Parse(const std::string& stmt, bool reparsed, |
47 | | const MemTrackerPtr& mem_tracker = nullptr, const bool internal = false); |
48 | | |
49 | | // Returns the generated parse tree. |
50 | | ParseTree::UniPtr Done(); |
51 | | |
52 | | // Entry function to scan the next token. Bison parser will call this function. |
53 | | GramProcessor::symbol_type Scan(); |
54 | | |
55 | | // Saves the generated parse tree from the parsing process. |
56 | 339k | void SaveGeneratedParseTree(TreeNode::SharedPtr generated_parse_tree) { |
57 | 339k | parse_context_->SaveGeneratedParseTree(generated_parse_tree); |
58 | 339k | } |
59 | | |
60 | | // Add a bind variable. |
61 | 15.4k | void AddBindVariable(PTBindVar *var) { |
62 | 15.4k | parse_context_->AddBindVariable(var); |
63 | 15.4k | } |
64 | | |
65 | | // Set the list of bind variables found during parsing in a DML statement. |
66 | 323k | void SetBindVariables(PTDmlStmt *stmt) { |
67 | 323k | parse_context_->GetBindVariables(&stmt->bind_variables()); |
68 | 323k | } |
69 | | |
70 | | // Access function for parse_context_. |
71 | 342k | ParseContext *parse_context() const { |
72 | 342k | return parse_context_.get(); |
73 | 342k | } |
74 | | |
75 | | // Converts a char* to MCString. |
76 | 369k | MCSharedPtr<MCString> MakeString(const char *str) { |
77 | 369k | return MCMakeShared<MCString>(PTreeMem(), str); |
78 | 369k | } |
79 | | |
80 | | // Memory pool for allocating and deallocating operating memory spaces during parsing process. |
81 | 0 | MemoryContext *PTempMem() const { |
82 | 0 | return parse_context_->PTempMem(); |
83 | 0 | } |
84 | | |
85 | | // Memory pool for constructing the parse tree of a statement. This context will have the same |
86 | | // lifetime as the parse tree. |
87 | 9.35M | MemoryContext *PTreeMem() const { |
88 | 9.35M | return parse_context_->PTreeMem(); |
89 | 9.35M | } |
90 | | |
91 | | // Raise parsing error. |
92 | 12 | void Error(const location& loc, ErrorCode error_code) { |
93 | | // Bison parser will raise exception, so we don't return Status::Error here. |
94 | 12 | Status s = parse_context_->Error(loc, error_code); |
95 | 12 | VLOG(3) << s.ToString()0 ; |
96 | 12 | } |
97 | | |
98 | 1 | void Error(const location& loc, const char *msg, ErrorCode error_code) { |
99 | | // Bison parser will raise exception, so we don't return Status::Error here. |
100 | 1 | Status s = parse_context_->Error(loc, msg, error_code); |
101 | 1 | VLOG(3) << s.ToString()0 ; |
102 | 1 | } |
103 | | |
104 | | private: |
105 | | //------------------------------------------------------------------------------------------------ |
106 | | // Parse context which consists of state variables and results. |
107 | | // NOTE: parse context must be FIRST class field to be destroyed by the class destructor |
108 | | // after all other dependent class fields (e.g. processors below). |
109 | | ParseContext::UniPtr parse_context_; |
110 | | |
111 | | // Lexical scanner. |
112 | | LexProcessor lex_processor_; |
113 | | |
114 | | // Grammar parser. |
115 | | GramProcessor gram_processor_; |
116 | | }; |
117 | | |
118 | | } // namespace ql |
119 | | } // namespace yb |
120 | | |
121 | | #endif // YB_YQL_CQL_QL_PARSER_PARSER_H_ |