optional_rule.hpp 2.3 KB

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