]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 1998-2002 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_SCANNER_HPP) | |
9 | #define BOOST_SPIRIT_SCANNER_HPP | |
10 | ||
11 | #include <iterator> | |
12 | #include <boost/config.hpp> | |
13 | #include <boost/spirit/home/classic/namespace.hpp> | |
14 | #include <boost/spirit/home/classic/core/match.hpp> | |
15 | #include <boost/spirit/home/classic/core/non_terminal/parser_id.hpp> | |
16 | #include <boost/detail/iterator.hpp> // for boost::detail::iterator_traits | |
17 | ||
18 | #include <boost/spirit/home/classic/core/scanner/scanner_fwd.hpp> | |
19 | ||
20 | namespace boost { namespace spirit { | |
21 | ||
22 | BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN | |
23 | ||
24 | /////////////////////////////////////////////////////////////////////////// | |
25 | // | |
26 | // iteration_policy class | |
27 | // | |
28 | /////////////////////////////////////////////////////////////////////////// | |
29 | struct iteration_policy | |
30 | { | |
31 | template <typename ScannerT> | |
32 | void | |
33 | advance(ScannerT const& scan) const | |
34 | { | |
35 | ++scan.first; | |
36 | } | |
37 | ||
38 | template <typename ScannerT> | |
39 | bool at_end(ScannerT const& scan) const | |
40 | { | |
41 | return scan.first == scan.last; | |
42 | } | |
43 | ||
44 | template <typename T> | |
45 | T filter(T ch) const | |
46 | { | |
47 | return ch; | |
48 | } | |
49 | ||
50 | template <typename ScannerT> | |
51 | typename ScannerT::ref_t | |
52 | get(ScannerT const& scan) const | |
53 | { | |
54 | return *scan.first; | |
55 | } | |
56 | }; | |
57 | ||
58 | /////////////////////////////////////////////////////////////////////////// | |
59 | // | |
60 | // match_policy class | |
61 | // | |
62 | /////////////////////////////////////////////////////////////////////////// | |
63 | struct match_policy | |
64 | { | |
65 | template <typename T> | |
66 | struct result { typedef match<T> type; }; | |
67 | ||
68 | const match<nil_t> | |
69 | no_match() const | |
70 | { | |
71 | return match<nil_t>(); | |
72 | } | |
73 | ||
74 | const match<nil_t> | |
75 | empty_match() const | |
76 | { | |
77 | return match<nil_t>(0, nil_t()); | |
78 | } | |
79 | ||
80 | template <typename AttrT, typename IteratorT> | |
81 | match<AttrT> | |
82 | create_match( | |
83 | std::size_t length, | |
84 | AttrT const& val, | |
85 | IteratorT const& /*first*/, | |
86 | IteratorT const& /*last*/) const | |
87 | { | |
88 | return match<AttrT>(length, val); | |
89 | } | |
90 | ||
91 | template <typename MatchT, typename IteratorT> | |
92 | void group_match( | |
93 | MatchT& /*m*/, | |
94 | parser_id const& /*id*/, | |
95 | IteratorT const& /*first*/, | |
96 | IteratorT const& /*last*/) const {} | |
97 | ||
98 | template <typename Match1T, typename Match2T> | |
99 | void concat_match(Match1T& l, Match2T const& r) const | |
100 | { | |
101 | l.concat(r); | |
102 | } | |
103 | }; | |
104 | ||
105 | /////////////////////////////////////////////////////////////////////////// | |
106 | // | |
107 | // match_result class | |
108 | // | |
109 | /////////////////////////////////////////////////////////////////////////// | |
110 | template <typename MatchPolicyT, typename T> | |
111 | struct match_result | |
112 | { | |
113 | typedef typename MatchPolicyT::template result<T>::type type; | |
114 | }; | |
115 | ||
116 | /////////////////////////////////////////////////////////////////////////// | |
117 | // | |
118 | // action_policy class | |
119 | // | |
120 | /////////////////////////////////////////////////////////////////////////// | |
121 | template <typename AttrT> | |
122 | struct attributed_action_policy | |
123 | { | |
124 | template <typename ActorT, typename IteratorT> | |
125 | static void | |
126 | call( | |
127 | ActorT const& actor, | |
128 | AttrT& val, | |
129 | IteratorT const&, | |
130 | IteratorT const&) | |
131 | { | |
132 | actor(val); | |
133 | } | |
134 | }; | |
135 | ||
136 | ////////////////////////////////// | |
137 | template <> | |
138 | struct attributed_action_policy<nil_t> | |
139 | { | |
140 | template <typename ActorT, typename IteratorT> | |
141 | static void | |
142 | call( | |
143 | ActorT const& actor, | |
144 | nil_t, | |
145 | IteratorT const& first, | |
146 | IteratorT const& last) | |
147 | { | |
148 | actor(first, last); | |
149 | } | |
150 | }; | |
151 | ||
152 | ////////////////////////////////// | |
153 | struct action_policy | |
154 | { | |
155 | template <typename ActorT, typename AttrT, typename IteratorT> | |
156 | void | |
157 | do_action( | |
158 | ActorT const& actor, | |
159 | AttrT& val, | |
160 | IteratorT const& first, | |
161 | IteratorT const& last) const | |
162 | { | |
163 | attributed_action_policy<AttrT>::call(actor, val, first, last); | |
164 | } | |
165 | }; | |
166 | ||
167 | /////////////////////////////////////////////////////////////////////////// | |
168 | // | |
169 | // scanner_policies class | |
170 | // | |
171 | /////////////////////////////////////////////////////////////////////////// | |
172 | template < | |
173 | typename IterationPolicyT, | |
174 | typename MatchPolicyT, | |
175 | typename ActionPolicyT> | |
176 | struct scanner_policies : | |
177 | public IterationPolicyT, | |
178 | public MatchPolicyT, | |
179 | public ActionPolicyT | |
180 | { | |
181 | typedef IterationPolicyT iteration_policy_t; | |
182 | typedef MatchPolicyT match_policy_t; | |
183 | typedef ActionPolicyT action_policy_t; | |
184 | ||
185 | scanner_policies( | |
186 | IterationPolicyT const& i_policy = IterationPolicyT(), | |
187 | MatchPolicyT const& m_policy = MatchPolicyT(), | |
188 | ActionPolicyT const& a_policy = ActionPolicyT()) | |
189 | : IterationPolicyT(i_policy) | |
190 | , MatchPolicyT(m_policy) | |
191 | , ActionPolicyT(a_policy) {} | |
192 | ||
193 | template <typename ScannerPoliciesT> | |
194 | scanner_policies(ScannerPoliciesT const& policies) | |
195 | : IterationPolicyT(policies) | |
196 | , MatchPolicyT(policies) | |
197 | , ActionPolicyT(policies) {} | |
198 | }; | |
199 | ||
200 | /////////////////////////////////////////////////////////////////////////// | |
201 | // | |
202 | // scanner_policies_base class: the base class of all scanners | |
203 | // | |
204 | /////////////////////////////////////////////////////////////////////////// | |
205 | struct scanner_base {}; | |
206 | ||
207 | /////////////////////////////////////////////////////////////////////////// | |
208 | // | |
209 | // scanner class | |
210 | // | |
211 | /////////////////////////////////////////////////////////////////////////// | |
212 | template < | |
213 | typename IteratorT, | |
214 | typename PoliciesT> | |
215 | class scanner : public PoliciesT, public scanner_base | |
216 | { | |
217 | public: | |
218 | ||
219 | typedef IteratorT iterator_t; | |
220 | typedef PoliciesT policies_t; | |
221 | ||
222 | typedef typename boost::detail:: | |
223 | iterator_traits<IteratorT>::value_type value_t; | |
224 | typedef typename boost::detail:: | |
225 | iterator_traits<IteratorT>::reference ref_t; | |
226 | typedef typename boost:: | |
227 | call_traits<IteratorT>::param_type iter_param_t; | |
228 | ||
229 | scanner( | |
230 | IteratorT& first_, | |
231 | iter_param_t last_, | |
232 | PoliciesT const& policies = PoliciesT()) | |
233 | : PoliciesT(policies), first(first_), last(last_) | |
234 | { | |
235 | at_end(); | |
236 | } | |
237 | ||
238 | scanner(scanner const& other) | |
239 | : PoliciesT(other), first(other.first), last(other.last) {} | |
240 | ||
241 | scanner(scanner const& other, IteratorT& first_) | |
242 | : PoliciesT(other), first(first_), last(other.last) {} | |
243 | ||
244 | template <typename PoliciesT1> | |
245 | scanner(scanner<IteratorT, PoliciesT1> const& other) | |
246 | : PoliciesT(other), first(other.first), last(other.last) {} | |
247 | ||
248 | bool | |
249 | at_end() const | |
250 | { | |
251 | typedef typename PoliciesT::iteration_policy_t iteration_policy_type; | |
252 | return iteration_policy_type::at_end(*this); | |
253 | } | |
254 | ||
255 | value_t | |
256 | operator*() const | |
257 | { | |
258 | typedef typename PoliciesT::iteration_policy_t iteration_policy_type; | |
259 | return iteration_policy_type::filter(iteration_policy_type::get(*this)); | |
260 | } | |
261 | ||
262 | scanner const& | |
263 | operator++() const | |
264 | { | |
265 | typedef typename PoliciesT::iteration_policy_t iteration_policy_type; | |
266 | iteration_policy_type::advance(*this); | |
267 | return *this; | |
268 | } | |
269 | ||
270 | template <typename PoliciesT2> | |
271 | struct rebind_policies | |
272 | { | |
273 | typedef scanner<IteratorT, PoliciesT2> type; | |
274 | }; | |
275 | ||
276 | template <typename PoliciesT2> | |
277 | scanner<IteratorT, PoliciesT2> | |
278 | change_policies(PoliciesT2 const& policies) const | |
279 | { | |
280 | return scanner<IteratorT, PoliciesT2>(first, last, policies); | |
281 | } | |
282 | ||
283 | template <typename IteratorT2> | |
284 | struct rebind_iterator | |
285 | { | |
286 | typedef scanner<IteratorT2, PoliciesT> type; | |
287 | }; | |
288 | ||
289 | template <typename IteratorT2> | |
290 | scanner<IteratorT2, PoliciesT> | |
291 | change_iterator(IteratorT2 const& first_, IteratorT2 const &last_) const | |
292 | { | |
293 | return scanner<IteratorT2, PoliciesT>(first_, last_, *this); | |
294 | } | |
295 | ||
296 | IteratorT& first; | |
297 | IteratorT const last; | |
298 | ||
299 | private: | |
300 | ||
301 | scanner& | |
302 | operator=(scanner const& other); | |
303 | }; | |
304 | ||
305 | /////////////////////////////////////////////////////////////////////////// | |
306 | // | |
307 | // rebind_scanner_policies class | |
308 | // | |
309 | /////////////////////////////////////////////////////////////////////////// | |
310 | template <typename ScannerT, typename PoliciesT> | |
311 | struct rebind_scanner_policies | |
312 | { | |
313 | typedef typename ScannerT::template | |
314 | rebind_policies<PoliciesT>::type type; | |
315 | }; | |
316 | ||
317 | ////////////////////////////////// | |
318 | template <typename ScannerT, typename IteratorT> | |
319 | struct rebind_scanner_iterator | |
320 | { | |
321 | typedef typename ScannerT::template | |
322 | rebind_iterator<IteratorT>::type type; | |
323 | }; | |
324 | ||
325 | BOOST_SPIRIT_CLASSIC_NAMESPACE_END | |
326 | ||
327 | }} | |
328 | ||
329 | #endif |