parse.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_ACTION_PARSE_HPP_INCLUDED
5 #define LEXY_ACTION_PARSE_HPP_INCLUDED
6 
8 #include <lexy/action/base.hpp>
10 #include <lexy/callback/base.hpp>
11 #include <lexy/callback/bind.hpp>
12 
13 namespace lexy
14 {
15 template <typename T, typename ErrorCallback>
17 {
19 
20 public:
21  using value_type = T;
22  using error_callback = ErrorCallback;
23  using error_type = typename _impl_t::error_type;
24 
25  //=== status ===//
26  constexpr explicit operator bool() const noexcept
27  {
28  return _impl.is_success();
29  }
30 
31  constexpr bool is_success() const noexcept
32  {
33  return _impl.is_success();
34  }
35  constexpr bool is_error() const noexcept
36  {
37  return _impl.is_error();
38  }
39  constexpr bool is_recovered_error() const noexcept
40  {
41  return _impl.is_recovered_error();
42  }
43  constexpr bool is_fatal_error() const noexcept
44  {
45  return _impl.is_fatal_error();
46  }
47 
48  //=== value ===//
49  constexpr bool has_value() const noexcept
50  {
51  return static_cast<bool>(_value);
52  }
53 
54  constexpr decltype(auto) value() const& noexcept
55  {
56  return *_value;
57  }
58  constexpr decltype(auto) value() && noexcept
59  {
60  return LEXY_MOV(*_value);
61  }
62 
63  //=== error ===//
64  constexpr std::size_t error_count() const noexcept
65  {
66  return _impl.error_count();
67  }
68 
69  constexpr const auto& errors() const& noexcept
70  {
71  return _impl.errors();
72  }
73  constexpr auto&& errors() && noexcept
74  {
75  return LEXY_MOV(_impl).errors();
76  }
77 
78 private:
79  constexpr explicit parse_result(_impl_t&& impl) noexcept : _impl(LEXY_MOV(impl)), _value() {}
80  template <typename U>
81  constexpr explicit parse_result(_impl_t&& impl, U&& v) noexcept : _impl(LEXY_MOV(impl))
82  {
83  LEXY_PRECONDITION(impl.is_success() || impl.is_recovered_error());
84  _value.emplace(LEXY_FWD(v));
85  }
86 
87  // In principle we could do a space optimization, as we can reconstruct the impl's status from
88  // the state of _value and error. Feel free to implement it.
91 
92  template <typename Reader>
93  friend class _ph;
94 };
95 } // namespace lexy
96 
97 namespace lexy
98 {
99 template <typename Reader>
100 class _ph
101 {
102  using iterator = typename Reader::iterator;
103 
104 public:
105  template <typename Input, typename Sink>
106  constexpr explicit _ph(const _detail::any_holder<const Input*>& input,
108  : _validate(input, sink)
109  {}
110 
112 
113  constexpr operator _vh<Reader>&()
114  {
115  return _validate;
116  }
117 
118  template <typename Production, typename State>
120 
121  template <typename Result, typename T>
122  constexpr auto get_result(bool rule_parse_result, T&& result) &&
123  {
125  return Result(LEXY_MOV(_validate).template get_result<validate_result>(rule_parse_result),
126  LEXY_MOV(result));
127  }
128  template <typename Result>
129  constexpr auto get_result(bool rule_parse_result) &&
130  {
132  return Result(LEXY_MOV(_validate).template get_result<validate_result>(rule_parse_result));
133  }
134 
135 private:
137 };
138 
139 template <typename State, typename Input, typename ErrorCallback>
141 {
142  const ErrorCallback* _callback;
143  State* _state = nullptr;
144 
146  using state = State;
147  using input = Input;
148 
149  template <typename T>
151 
152  constexpr explicit parse_action(const ErrorCallback& callback) : _callback(&callback) {}
153  template <typename U = State>
154  constexpr explicit parse_action(U& state, const ErrorCallback& callback)
156  {}
157 
158  template <typename Production>
159  constexpr auto operator()(Production, const Input& input) const
160  {
161  _detail::any_holder input_holder(&input);
163  auto reader = input.reader();
164  return lexy::do_action<Production, result_type>(handler(input_holder, sink), _state,
165  reader);
166  }
167 };
168 
170 template <typename Production, typename Input, typename ErrorCallback>
171 constexpr auto parse(const Input& input, const ErrorCallback& callback)
172 {
173  return parse_action<void, Input, ErrorCallback>(callback)(Production{}, input);
174 }
175 
178 template <typename Production, typename Input, typename State, typename ErrorCallback>
179 constexpr auto parse(const Input& input, State& state, const ErrorCallback& callback)
180 {
181  return parse_action<State, Input, ErrorCallback>(state, callback)(Production{}, input);
182 }
183 template <typename Production, typename Input, typename State, typename ErrorCallback>
184 constexpr auto parse(const Input& input, const State& state, const ErrorCallback& callback)
185 {
186  return parse_action<const State, Input, ErrorCallback>(state, callback)(Production{}, input);
187 }
188 } // namespace lexy
189 
190 #endif // LEXY_ACTION_PARSE_HPP_INCLUDED
191 
lexy::parse_action::input
Input input
Definition: parse.hpp:147
lexy::parse_result::_value
lexy::_detail::lazy_init< T > _value
Definition: parse.hpp:90
LEXY_MOV
#define LEXY_MOV(...)
Definition: config.hpp:29
lexy::parse_result
Definition: parse.hpp:16
base.hpp
lexy::parse_action::_state
State * _state
Definition: parse.hpp:143
lexy::_ph::get_result
constexpr auto get_result(bool rule_parse_result) &&
Definition: parse.hpp:129
lexy::parse_result::parse_result
constexpr parse_result(_impl_t &&impl, U &&v) noexcept
Definition: parse.hpp:81
lexy::_ph::iterator
typename Reader::iterator iterator
Definition: parse.hpp:102
lexy::_ph::get_result
constexpr auto get_result(bool rule_parse_result, T &&result) &&
Definition: parse.hpp:122
lexy::_vh
Definition: validate.hpp:167
LEXY_FWD
#define LEXY_FWD(...)
Definition: config.hpp:30
lexy::parse_action::_callback
const ErrorCallback * _callback
Definition: parse.hpp:142
lexy::parse_result::errors
constexpr const auto & errors() const &noexcept
Definition: parse.hpp:69
bind.hpp
lexy::validate_result
Definition: validate.hpp:45
lexy
Definition: any_ref.hpp:12
LEXY_PRECONDITION
#define LEXY_PRECONDITION(Expr)
Definition: assert.hpp:36
lexy::parse_result::error_type
typename _impl_t::error_type error_type
Definition: parse.hpp:23
lexy::validate_result::is_fatal_error
constexpr bool is_fatal_error() const noexcept
Definition: validate.hpp:71
lexy::parse_action::state
State state
Definition: parse.hpp:146
lexy::parse_result::errors
constexpr auto && errors() &&noexcept
Definition: parse.hpp:73
lexy::_callback
Definition: adapter.hpp:12
lexy::callback
constexpr auto callback(Fns &&... fns)
Creates a callback.
Definition: adapter.hpp:48
lexy::parse_action::parse_action
constexpr parse_action(U &state, const ErrorCallback &callback)
Definition: parse.hpp:154
lexy::validate_result::error_count
constexpr std::size_t error_count() const noexcept
Definition: validate.hpp:76
lexy::parse_action::parse_action
constexpr parse_action(const ErrorCallback &callback)
Definition: parse.hpp:152
lexy::parse_result::error_count
constexpr std::size_t error_count() const noexcept
Definition: parse.hpp:64
lexy::parse
constexpr auto parse(const Input &input, const ErrorCallback &callback)
Parses the production into a value, invoking the callback on error.
Definition: parse.hpp:171
lexy::_get_error_sink
constexpr auto _get_error_sink(const Callback &callback)
Definition: validate.hpp:19
lexy::production_value_callback
Definition: grammar.hpp:296
lexy::parse_result::is_fatal_error
constexpr bool is_fatal_error() const noexcept
Definition: parse.hpp:43
lexy::_detail::lazy_init
Definition: lazy_init.hpp:109
lexy::validate_result::is_success
constexpr bool is_success() const noexcept
Definition: validate.hpp:59
lexy::validate_result::errors
constexpr const auto & errors() const &noexcept
Definition: validate.hpp:86
lexy::parse_result::has_value
constexpr bool has_value() const noexcept
Definition: parse.hpp:49
invoke.hpp
lexy::_ph::_ph
constexpr _ph(const _detail::any_holder< const Input * > &input, _detail::any_holder< Sink > &sink)
Definition: parse.hpp:106
lexy::parse_result::is_success
constexpr bool is_success() const noexcept
Definition: parse.hpp:31
lexy::parse_result::error_callback
ErrorCallback error_callback
Definition: parse.hpp:22
lexy::validate_result::is_error
constexpr bool is_error() const noexcept
Definition: validate.hpp:63
lexy::parse_result::value_type
T value_type
Definition: parse.hpp:21
lexy::_ph
Definition: parse.hpp:100
validate.hpp
lexy::parse_result::parse_result
constexpr parse_result(_impl_t &&impl) noexcept
Definition: parse.hpp:79
lexy::parse_result::is_recovered_error
constexpr bool is_recovered_error() const noexcept
Definition: parse.hpp:39
lexy::_ph::_validate
_vh< Reader > _validate
Definition: parse.hpp:136
lexy::validate_result::is_recovered_error
constexpr bool is_recovered_error() const noexcept
Definition: validate.hpp:67
lexy::parse_action
Definition: parse.hpp:140
BT::Result
Expected< std::monostate > Result
Definition: basic_types.h:333
lexy::parse_result::value
constexpr decltype(auto) value() const &noexcept
Definition: parse.hpp:54
base.hpp
lexy::validate_result::error_type
typename _sink_t::return_type error_type
Definition: validate.hpp:51
lexy::_vh::event_handler
Definition: validate.hpp:176
lexy::parse_action::operator()
constexpr auto operator()(Production, const Input &input) const
Definition: parse.hpp:159
lexy::parse_action::handler
_ph< lexy::input_reader< Input > > handler
Definition: parse.hpp:145
lexy::_detail::any_holder< const Input * >
lexy::_ph::event_handler
typename _vh< Reader >::event_handler event_handler
Definition: parse.hpp:111
lexy::parse_result::_impl
_impl_t _impl
Definition: parse.hpp:89
lexy::parse_result::is_error
constexpr bool is_error() const noexcept
Definition: parse.hpp:35


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17