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 | Use, modification and distribution is subject to the Boost Software |
---|
7 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
---|
8 | http://www.boost.org/LICENSE_1_0.txt) |
---|
9 | =============================================================================*/ |
---|
10 | #ifndef BOOST_SPIRIT_FUNCTOR_PARSER_HPP |
---|
11 | #define BOOST_SPIRIT_FUNCTOR_PARSER_HPP |
---|
12 | |
---|
13 | /////////////////////////////////////////////////////////////////////////////// |
---|
14 | #include <boost/spirit/core/parser.hpp> |
---|
15 | |
---|
16 | /////////////////////////////////////////////////////////////////////////////// |
---|
17 | namespace boost { namespace spirit { |
---|
18 | |
---|
19 | /////////////////////////////////////////////////////////////////////////// |
---|
20 | // |
---|
21 | // functor_parser class |
---|
22 | // |
---|
23 | // Once a functor parser has been defined, you can build a real |
---|
24 | // parser from it by passing it to this class as the template |
---|
25 | // parameter. |
---|
26 | // |
---|
27 | /////////////////////////////////////////////////////////////////////////// |
---|
28 | template < class FunctorT > |
---|
29 | struct functor_parser : public parser<functor_parser<FunctorT> > |
---|
30 | { |
---|
31 | FunctorT functor; |
---|
32 | |
---|
33 | functor_parser(): functor() {} |
---|
34 | functor_parser(FunctorT const& functor_): functor(functor_) {} |
---|
35 | |
---|
36 | typedef typename FunctorT::result_t functor_result_t; |
---|
37 | typedef functor_parser<FunctorT> self_t; |
---|
38 | |
---|
39 | template <typename ScannerT> |
---|
40 | struct result |
---|
41 | { |
---|
42 | typedef typename match_result<ScannerT, functor_result_t>::type |
---|
43 | type; |
---|
44 | }; |
---|
45 | |
---|
46 | template <typename ScannerT> |
---|
47 | typename parser_result<self_t, ScannerT>::type |
---|
48 | parse(ScannerT const& scan) const |
---|
49 | { |
---|
50 | typedef typename parser_result<self_t, ScannerT>::type result_t; |
---|
51 | typedef typename ScannerT::value_t value_t; |
---|
52 | typedef typename ScannerT::iterator_t iterator_t; |
---|
53 | |
---|
54 | iterator_t const s(scan.first); |
---|
55 | functor_result_t result; |
---|
56 | std::ptrdiff_t len = functor(scan, result); |
---|
57 | |
---|
58 | if (len < 0) |
---|
59 | return scan.no_match(); |
---|
60 | else |
---|
61 | return scan.create_match(std::size_t(len), result, s, scan.first); |
---|
62 | } |
---|
63 | }; |
---|
64 | |
---|
65 | }} // namespace boost::spirit |
---|
66 | |
---|
67 | #endif |
---|