source: NonGTP/Boost/boost/spirit/core/scanner/scanner.hpp @ 857

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