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/http_proto
8  
// Official repository: https://github.com/boostorg/http_proto
8  
//
9  
//
9  

10  

10  
#ifndef BOOST_URL_GRAMMAR_TOKEN_RULE_HPP
11  
#ifndef BOOST_URL_GRAMMAR_TOKEN_RULE_HPP
11  
#define BOOST_URL_GRAMMAR_TOKEN_RULE_HPP
12  
#define BOOST_URL_GRAMMAR_TOKEN_RULE_HPP
12  

13  

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

20  

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

24  

24  
namespace implementation_defined {
25  
namespace implementation_defined {
25  
template<class CharSet>
26  
template<class CharSet>
26  
struct token_rule_t
27  
struct token_rule_t
27  
    : private empty_value<CharSet>
28  
    : private empty_value<CharSet>
28  
{
29  
{
29  
    using value_type = core::string_view;
30  
    using value_type = core::string_view;
30  

31  

31  
    static_assert(
32  
    static_assert(
32  
        is_charset<CharSet>::value,
33  
        is_charset<CharSet>::value,
33  
        "CharSet requirements not met");
34  
        "CharSet requirements not met");
34  

35  

 
36 +
    BOOST_URL_CXX14_CONSTEXPR
35  
    auto
37  
    auto
36  
    parse(
38  
    parse(
37  
        char const*& it,
39  
        char const*& it,
38  
        char const* end
40  
        char const* end
39  
            ) const noexcept ->
41  
            ) const noexcept ->
40  
        system::result<value_type>;
42  
        system::result<value_type>;
41  

43  

42  
    constexpr
44  
    constexpr
43  
    token_rule_t(
45  
    token_rule_t(
44  
        CharSet const& cs) noexcept
46  
        CharSet const& cs) noexcept
45  
        : empty_value<CharSet>(
47  
        : empty_value<CharSet>(
46  
            empty_init, cs)
48  
            empty_init, cs)
47  
    {
49  
    {
48  
    }
50  
    }
49  

51  

50  
    template<class CS = CharSet>
52  
    template<class CS = CharSet>
51  
    constexpr
53  
    constexpr
52  
    token_rule_t(
54  
    token_rule_t(
53  
        typename std::enable_if<
55  
        typename std::enable_if<
54  
            std::is_default_constructible<CS>::value,
56  
            std::is_default_constructible<CS>::value,
55  
            int>::type = 0) noexcept
57  
            int>::type = 0) noexcept
56  
        : empty_value<CharSet>(
58  
        : empty_value<CharSet>(
57  
            empty_init)
59  
            empty_init)
58  
    {
60  
    {
59  
    }
61  
    }
60  
};
62  
};
61  
}
63  
}
62  

64  

63  
/** Match a non-empty string of characters from a set
65  
/** Match a non-empty string of characters from a set
64  

66  

65  
    If there is no more input, the error code
67  
    If there is no more input, the error code
66  
    @ref error::need_more is returned.
68  
    @ref error::need_more is returned.
67  

69  

68  
    @par Value Type
70  
    @par Value Type
69  
    @code
71  
    @code
70  
    using value_type = core::string_view;
72  
    using value_type = core::string_view;
71  
    @endcode
73  
    @endcode
72  

74  

73  
    @par Example
75  
    @par Example
74  
    Rules are used with the function @ref parse.
76  
    Rules are used with the function @ref parse.
75  
    @code
77  
    @code
76  
    system::result< core::string_view > rv = parse( "abcdef", token_rule( alpha_chars ) );
78  
    system::result< core::string_view > rv = parse( "abcdef", token_rule( alpha_chars ) );
77  
    @endcode
79  
    @endcode
78  

80  

79  
    @par BNF
81  
    @par BNF
80  
    @code
82  
    @code
81  
    token     = 1*( ch )
83  
    token     = 1*( ch )
82  
    @endcode
84  
    @endcode
83  

85  

84  
    @param cs The character set to use
86  
    @param cs The character set to use
85  
    @return The token rule
87  
    @return The token rule
86  

88  

87  
    @see
89  
    @see
88  
        @ref alpha_chars,
90  
        @ref alpha_chars,
89  
        @ref parse.
91  
        @ref parse.
90  
*/
92  
*/
91  
template<BOOST_URL_CONSTRAINT(CharSet) CS>
93  
template<BOOST_URL_CONSTRAINT(CharSet) CS>
92  
constexpr
94  
constexpr
93  
auto
95  
auto
94  
token_rule(
96  
token_rule(
95  
    CS const& cs) noexcept ->
97  
    CS const& cs) noexcept ->
96  
        implementation_defined::token_rule_t<CS>
98  
        implementation_defined::token_rule_t<CS>
97  
{
99  
{
98  
    return {cs};
100  
    return {cs};
99  
}
101  
}
100  

102  

101  
/** Match a non-empty string of characters from a default-constructible set
103  
/** Match a non-empty string of characters from a default-constructible set
102  

104  

103  
    This overload is only available when CharSet is
105  
    This overload is only available when CharSet is
104  
    default constructible.
106  
    default constructible.
105  

107  

106  
    If there is no more input, the error code
108  
    If there is no more input, the error code
107  
    @ref error::need_more is returned.
109  
    @ref error::need_more is returned.
108  

110  

109  
    @par Value Type
111  
    @par Value Type
110  
    @code
112  
    @code
111  
    using value_type = core::string_view;
113  
    using value_type = core::string_view;
112  
    @endcode
114  
    @endcode
113  

115  

114  
    @par Example
116  
    @par Example
115  
    Rules are used with the function @ref parse.
117  
    Rules are used with the function @ref parse.
116  
    @code
118  
    @code
117  
    system::result< core::string_view > rv = parse( "abcdef", token_rule<alpha_chars_t>() );
119  
    system::result< core::string_view > rv = parse( "abcdef", token_rule<alpha_chars_t>() );
118  
    @endcode
120  
    @endcode
119  

121  

120  
    @par BNF
122  
    @par BNF
121  
    @code
123  
    @code
122  
    token     = 1*( ch )
124  
    token     = 1*( ch )
123  
    @endcode
125  
    @endcode
124  

126  

125  
    @tparam CharSet The character set type to use
127  
    @tparam CharSet The character set type to use
126  
    @return The token rule
128  
    @return The token rule
127  

129  

128  
    @see
130  
    @see
129  
        @ref alpha_chars,
131  
        @ref alpha_chars,
130  
        @ref parse.
132  
        @ref parse.
131  
*/
133  
*/
132  
template<BOOST_URL_CONSTRAINT(CharSet) CharSet>
134  
template<BOOST_URL_CONSTRAINT(CharSet) CharSet>
133  
constexpr
135  
constexpr
134  
auto
136  
auto
135  
token_rule() noexcept ->
137  
token_rule() noexcept ->
136  
    typename std::enable_if<
138  
    typename std::enable_if<
137  
        std::is_default_constructible<CharSet>::value,
139  
        std::is_default_constructible<CharSet>::value,
138  
        implementation_defined::token_rule_t<CharSet>>::type
140  
        implementation_defined::token_rule_t<CharSet>>::type
139  
{
141  
{
140  
    return implementation_defined::token_rule_t<CharSet>();
142  
    return implementation_defined::token_rule_t<CharSet>();
141  
}
143  
}
142  

144  

143  
} // grammar
145  
} // grammar
144  
} // urls
146  
} // urls
145  
} // boost
147  
} // boost
146  

148  

147  
#include <boost/url/grammar/impl/token_rule.hpp>
149  
#include <boost/url/grammar/impl/token_rule.hpp>
148  

150  

149  
#endif
151  
#endif