parse.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_PARSE_HPP
  10. #define BOOST_URL_GRAMMAR_PARSE_HPP
  11. #include <boost/url/detail/config.hpp>
  12. #include <boost/url/error_types.hpp>
  13. #include <boost/core/detail/string_view.hpp>
  14. #include <boost/url/grammar/type_traits.hpp>
  15. namespace boost {
  16. namespace urls {
  17. namespace grammar {
  18. //------------------------------------------------
  19. /** Parse a character buffer using a rule
  20. @param it A pointer to the start. The
  21. caller's variable is changed to
  22. reflect the amount of input consumed.
  23. @param end A pointer to the end.
  24. @param r The rule to use
  25. @return The parsed value upon success,
  26. otherwise an error.
  27. */
  28. template<BOOST_URL_CONSTRAINT(Rule) R>
  29. system::result<typename R::value_type>
  30. parse(
  31. char const*& it,
  32. char const* end,
  33. R const& r);
  34. /** Parse a character buffer using a rule
  35. This function parses a complete string into
  36. the specified sequence of rules. If the
  37. string is not completely consumed, an
  38. error is returned instead.
  39. @param s The input string
  40. @param r The rule to use
  41. @return The parsed value upon success,
  42. otherwise an error.
  43. */
  44. template<BOOST_URL_CONSTRAINT(Rule) R>
  45. system::result<typename R::value_type>
  46. parse(
  47. core::string_view s,
  48. R const& r);
  49. //------------------------------------------------
  50. namespace implementation_defined {
  51. template<class Rule>
  52. struct rule_ref
  53. {
  54. Rule const& r_;
  55. using value_type =
  56. typename Rule::value_type;
  57. system::result<value_type>
  58. parse(
  59. char const*& it,
  60. char const* end) const
  61. {
  62. return r_.parse(it, end);
  63. }
  64. };
  65. } // implementation_defined
  66. /** Return a reference to a rule
  67. This function returns a rule which
  68. references the specified object. This is
  69. used to reduce the number of bytes of
  70. storage (`sizeof`) required by a combinator
  71. when it stores a copy of the object.
  72. <br>
  73. Ownership of the object is not transferred;
  74. the caller is responsible for ensuring the
  75. lifetime of the object is extended until it
  76. is no longer referenced. For best results,
  77. `ref` should only be used with compile-time
  78. constants.
  79. @param r The rule to use
  80. @return The rule as a reference type
  81. */
  82. template<BOOST_URL_CONSTRAINT(Rule) R>
  83. constexpr
  84. typename std::enable_if<
  85. is_rule<R>::value &&
  86. ! std::is_same<R,
  87. implementation_defined::rule_ref<R> >::value,
  88. implementation_defined::rule_ref<R> >::type
  89. ref(R const& r) noexcept
  90. {
  91. return implementation_defined::rule_ref<R>{r};
  92. }
  93. #ifndef BOOST_URL_DOCS
  94. #ifndef BOOST_URL_MRDOCS
  95. // If you get a compile error here it
  96. // means you called ref with something
  97. // that is not a CharSet or Rule!
  98. constexpr
  99. void
  100. ref(...) = delete;
  101. #endif
  102. #endif
  103. } // grammar
  104. } // urls
  105. } // boost
  106. #include <boost/url/grammar/impl/parse.hpp>
  107. #endif