1  
//
1  
//
2  
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
2  
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
3  
// Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
3  
// Copyright (c) 2022 Alan de Freitas (alandefreitas@gmail.com)
4  
//
4  
//
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  
//
7  
//
8  
// Official repository: https://github.com/boostorg/url
8  
// Official repository: https://github.com/boostorg/url
9  
//
9  
//
10  

10  

11  
#ifndef BOOST_URL_GRAMMAR_PARSE_HPP
11  
#ifndef BOOST_URL_GRAMMAR_PARSE_HPP
12  
#define BOOST_URL_GRAMMAR_PARSE_HPP
12  
#define BOOST_URL_GRAMMAR_PARSE_HPP
13  

13  

14  
#include <boost/url/detail/config.hpp>
14  
#include <boost/url/detail/config.hpp>
15  
#include <boost/url/error_types.hpp>
15  
#include <boost/url/error_types.hpp>
16  
#include <boost/core/detail/string_view.hpp>
16  
#include <boost/core/detail/string_view.hpp>
17  
#include <boost/url/grammar/type_traits.hpp>
17  
#include <boost/url/grammar/type_traits.hpp>
18  

18  

19  
namespace boost {
19  
namespace boost {
20  
namespace urls {
20  
namespace urls {
21  
namespace grammar {
21  
namespace grammar {
22  

22  

23  
//------------------------------------------------
23  
//------------------------------------------------
24  

24  

25  
/** Parse a character buffer using a rule
25  
/** Parse a character buffer using a rule
26  

26  

27  
    @param it A pointer to the start. The
27  
    @param it A pointer to the start. The
28  
    caller's variable is changed to
28  
    caller's variable is changed to
29  
    reflect the amount of input consumed.
29  
    reflect the amount of input consumed.
30  

30  

31  
    @param end A pointer to the end.
31  
    @param end A pointer to the end.
32  

32  

33  
    @param r The rule to use
33  
    @param r The rule to use
34  

34  

35  
    @return The parsed value upon success,
35  
    @return The parsed value upon success,
36  
    otherwise an error.
36  
    otherwise an error.
37  
*/
37  
*/
38  
template<BOOST_URL_CONSTRAINT(Rule) R>
38  
template<BOOST_URL_CONSTRAINT(Rule) R>
39  
BOOST_URL_CXX14_CONSTEXPR
39  
BOOST_URL_CXX14_CONSTEXPR
40  
system::result<typename R::value_type>
40  
system::result<typename R::value_type>
41  
parse(
41  
parse(
42  
    char const*& it,
42  
    char const*& it,
43  
    char const* end,
43  
    char const* end,
44  
    R const& r);
44  
    R const& r);
45  

45  

46  
/** Parse a character buffer using a rule
46  
/** Parse a character buffer using a rule
47  

47  

48  
    This function parses a complete string into
48  
    This function parses a complete string into
49  
    the specified sequence of rules. If the
49  
    the specified sequence of rules. If the
50  
    string is not completely consumed, an
50  
    string is not completely consumed, an
51  
    error is returned instead.
51  
    error is returned instead.
52  

52  

53  
    @param s The input string
53  
    @param s The input string
54  

54  

55  
    @param r The rule to use
55  
    @param r The rule to use
56  

56  

57  
    @return The parsed value upon success,
57  
    @return The parsed value upon success,
58  
    otherwise an error.
58  
    otherwise an error.
59  
*/
59  
*/
60  
template<BOOST_URL_CONSTRAINT(Rule) R>
60  
template<BOOST_URL_CONSTRAINT(Rule) R>
61  
BOOST_URL_CXX14_CONSTEXPR
61  
BOOST_URL_CXX14_CONSTEXPR
62  
system::result<typename R::value_type>
62  
system::result<typename R::value_type>
63  
parse(
63  
parse(
64  
    core::string_view s,
64  
    core::string_view s,
65  
    R const& r);
65  
    R const& r);
66  

66  

67  
//------------------------------------------------
67  
//------------------------------------------------
68  

68  

69  
namespace implementation_defined {
69  
namespace implementation_defined {
70  
template<class Rule>
70  
template<class Rule>
71  
struct rule_ref
71  
struct rule_ref
72  
{
72  
{
73  
    Rule const& r_;
73  
    Rule const& r_;
74  

74  

75  
    using value_type =
75  
    using value_type =
76  
        typename Rule::value_type;
76  
        typename Rule::value_type;
77  

77  

78  
    system::result<value_type>
78  
    system::result<value_type>
79  
    parse(
79  
    parse(
80  
        char const*& it,
80  
        char const*& it,
81  
        char const* end) const
81  
        char const* end) const
82  
    {
82  
    {
83  
        return r_.parse(it, end);
83  
        return r_.parse(it, end);
84  
    }
84  
    }
85  
};
85  
};
86  
} // implementation_defined
86  
} // implementation_defined
87  

87  

88  
/** Return a reference to a rule
88  
/** Return a reference to a rule
89  

89  

90  
    This function returns a rule which
90  
    This function returns a rule which
91  
    references the specified object. This is
91  
    references the specified object. This is
92  
    used to reduce the number of bytes of
92  
    used to reduce the number of bytes of
93  
    storage (`sizeof`) required by a combinator
93  
    storage (`sizeof`) required by a combinator
94  
    when it stores a copy of the object.
94  
    when it stores a copy of the object.
95  
    <br>
95  
    <br>
96  
    Ownership of the object is not transferred;
96  
    Ownership of the object is not transferred;
97  
    the caller is responsible for ensuring the
97  
    the caller is responsible for ensuring the
98  
    lifetime of the object is extended until it
98  
    lifetime of the object is extended until it
99  
    is no longer referenced. For best results,
99  
    is no longer referenced. For best results,
100  
    `ref` should only be used with compile-time
100  
    `ref` should only be used with compile-time
101  
    constants.
101  
    constants.
102  

102  

103  
    @param r The rule to use
103  
    @param r The rule to use
104  
    @return The rule as a reference type
104  
    @return The rule as a reference type
105  
*/
105  
*/
106  
template<BOOST_URL_CONSTRAINT(Rule) R>
106  
template<BOOST_URL_CONSTRAINT(Rule) R>
107  
constexpr
107  
constexpr
108  
typename std::enable_if<
108  
typename std::enable_if<
109  
    is_rule<R>::value &&
109  
    is_rule<R>::value &&
110  
    ! std::is_same<R,
110  
    ! std::is_same<R,
111  
        implementation_defined::rule_ref<R> >::value,
111  
        implementation_defined::rule_ref<R> >::value,
112  
    implementation_defined::rule_ref<R> >::type
112  
    implementation_defined::rule_ref<R> >::type
113  
ref(R const& r) noexcept
113  
ref(R const& r) noexcept
114  
{
114  
{
115  
    return implementation_defined::rule_ref<R>{r};
115  
    return implementation_defined::rule_ref<R>{r};
116  
}
116  
}
117  

117  

118  
#ifndef BOOST_URL_DOCS
118  
#ifndef BOOST_URL_DOCS
119  
#ifndef BOOST_URL_MRDOCS
119  
#ifndef BOOST_URL_MRDOCS
120  
// If you get a compile error here it
120  
// If you get a compile error here it
121  
// means you called ref with something
121  
// means you called ref with something
122  
// that is not a CharSet or Rule!
122  
// that is not a CharSet or Rule!
123  
constexpr
123  
constexpr
124  
void
124  
void
125  
ref(...) = delete;
125  
ref(...) = delete;
126  
#endif
126  
#endif
127  
#endif
127  
#endif
128  

128  

129  
} // grammar
129  
} // grammar
130  
} // urls
130  
} // urls
131  
} // boost
131  
} // boost
132  

132  

133  
#include <boost/url/grammar/impl/parse.hpp>
133  
#include <boost/url/grammar/impl/parse.hpp>
134  

134  

135  
#endif
135  
#endif