source: NonGTP/Boost/boost/spirit/dynamic/impl/conditions.ipp @ 857

Revision 857, 3.5 KB checked in by igarcia, 18 years ago (diff)
Line 
1/*=============================================================================
2    Copyright (c) 2002-2003 Martin Wille
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#ifndef BOOST_SPIRIT_CONDITIONS_IPP
10#define BOOST_SPIRIT_CONDITIONS_IPP
11
12///////////////////////////////////////////////////////////////////////////////
13#include <boost/spirit/meta/parser_traits.hpp>
14#include <boost/spirit/core/composite/epsilon.hpp>
15
16///////////////////////////////////////////////////////////////////////////////
17namespace boost { namespace spirit {
18
19    namespace impl {
20
21///////////////////////////////////////////////////////////////////////////////
22//
23// condition evaluation
24//
25///////////////////////////////////////////////////////////////////////////////
26    //////////////////////////////////
27    // condition_parser_selector, decides which parser to use for a condition
28    // If the template argument is a parser then that parser is used.
29    // If the template argument is a functor then a condition parser using
30    // the functor is chosen
31
32    template <typename T> struct embed_t_accessor
33    {
34        typedef typename T::embed_t type;
35    };
36
37#if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
38    template <> struct embed_t_accessor<int>
39    {
40        typedef int type;
41    };
42#endif
43
44    template <typename ConditionT>
45    struct condition_parser_selector
46    {
47        typedef
48            typename mpl::if_<
49                    is_parser<ConditionT>,
50                    ConditionT,
51                    condition_parser<ConditionT>
52                >::type
53            type;
54
55        typedef typename embed_t_accessor<type>::type embed_t;
56    };
57
58    //////////////////////////////////
59    // condition_evaluator, uses a parser to check wether a condition is met
60    // takes a parser or a functor that can be evaluated in boolean context
61    // as template parameter.
62
63    // JDG 4-15-03 refactored
64    template <typename ConditionT>
65    struct condition_evaluator
66    {
67        typedef condition_parser_selector<ConditionT>       selector_t;
68        typedef typename selector_t::type                   selected_t;
69        typedef typename selector_t::embed_t                cond_embed_t;
70
71        typedef typename boost::call_traits<cond_embed_t>::param_type
72            param_t;
73
74        condition_evaluator(param_t s) : cond(s) {}
75
76        /////////////////////////////
77        // evaluate, checks wether condition is met
78        // returns length of a match or a negative number for no-match
79        template <typename ScannerT>
80        std::ptrdiff_t
81        evaluate(ScannerT const &scan) const
82        {
83            typedef typename ScannerT::iterator_t iterator_t;
84            typedef typename parser_result<selected_t, ScannerT>::type cres_t;
85            iterator_t save(scan.first);
86            cres_t result = cond.parse(scan);
87            if (!result)            // reset the position if evaluation
88                scan.first = save;  // fails.
89            return result.length();
90        }
91
92        cond_embed_t cond;
93    };
94
95///////////////////////////////////////////////////////////////////////////////
96    } // namespace impl
97
98}} // namespace boost::spirit
99
100#endif
Note: See TracBrowser for help on using the repository browser.