1 /*=============================================================================
2 Copyright (c) 1998-2003 Joel de Guzman
3 http://spirit.sourceforge.net/
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 #if !defined(BOOST_SPIRIT_MATCH_HPP)
9 #define BOOST_SPIRIT_MATCH_HPP
11 #include <boost/spirit/home/classic/namespace.hpp>
12 #include <boost/spirit/home/classic/core/config.hpp>
13 #include <boost/spirit/home/classic/core/nil.hpp>
14 #include <boost/call_traits.hpp>
15 #include <boost/optional.hpp>
16 #include <boost/spirit/home/classic/core/assert.hpp>
17 #include <boost/spirit/home/classic/core/safe_bool.hpp>
18 #include <boost/spirit/home/classic/core/impl/match_attr_traits.ipp>
19 #include <boost/type_traits/add_const.hpp>
20 #include <boost/type_traits/is_reference.hpp>
22 namespace boost { namespace spirit {
24 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
26 ///////////////////////////////////////////////////////////////////////////
30 // The match holds the result of a parser. A match object evaluates
31 // to true when a successful match is found, otherwise false. The
32 // length of the match is the number of characters (or tokens) that
33 // is successfully matched. This can be queried through its length()
34 // member function. A negative value means that the match is
37 // Each parser may have an associated attribute. This attribute is
38 // also returned back to the client on a successful parse through
39 // the match object. The match's value() member function returns the
42 // A match attribute is valid:
44 // * on a successful match
45 // * when its value is set through the value(val) member function
46 // * if it is assigned or copied from a compatible match object
47 // (e.g. match<double> from match<int>) with a valid attribute.
49 // The match attribute is undefined:
51 // * on an unsuccessful match
52 // * when an attempt to copy or assign from another match object
53 // with an incompatible attribute type (e.g. match<std::string>
56 // The member function has_valid_attribute() can be queried to know if
57 // it is safe to get the match's attribute. The attribute may be set
58 // through the member function value(v) where v is the new attribute
61 ///////////////////////////////////////////////////////////////////////////
62 template <typename T = nil_t>
63 class match : public safe_bool<match<T> >
68 typedef typename boost::optional<T> optional_type;
69 typedef typename optional_type::argument_type ctor_param_t;
70 typedef typename optional_type::reference_const_type return_t;
74 explicit match(std::size_t length);
75 match(std::size_t length, ctor_param_t val);
77 bool operator!() const;
78 std::ptrdiff_t length() const;
79 bool has_valid_attribute() const;
80 return_t value() const;
81 void swap(match& other);
83 template <typename T2>
84 match(match<T2> const& other)
85 : len(other.length()), val()
87 impl::match_attr_traits<T>::copy(val, other);
90 template <typename T2>
92 operator=(match<T2> const& other)
94 impl::match_attr_traits<T>::assign(val, other);
99 template <typename MatchT>
101 concat(MatchT const& other)
103 BOOST_SPIRIT_ASSERT(*this && other);
104 len += other.length();
107 template <typename ValueT>
109 value(ValueT const& val_)
111 impl::match_attr_traits<T>::set_value(val, val_, is_reference<T>());
114 bool operator_bool() const
125 ///////////////////////////////////////////////////////////////////////////
127 // match class specialization for nil_t values
129 ///////////////////////////////////////////////////////////////////////////
131 class match<nil_t> : public safe_bool<match<nil_t> >
135 typedef nil_t attr_t;
136 typedef nil_t return_t;
139 explicit match(std::size_t length);
140 match(std::size_t length, nil_t);
142 bool operator!() const;
143 bool has_valid_attribute() const;
144 std::ptrdiff_t length() const;
147 void swap(match& other);
149 template <typename T>
150 match(match<T> const& other)
151 : len(other.length()) {}
153 template <typename T>
155 operator=(match<T> const& other)
157 len = other.length();
161 template <typename T>
163 concat(match<T> const& other)
165 BOOST_SPIRIT_ASSERT(*this && other);
166 len += other.length();
169 bool operator_bool() const
179 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
181 }} // namespace BOOST_SPIRIT_CLASSIC_NS
184 #include <boost/spirit/home/classic/core/impl/match.ipp>