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