error.hpp
Go to the documentation of this file.
1 // Copyright (C) 2020-2023 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  constexpr auto position() const noexcept
30  {
31  return _pos;
32  }
33 
34  constexpr const char* message() const noexcept
35  {
36  return _msg;
37  }
38  template <typename Tag>
39  constexpr bool is(Tag = {}) const noexcept
40  {
41  return _detail::string_view(_msg) == _detail::type_name<Tag>();
42  }
43 
44  constexpr auto begin() const noexcept
45  {
46  return _pos;
47  }
48  constexpr auto end() const noexcept
49  {
50  return _end;
51  }
52 
53 private:
54  typename Reader::iterator _pos;
55  typename Reader::iterator _end;
56  const char* _msg;
57 };
58 
60 template <typename Reader, typename Tag>
61 class error : public error<Reader, void>
62 {
63 public:
64  constexpr explicit error(typename Reader::iterator pos) noexcept
65  : error<Reader, void>(pos, _detail::type_name<Tag>())
66  {}
67  constexpr explicit error(typename Reader::iterator begin,
68  typename Reader::iterator end) noexcept
69  : error<Reader, void>(begin, end, _detail::type_name<Tag>())
70  {}
71 };
72 
75 {};
76 template <typename Reader>
77 class error<Reader, expected_literal>
78 {
79 public:
80  constexpr explicit error(typename Reader::iterator pos,
81  const typename Reader::encoding::char_type* str, std::size_t index,
82  std::size_t length) noexcept
83  : _pos(pos), _str(str), _idx(index), _length(length)
84  {}
85 
86  constexpr auto position() const noexcept
87  {
88  return _pos;
89  }
90 
91  constexpr auto string() const noexcept -> const typename Reader::encoding::char_type*
92  {
93  return _str;
94  }
95 
96  constexpr std::size_t index() const noexcept
97  {
98  return _idx;
99  }
100 
101  constexpr std::size_t length() const noexcept
102  {
103  return _length;
104  }
105 
106  constexpr auto character() const noexcept
107  {
108  return _str[_idx];
109  }
110 
111 private:
112  typename Reader::iterator _pos;
114  std::size_t _idx, _length;
115 };
116 
120 {};
121 template <typename Reader>
122 class error<Reader, expected_keyword>
123 {
124 public:
125  constexpr explicit error(typename Reader::iterator begin, typename Reader::iterator end,
126  const typename Reader::encoding::char_type* str, std::size_t length)
127  : _begin(begin), _end(end), _str(str), _length(length)
128  {}
129 
130  constexpr auto position() const noexcept
131  {
132  return _begin;
133  }
134 
135  constexpr auto begin() const noexcept
136  {
137  return _begin;
138  }
139  constexpr auto end() const noexcept
140  {
141  return _end;
142  }
143 
144  constexpr auto string() const noexcept -> const typename Reader::encoding::char_type*
145  {
146  return _str;
147  }
148 
149  constexpr std::size_t length() const noexcept
150  {
151  return _length;
152  }
153 
154 private:
155  typename Reader::iterator _begin;
156  typename Reader::iterator _end;
158  std::size_t _length;
159 };
160 
163 {};
164 template <typename Reader>
166 {
167 public:
168  constexpr explicit error(typename Reader::iterator pos, const char* name) noexcept
169  : _pos(pos), _name(name)
170  {}
171 
172  constexpr auto position() const noexcept
173  {
174  return _pos;
175  }
176 
177  constexpr const char* name() const noexcept
178  {
179  return _name;
180  }
181 
182 private:
183  typename Reader::iterator _pos;
184  const char* _name;
185 };
186 
187 template <typename Input, typename Tag>
189 } // namespace lexy
190 
191 namespace lexy
192 {
193 template <typename Input>
194 using _detect_parent_input = decltype(LEXY_DECLVAL(Input).parent_input());
195 
197 template <typename Input>
199 {
200 public:
201  constexpr explicit error_context(lexy::production_info production, const Input& input,
202  typename input_reader<Input>::iterator pos) noexcept
203  : _input(&input), _pos(pos), _production(production.name)
204  {}
205 
207  constexpr const auto& input() const noexcept
208  {
209  if constexpr (_detail::is_detected<_detect_parent_input, Input>)
210  return _input->parent_input();
211  else
212  return *_input;
213  }
214 
216  const char* production() const noexcept
217  {
218  return _production;
219  }
220 
222  constexpr auto position() const noexcept
223  {
224  return _pos;
225  }
226 
227 private:
228  const Input* _input;
230  const char* _production;
231 };
232 } // namespace lexy
233 
234 #endif // LEXY_ERROR_HPP_INCLUDED
235 
lexy::error_context::position
constexpr auto position() const noexcept
The starting position of the production.
Definition: error.hpp:222
lexy::error< Reader, expected_literal >::character
constexpr auto character() const noexcept
Definition: error.hpp:106
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:172
lexy::error::error
constexpr error(typename Reader::iterator begin, typename Reader::iterator end) noexcept
Definition: error.hpp:67
lexy::error< Reader, expected_keyword >::string
constexpr auto string() const noexcept -> const typename Reader::encoding::char_type *
Definition: error.hpp:144
lexy::error< Reader, expected_literal >::_str
const Reader::encoding::char_type * _str
Definition: error.hpp:113
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:29
lexy::error::error
constexpr error(typename Reader::iterator pos) noexcept
Definition: error.hpp:64
lexy::error_context::_pos
input_reader< Input >::iterator _pos
Definition: error.hpp:229
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:201
lexy::error_context
Contains information about the context of an error, production is type-erased.
Definition: error.hpp:198
lexy::error< Reader, void >::is
constexpr bool is(Tag={}) const noexcept
Definition: error.hpp:39
lexy
Definition: any_ref.hpp:12
lexy::error< Reader, void >::end
constexpr auto end() const noexcept
Definition: error.hpp:48
lexy::error_context::production
const char * production() const noexcept
The name of the production where the error occurred.
Definition: error.hpp:216
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:139
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:168
lexy::error< Reader, expected_literal >::_length
std::size_t _length
Definition: error.hpp:114
lexy::error< Reader, expected_keyword >::_length
std::size_t _length
Definition: error.hpp:158
lexy::error
Generic failure.
Definition: error.hpp:14
lexy::error< Reader, void >::_msg
const char * _msg
Definition: error.hpp:56
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:194
lexy::error< Reader, expected_char_class >::_pos
Reader::iterator _pos
Definition: error.hpp:183
lexy::error< Reader, expected_keyword >::_str
const Reader::encoding::char_type * _str
Definition: error.hpp:157
lexy::error_context::_input
const Input * _input
Definition: error.hpp:228
lexy::error< Reader, expected_literal >::length
constexpr std::size_t length() const noexcept
Definition: error.hpp:101
lexy::error< Reader, expected_keyword >::begin
constexpr auto begin() const noexcept
Definition: error.hpp:135
lexy::expected_keyword
Definition: error.hpp:119
lexy::error< Reader, expected_keyword >::length
constexpr std::size_t length() const noexcept
Definition: error.hpp:149
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:74
lexy::error< Reader, expected_char_class >::_name
const char * _name
Definition: error.hpp:184
lexy::error< Reader, void >::begin
constexpr auto begin() const noexcept
Definition: error.hpp:44
lexy::error< Reader, expected_literal >::string
constexpr auto string() const noexcept -> const typename Reader::encoding::char_type *
Definition: error.hpp:91
lexy::error< Reader, expected_literal >::position
constexpr auto position() const noexcept
Definition: error.hpp:86
lexy::error< Reader, void >::_pos
Reader::iterator _pos
Definition: error.hpp:54
lexy::error< Reader, void >::message
constexpr const char * message() const noexcept
Definition: error.hpp:34
lexy::production_info
Definition: grammar.hpp:178
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:125
lexy::error< Reader, expected_keyword >::position
constexpr auto position() const noexcept
Definition: error.hpp:130
lexy::error_context::input
constexpr const auto & input() const noexcept
The input.
Definition: error.hpp:207
base.hpp
lexy::error< Reader, expected_literal >::index
constexpr std::size_t index() const noexcept
Definition: error.hpp:96
lexy::error< Reader, expected_keyword >::_begin
Reader::iterator _begin
Definition: error.hpp:155
LEXY_DECLVAL
#define LEXY_DECLVAL(...)
Definition: config.hpp:24
lexy::error< Reader, void >::_end
Reader::iterator _end
Definition: error.hpp:55
lexy::error< Reader, expected_char_class >::name
constexpr const char * name() const noexcept
Definition: error.hpp:177
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:80
lexy::error< Reader, expected_keyword >::_end
Reader::iterator _end
Definition: error.hpp:156
lexy::error< Reader, expected_literal >::_pos
Reader::iterator _pos
Definition: error.hpp:112
lexy::expected_char_class
Expected a character of the specified character class.
Definition: error.hpp:162
lexy::input_reader
decltype(LEXY_DECLVAL(Input).reader()) input_reader
Definition: input/base.hpp:92
lexy::error_context::_production
const char * _production
Definition: error.hpp:230
lexy::_detail::string_view
basic_string_view< char > string_view
Definition: string_view.hpp:184


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07