match.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*=============================================================================
  2. Copyright (c) 1998-2003 Joel de Guzman
  3. http://spirit.sourceforge.net/
  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. #if !defined(BOOST_SPIRIT_MATCH_HPP)
  8. #define BOOST_SPIRIT_MATCH_HPP
  9. #include <boost/spirit/home/classic/namespace.hpp>
  10. #include <boost/spirit/home/classic/core/config.hpp>
  11. #include <boost/spirit/home/classic/core/nil.hpp>
  12. #include <boost/call_traits.hpp>
  13. #include <boost/optional.hpp>
  14. #include <boost/spirit/home/classic/core/assert.hpp>
  15. #include <boost/spirit/home/classic/core/safe_bool.hpp>
  16. #include <boost/spirit/home/classic/core/impl/match_attr_traits.ipp>
  17. #include <boost/type_traits/add_const.hpp>
  18. #include <boost/type_traits/add_reference.hpp>
  19. #include <boost/type_traits/conditional.hpp>
  20. #include <boost/type_traits/is_reference.hpp>
  21. namespace boost { namespace spirit {
  22. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  23. ///////////////////////////////////////////////////////////////////////////
  24. //
  25. // match class
  26. //
  27. // The match holds the result of a parser. A match object evaluates
  28. // to true when a successful match is found, otherwise false. The
  29. // length of the match is the number of characters (or tokens) that
  30. // is successfully matched. This can be queried through its length()
  31. // member function. A negative value means that the match is
  32. // unsucessful.
  33. //
  34. // Each parser may have an associated attribute. This attribute is
  35. // also returned back to the client on a successful parse through
  36. // the match object. The match's value() member function returns the
  37. // match's attribute.
  38. //
  39. // A match attribute is valid:
  40. //
  41. // * on a successful match
  42. // * when its value is set through the value(val) member function
  43. // * if it is assigned or copied from a compatible match object
  44. // (e.g. match<double> from match<int>) with a valid attribute.
  45. //
  46. // The match attribute is undefined:
  47. //
  48. // * on an unsuccessful match
  49. // * when an attempt to copy or assign from another match object
  50. // with an incompatible attribute type (e.g. match<std::string>
  51. // from match<int>).
  52. //
  53. // The member function has_valid_attribute() can be queried to know if
  54. // it is safe to get the match's attribute. The attribute may be set
  55. // through the member function value(v) where v is the new attribute
  56. // value.
  57. //
  58. ///////////////////////////////////////////////////////////////////////////
  59. template <typename T = nil_t>
  60. class match : public safe_bool<match<T> >
  61. {
  62. typedef typename
  63. conditional<
  64. is_reference<T>::value
  65. , T
  66. , typename add_reference<
  67. typename add_const<T>::type
  68. >::type
  69. >::type attr_ref_t;
  70. public:
  71. typedef typename boost::optional<T> optional_type;
  72. typedef attr_ref_t ctor_param_t;
  73. typedef attr_ref_t return_t;
  74. typedef T attr_t;
  75. match();
  76. explicit match(std::size_t length);
  77. match(std::size_t length, ctor_param_t val);
  78. bool operator!() const;
  79. std::ptrdiff_t length() const;
  80. bool has_valid_attribute() const;
  81. return_t value() const;
  82. void swap(match& other);
  83. template <typename T2>
  84. match(match<T2> const& other)
  85. : len(other.length()), val()
  86. {
  87. impl::match_attr_traits<T>::copy(val, other);
  88. }
  89. template <typename T2>
  90. match&
  91. operator=(match<T2> const& other)
  92. {
  93. impl::match_attr_traits<T>::assign(val, other);
  94. len = other.length();
  95. return *this;
  96. }
  97. template <typename MatchT>
  98. void
  99. concat(MatchT const& other)
  100. {
  101. BOOST_SPIRIT_ASSERT(*this && other);
  102. len += other.length();
  103. }
  104. template <typename ValueT>
  105. void
  106. value(ValueT const& val_)
  107. {
  108. impl::match_attr_traits<T>::set_value(val, val_, is_reference<T>());
  109. }
  110. bool operator_bool() const
  111. {
  112. return len >= 0;
  113. }
  114. private:
  115. std::ptrdiff_t len;
  116. optional_type val;
  117. };
  118. ///////////////////////////////////////////////////////////////////////////
  119. //
  120. // match class specialization for nil_t values
  121. //
  122. ///////////////////////////////////////////////////////////////////////////
  123. template <>
  124. class match<nil_t> : public safe_bool<match<nil_t> >
  125. {
  126. public:
  127. typedef nil_t attr_t;
  128. typedef nil_t return_t;
  129. match();
  130. explicit match(std::size_t length);
  131. match(std::size_t length, nil_t);
  132. bool operator!() const;
  133. bool has_valid_attribute() const;
  134. std::ptrdiff_t length() const;
  135. nil_t value() const;
  136. void value(nil_t);
  137. void swap(match& other);
  138. template <typename T>
  139. match(match<T> const& other)
  140. : len(other.length()) {}
  141. template <typename T>
  142. match<>&
  143. operator=(match<T> const& other)
  144. {
  145. len = other.length();
  146. return *this;
  147. }
  148. template <typename T>
  149. void
  150. concat(match<T> const& other)
  151. {
  152. BOOST_SPIRIT_ASSERT(*this && other);
  153. len += other.length();
  154. }
  155. bool operator_bool() const
  156. {
  157. return len >= 0;
  158. }
  159. private:
  160. std::ptrdiff_t len;
  161. };
  162. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  163. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  164. #endif
  165. #include <boost/spirit/home/classic/core/impl/match.ipp>