MySQL 9.6.0
Source Code Documentation
parse_result.h
Go to the documentation of this file.
1// Copyright (c) 2024, 2025, Oracle and/or its affiliates.
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of MySQL hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
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, version 2.0, 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#ifndef MYSQL_STRCONV_DECODE_PARSE_RESULT_H
25#define MYSQL_STRCONV_DECODE_PARSE_RESULT_H
26
27/// @file
28/// Experimental API header
29
30#include <cassert> // assert
31#include <string> // string
32#include <string_view> // string_view
33#include <variant> // variant
34#include "mysql/strconv/decode/parse_status.h" // Parse_status
35
36/// @addtogroup GroupLibsMysqlStrconv
37/// @{
38
39namespace mysql::strconv::detail {
40
41class Parser_internals;
42
43/// Class holding the result from parsing a string, in the form of a status and
44/// a message.
46 public:
48 assert(m_status == Parse_status::ok ||
51 }
52
53 /// Update the position of a parse error to the given position.
54 ///
55 /// This requires that the current sattus is `parse_error`.
56 ///
57 /// @param position New position
58 void update_parse_error_pos(std::size_t position) {
60 m_parse_error_position = position;
61 }
62
63 /// Store a result representing that the requested object could
64 /// not be parsed because something went wrong which is not the string's
65 /// fault.
66 ///
67 /// @param message Error message describing what went wrong.
68 void set_store_error(const std::string_view &message) noexcept {
71 }
72
73 /// Store a result representing that an object was successfully parsed from a
74 /// prefix of the string, but the user invoked a function such as
75 /// `decode` which requires that the object description extends to the
76 /// end of the string.
77 ///
78 /// This should only be used by the framework, not by user-defined
79 /// decode_impl functions.
81 assert(is_ok());
83 }
84
85 /// Store a result representing that the requested object could not be parsed
86 /// because and out-of-memory condition occurred. This is shorthand for
87 /// set_store_error("Out of memory").
89
90 // ==== Query the success/failure status ====
91
92 /// Return true if the last operation succeeded, i.e., either a full match was
93 /// requested and an object was found which extended to the end of the string;
94 /// or a prefix match was requested and an object was found, possibly followed
95 /// by unparsed character; or an optional match was requested and the object
96 /// was found or was completely absent (but no error occurred).
97 [[nodiscard]] bool is_ok() const {
98 return status() == Parse_status::ok ||
100 }
101
102 /// Return true if either the last operation succeeded, or failed because a
103 /// full match was requested and only a prefix match was found.
104 [[nodiscard]] bool is_prefix_ok() const {
106 }
107
108 /// Return the number of repetitions found in the last call.
109 ///
110 /// This may be nonzero if the status is `ok` or `fullmatch_error`; in other
111 /// cases it is 0.
112 [[nodiscard]] std::size_t found_count() const {
113 if (!is_ok() && !is_fullmatch_error()) return 0;
114 return m_found_count;
115 }
116
117 /// Return true if found_count() != 0.
118 [[nodiscard]] bool is_found() const { return found_count() != 0; }
119
120 /// Return true if an environment error occurred.
121 [[nodiscard]] bool is_store_error() const {
123 }
124
125 /// Return true if a parse error occurred.
126 [[nodiscard]] bool is_parse_error() const {
127 return status() == Parse_status::parse_error ||
129 }
130
131 /// Return true if the object was parsed successfully, but there were more
132 /// characters after the end.
133 [[nodiscard]] bool is_fullmatch_error() const {
135 }
136
137 protected:
138 /// The form of the message stored in this object.
139 // NOLINTNEXTLINE(performance-enum-size): silence clang-tidy's pointless hint
140 enum class Message_form {
141 /// The message is expressed as a full sentence, for example "Value out of
142 /// range". It should begin with a capital letter and *not* end with a
143 /// period. It will be exended with a string like
144 /// " after 5 characters, near [HERE] in "foo: [HERE]123".".
145 sentence,
146
147 /// The message is a string that was expected but not found at the current
148 /// position. It must be a literal string, not a descriptive name of a
149 /// token. For example, if a comma was missing, the string should be ",".
150 /// It will be escaped and quoted, and the result inserted in a string like
151 /// "Expected "," after 4 characters, near [HERE] in "foo [HERE]bar"."
153 };
154
155 /// Common implementation of `set_parse_error` and
156 /// `set_parse_error_expected_string`.
157 void do_set_parse_error(const std::string_view &string,
158 Message_form message_form, std::size_t position) {
159 assert(is_ok());
160 auto sposition = static_cast<std::ptrdiff_t>(position);
162 sposition >= m_parse_error_position) {
163 m_message_form = message_form;
165 m_parse_error_position = sposition;
166 }
168 }
169
170 /// Store a result representing that the requested object was successfully
171 /// parsed, overriding a previous error state.
172 ///
173 /// @param count The number of successfully parsed repetitions of the parsed
174 /// token.
175 void set_match_count(std::size_t count) noexcept {
176 assert(m_status == Parse_status::ok ||
179 }
180
181 private:
182 /// Return the current status.
183 [[nodiscard]] Parse_status status() const { return m_status; }
184
185 /// Return the message.
186 [[nodiscard]] std::string_view message() const { return m_message; }
187
188 /// Message to use on out-of-memory.
189 static constexpr char oom_message[] = "Out of memory";
190
191 /// The message.
192 std::string_view m_message{};
193
194 /// If the status is "ok", the number of tokens found.
195 std::size_t m_found_count{0};
196
197 /// The position of the most recent "parse_error".
198 std::ptrdiff_t m_parse_error_position{-1};
199
200 /// The status.
202
203 /// The form of the message.
205
207}; // class Parse_result
208
209/// Helper class that exports the internals from Parse_result.
210///
211/// This is only intended to be used by encode_impl.
213 public:
215
216 explicit Parse_result_internals(const Parse_result &parse_result)
217 : m_parse_result(parse_result) {}
218
219 [[nodiscard]] std::ptrdiff_t parse_error_position() const {
221 }
222
223 [[nodiscard]] Parse_status status() const { return m_parse_result.status(); }
224
225 [[nodiscard]] auto message_form() const {
227 }
228
229 [[nodiscard]] std::string_view message() const {
230 return m_parse_result.message();
231 }
232
234};
235
236} // namespace mysql::strconv::detail
237
238// addtogroup GroupLibsMysqlStrconv
239/// @}
240
241#endif // ifndef MYSQL_STRCONV_DECODE_PARSE_RESULT_H
Helper class that exports the internals from Parse_result.
Definition: parse_result.h:212
std::ptrdiff_t parse_error_position() const
Definition: parse_result.h:219
Parse_status status() const
Definition: parse_result.h:223
Parse_result_internals(const Parse_result &parse_result)
Definition: parse_result.h:216
const Parse_result & m_parse_result
Definition: parse_result.h:233
auto message_form() const
Definition: parse_result.h:225
std::string_view message() const
Definition: parse_result.h:229
Class holding the result from parsing a string, in the form of a status and a message.
Definition: parse_result.h:45
bool is_found() const
Return true if found_count() != 0.
Definition: parse_result.h:118
std::ptrdiff_t m_parse_error_position
The position of the most recent "parse_error".
Definition: parse_result.h:198
Parse_status m_status
The status.
Definition: parse_result.h:201
void set_match_count(std::size_t count) noexcept
Store a result representing that the requested object was successfully parsed, overriding a previous ...
Definition: parse_result.h:175
Message_form m_message_form
The form of the message.
Definition: parse_result.h:204
static constexpr char oom_message[]
Message to use on out-of-memory.
Definition: parse_result.h:189
bool is_prefix_ok() const
Return true if either the last operation succeeded, or failed because a full match was requested and ...
Definition: parse_result.h:104
void revert_parse_error_to_ok() noexcept
Definition: parse_result.h:47
bool is_fullmatch_error() const
Return true if the object was parsed successfully, but there were more characters after the end.
Definition: parse_result.h:133
Message_form
The form of the message stored in this object.
Definition: parse_result.h:140
@ sentence
The message is expressed as a full sentence, for example "Value out of range".
@ expected_string
The message is a string that was expected but not found at the current position.
void update_parse_error_pos(std::size_t position)
Update the position of a parse error to the given position.
Definition: parse_result.h:58
bool is_parse_error() const
Return true if a parse error occurred.
Definition: parse_result.h:126
void set_oom() noexcept
Store a result representing that the requested object could not be parsed because and out-of-memory c...
Definition: parse_result.h:88
std::size_t found_count() const
Return the number of repetitions found in the last call.
Definition: parse_result.h:112
void set_fullmatch_error() noexcept
Store a result representing that an object was successfully parsed from a prefix of the string,...
Definition: parse_result.h:80
void set_store_error(const std::string_view &message) noexcept
Store a result representing that the requested object could not be parsed because something went wron...
Definition: parse_result.h:68
std::size_t m_found_count
If the status is "ok", the number of tokens found.
Definition: parse_result.h:195
bool is_ok() const
Return true if the last operation succeeded, i.e., either a full match was requested and an object wa...
Definition: parse_result.h:97
Parse_status status() const
Return the current status.
Definition: parse_result.h:183
std::string_view message() const
Return the message.
Definition: parse_result.h:186
std::string_view m_message
The message.
Definition: parse_result.h:192
void do_set_parse_error(const std::string_view &string, Message_form message_form, std::size_t position)
Common implementation of set_parse_error and set_parse_error_expected_string.
Definition: parse_result.h:157
bool is_store_error() const
Return true if an environment error occurred.
Definition: parse_result.h:121
static int count
Definition: myisam_ftdump.cc:45
Definition: gtid_binary_format_conv.h:252
Parse_status
The status after parsing an object from a string.
Definition: parse_status.h:39
@ store_error
No parse error was found in the string, but an error occurred when storing into the output object - f...
@ ok
The object could be successfully parsed.
@ parse_error
The object could not be successfully parsed because the string was wrong.
@ ok_backtracked_from_parse_error
There was previously a parse error; the parser has backtracked to a non-error status; the information...
@ fullmatch_error
An object was parsed successfully from a prefix of the string, but there were extra characters after ...
@ string
String represented as std::string
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
Experimental API header.