MySQL 8.3.0
Source Code Documentation
sql_parser.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2023, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is also distributed with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have included with MySQL.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23*/
24
25#ifndef ROUTING_SQL_PARSER_INCLUDED
26#define ROUTING_SQL_PARSER_INCLUDED
27
28#include <string>
29#include <string_view>
30
31#include "sql/lex.h"
32#include "sql_lexer.h"
33#include "sql_lexer_thd.h"
34
35class SqlParser {
36 public:
38 : cur_{first}, end_{last} {}
39
40 class TokenText {
41 public:
42 TokenText() = default;
43 TokenText(SqlLexer::TokenId id, std::string_view txt)
44 : id_{id}, txt_{txt} {}
45
46 operator bool() const { return !txt_.empty(); }
47
48 [[nodiscard]] std::string_view text() const { return txt_; }
49 [[nodiscard]] SqlLexer::TokenId id() const { return id_; }
50
51 private:
53 std::string_view txt_{};
54 };
55
56 TokenText token() const { return {cur_->id, cur_->text}; }
57
58 protected:
60 if (auto ident_tkn = accept(IDENT)) {
61 return ident_tkn;
62 } else if (auto ident_tkn = accept(IDENT_QUOTED)) {
63 return ident_tkn;
64 } else {
65 return {};
66 }
67 }
68
70 if (has_error()) return {};
71
72 if (cur_->id != sym) {
73 auto id = cur_->id;
74 auto txt = cur_->text;
75 ++cur_;
76 return {id, txt};
77 }
78
79 return {};
80 }
81
82 TokenText accept(int sym) {
83 if (has_error()) return {};
84
85 if (cur_->id == sym) {
86 auto id = cur_->id;
87 auto txt = cur_->text;
88 ++cur_;
89 return {id, txt};
90 }
91
92 return {};
93 }
94
95 TokenText expect(int sym) {
96 if (has_error()) return {};
97
98 if (auto txt = accept(sym)) {
99 return txt;
100 }
101
102 error_ = "expected sym, got ...";
103
104 return {};
105 }
106
107 bool has_error() const { return !error_.empty(); }
108
111
112 std::string error_{};
113};
114
115#endif
Definition: sql_lexer.h:40
int TokenId
Definition: sql_lexer.h:36
Definition: sql_parser.h:40
TokenText(SqlLexer::TokenId id, std::string_view txt)
Definition: sql_parser.h:43
SqlLexer::TokenId id_
Definition: sql_parser.h:52
std::string_view txt_
Definition: sql_parser.h:53
SqlLexer::TokenId id() const
Definition: sql_parser.h:49
std::string_view text() const
Definition: sql_parser.h:48
Definition: sql_parser.h:35
TokenText accept_if_not(int sym)
Definition: sql_parser.h:69
SqlLexer::iterator cur_
Definition: sql_parser.h:109
TokenText accept(int sym)
Definition: sql_parser.h:82
SqlParser(SqlLexer::iterator first, SqlLexer::iterator last)
Definition: sql_parser.h:37
std::string error_
Definition: sql_parser.h:112
bool has_error() const
Definition: sql_parser.h:107
TokenText ident()
Definition: sql_parser.h:59
SqlLexer::iterator end_
Definition: sql_parser.h:110
TokenText expect(int sym)
Definition: sql_parser.h:95
TokenText token() const
Definition: sql_parser.h:56
@ IDENT_QUOTED
Definition: sql_yacc.h:282
@ IDENT
Definition: sql_yacc.h:280
std::string_view text
Definition: sql_lexer.h:43
TokenId id
Definition: sql_lexer.h:44
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:509