parse.hpp 3.0 KB

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