Iros
 
Loading...
Searching...
No Matches
and_then.h
Go to the documentation of this file.
1#pragma once
2
8
9namespace di::parser {
10namespace detail {
11 template<typename Parser, typename Fun>
12 class AndThenParser : public ParserBase<AndThenParser<Parser, Fun>> {
13 public:
14 template<typename P, typename F>
15 constexpr explicit AndThenParser(InPlace, P&& parser, F&& function)
16 : m_parser(util::forward<P>(parser)), m_function(util::forward<F>(function)) {}
17
18 template<concepts::ParserContext Context, typename Value = meta::ParserValue<Context, Parser>>
20 constexpr auto parse(Context& context) const {
21 auto begin_save = container::begin(context);
22 if constexpr (concepts::LanguageVoid<Value>) {
23 return m_parser.parse(context).and_then([&] {
24 return m_function(context) | if_error([&](auto&&) {
25 context.advance(begin_save);
26 });
27 });
28 } else {
29 return m_parser.parse(context).and_then([&](Value&& value) {
30 return m_function(context, util::forward<Value>(value)) | if_error([&](auto&&) {
31 context.advance(begin_save);
32 });
33 });
34 }
35 }
36
37 private:
38 Parser m_parser;
39 Fun m_function;
40 };
41
43 template<concepts::DecayConstructible Parser, concepts::DecayConstructible Fun>
44 constexpr auto operator()(Parser&& parser, Fun&& function) const {
46 util::forward<Fun>(function));
47 }
48 };
49}
50
51constexpr inline auto and_then = detail::AndThenFunction {};
52
53template<concepts::DecayConstructible Parser, concepts::DecayConstructible Fun>
55constexpr auto operator<<(Parser&& parser, Fun&& function) {
56 return and_then(util::forward<Parser>(parser), util::forward<Fun>(function));
57}
58}
Definition parser_base.h:5
Definition and_then.h:12
constexpr auto parse(Context &context) const
Definition and_then.h:20
constexpr AndThenParser(InPlace, P &&parser, F &&function)
Definition and_then.h:15
Definition operations.h:114
Definition core.h:128
Definition parser.h:10
constexpr auto begin
Definition begin.h:44
Definition as_bool.h:8
Conditional< concepts::LanguageArray< RemoveReference< T > >, RemoveExtent< RemoveReference< T > > *, Conditional< concepts::LanguageFunction< RemoveReference< T > >, AddPointer< RemoveReference< T > >, RemoveCVRef< T > > > Decay
Definition language.h:574
Definition zstring_parser.h:10
Definition zstring_parser.h:9
constexpr auto and_then
Definition and_then.h:51
Definition vocab.h:96
constexpr auto if_error
Definition if_error.h:28
constexpr auto in_place
Definition in_place.h:8
Definition and_then.h:42
constexpr auto operator()(Parser &&parser, Fun &&function) const
Definition and_then.h:44
Definition in_place.h:4