]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 1998-2003 Joel de Guzman | |
3 | http://spirit.sourceforge.net/ | |
4 | ||
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 | |
10 | ||
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> | |
21 | ||
22 | namespace boost { namespace spirit { | |
23 | ||
24 | BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN | |
25 | ||
26 | /////////////////////////////////////////////////////////////////////////// | |
27 | // | |
28 | // match class | |
29 | // | |
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 | |
35 | // unsucessful. | |
36 | // | |
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 | |
40 | // match's attribute. | |
41 | // | |
42 | // A match attribute is valid: | |
43 | // | |
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. | |
48 | // | |
49 | // The match attribute is undefined: | |
50 | // | |
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> | |
54 | // from match<int>). | |
55 | // | |
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 | |
59 | // value. | |
60 | // | |
61 | /////////////////////////////////////////////////////////////////////////// | |
62 | template <typename T = nil_t> | |
63 | class match : public safe_bool<match<T> > | |
64 | { | |
65 | ||
66 | public: | |
67 | ||
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; | |
71 | typedef T attr_t; | |
72 | ||
73 | match(); | |
74 | explicit match(std::size_t length); | |
75 | match(std::size_t length, ctor_param_t val); | |
76 | ||
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); | |
82 | ||
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 | ||
90 | template <typename T2> | |
91 | match& | |
92 | operator=(match<T2> const& other) | |
93 | { | |
94 | impl::match_attr_traits<T>::assign(val, other); | |
95 | len = other.length(); | |
96 | return *this; | |
97 | } | |
98 | ||
99 | template <typename MatchT> | |
100 | void | |
101 | concat(MatchT const& other) | |
102 | { | |
103 | BOOST_SPIRIT_ASSERT(*this && other); | |
104 | len += other.length(); | |
105 | } | |
106 | ||
107 | template <typename ValueT> | |
108 | void | |
109 | value(ValueT const& val_) | |
110 | { | |
111 | impl::match_attr_traits<T>::set_value(val, val_, is_reference<T>()); | |
112 | } | |
113 | ||
114 | bool operator_bool() const | |
115 | { | |
116 | return len >= 0; | |
117 | } | |
118 | ||
119 | private: | |
120 | ||
121 | std::ptrdiff_t len; | |
122 | optional_type val; | |
123 | }; | |
124 | ||
125 | /////////////////////////////////////////////////////////////////////////// | |
126 | // | |
127 | // match class specialization for nil_t values | |
128 | // | |
129 | /////////////////////////////////////////////////////////////////////////// | |
130 | template <> | |
131 | class match<nil_t> : public safe_bool<match<nil_t> > | |
132 | { | |
133 | public: | |
134 | ||
135 | typedef nil_t attr_t; | |
136 | typedef nil_t return_t; | |
137 | ||
138 | match(); | |
139 | explicit match(std::size_t length); | |
140 | match(std::size_t length, nil_t); | |
141 | ||
142 | bool operator!() const; | |
143 | bool has_valid_attribute() const; | |
144 | std::ptrdiff_t length() const; | |
145 | nil_t value() const; | |
146 | void value(nil_t); | |
147 | void swap(match& other); | |
148 | ||
149 | template <typename T> | |
150 | match(match<T> const& other) | |
151 | : len(other.length()) {} | |
152 | ||
153 | template <typename T> | |
154 | match<>& | |
155 | operator=(match<T> const& other) | |
156 | { | |
157 | len = other.length(); | |
158 | return *this; | |
159 | } | |
160 | ||
161 | template <typename T> | |
162 | void | |
163 | concat(match<T> const& other) | |
164 | { | |
165 | BOOST_SPIRIT_ASSERT(*this && other); | |
166 | len += other.length(); | |
167 | } | |
168 | ||
169 | bool operator_bool() const | |
170 | { | |
171 | return len >= 0; | |
172 | } | |
173 | ||
174 | private: | |
175 | ||
176 | std::ptrdiff_t len; | |
177 | }; | |
178 | ||
179 | BOOST_SPIRIT_CLASSIC_NAMESPACE_END | |
180 | ||
181 | }} // namespace BOOST_SPIRIT_CLASSIC_NS | |
182 | ||
183 | #endif | |
184 | #include <boost/spirit/home/classic/core/impl/match.ipp> | |
185 |