MySQL 8.0.37
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 protected:
42 class TokenText {
43 public:
44 TokenText() = default;
45 TokenText(std::string_view txt) : txt_{txt} {}
46
47 operator bool() const { return !txt_.empty(); }
48
49 [[nodiscard]] std::string_view text() const { return txt_; }
50
51 private:
52 std::string_view txt_{};
53 };
54
56 if (auto ident_tkn = accept(IDENT)) {
57 return ident_tkn;
58 } else if (auto ident_tkn = accept(IDENT_QUOTED)) {
59 return ident_tkn;
60 } else {
61 return {};
62 }
63 }
64
65 TokenText accept(int sym) {
66 if (has_error()) return {};
67
68 if (cur_->id == sym) {
69 auto txt = cur_->text;
70 ++cur_;
71 return txt;
72 }
73
74 return {};
75 }
76
77 TokenText expect(int sym) {
78 if (has_error()) return {};
79
80 if (auto txt = accept(sym)) {
81 return txt;
82 }
83
84 error_ = "expected sym, got ...";
85
86 return {};
87 }
88
89 bool has_error() const { return !error_.empty(); }
90
93
94 std::string error_{};
95};
96
97#endif
Definition: sql_lexer.h:41
Definition: sql_parser.h:42
std::string_view txt_
Definition: sql_parser.h:52
TokenText(std::string_view txt)
Definition: sql_parser.h:45
std::string_view text() const
Definition: sql_parser.h:49
Definition: sql_parser.h:36
SqlLexer::iterator cur_
Definition: sql_parser.h:91
TokenText accept(int sym)
Definition: sql_parser.h:65
SqlParser(SqlLexer::iterator first, SqlLexer::iterator last)
Definition: sql_parser.h:38
std::string error_
Definition: sql_parser.h:94
bool has_error() const
Definition: sql_parser.h:89
TokenText ident()
Definition: sql_parser.h:55
SqlLexer::iterator end_
Definition: sql_parser.h:92
TokenText expect(int sym)
Definition: sql_parser.h:77
@ IDENT_QUOTED
Definition: sql_yacc.h:274
@ IDENT
Definition: sql_yacc.h:272
std::string_view text
Definition: sql_lexer.h:44
TokenId id
Definition: sql_lexer.h:45