• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 #include <boost/spirit/home/classic/core/scanner/scanner_fwd.hpp>
18 
19 namespace boost { namespace spirit {
20 
21 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
22 
23     ///////////////////////////////////////////////////////////////////////////
24     //
25     //  iteration_policy class
26     //
27     ///////////////////////////////////////////////////////////////////////////
28     struct iteration_policy
29     {
30         template <typename ScannerT>
31         void
advanceboost::spirit::iteration_policy32         advance(ScannerT const& scan) const
33         {
34             ++scan.first;
35         }
36 
37         template <typename ScannerT>
at_endboost::spirit::iteration_policy38         bool at_end(ScannerT const& scan) const
39         {
40             return scan.first == scan.last;
41         }
42 
43         template <typename T>
filterboost::spirit::iteration_policy44         T filter(T ch) const
45         {
46             return ch;
47         }
48 
49         template <typename ScannerT>
50         typename ScannerT::ref_t
getboost::spirit::iteration_policy51         get(ScannerT const& scan) const
52         {
53             return *scan.first;
54         }
55     };
56 
57     ///////////////////////////////////////////////////////////////////////////
58     //
59     //  match_policy class
60     //
61     ///////////////////////////////////////////////////////////////////////////
62     struct match_policy
63     {
64         template <typename T>
65         struct result { typedef match<T> type; };
66 
67         const match<nil_t>
no_matchboost::spirit::match_policy68         no_match() const
69         {
70             return match<nil_t>();
71         }
72 
73         const match<nil_t>
empty_matchboost::spirit::match_policy74         empty_match() const
75         {
76             return match<nil_t>(0, nil_t());
77         }
78 
79         template <typename AttrT, typename IteratorT>
80         match<AttrT>
create_matchboost::spirit::match_policy81         create_match(
82             std::size_t         length,
83             AttrT const&        val,
84             IteratorT const&    /*first*/,
85             IteratorT const&    /*last*/) const
86         {
87             return match<AttrT>(length, val);
88         }
89 
90         template <typename MatchT, typename IteratorT>
group_matchboost::spirit::match_policy91         void group_match(
92             MatchT&             /*m*/,
93             parser_id const&    /*id*/,
94             IteratorT const&    /*first*/,
95             IteratorT const&    /*last*/) const {}
96 
97         template <typename Match1T, typename Match2T>
concat_matchboost::spirit::match_policy98         void concat_match(Match1T& l, Match2T const& r) const
99         {
100             l.concat(r);
101         }
102     };
103 
104     ///////////////////////////////////////////////////////////////////////////
105     //
106     //  match_result class
107     //
108     ///////////////////////////////////////////////////////////////////////////
109     template <typename MatchPolicyT, typename T>
110     struct match_result
111     {
112         typedef typename MatchPolicyT::template result<T>::type type;
113     };
114 
115     ///////////////////////////////////////////////////////////////////////////
116     //
117     //  action_policy class
118     //
119     ///////////////////////////////////////////////////////////////////////////
120     template <typename AttrT>
121     struct attributed_action_policy
122     {
123         template <typename ActorT, typename IteratorT>
124         static void
callboost::spirit::attributed_action_policy125         call(
126             ActorT const& actor,
127             AttrT& val,
128             IteratorT const&,
129             IteratorT const&)
130         {
131             actor(val);
132         }
133     };
134 
135     //////////////////////////////////
136     template <>
137     struct attributed_action_policy<nil_t>
138     {
139         template <typename ActorT, typename IteratorT>
140         static void
callboost::spirit::attributed_action_policy141         call(
142             ActorT const& actor,
143             nil_t,
144             IteratorT const& first,
145             IteratorT const& last)
146         {
147             actor(first, last);
148         }
149     };
150 
151     //////////////////////////////////
152     struct action_policy
153     {
154         template <typename ActorT, typename AttrT, typename IteratorT>
155         void
do_actionboost::spirit::action_policy156         do_action(
157             ActorT const&       actor,
158             AttrT&              val,
159             IteratorT const&    first,
160             IteratorT const&    last) const
161         {
162             attributed_action_policy<AttrT>::call(actor, val, first, last);
163         }
164     };
165 
166     ///////////////////////////////////////////////////////////////////////////
167     //
168     //  scanner_policies class
169     //
170     ///////////////////////////////////////////////////////////////////////////
171     template <
172         typename IterationPolicyT,
173         typename MatchPolicyT,
174         typename ActionPolicyT>
175     struct scanner_policies :
176         public IterationPolicyT,
177         public MatchPolicyT,
178         public ActionPolicyT
179     {
180         typedef IterationPolicyT    iteration_policy_t;
181         typedef MatchPolicyT        match_policy_t;
182         typedef ActionPolicyT       action_policy_t;
183 
scanner_policiesboost::spirit::scanner_policies184         scanner_policies(
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) {}
191 
192         template <typename ScannerPoliciesT>
scanner_policiesboost::spirit::scanner_policies193         scanner_policies(ScannerPoliciesT const& policies)
194         : IterationPolicyT(policies)
195         , MatchPolicyT(policies)
196         , ActionPolicyT(policies) {}
197     };
198 
199     ///////////////////////////////////////////////////////////////////////////
200     //
201     //  scanner_policies_base class: the base class of all scanners
202     //
203     ///////////////////////////////////////////////////////////////////////////
204     struct scanner_base {};
205 
206     ///////////////////////////////////////////////////////////////////////////
207     //
208     //  scanner class
209     //
210     ///////////////////////////////////////////////////////////////////////////
211     template <
212         typename IteratorT,
213         typename PoliciesT>
214     class scanner : public PoliciesT, public scanner_base
215     {
216     public:
217 
218         typedef IteratorT iterator_t;
219         typedef PoliciesT policies_t;
220 
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;
227 
scanner(IteratorT & first_,iter_param_t last_,PoliciesT const & policies=PoliciesT ())228         scanner(
229             IteratorT&          first_,
230             iter_param_t        last_,
231             PoliciesT const&    policies = PoliciesT())
232         : PoliciesT(policies), first(first_), last(last_)
233         {
234             at_end();
235         }
236 
scanner(scanner const & other)237         scanner(scanner const& other)
238         : PoliciesT(other), first(other.first), last(other.last) {}
239 
scanner(scanner const & other,IteratorT & first_)240         scanner(scanner const& other, IteratorT& first_)
241         : PoliciesT(other), first(first_), last(other.last) {}
242 
243         template <typename PoliciesT1>
scanner(scanner<IteratorT,PoliciesT1> const & other)244         scanner(scanner<IteratorT, PoliciesT1> const& other)
245         : PoliciesT(other), first(other.first), last(other.last) {}
246 
247         bool
at_end() const248         at_end() const
249         {
250             typedef typename PoliciesT::iteration_policy_t iteration_policy_type;
251             return iteration_policy_type::at_end(*this);
252         }
253 
254         value_t
operator *() const255         operator*() const
256         {
257             typedef typename PoliciesT::iteration_policy_t iteration_policy_type;
258             return iteration_policy_type::filter(iteration_policy_type::get(*this));
259         }
260 
261         scanner const&
operator ++() const262         operator++() const
263         {
264             typedef typename PoliciesT::iteration_policy_t iteration_policy_type;
265             iteration_policy_type::advance(*this);
266             return *this;
267         }
268 
269         template <typename PoliciesT2>
270         struct rebind_policies
271         {
272             typedef scanner<IteratorT, PoliciesT2> type;
273         };
274 
275         template <typename PoliciesT2>
276         scanner<IteratorT, PoliciesT2>
change_policies(PoliciesT2 const & policies) const277         change_policies(PoliciesT2 const& policies) const
278         {
279             return scanner<IteratorT, PoliciesT2>(first, last, policies);
280         }
281 
282         template <typename IteratorT2>
283         struct rebind_iterator
284         {
285             typedef scanner<IteratorT2, PoliciesT> type;
286         };
287 
288         template <typename IteratorT2>
289         scanner<IteratorT2, PoliciesT>
change_iterator(IteratorT2 const & first_,IteratorT2 const & last_) const290         change_iterator(IteratorT2 const& first_, IteratorT2 const &last_) const
291         {
292             return scanner<IteratorT2, PoliciesT>(first_, last_, *this);
293         }
294 
295         IteratorT& first;
296         IteratorT const last;
297 
298     private:
299 
300         scanner&
301         operator=(scanner const& other);
302     };
303 
304     ///////////////////////////////////////////////////////////////////////////
305     //
306     //  rebind_scanner_policies class
307     //
308     ///////////////////////////////////////////////////////////////////////////
309     template <typename ScannerT, typename PoliciesT>
310     struct rebind_scanner_policies
311     {
312         typedef typename ScannerT::template
313             rebind_policies<PoliciesT>::type type;
314     };
315 
316     //////////////////////////////////
317     template <typename ScannerT, typename IteratorT>
318     struct rebind_scanner_iterator
319     {
320         typedef typename ScannerT::template
321             rebind_iterator<IteratorT>::type type;
322     };
323 
324 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
325 
326 }}
327 
328 #endif
329