Line data Source code
1 : //
2 : // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3 : //
4 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 : //
7 : // Official repository: https://github.com/boostorg/url
8 : //
9 :
10 : #ifndef BOOST_URL_GRAMMAR_OPTIONAL_RULE_HPP
11 : #define BOOST_URL_GRAMMAR_OPTIONAL_RULE_HPP
12 :
13 : #include <boost/url/detail/config.hpp>
14 : #include <boost/url/optional.hpp>
15 : #include <boost/url/error_types.hpp>
16 : #include <boost/url/grammar/type_traits.hpp>
17 : #include <boost/core/empty_value.hpp>
18 : #include <boost/assert.hpp>
19 :
20 : namespace boost {
21 : namespace urls {
22 : namespace grammar {
23 :
24 : namespace implementation_defined {
25 : template<class Rule>
26 : struct optional_rule_t
27 : : private empty_value<Rule>
28 : {
29 : using value_type = boost::optional<
30 : typename Rule::value_type>;
31 :
32 : system::result<value_type>
33 : parse(
34 : char const*& it,
35 : char const* end) const;
36 :
37 : constexpr
38 1967 : optional_rule_t(
39 : Rule const& r) noexcept
40 : : empty_value<Rule>(
41 : empty_init,
42 1967 : r)
43 : {
44 1967 : }
45 : };
46 : } // implementation_defined
47 :
48 : /** Match a rule, or the empty string
49 :
50 : Optional BNF elements are denoted with
51 : square brackets. If the specified rule
52 : returns any error it is treated as if
53 : the rule did not match.
54 :
55 : @par Value Type
56 : @code
57 : using value_type = optional< typename Rule::value_type >;
58 : @endcode
59 :
60 : @par Example
61 : Rules are used with the function @ref grammar::parse.
62 : @code
63 : system::result< optional< core::string_view > > rv = parse( "", optional_rule( token_rule( alpha_chars ) ) );
64 : @endcode
65 :
66 : @par BNF
67 : @code
68 : optional = [ rule ]
69 : @endcode
70 :
71 : @par Specification
72 : @li <a href="https://datatracker.ietf.org/doc/html/rfc5234#section-3.8"
73 : >3.8. Optional Sequence (rfc5234)</a>
74 :
75 : @param r The rule to match
76 :
77 : @see
78 : @ref alpha_chars,
79 : @ref parse,
80 : @ref optional,
81 : @ref token_rule.
82 : */
83 : template<BOOST_URL_CONSTRAINT(Rule) R>
84 : auto
85 : constexpr
86 1967 : optional_rule(
87 : R const& r) ->
88 : implementation_defined::optional_rule_t<R>
89 : {
90 : BOOST_STATIC_ASSERT(grammar::is_rule<R>::value);
91 1967 : return { r };
92 : }
93 :
94 : } // grammar
95 : } // urls
96 : } // boost
97 :
98 : #include <boost/url/grammar/impl/optional_rule.hpp>
99 :
100 : #endif
|