1 /*=============================================================================
2 Copyright (c) 1998-2002 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_SCANNER_HPP)
9 #define BOOST_SPIRIT_SCANNER_HPP
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>
17 #include <boost/spirit/home/classic/core/scanner/scanner_fwd.hpp>
19 namespace boost { namespace spirit {
21 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
23 ///////////////////////////////////////////////////////////////////////////
25 // iteration_policy class
27 ///////////////////////////////////////////////////////////////////////////
28 struct iteration_policy
30 template <typename ScannerT>
32 advance(ScannerT const& scan) const
37 template <typename ScannerT>
38 bool at_end(ScannerT const& scan) const
40 return scan.first == scan.last;
49 template <typename ScannerT>
50 typename ScannerT::ref_t
51 get(ScannerT const& scan) const
57 ///////////////////////////////////////////////////////////////////////////
61 ///////////////////////////////////////////////////////////////////////////
65 struct result { typedef match<T> type; };
70 return match<nil_t>();
76 return match<nil_t>(0, nil_t());
79 template <typename AttrT, typename IteratorT>
84 IteratorT const& /*first*/,
85 IteratorT const& /*last*/) const
87 return match<AttrT>(length, val);
90 template <typename MatchT, typename IteratorT>
93 parser_id const& /*id*/,
94 IteratorT const& /*first*/,
95 IteratorT const& /*last*/) const {}
97 template <typename Match1T, typename Match2T>
98 void concat_match(Match1T& l, Match2T const& r) const
104 ///////////////////////////////////////////////////////////////////////////
106 // match_result class
108 ///////////////////////////////////////////////////////////////////////////
109 template <typename MatchPolicyT, typename T>
112 typedef typename MatchPolicyT::template result<T>::type type;
115 ///////////////////////////////////////////////////////////////////////////
117 // action_policy class
119 ///////////////////////////////////////////////////////////////////////////
120 template <typename AttrT>
121 struct attributed_action_policy
123 template <typename ActorT, typename IteratorT>
135 //////////////////////////////////
137 struct attributed_action_policy<nil_t>
139 template <typename ActorT, typename IteratorT>
144 IteratorT const& first,
145 IteratorT const& last)
151 //////////////////////////////////
154 template <typename ActorT, typename AttrT, typename IteratorT>
159 IteratorT const& first,
160 IteratorT const& last) const
162 attributed_action_policy<AttrT>::call(actor, val, first, last);
166 ///////////////////////////////////////////////////////////////////////////
168 // scanner_policies class
170 ///////////////////////////////////////////////////////////////////////////
172 typename IterationPolicyT,
173 typename MatchPolicyT,
174 typename ActionPolicyT>
175 struct scanner_policies :
176 public IterationPolicyT,
180 typedef IterationPolicyT iteration_policy_t;
181 typedef MatchPolicyT match_policy_t;
182 typedef ActionPolicyT action_policy_t;
185 IterationPolicyT const& i_policy = IterationPolicyT(),
186 MatchPolicyT const& m_policy = MatchPolicyT(),
187 ActionPolicyT const& a_policy = ActionPolicyT())
188 : IterationPolicyT(i_policy)
189 , MatchPolicyT(m_policy)
190 , ActionPolicyT(a_policy) {}
192 template <typename ScannerPoliciesT>
193 scanner_policies(ScannerPoliciesT const& policies)
194 : IterationPolicyT(policies)
195 , MatchPolicyT(policies)
196 , ActionPolicyT(policies) {}
199 ///////////////////////////////////////////////////////////////////////////
201 // scanner_policies_base class: the base class of all scanners
203 ///////////////////////////////////////////////////////////////////////////
204 struct scanner_base {};
206 ///////////////////////////////////////////////////////////////////////////
210 ///////////////////////////////////////////////////////////////////////////
214 class scanner : public PoliciesT, public scanner_base
218 typedef IteratorT iterator_t;
219 typedef PoliciesT policies_t;
221 typedef typename std::
222 iterator_traits<IteratorT>::value_type value_t;
223 typedef typename std::
224 iterator_traits<IteratorT>::reference ref_t;
225 typedef typename boost::
226 call_traits<IteratorT>::param_type iter_param_t;
231 PoliciesT const& policies = PoliciesT())
232 : PoliciesT(policies), first(first_), last(last_)
237 scanner(scanner const& other)
238 : PoliciesT(other), first(other.first), last(other.last) {}
240 scanner(scanner const& other, IteratorT& first_)
241 : PoliciesT(other), first(first_), last(other.last) {}
243 template <typename PoliciesT1>
244 scanner(scanner<IteratorT, PoliciesT1> const& other)
245 : PoliciesT(other), first(other.first), last(other.last) {}
250 typedef typename PoliciesT::iteration_policy_t iteration_policy_type;
251 return iteration_policy_type::at_end(*this);
257 typedef typename PoliciesT::iteration_policy_t iteration_policy_type;
258 return iteration_policy_type::filter(iteration_policy_type::get(*this));
264 typedef typename PoliciesT::iteration_policy_t iteration_policy_type;
265 iteration_policy_type::advance(*this);
269 template <typename PoliciesT2>
270 struct rebind_policies
272 typedef scanner<IteratorT, PoliciesT2> type;
275 template <typename PoliciesT2>
276 scanner<IteratorT, PoliciesT2>
277 change_policies(PoliciesT2 const& policies) const
279 return scanner<IteratorT, PoliciesT2>(first, last, policies);
282 template <typename IteratorT2>
283 struct rebind_iterator
285 typedef scanner<IteratorT2, PoliciesT> type;
288 template <typename IteratorT2>
289 scanner<IteratorT2, PoliciesT>
290 change_iterator(IteratorT2 const& first_, IteratorT2 const &last_) const
292 return scanner<IteratorT2, PoliciesT>(first_, last_, *this);
296 IteratorT const last;
301 operator=(scanner const& other);
304 ///////////////////////////////////////////////////////////////////////////
306 // rebind_scanner_policies class
308 ///////////////////////////////////////////////////////////////////////////
309 template <typename ScannerT, typename PoliciesT>
310 struct rebind_scanner_policies
312 typedef typename ScannerT::template
313 rebind_policies<PoliciesT>::type type;
316 //////////////////////////////////
317 template <typename ScannerT, typename IteratorT>
318 struct rebind_scanner_iterator
320 typedef typename ScannerT::template
321 rebind_iterator<IteratorT>::type type;
324 BOOST_SPIRIT_CLASSIC_NAMESPACE_END