os/ossrv/ossrv_pub/boost_apis/boost/spirit/dynamic/while.hpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/ossrv_pub/boost_apis/boost/spirit/dynamic/while.hpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,185 @@
     1.4 +/*=============================================================================
     1.5 +    Copyright (c) 2002-2003 Joel de Guzman
     1.6 +    Copyright (c) 2002-2003 Martin Wille
     1.7 +    http://spirit.sourceforge.net/
     1.8 +
     1.9 +    Use, modification and distribution is subject to the Boost Software
    1.10 +    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    1.11 +    http://www.boost.org/LICENSE_1_0.txt)
    1.12 +=============================================================================*/
    1.13 +#ifndef BOOST_SPIRIT_WHILE_HPP
    1.14 +#define BOOST_SPIRIT_WHILE_HPP
    1.15 +
    1.16 +#include <boost/spirit/core/parser.hpp>
    1.17 +#include <boost/spirit/core/composite/composite.hpp>
    1.18 +#include <boost/spirit/dynamic/impl/conditions.ipp>
    1.19 +
    1.20 +////////////////////////////////////////////////////////////////////////////////
    1.21 +namespace boost { namespace spirit {
    1.22 +
    1.23 +    namespace impl {
    1.24 +
    1.25 +    //////////////////////////////////
    1.26 +    // while parser
    1.27 +    // object are created by while_parser_gen and do_parser_gen
    1.28 +    template <typename ParsableT, typename CondT, bool is_do_parser>
    1.29 +    struct while_parser
    1.30 +        : public condition_evaluator< typename as_parser<CondT>::type >
    1.31 +        , public unary // the parent stores a copy of the body parser
    1.32 +        <
    1.33 +            typename as_parser<ParsableT>::type,
    1.34 +            parser<while_parser<ParsableT, CondT, is_do_parser> >
    1.35 +        >
    1.36 +    {
    1.37 +        typedef while_parser<ParsableT, CondT, is_do_parser> self_t;
    1.38 +
    1.39 +        typedef as_parser<ParsableT>            as_parser_t;
    1.40 +        typedef typename as_parser_t::type      parser_t;
    1.41 +        typedef as_parser<CondT>                cond_as_parser_t;
    1.42 +        typedef typename cond_as_parser_t::type condition_t;
    1.43 +
    1.44 +        typedef unary<parser_t, parser<self_t> > base_t;
    1.45 +        typedef condition_evaluator<condition_t> eval_t;
    1.46 +
    1.47 +
    1.48 +        //////////////////////////////
    1.49 +        // constructor, saves condition and body parser
    1.50 +        while_parser(ParsableT const &body, CondT const &cond)
    1.51 +            : eval_t(cond_as_parser_t::convert(cond))
    1.52 +            , base_t(as_parser_t::convert(body))
    1.53 +        {}
    1.54 +
    1.55 +        //////////////////////////////
    1.56 +        // result type computer.
    1.57 +        template <typename ScannerT>
    1.58 +        struct result
    1.59 +        {
    1.60 +            typedef typename match_result
    1.61 +                <ScannerT, nil_t>::type type;
    1.62 +        };
    1.63 +
    1.64 +        //////////////////////////////
    1.65 +        // parse member function
    1.66 +        template <typename ScannerT>
    1.67 +        typename parser_result<self_t, ScannerT>::type
    1.68 +        parse(ScannerT const& scan) const
    1.69 +        {
    1.70 +            typedef typename parser_result<parser_t, ScannerT>::type sresult_t;
    1.71 +            typedef typename ScannerT::iterator_t                    iterator_t;
    1.72 +
    1.73 +            iterator_t save(scan.first);
    1.74 +            std::size_t length = 0;
    1.75 +            int eval_length = 0;
    1.76 +
    1.77 +            bool dont_check_condition = is_do_parser;
    1.78 +
    1.79 +            while (dont_check_condition || (eval_length=this->evaluate(scan))>=0)
    1.80 +            {
    1.81 +                dont_check_condition = false;
    1.82 +                length += eval_length;
    1.83 +                sresult_t tmp(this->subject().parse(scan));
    1.84 +                if (tmp)
    1.85 +                {
    1.86 +                    length+=tmp.length();
    1.87 +                }
    1.88 +                else
    1.89 +                {
    1.90 +                    return scan.no_match();
    1.91 +                }
    1.92 +            }
    1.93 +            return scan.create_match(length, nil_t(), save, scan.first);
    1.94 +        }
    1.95 +    };
    1.96 +
    1.97 +    //////////////////////////////////
    1.98 +    // while-parser generator, takes the body-parser in brackets
    1.99 +    // and returns the actual while-parser.
   1.100 +    template <typename CondT>
   1.101 +    struct while_parser_gen
   1.102 +    {
   1.103 +        //////////////////////////////
   1.104 +        // constructor, saves the condition for use by operator[]
   1.105 +        while_parser_gen(CondT const& cond_) : cond(cond_) {}
   1.106 +
   1.107 +        //////////////////////////////
   1.108 +        // operator[] returns the actual while-parser object
   1.109 +        template <typename ParsableT>
   1.110 +        while_parser<ParsableT, CondT, false>
   1.111 +        operator[](ParsableT const &subject) const
   1.112 +        {
   1.113 +            return while_parser<ParsableT, CondT, false>(subject, cond);
   1.114 +        }
   1.115 +    private:
   1.116 +
   1.117 +        //////////////////////////////
   1.118 +        // the condition is stored by reference here.
   1.119 +        // this should not cause any harm since object of type
   1.120 +        // while_parser_gen<> are only used as temporaries
   1.121 +        // the while-parser object constructed by the operator[]
   1.122 +        // stores a copy of the condition.
   1.123 +        CondT const &cond;
   1.124 +    };
   1.125 +
   1.126 +    //////////////////////////////////
   1.127 +    // do-while-parser generator, takes the condition as
   1.128 +    // parameter to while_p member function and returns the
   1.129 +    // actual do-while-parser.
   1.130 +    template <typename ParsableT>
   1.131 +    struct do_while_parser_gen
   1.132 +    {
   1.133 +        //////////////////////////////
   1.134 +        // constructor. saves the body parser for use by while_p.
   1.135 +        explicit do_while_parser_gen(ParsableT const &body_parser)
   1.136 +            : body(body_parser)
   1.137 +        {}
   1.138 +
   1.139 +        //////////////////////////////
   1.140 +        // while_p returns the actual while-parser object
   1.141 +        template <typename CondT>
   1.142 +        while_parser<ParsableT, CondT, true>
   1.143 +        while_p(CondT cond) const
   1.144 +        {
   1.145 +            return while_parser<ParsableT, CondT, true>(body, cond);
   1.146 +        }
   1.147 +    private:
   1.148 +
   1.149 +        //////////////////////////////
   1.150 +        // the body is stored by reference here
   1.151 +        // this should not cause any harm since object of type
   1.152 +        // do_while_parser_gen<> are only used as temporaries
   1.153 +        // the while-parser object constructed by the while_p
   1.154 +        // member function stores a copy of the body parser.
   1.155 +        ParsableT const &body;
   1.156 +    };
   1.157 +
   1.158 +    struct do_parser_gen
   1.159 +    {
   1.160 +        inline do_parser_gen() {}
   1.161 +
   1.162 +        template <typename ParsableT>
   1.163 +        impl::do_while_parser_gen<ParsableT>
   1.164 +        operator[](ParsableT const& body) const
   1.165 +        {
   1.166 +            return impl::do_while_parser_gen<ParsableT>(body);
   1.167 +        }
   1.168 +    };
   1.169 +} // namespace impl
   1.170 +
   1.171 +//////////////////////////////////
   1.172 +// while_p function, while-parser generator
   1.173 +// Usage: spirit::while_p(Condition)[Body]
   1.174 +template <typename CondT>
   1.175 +impl::while_parser_gen<CondT>
   1.176 +while_p(CondT const& cond)
   1.177 +{
   1.178 +    return impl::while_parser_gen<CondT>(cond);
   1.179 +}
   1.180 +
   1.181 +//////////////////////////////////
   1.182 +// do_p functor, do-while-parser generator
   1.183 +// Usage: spirit::do_p[Body].while_p(Condition)
   1.184 +impl::do_parser_gen const do_p = impl::do_parser_gen();
   1.185 +
   1.186 +}} // namespace boost::spirit
   1.187 +
   1.188 +#endif // BOOST_SPIRIT_WHILE_HPP