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

10  

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

13  

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

18  

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

22  

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

24  

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

26  

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

30  

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

32  

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

34  

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

45  

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

47  

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

52  

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

54  

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

56  

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

66  

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

68  

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

74  

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

77  

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

87  

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

89  

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

102  

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

117  

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

128  

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

132  

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

134  

132  
#endif
135  
#endif