token_rule.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  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)
  6. //
  7. // Official repository: https://github.com/boostorg/http_proto
  8. //
  9. #ifndef BOOST_URL_GRAMMAR_TOKEN_RULE_HPP
  10. #define BOOST_URL_GRAMMAR_TOKEN_RULE_HPP
  11. #include <boost/url/detail/config.hpp>
  12. #include <boost/url/grammar/charset.hpp>
  13. #include <boost/url/error_types.hpp>
  14. #include <boost/core/detail/string_view.hpp>
  15. namespace boost {
  16. namespace urls {
  17. namespace grammar {
  18. /** Match a non-empty string of characters from a set
  19. If there is no more input, the error code
  20. @ref error::need_more is returned.
  21. @par Value Type
  22. @code
  23. using value_type = core::string_view;
  24. @endcode
  25. @par Example
  26. Rules are used with the function @ref parse.
  27. @code
  28. system::result< core::string_view > rv = parse( "abcdef", token_rule( alpha_chars ) );
  29. @endcode
  30. @par BNF
  31. @code
  32. token = 1*( ch )
  33. @endcode
  34. @param cs The character set to use
  35. @see
  36. @ref alpha_chars,
  37. @ref parse.
  38. */
  39. #ifdef BOOST_URL_DOCS
  40. template<class CharSet>
  41. constexpr
  42. __implementation_defined__
  43. token_rule(
  44. CharSet cs) noexcept;
  45. #else
  46. template<class CharSet>
  47. struct token_rule_t
  48. {
  49. using value_type = core::string_view;
  50. static_assert(
  51. is_charset<CharSet>::value,
  52. "CharSet requirements not met");
  53. auto
  54. parse(
  55. char const*& it,
  56. char const* end
  57. ) const noexcept ->
  58. system::result<value_type>;
  59. private:
  60. template<class CharSet_>
  61. friend
  62. constexpr
  63. auto
  64. token_rule(
  65. CharSet_ const&) noexcept ->
  66. token_rule_t<CharSet_>;
  67. constexpr
  68. token_rule_t(
  69. CharSet const& cs) noexcept
  70. : cs_(cs)
  71. {
  72. }
  73. CharSet const cs_;
  74. };
  75. template<class CharSet>
  76. constexpr
  77. auto
  78. token_rule(
  79. CharSet const& cs) noexcept ->
  80. token_rule_t<CharSet>
  81. {
  82. return {cs};
  83. }
  84. #endif
  85. } // grammar
  86. } // urls
  87. } // boost
  88. #include <boost/url/grammar/impl/token_rule.hpp>
  89. #endif