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

9  

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

12  

13  
#include <boost/url/detail/config.hpp>
13  
#include <boost/url/detail/config.hpp>
14  
#include <boost/url/grammar/charset.hpp>
14  
#include <boost/url/grammar/charset.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/core/empty_value.hpp>
17  
#include <boost/core/empty_value.hpp>
18  
#include <type_traits>
18  
#include <type_traits>
19  

19  

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

23  

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

30  

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

34  

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

41  

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

49  

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

62  

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

64  

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

67  

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

72  

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

78  

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

83  

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

86  

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

100  

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

102  

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

105  

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

108  

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

113  

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

119  

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

124  

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

127  

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

142  

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

146  

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

148  

149  
#endif
149  
#endif