1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/spirit/dynamic/if.hpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,225 @@
1.4 +/*=============================================================================
1.5 + Copyright (c) 2002-2003 Joel de Guzman
1.6 + Copyright (c) 2002 Juan Carlos Arevalo-Baeza
1.7 + Copyright (c) 2002-2003 Martin Wille
1.8 + http://spirit.sourceforge.net/
1.9 +
1.10 + Use, modification and distribution is subject to the Boost Software
1.11 + License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
1.12 + http://www.boost.org/LICENSE_1_0.txt)
1.13 +=============================================================================*/
1.14 +#ifndef BOOST_SPIRIT_IF_HPP
1.15 +#define BOOST_SPIRIT_IF_HPP
1.16 +
1.17 +#include <boost/spirit/core/parser.hpp>
1.18 +#include <boost/spirit/core/composite/composite.hpp>
1.19 +#include <boost/spirit/dynamic/impl/conditions.ipp>
1.20 +
1.21 +namespace boost { namespace spirit {
1.22 +
1.23 + namespace impl {
1.24 +
1.25 + //////////////////////////////////
1.26 + // if-else-parser, holds two alternative parsers and a conditional functor
1.27 + // that selects between them.
1.28 + template <typename ParsableTrueT, typename ParsableFalseT, typename CondT>
1.29 + struct if_else_parser
1.30 + : public condition_evaluator<typename as_parser<CondT>::type>
1.31 + , public binary
1.32 + <
1.33 + typename as_parser<ParsableTrueT>::type,
1.34 + typename as_parser<ParsableFalseT>::type,
1.35 + parser< if_else_parser<ParsableTrueT, ParsableFalseT, CondT> >
1.36 + >
1.37 + {
1.38 + typedef if_else_parser<ParsableTrueT, ParsableFalseT, CondT> self_t;
1.39 +
1.40 + typedef as_parser<ParsableTrueT> as_parser_true_t;
1.41 + typedef as_parser<ParsableFalseT> as_parser_false_t;
1.42 + typedef typename as_parser_true_t::type parser_true_t;
1.43 + typedef typename as_parser_false_t::type parser_false_t;
1.44 + typedef as_parser<CondT> cond_as_parser_t;
1.45 + typedef typename cond_as_parser_t::type condition_t;
1.46 +
1.47 + typedef binary<parser_true_t, parser_false_t, parser<self_t> > base_t;
1.48 + typedef condition_evaluator<condition_t> eval_t;
1.49 +
1.50 + if_else_parser
1.51 + (
1.52 + ParsableTrueT const& p_true,
1.53 + ParsableFalseT const& p_false,
1.54 + CondT const& cond_
1.55 + )
1.56 + : eval_t(cond_as_parser_t::convert(cond_))
1.57 + , base_t
1.58 + (
1.59 + as_parser_true_t::convert(p_true),
1.60 + as_parser_false_t::convert(p_false)
1.61 + )
1.62 + { }
1.63 +
1.64 + template <typename ScannerT>
1.65 + struct result
1.66 + {
1.67 + typedef typename match_result<ScannerT, nil_t>::type type;
1.68 + };
1.69 +
1.70 + template <typename ScannerT>
1.71 + typename parser_result<self_t, ScannerT>::type
1.72 + parse(ScannerT const& scan) const
1.73 + {
1.74 + typedef typename parser_result
1.75 + <parser_true_t, ScannerT>::type then_result_t;
1.76 + typedef typename parser_result
1.77 + <parser_false_t, ScannerT>::type else_result_t;
1.78 +
1.79 + typename ScannerT::iterator_t const save(scan.first);
1.80 +
1.81 + std::ptrdiff_t length = this->evaluate(scan);
1.82 + if (length >= 0)
1.83 + {
1.84 + then_result_t then_result(this->left().parse(scan));
1.85 + if (then_result)
1.86 + {
1.87 + length += then_result.length();
1.88 + return scan.create_match(std::size_t(length), nil_t(), save, scan.first);
1.89 + }
1.90 + }
1.91 + else
1.92 + {
1.93 + else_result_t else_result(this->right().parse(scan));
1.94 + if (else_result)
1.95 + {
1.96 + length = else_result.length();
1.97 + return scan.create_match(std::size_t(length), nil_t(), save, scan.first);
1.98 + }
1.99 + }
1.100 + return scan.no_match();
1.101 + }
1.102 + };
1.103 +
1.104 + //////////////////////////////////
1.105 + // if-else-parser generator, takes the false-parser in brackets
1.106 + // and returns the if-else-parser.
1.107 + template <typename ParsableTrueT, typename CondT>
1.108 + struct if_else_parser_gen
1.109 + {
1.110 + if_else_parser_gen(ParsableTrueT const& p_true_, CondT const& cond_)
1.111 + : p_true(p_true_)
1.112 + , cond(cond_) {}
1.113 +
1.114 + template <typename ParsableFalseT>
1.115 + if_else_parser
1.116 + <
1.117 + ParsableTrueT,
1.118 + ParsableFalseT,
1.119 + CondT
1.120 + >
1.121 + operator[](ParsableFalseT const& p_false) const
1.122 + {
1.123 + return if_else_parser<ParsableTrueT, ParsableFalseT, CondT>
1.124 + (
1.125 + p_true,
1.126 + p_false,
1.127 + cond
1.128 + );
1.129 + }
1.130 +
1.131 + ParsableTrueT const &p_true;
1.132 + CondT const &cond;
1.133 + };
1.134 +
1.135 + //////////////////////////////////
1.136 + // if-parser, conditionally runs a parser is a functor condition is true.
1.137 + // If the condition is fales, it fails the parse.
1.138 + // It can optionally become an if-else-parser through the member else_p.
1.139 + template <typename ParsableT, typename CondT>
1.140 + struct if_parser
1.141 + : public condition_evaluator<typename as_parser<CondT>::type>
1.142 + , public unary
1.143 + <
1.144 + typename as_parser<ParsableT>::type,
1.145 + parser<if_parser<ParsableT, CondT> > >
1.146 + {
1.147 + typedef if_parser<ParsableT, CondT> self_t;
1.148 + typedef as_parser<ParsableT> as_parser_t;
1.149 + typedef typename as_parser_t::type parser_t;
1.150 +
1.151 + typedef as_parser<CondT> cond_as_parser_t;
1.152 + typedef typename cond_as_parser_t::type condition_t;
1.153 + typedef condition_evaluator<condition_t> eval_t;
1.154 + typedef unary<parser_t, parser<self_t> > base_t;
1.155 +
1.156 + if_parser(ParsableT const& p, CondT const& cond_)
1.157 + : eval_t(cond_as_parser_t::convert(cond_))
1.158 + , base_t(as_parser_t::convert(p))
1.159 + , else_p(p, cond_)
1.160 + {}
1.161 +
1.162 + template <typename ScannerT>
1.163 + struct result
1.164 + {
1.165 + typedef typename match_result<ScannerT, nil_t>::type type;
1.166 + };
1.167 +
1.168 + template <typename ScannerT>
1.169 + typename parser_result<self_t, ScannerT>::type
1.170 + parse(ScannerT const& scan) const
1.171 + {
1.172 + typedef typename parser_result<parser_t, ScannerT>::type t_result_t;
1.173 + typename ScannerT::iterator_t const save(scan.first);
1.174 +
1.175 + std::ptrdiff_t length = this->evaluate(scan);
1.176 + if (length >= 0)
1.177 + {
1.178 + t_result_t then_result(this->subject().parse(scan));
1.179 + if (then_result)
1.180 + {
1.181 + length += then_result.length();
1.182 + return scan.create_match(std::size_t(length), nil_t(), save, scan.first);
1.183 + }
1.184 + return scan.no_match();
1.185 + }
1.186 + return scan.empty_match();
1.187 + }
1.188 +
1.189 + if_else_parser_gen<ParsableT, CondT> else_p;
1.190 + };
1.191 +
1.192 + //////////////////////////////////
1.193 + // if-parser generator, takes the true-parser in brackets and returns the
1.194 + // if-parser.
1.195 + template <typename CondT>
1.196 + struct if_parser_gen
1.197 + {
1.198 + if_parser_gen(CondT const& cond_) : cond(cond_) {}
1.199 +
1.200 + template <typename ParsableT>
1.201 + if_parser
1.202 + <
1.203 + ParsableT,
1.204 + CondT
1.205 + >
1.206 + operator[](ParsableT const& subject) const
1.207 + {
1.208 + return if_parser<ParsableT, CondT>(subject, cond);
1.209 + }
1.210 +
1.211 + CondT const &cond;
1.212 + };
1.213 +
1.214 +} // namespace impl
1.215 +
1.216 +//////////////////////////////////
1.217 +// if_p function, returns "if" parser generator
1.218 +
1.219 +template <typename CondT>
1.220 +impl::if_parser_gen<CondT>
1.221 +if_p(CondT const& cond)
1.222 +{
1.223 + return impl::if_parser_gen<CondT>(cond);
1.224 +}
1.225 +
1.226 +}} // namespace boost::spirit
1.227 +
1.228 +#endif // BOOST_SPIRIT_IF_HPP