di 0.1.0
Loading...
Searching...
No Matches
optional.h
Go to the documentation of this file.
1#pragma once
2
3#include "di/meta/util.h"
10
11namespace di::parser {
12namespace detail {
13 template<typename Parser>
14 class OptionalParser : public ParserBase<OptionalParser<Parser>> {
15 public:
16 template<typename P>
17 constexpr explicit OptionalParser(InPlace, P&& parser) : m_parser(util::forward<P>(parser)) {}
18
19 template<concepts::ParserContext Context, typename Value = meta::ParserValue<Context, Parser>>
20 requires(concepts::Parser<Parser, Context>)
21 constexpr auto parse(Context& context) const -> meta::ParserContextResult<Optional<Value>, Context> {
22 auto begin_save = container::begin(context);
23 return (m_parser.parse(context) | if_error([&](auto&&) {
24 context.advance(begin_save);
25 }))
26 .optional_value();
27 }
28
29 private:
30 Parser m_parser;
31 };
32
33 struct OptionalFunction {
34 template<concepts::DecayConstructible Parser>
35 constexpr auto operator()(Parser&& parser) const {
36 return OptionalParser<meta::Decay<Parser>>(in_place, util::forward<Parser>(parser));
37 }
38 };
39}
40
41constexpr inline auto optional = detail::OptionalFunction {};
42
43template<concepts::DecayConstructible Parser>
45constexpr auto operator-(Parser&& parser) {
46 return optional(util::forward<Parser>(parser));
47}
48}
Definition operations.h:114
constexpr auto begin
Definition begin.h:52
vocab::Expected< T, meta::ParserContextError< Context > > ParserContextResult
Definition parser_context_result.h:8
Definition code_point_parser.h:10
constexpr auto optional
Definition optional.h:41
constexpr auto parse
Definition parse.h:23
constexpr auto if_error
Definition if_error.h:28
constexpr auto in_place
Definition in_place.h:8