GCC Code Coverage Report


Directory: libs/url/
File: include/boost/url/grammar/optional_rule.hpp
Date: 2025-04-22 15:26:26
Exec Total Coverage
Lines: 5 5 100.0%
Functions: 7 7 100.0%
Branches: 0 0 -%

Line Branch Exec Source
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 2031 optional_rule_t(
39 Rule const& r) noexcept
40 : empty_value<Rule>(
41 empty_init,
42 2031 r)
43 {
44 2031 }
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 @return The adapted rule
77
78 @see
79 @ref alpha_chars,
80 @ref parse,
81 @ref optional,
82 @ref token_rule.
83 */
84 template<BOOST_URL_CONSTRAINT(Rule) R>
85 auto
86 constexpr
87 1967 optional_rule(
88 R const& r) ->
89 implementation_defined::optional_rule_t<R>
90 {
91 BOOST_STATIC_ASSERT(grammar::is_rule<R>::value);
92 1967 return { r };
93 }
94
95 } // grammar
96 } // urls
97 } // boost
98
99 #include <boost/url/grammar/impl/optional_rule.hpp>
100
101 #endif
102