Update contrib.
1 /*=============================================================================
2 Copyright (c) 2002-2003 Joel de Guzman
3 Copyright (c) 2002 Juan Carlos Arevalo-Baeza
4 Copyright (c) 2002-2003 Martin Wille
5 http://spirit.sourceforge.net/
7 Use, modification and distribution is subject to the Boost Software
8 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 http://www.boost.org/LICENSE_1_0.txt)
10 =============================================================================*/
11 #ifndef BOOST_SPIRIT_IF_HPP
12 #define BOOST_SPIRIT_IF_HPP
14 #include <boost/spirit/core/parser.hpp>
15 #include <boost/spirit/core/composite/composite.hpp>
16 #include <boost/spirit/dynamic/impl/conditions.ipp>
18 namespace boost { namespace spirit {
22 //////////////////////////////////
23 // if-else-parser, holds two alternative parsers and a conditional functor
24 // that selects between them.
25 template <typename ParsableTrueT, typename ParsableFalseT, typename CondT>
27 : public condition_evaluator<typename as_parser<CondT>::type>
30 typename as_parser<ParsableTrueT>::type,
31 typename as_parser<ParsableFalseT>::type,
32 parser< if_else_parser<ParsableTrueT, ParsableFalseT, CondT> >
35 typedef if_else_parser<ParsableTrueT, ParsableFalseT, CondT> self_t;
37 typedef as_parser<ParsableTrueT> as_parser_true_t;
38 typedef as_parser<ParsableFalseT> as_parser_false_t;
39 typedef typename as_parser_true_t::type parser_true_t;
40 typedef typename as_parser_false_t::type parser_false_t;
41 typedef as_parser<CondT> cond_as_parser_t;
42 typedef typename cond_as_parser_t::type condition_t;
44 typedef binary<parser_true_t, parser_false_t, parser<self_t> > base_t;
45 typedef condition_evaluator<condition_t> eval_t;
49 ParsableTrueT const& p_true,
50 ParsableFalseT const& p_false,
53 : eval_t(cond_as_parser_t::convert(cond_))
56 as_parser_true_t::convert(p_true),
57 as_parser_false_t::convert(p_false)
61 template <typename ScannerT>
64 typedef typename match_result<ScannerT, nil_t>::type type;
67 template <typename ScannerT>
68 typename parser_result<self_t, ScannerT>::type
69 parse(ScannerT const& scan) const
71 typedef typename parser_result
72 <parser_true_t, ScannerT>::type then_result_t;
73 typedef typename parser_result
74 <parser_false_t, ScannerT>::type else_result_t;
76 typename ScannerT::iterator_t const save(scan.first);
78 std::ptrdiff_t length = this->evaluate(scan);
81 then_result_t then_result(this->left().parse(scan));
84 length += then_result.length();
85 return scan.create_match(std::size_t(length), nil_t(), save, scan.first);
90 else_result_t else_result(this->right().parse(scan));
93 length = else_result.length();
94 return scan.create_match(std::size_t(length), nil_t(), save, scan.first);
97 return scan.no_match();
101 //////////////////////////////////
102 // if-else-parser generator, takes the false-parser in brackets
103 // and returns the if-else-parser.
104 template <typename ParsableTrueT, typename CondT>
105 struct if_else_parser_gen
107 if_else_parser_gen(ParsableTrueT const& p_true_, CondT const& cond_)
111 template <typename ParsableFalseT>
118 operator[](ParsableFalseT const& p_false) const
120 return if_else_parser<ParsableTrueT, ParsableFalseT, CondT>
128 ParsableTrueT const &p_true;
132 //////////////////////////////////
133 // if-parser, conditionally runs a parser is a functor condition is true.
134 // If the condition is fales, it fails the parse.
135 // It can optionally become an if-else-parser through the member else_p.
136 template <typename ParsableT, typename CondT>
138 : public condition_evaluator<typename as_parser<CondT>::type>
141 typename as_parser<ParsableT>::type,
142 parser<if_parser<ParsableT, CondT> > >
144 typedef if_parser<ParsableT, CondT> self_t;
145 typedef as_parser<ParsableT> as_parser_t;
146 typedef typename as_parser_t::type parser_t;
148 typedef as_parser<CondT> cond_as_parser_t;
149 typedef typename cond_as_parser_t::type condition_t;
150 typedef condition_evaluator<condition_t> eval_t;
151 typedef unary<parser_t, parser<self_t> > base_t;
153 if_parser(ParsableT const& p, CondT const& cond_)
154 : eval_t(cond_as_parser_t::convert(cond_))
155 , base_t(as_parser_t::convert(p))
159 template <typename ScannerT>
162 typedef typename match_result<ScannerT, nil_t>::type type;
165 template <typename ScannerT>
166 typename parser_result<self_t, ScannerT>::type
167 parse(ScannerT const& scan) const
169 typedef typename parser_result<parser_t, ScannerT>::type t_result_t;
170 typename ScannerT::iterator_t const save(scan.first);
172 std::ptrdiff_t length = this->evaluate(scan);
175 t_result_t then_result(this->subject().parse(scan));
178 length += then_result.length();
179 return scan.create_match(std::size_t(length), nil_t(), save, scan.first);
181 return scan.no_match();
183 return scan.empty_match();
186 if_else_parser_gen<ParsableT, CondT> else_p;
189 //////////////////////////////////
190 // if-parser generator, takes the true-parser in brackets and returns the
192 template <typename CondT>
195 if_parser_gen(CondT const& cond_) : cond(cond_) {}
197 template <typename ParsableT>
203 operator[](ParsableT const& subject) const
205 return if_parser<ParsableT, CondT>(subject, cond);
213 //////////////////////////////////
214 // if_p function, returns "if" parser generator
216 template <typename CondT>
217 impl::if_parser_gen<CondT>
218 if_p(CondT const& cond)
220 return impl::if_parser_gen<CondT>(cond);
223 }} // namespace boost::spirit
225 #endif // BOOST_SPIRIT_IF_HPP