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