optional_rule.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/url
  8. //
  9. #ifndef BOOST_URL_GRAMMAR_OPTIONAL_RULE_HPP
  10. #define BOOST_URL_GRAMMAR_OPTIONAL_RULE_HPP
  11. #include <boost/url/detail/config.hpp>
  12. #include <boost/url/optional.hpp>
  13. #include <boost/url/error_types.hpp>
  14. #include <boost/core/empty_value.hpp>
  15. #include <boost/assert.hpp>
  16. namespace boost {
  17. namespace urls {
  18. namespace grammar {
  19. /** Match a rule, or the empty string
  20. Optional BNF elements are denoted with
  21. square brackets. If the specified rule
  22. returns any error it is treated as if
  23. the rule did not match.
  24. @par Value Type
  25. @code
  26. using value_type = optional< typename Rule::value_type >;
  27. @endcode
  28. @par Example
  29. Rules are used with the function @ref grammar::parse.
  30. @code
  31. system::result< optional< core::string_view > > rv = parse( "", optional_rule( token_rule( alpha_chars ) ) );
  32. @endcode
  33. @par BNF
  34. @code
  35. optional = [ rule ]
  36. @endcode
  37. @par Specification
  38. @li <a href="https://datatracker.ietf.org/doc/html/rfc5234#section-3.8"
  39. >3.8. Optional Sequence (rfc5234)</a>
  40. @param r The rule to match
  41. @see
  42. @ref alpha_chars,
  43. @ref parse,
  44. @ref optional,
  45. @ref token_rule.
  46. */
  47. #ifdef BOOST_URL_DOCS
  48. template<class Rule>
  49. constexpr
  50. __implementation_defined__
  51. optional_rule( Rule r ) noexcept;
  52. #else
  53. template<class Rule>
  54. struct optional_rule_t
  55. : private empty_value<Rule>
  56. {
  57. using value_type = boost::optional<
  58. typename Rule::value_type>;
  59. system::result<value_type>
  60. parse(
  61. char const*& it,
  62. char const* end) const;
  63. template<class R_>
  64. friend
  65. constexpr
  66. auto
  67. optional_rule(
  68. R_ const& r) ->
  69. optional_rule_t<R_>;
  70. private:
  71. constexpr
  72. optional_rule_t(
  73. Rule const& r) noexcept
  74. : empty_value<Rule>(
  75. empty_init,
  76. r)
  77. {
  78. }
  79. };
  80. template<class Rule>
  81. auto
  82. constexpr
  83. optional_rule(
  84. Rule const& r) ->
  85. optional_rule_t<Rule>
  86. {
  87. return { r };
  88. }
  89. #endif
  90. } // grammar
  91. } // urls
  92. } // boost
  93. #include <boost/url/grammar/impl/optional_rule.hpp>
  94. #endif