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 Distributed under the Boost Software License, Version 1.0. (See accompanying
8 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10 #ifndef BOOST_SPIRIT_IF_HPP
11 #define BOOST_SPIRIT_IF_HPP
13 #include <boost/spirit/home/classic/namespace.hpp>
14 #include <boost/spirit/home/classic/core/parser.hpp>
15 #include <boost/spirit/home/classic/core/composite/composite.hpp>
16 #include <boost/spirit/home/classic/dynamic/impl/conditions.ipp>
18 namespace boost { namespace spirit {
20 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
24 //////////////////////////////////
25 // if-else-parser, holds two alternative parsers and a conditional functor
26 // that selects between them.
27 template <typename ParsableTrueT, typename ParsableFalseT, typename CondT>
29 : public condition_evaluator<typename as_parser<CondT>::type>
32 typename as_parser<ParsableTrueT>::type,
33 typename as_parser<ParsableFalseT>::type,
34 parser< if_else_parser<ParsableTrueT, ParsableFalseT, CondT> >
37 typedef if_else_parser<ParsableTrueT, ParsableFalseT, CondT> self_t;
39 typedef as_parser<ParsableTrueT> as_parser_true_t;
40 typedef as_parser<ParsableFalseT> as_parser_false_t;
41 typedef typename as_parser_true_t::type parser_true_t;
42 typedef typename as_parser_false_t::type parser_false_t;
43 typedef as_parser<CondT> cond_as_parser_t;
44 typedef typename cond_as_parser_t::type condition_t;
46 typedef binary<parser_true_t, parser_false_t, parser<self_t> > base_t;
47 typedef condition_evaluator<condition_t> eval_t;
51 ParsableTrueT const& p_true,
52 ParsableFalseT const& p_false,
55 : eval_t(cond_as_parser_t::convert(cond_))
58 as_parser_true_t::convert(p_true),
59 as_parser_false_t::convert(p_false)
63 template <typename ScannerT>
66 typedef typename match_result<ScannerT, nil_t>::type type;
69 template <typename ScannerT>
70 typename parser_result<self_t, ScannerT>::type
71 parse(ScannerT const& scan) const
73 typedef typename parser_result
74 <parser_true_t, ScannerT>::type then_result_t;
75 typedef typename parser_result
76 <parser_false_t, ScannerT>::type else_result_t;
78 typename ScannerT::iterator_t const save(scan.first);
80 std::ptrdiff_t length = this->evaluate(scan);
83 then_result_t then_result(this->left().parse(scan));
86 length += then_result.length();
87 return scan.create_match(std::size_t(length), nil_t(), save, scan.first);
92 else_result_t else_result(this->right().parse(scan));
95 length = else_result.length();
96 return scan.create_match(std::size_t(length), nil_t(), save, scan.first);
99 return scan.no_match();
103 //////////////////////////////////
104 // if-else-parser generator, takes the false-parser in brackets
105 // and returns the if-else-parser.
106 template <typename ParsableTrueT, typename CondT>
107 struct if_else_parser_gen
109 if_else_parser_gen(ParsableTrueT const& p_true_, CondT const& cond_)
113 template <typename ParsableFalseT>
120 operator[](ParsableFalseT const& p_false) const
122 return if_else_parser<ParsableTrueT, ParsableFalseT, CondT>
130 ParsableTrueT const &p_true;
134 //////////////////////////////////
135 // if-parser, conditionally runs a parser is a functor condition is true.
136 // If the condition is fales, it fails the parse.
137 // It can optionally become an if-else-parser through the member else_p.
138 template <typename ParsableT, typename CondT>
140 : public condition_evaluator<typename as_parser<CondT>::type>
143 typename as_parser<ParsableT>::type,
144 parser<if_parser<ParsableT, CondT> > >
146 typedef if_parser<ParsableT, CondT> self_t;
147 typedef as_parser<ParsableT> as_parser_t;
148 typedef typename as_parser_t::type parser_t;
150 typedef as_parser<CondT> cond_as_parser_t;
151 typedef typename cond_as_parser_t::type condition_t;
152 typedef condition_evaluator<condition_t> eval_t;
153 typedef unary<parser_t, parser<self_t> > base_t;
155 if_parser(ParsableT const& p, CondT const& cond_)
156 : eval_t(cond_as_parser_t::convert(cond_))
157 , base_t(as_parser_t::convert(p))
161 template <typename ScannerT>
164 typedef typename match_result<ScannerT, nil_t>::type type;
167 template <typename ScannerT>
168 typename parser_result<self_t, ScannerT>::type
169 parse(ScannerT const& scan) const
171 typedef typename parser_result<parser_t, ScannerT>::type t_result_t;
172 typename ScannerT::iterator_t const save(scan.first);
174 std::ptrdiff_t length = this->evaluate(scan);
177 t_result_t then_result(this->subject().parse(scan));
180 length += then_result.length();
181 return scan.create_match(std::size_t(length), nil_t(), save, scan.first);
183 return scan.no_match();
185 return scan.empty_match();
188 if_else_parser_gen<ParsableT, CondT> else_p;
191 //////////////////////////////////
192 // if-parser generator, takes the true-parser in brackets and returns the
194 template <typename CondT>
197 if_parser_gen(CondT const& cond_) : cond(cond_) {}
199 template <typename ParsableT>
205 operator[](ParsableT const& subject) const
207 return if_parser<ParsableT, CondT>(subject, cond);
215 //////////////////////////////////
216 // if_p function, returns "if" parser generator
218 template <typename CondT>
219 impl::if_parser_gen<CondT>
220 if_p(CondT const& cond)
222 return impl::if_parser_gen<CondT>(cond);
225 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
227 }} // namespace BOOST_SPIRIT_CLASSIC_NS
229 #endif // BOOST_SPIRIT_IF_HPP