error.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2024 Jonathan Müller and lexy contributors
2 // SPDX-License-Identifier: BSL-1.0
3 
4 #ifndef LEXY_ERROR_HPP_INCLUDED
5 #define LEXY_ERROR_HPP_INCLUDED
6 
8 #include <lexy/grammar.hpp>
9 #include <lexy/input/base.hpp>
10 
11 namespace lexy
12 {
13 template <typename Reader, typename Tag>
14 class error;
15 
17 template <typename Reader>
18 class error<Reader, void>
19 {
20 public:
21  constexpr explicit error(typename Reader::iterator pos, const char* msg) noexcept
22  : _pos(pos), _end(pos), _msg(msg)
23  {}
24  constexpr explicit error(typename Reader::iterator begin, typename Reader::iterator end,
25  const char* msg) noexcept
26  : _pos(begin), _end(end), _msg(msg)
27  {}
28 
29  template <typename OtherReader, typename = std::enable_if_t<std::is_same_v<
30  typename Reader::iterator, typename OtherReader::iterator>>>
31  constexpr operator error<OtherReader, void>() const noexcept
32  {
33  return error<OtherReader, void>(_pos, _end, _msg);
34  }
35 
36  constexpr auto position() const noexcept
37  {
38  return _pos;
39  }
40 
41  constexpr const char* message() const noexcept
42  {
43  return _msg;
44  }
45  template <typename Tag>
46  constexpr bool is(Tag = {}) const noexcept
47  {
48  return _detail::string_view(_msg) == _detail::type_name<Tag>();
49  }
50 
51  constexpr auto begin() const noexcept
52  {
53  return _pos;
54  }
55  constexpr auto end() const noexcept
56  {
57  return _end;
58  }
59 
60 private:
61  typename Reader::iterator _pos;
62  typename Reader::iterator _end;
63  const char* _msg;
64 };
65 
67 template <typename Reader, typename Tag>
68 class error : public error<Reader, void>
69 {
70 public:
71  constexpr explicit error(typename Reader::iterator pos) noexcept
72  : error<Reader, void>(pos, _detail::type_name<Tag>())
73  {}
74  constexpr explicit error(typename Reader::iterator begin,
75  typename Reader::iterator end) noexcept
76  : error<Reader, void>(begin, end, _detail::type_name<Tag>())
77  {}
78 
79  template <typename OtherReader, typename = std::enable_if_t<std::is_same_v<
80  typename Reader::iterator, typename OtherReader::iterator>>>
81  constexpr operator error<OtherReader, Tag>() const noexcept
82  {
83  return error<OtherReader, Tag>(this->begin(), this->end());
84  }
85 };
86 
89 {};
90 template <typename Reader>
91 class error<Reader, expected_literal>
92 {
93 public:
94  constexpr explicit error(typename Reader::iterator pos,
95  const typename Reader::encoding::char_type* str, std::size_t index,
96  std::size_t length) noexcept
97  : _pos(pos), _str(str), _idx(index), _length(length)
98  {}
99 
100  template <typename OtherReader, typename = std::enable_if_t<std::is_same_v<
101  typename Reader::iterator, typename OtherReader::iterator>>>
102  constexpr operator error<OtherReader, expected_literal>() const noexcept
103  {
104  return error<OtherReader, expected_literal>(_pos, _str, _idx, _length);
105  }
106 
107  constexpr auto position() const noexcept
108  {
109  return _pos;
110  }
111 
112  constexpr auto string() const noexcept -> const typename Reader::encoding::char_type*
113  {
114  return _str;
115  }
116 
117  constexpr std::size_t index() const noexcept
118  {
119  return _idx;
120  }
121 
122  constexpr std::size_t length() const noexcept
123  {
124  return _length;
125  }
126 
127  constexpr auto character() const noexcept
128  {
129  return _str[_idx];
130  }
131 
132 private:
133  typename Reader::iterator _pos;
135  std::size_t _idx, _length;
136 };
137 
141 {};
142 template <typename Reader>
143 class error<Reader, expected_keyword>
144 {
145 public:
146  constexpr explicit error(typename Reader::iterator begin, typename Reader::iterator end,
147  const typename Reader::encoding::char_type* str, std::size_t length)
148  : _begin(begin), _end(end), _str(str), _length(length)
149  {}
150 
151  template <typename OtherReader, typename = std::enable_if_t<std::is_same_v<
152  typename Reader::iterator, typename OtherReader::iterator>>>
153  constexpr operator error<OtherReader, expected_keyword>() const noexcept
154  {
155  return error<OtherReader, expected_keyword>(_begin, _end, _str, _length);
156  }
157 
158  constexpr auto position() const noexcept
159  {
160  return _begin;
161  }
162 
163  constexpr auto begin() const noexcept
164  {
165  return _begin;
166  }
167  constexpr auto end() const noexcept
168  {
169  return _end;
170  }
171 
172  constexpr auto string() const noexcept -> const typename Reader::encoding::char_type*
173  {
174  return _str;
175  }
176 
177  constexpr std::size_t length() const noexcept
178  {
179  return _length;
180  }
181 
182 private:
183  typename Reader::iterator _begin;
184  typename Reader::iterator _end;
186  std::size_t _length;
187 };
188 
191 {};
192 template <typename Reader>
194 {
195 public:
196  constexpr explicit error(typename Reader::iterator pos, const char* name) noexcept
197  : _pos(pos), _name(name)
198  {}
199 
200  template <typename OtherReader, typename = std::enable_if_t<std::is_same_v<
201  typename Reader::iterator, typename OtherReader::iterator>>>
202  constexpr operator error<OtherReader, expected_char_class>() const noexcept
203  {
204  return error<OtherReader, expected_char_class>(_pos, _name);
205  }
206 
207  constexpr auto position() const noexcept
208  {
209  return _pos;
210  }
211 
212  constexpr const char* name() const noexcept
213  {
214  return _name;
215  }
216 
217 private:
218  typename Reader::iterator _pos;
219  const char* _name;
220 };
221 
222 template <typename Input, typename Tag>
224 } // namespace lexy
225 
226 namespace lexy
227 {
228 template <typename Input>
229 using _detect_parent_input = decltype(LEXY_DECLVAL(Input).parent_input());
230 
232 template <typename Input>
234 {
235 public:
236  constexpr explicit error_context(lexy::production_info production, const Input& input,
237  typename input_reader<Input>::iterator pos) noexcept
238  : _input(&input), _pos(pos), _production(production.name)
239  {}
240 
242  constexpr const auto& input() const noexcept
243  {
244  if constexpr (_detail::is_detected<_detect_parent_input, Input>)
245  return _input->parent_input();
246  else
247  return *_input;
248  }
249 
251  const char* production() const noexcept
252  {
253  return _production;
254  }
255 
257  constexpr auto position() const noexcept
258  {
259  return _pos;
260  }
261 
262 private:
263  const Input* _input;
265  const char* _production;
266 };
267 } // namespace lexy
268 
269 #endif // LEXY_ERROR_HPP_INCLUDED
270 
lexy::error_context::position
constexpr auto position() const noexcept
The starting position of the production.
Definition: error.hpp:257
lexy::error< Reader, expected_literal >::character
constexpr auto character() const noexcept
Definition: error.hpp:127
magic_enum::char_type
string_view::value_type char_type
Definition: magic_enum.hpp:145
config.hpp
lexy::error< Reader, expected_char_class >::position
constexpr auto position() const noexcept
Definition: error.hpp:207
lexy::error::error
constexpr error(typename Reader::iterator begin, typename Reader::iterator end) noexcept
Definition: error.hpp:74
lexy::error< Reader, expected_keyword >::string
constexpr auto string() const noexcept -> const typename Reader::encoding::char_type *
Definition: error.hpp:172
lexy::error< Reader, expected_literal >::_str
const Reader::encoding::char_type * _str
Definition: error.hpp:134
lexy::error< Reader, void >::error
constexpr error(typename Reader::iterator begin, typename Reader::iterator end, const char *msg) noexcept
Definition: error.hpp:24
lexy::error< Reader, void >::position
constexpr auto position() const noexcept
Definition: error.hpp:36
lexy::error::error
constexpr error(typename Reader::iterator pos) noexcept
Definition: error.hpp:71
lexy::error_context::_pos
input_reader< Input >::iterator _pos
Definition: error.hpp:264
lexy::error< Reader, void >::error
constexpr error(typename Reader::iterator pos, const char *msg) noexcept
Definition: error.hpp:21
lexy::error_context::error_context
constexpr error_context(lexy::production_info production, const Input &input, typename input_reader< Input >::iterator pos) noexcept
Definition: error.hpp:236
lexy::error_context
Contains information about the context of an error, production is type-erased.
Definition: error.hpp:233
lexy::error< Reader, void >::is
constexpr bool is(Tag={}) const noexcept
Definition: error.hpp:46
lexy
Definition: any_ref.hpp:12
lexy::error< Reader, void >::end
constexpr auto end() const noexcept
Definition: error.hpp:55
lexy::error_context::production
const char * production() const noexcept
The name of the production where the error occurred.
Definition: error.hpp:251
cx::end
constexpr auto end(const C &c) -> decltype(c.end())
Definition: wildcards.hpp:686
lexy::error< Reader, expected_keyword >::end
constexpr auto end() const noexcept
Definition: error.hpp:167
detail::void
j template void())
Definition: json.hpp:4893
grammar.hpp
lexy::error< Reader, expected_char_class >::error
constexpr error(typename Reader::iterator pos, const char *name) noexcept
Definition: error.hpp:196
lexy::error< Reader, expected_literal >::_length
std::size_t _length
Definition: error.hpp:135
lexy::error< Reader, expected_keyword >::_length
std::size_t _length
Definition: error.hpp:186
lexy::error
Generic failure.
Definition: error.hpp:14
lexy::error< Reader, void >::_msg
const char * _msg
Definition: error.hpp:63
lexy::error< Reader, void >
Type erased generic failure.
Definition: error.hpp:18
lexy::_detect_parent_input
decltype(LEXY_DECLVAL(Input).parent_input()) _detect_parent_input
Definition: error.hpp:229
lexy::error< Reader, expected_char_class >::_pos
Reader::iterator _pos
Definition: error.hpp:218
lexy::error< Reader, expected_keyword >::_str
const Reader::encoding::char_type * _str
Definition: error.hpp:185
lexy::error_context::_input
const Input * _input
Definition: error.hpp:263
lexy::error< Reader, expected_literal >::length
constexpr std::size_t length() const noexcept
Definition: error.hpp:122
lexy::error< Reader, expected_keyword >::begin
constexpr auto begin() const noexcept
Definition: error.hpp:163
lexy::expected_keyword
Definition: error.hpp:140
lexy::error< Reader, expected_keyword >::length
constexpr std::size_t length() const noexcept
Definition: error.hpp:177
cx::begin
constexpr auto begin(const C &c) -> decltype(c.begin())
Definition: wildcards.hpp:661
lexy::expected_literal
Expected the literal character sequence.
Definition: error.hpp:88
lexy::error< Reader, expected_char_class >::_name
const char * _name
Definition: error.hpp:219
lexy::error< Reader, void >::begin
constexpr auto begin() const noexcept
Definition: error.hpp:51
lexy::error< Reader, expected_literal >::string
constexpr auto string() const noexcept -> const typename Reader::encoding::char_type *
Definition: error.hpp:112
lexy::error< Reader, expected_literal >::position
constexpr auto position() const noexcept
Definition: error.hpp:107
lexy::error< Reader, void >::_pos
Reader::iterator _pos
Definition: error.hpp:61
lexy::error< Reader, void >::message
constexpr const char * message() const noexcept
Definition: error.hpp:41
lexy::production_info
Definition: grammar.hpp:192
lexy::error< Reader, expected_keyword >::error
constexpr error(typename Reader::iterator begin, typename Reader::iterator end, const typename Reader::encoding::char_type *str, std::size_t length)
Definition: error.hpp:146
lexy::error< Reader, expected_keyword >::position
constexpr auto position() const noexcept
Definition: error.hpp:158
lexy::error_context::input
constexpr const auto & input() const noexcept
The input.
Definition: error.hpp:242
base.hpp
lexy::error< Reader, expected_literal >::index
constexpr std::size_t index() const noexcept
Definition: error.hpp:117
lexy::error< Reader, expected_keyword >::_begin
Reader::iterator _begin
Definition: error.hpp:183
LEXY_DECLVAL
#define LEXY_DECLVAL(...)
Definition: config.hpp:32
lexy::error< Reader, void >::_end
Reader::iterator _end
Definition: error.hpp:62
lexy::error< Reader, expected_char_class >::name
constexpr const char * name() const noexcept
Definition: error.hpp:212
lexy::error< Reader, expected_literal >::error
constexpr error(typename Reader::iterator pos, const typename Reader::encoding::char_type *str, std::size_t index, std::size_t length) noexcept
Definition: error.hpp:94
lexy::error< Reader, expected_keyword >::_end
Reader::iterator _end
Definition: error.hpp:184
lexy::error< Reader, expected_literal >::_pos
Reader::iterator _pos
Definition: error.hpp:133
lexy::expected_char_class
Expected a character of the specified character class.
Definition: error.hpp:190
lexy::input_reader
decltype(LEXY_DECLVAL(Input).reader()) input_reader
Definition: input/base.hpp:106
lexy::error_context::_production
const char * _production
Definition: error.hpp:265
lexy::_detail::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:192
detail::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: json.hpp:3095


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Nov 1 2024 02:20:50