1 /*=============================================================================
2     Copyright (c) 2002-2003 Joel de Guzman
3     Copyright (c) 2002-2003 Juan Carlos Arevalo-Baeza
4     http://spirit.sourceforge.net/
5 
6   Distributed under the Boost Software License, Version 1.0. (See accompanying
7   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 =============================================================================*/
9 #ifndef BOOST_SPIRIT_FUNCTOR_PARSER_HPP
10 #define BOOST_SPIRIT_FUNCTOR_PARSER_HPP
11 
12 ///////////////////////////////////////////////////////////////////////////////
13 #include <boost/spirit/home/classic/namespace.hpp>
14 #include <boost/spirit/home/classic/core/parser.hpp>
15 
16 ///////////////////////////////////////////////////////////////////////////////
17 namespace boost { namespace spirit {
18 
19 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
20 
21     ///////////////////////////////////////////////////////////////////////////
22     //
23     //  functor_parser class
24     //
25     //      Once a functor parser has been defined, you can build a real
26     //      parser from it by passing it to this class as the template
27     //      parameter.
28     //
29     ///////////////////////////////////////////////////////////////////////////
30     template < class FunctorT >
31     struct functor_parser : public parser<functor_parser<FunctorT> >
32     {
33         FunctorT functor;
34 
functor_parserboost::spirit::functor_parser35         functor_parser(): functor() {}
functor_parserboost::spirit::functor_parser36         functor_parser(FunctorT const& functor_): functor(functor_) {}
37 
38         typedef typename FunctorT::result_t functor_result_t;
39         typedef functor_parser<FunctorT> self_t;
40 
41         template <typename ScannerT>
42         struct result
43         {
44             typedef typename match_result<ScannerT, functor_result_t>::type
45             type;
46         };
47 
48         template <typename ScannerT>
49         typename parser_result<self_t, ScannerT>::type
parseboost::spirit::functor_parser50         parse(ScannerT const& scan) const
51         {
52             typedef typename ScannerT::iterator_t   iterator_t;
53 
54             iterator_t const s(scan.first);
55             functor_result_t functor_result;
56             std::ptrdiff_t len = functor(scan, functor_result);
57 
58             if (len < 0)
59                 return scan.no_match();
60             else
61                 return scan.create_match(std::size_t(len), functor_result, s, scan.first);
62         }
63     };
64 
65 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
66 
67 }} // namespace BOOST_SPIRIT_CLASSIC_NS
68 
69 #endif
70