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>
16 #include <boost/detail/iterator.hpp> // for boost::detail::iterator_traits
18 #include <boost/spirit/home/classic/core/scanner/scanner_fwd.hpp>
20 namespace boost { namespace spirit {
22 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
24 ///////////////////////////////////////////////////////////////////////////
26 // iteration_policy class
28 ///////////////////////////////////////////////////////////////////////////
29 struct iteration_policy
31 template <typename ScannerT>
33 advance(ScannerT const& scan) const
38 template <typename ScannerT>
39 bool at_end(ScannerT const& scan) const
41 return scan.first == scan.last;
50 template <typename ScannerT>
51 typename ScannerT::ref_t
52 get(ScannerT const& scan) const
58 ///////////////////////////////////////////////////////////////////////////
62 ///////////////////////////////////////////////////////////////////////////
66 struct result { typedef match<T> type; };
71 return match<nil_t>();
77 return match<nil_t>(0, nil_t());
80 template <typename AttrT, typename IteratorT>
85 IteratorT const& /*first*/,
86 IteratorT const& /*last*/) const
88 return match<AttrT>(length, val);
91 template <typename MatchT, typename IteratorT>
94 parser_id const& /*id*/,
95 IteratorT const& /*first*/,
96 IteratorT const& /*last*/) const {}
98 template <typename Match1T, typename Match2T>
99 void concat_match(Match1T& l, Match2T const& r) const
105 ///////////////////////////////////////////////////////////////////////////
107 // match_result class
109 ///////////////////////////////////////////////////////////////////////////
110 template <typename MatchPolicyT, typename T>
113 typedef typename MatchPolicyT::template result<T>::type type;
116 ///////////////////////////////////////////////////////////////////////////
118 // action_policy class
120 ///////////////////////////////////////////////////////////////////////////
121 template <typename AttrT>
122 struct attributed_action_policy
124 template <typename ActorT, typename IteratorT>
136 //////////////////////////////////
138 struct attributed_action_policy<nil_t>
140 template <typename ActorT, typename IteratorT>
145 IteratorT const& first,
146 IteratorT const& last)
152 //////////////////////////////////
155 template <typename ActorT, typename AttrT, typename IteratorT>
160 IteratorT const& first,
161 IteratorT const& last) const
163 attributed_action_policy<AttrT>::call(actor, val, first, last);
167 ///////////////////////////////////////////////////////////////////////////
169 // scanner_policies class
171 ///////////////////////////////////////////////////////////////////////////
173 typename IterationPolicyT,
174 typename MatchPolicyT,
175 typename ActionPolicyT>
176 struct scanner_policies :
177 public IterationPolicyT,
181 typedef IterationPolicyT iteration_policy_t;
182 typedef MatchPolicyT match_policy_t;
183 typedef ActionPolicyT action_policy_t;
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) {}
193 template <typename ScannerPoliciesT>
194 scanner_policies(ScannerPoliciesT const& policies)
195 : IterationPolicyT(policies)
196 , MatchPolicyT(policies)
197 , ActionPolicyT(policies) {}
200 ///////////////////////////////////////////////////////////////////////////
202 // scanner_policies_base class: the base class of all scanners
204 ///////////////////////////////////////////////////////////////////////////
205 struct scanner_base {};
207 ///////////////////////////////////////////////////////////////////////////
211 ///////////////////////////////////////////////////////////////////////////
215 class scanner : public PoliciesT, public scanner_base
219 typedef IteratorT iterator_t;
220 typedef PoliciesT policies_t;
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;
232 PoliciesT const& policies = PoliciesT())
233 : PoliciesT(policies), first(first_), last(last_)
238 scanner(scanner const& other)
239 : PoliciesT(other), first(other.first), last(other.last) {}
241 scanner(scanner const& other, IteratorT& first_)
242 : PoliciesT(other), first(first_), last(other.last) {}
244 template <typename PoliciesT1>
245 scanner(scanner<IteratorT, PoliciesT1> const& other)
246 : PoliciesT(other), first(other.first), last(other.last) {}
251 typedef typename PoliciesT::iteration_policy_t iteration_policy_type;
252 return iteration_policy_type::at_end(*this);
258 typedef typename PoliciesT::iteration_policy_t iteration_policy_type;
259 return iteration_policy_type::filter(iteration_policy_type::get(*this));
265 typedef typename PoliciesT::iteration_policy_t iteration_policy_type;
266 iteration_policy_type::advance(*this);
270 template <typename PoliciesT2>
271 struct rebind_policies
273 typedef scanner<IteratorT, PoliciesT2> type;
276 template <typename PoliciesT2>
277 scanner<IteratorT, PoliciesT2>
278 change_policies(PoliciesT2 const& policies) const
280 return scanner<IteratorT, PoliciesT2>(first, last, policies);
283 template <typename IteratorT2>
284 struct rebind_iterator
286 typedef scanner<IteratorT2, PoliciesT> type;
289 template <typename IteratorT2>
290 scanner<IteratorT2, PoliciesT>
291 change_iterator(IteratorT2 const& first_, IteratorT2 const &last_) const
293 return scanner<IteratorT2, PoliciesT>(first_, last_, *this);
297 IteratorT const last;
302 operator=(scanner const& other);
305 ///////////////////////////////////////////////////////////////////////////
307 // rebind_scanner_policies class
309 ///////////////////////////////////////////////////////////////////////////
310 template <typename ScannerT, typename PoliciesT>
311 struct rebind_scanner_policies
313 typedef typename ScannerT::template
314 rebind_policies<PoliciesT>::type type;
317 //////////////////////////////////
318 template <typename ScannerT, typename IteratorT>
319 struct rebind_scanner_iterator
321 typedef typename ScannerT::template
322 rebind_iterator<IteratorT>::type type;
325 BOOST_SPIRIT_CLASSIC_NAMESPACE_END