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  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
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)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/boostorg/url
7  
// Official repository: https://github.com/boostorg/url
8  
//
8  
//
9  

9  

10  
#ifndef BOOST_URL_GRAMMAR_PARSE_HPP
10  
#ifndef BOOST_URL_GRAMMAR_PARSE_HPP
11  
#define BOOST_URL_GRAMMAR_PARSE_HPP
11  
#define BOOST_URL_GRAMMAR_PARSE_HPP
12  

12  

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

17  

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

21  

22  
//------------------------------------------------
22  
//------------------------------------------------
23  

23  

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

25  

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

29  

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

31  

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

33  

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

43  

44  
/** Parse a character buffer using a rule
44  
/** Parse a character buffer using a rule
45  

45  

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

50  

51  
    @param s The input string
51  
    @param s The input string
52  

52  

53  
    @param r The rule to use
53  
    @param r The rule to use
54  

54  

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

63  

64  
//------------------------------------------------
64  
//------------------------------------------------
65  

65  

66  
namespace implementation_defined {
66  
namespace implementation_defined {
67  
template<class Rule>
67  
template<class Rule>
68  
struct rule_ref
68  
struct rule_ref
69  
{
69  
{
70  
    Rule const& r_;
70  
    Rule const& r_;
71  

71  

72  
    using value_type =
72  
    using value_type =
73  
        typename Rule::value_type;
73  
        typename Rule::value_type;
74  

74  

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

84  

85  
/** Return a reference to a rule
85  
/** Return a reference to a rule
86  

86  

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

99  

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

114  

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

125  

126  
} // grammar
126  
} // grammar
127  
} // urls
127  
} // urls
128  
} // boost
128  
} // boost
129  

129  

130  
#include <boost/url/grammar/impl/parse.hpp>
130  
#include <boost/url/grammar/impl/parse.hpp>
131  

131  

132  
#endif
132  
#endif