Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:01:58

0001 /*=============================================================================
0002     Copyright (c) 2002-2003 Hartmut Kaiser
0003     http://spirit.sourceforge.net/
0004 
0005   Distributed under the Boost Software License, Version 1.0. (See accompanying
0006   file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 =============================================================================*/
0008 #ifndef BOOST_SPIRIT_REFACTORING_HPP
0009 #define BOOST_SPIRIT_REFACTORING_HPP
0010 
0011 ///////////////////////////////////////////////////////////////////////////////
0012 #include <boost/static_assert.hpp>
0013 #include <boost/spirit/home/classic/namespace.hpp>
0014 #include <boost/spirit/home/classic/meta/as_parser.hpp>
0015 #include <boost/spirit/home/classic/core/parser.hpp>
0016 #include <boost/spirit/home/classic/core/composite/composite.hpp>
0017 #include <boost/spirit/home/classic/meta/impl/refactoring.ipp>
0018 
0019 ///////////////////////////////////////////////////////////////////////////////
0020 namespace boost { namespace spirit {
0021 
0022 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
0023 
0024 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
0025 #pragma warning(push)
0026 #pragma warning(disable:4512) //assignment operator could not be generated
0027 #endif
0028 
0029 ///////////////////////////////////////////////////////////////////////////////
0030 //
0031 //  refactor_unary_parser class
0032 //
0033 //      This helper template allows to attach an unary operation to a newly
0034 //      constructed parser, which combines the subject of the left operand of
0035 //      the original given parser (BinaryT) with the right operand of the
0036 //      original binary parser through the original binary operation and
0037 //      rewraps the resulting parser with the original unary operator.
0038 //
0039 //      For instance given the parser:
0040 //          *some_parser - another_parser
0041 //
0042 //      will be refactored to:
0043 //          *(some_parser - another_parser)
0044 //
0045 //      If the parser to refactor is not a unary parser, no refactoring is done
0046 //      at all.
0047 //
0048 //      The original parser should be a binary_parser_category parser,
0049 //      else the compilation will fail
0050 //
0051 ///////////////////////////////////////////////////////////////////////////////
0052 
0053 template <typename NestedT = non_nested_refactoring>
0054 class refactor_unary_gen;
0055 
0056 template <typename BinaryT, typename NestedT = non_nested_refactoring>
0057 class refactor_unary_parser :
0058     public parser<refactor_unary_parser<BinaryT, NestedT> > {
0059 
0060 public:
0061     //  the parser to refactor has to be at least a binary_parser_category
0062     //  parser
0063     BOOST_STATIC_ASSERT((
0064         boost::is_convertible<typename BinaryT::parser_category_t,
0065             binary_parser_category>::value
0066     ));
0067 
0068     refactor_unary_parser(BinaryT const& binary_, NestedT const& nested_)
0069     : binary(binary_), nested(nested_) {}
0070 
0071     typedef refactor_unary_parser<BinaryT, NestedT> self_t;
0072     typedef refactor_unary_gen<NestedT> parser_generator_t;
0073     typedef typename BinaryT::left_t::parser_category_t parser_category_t;
0074 
0075     template <typename ScannerT>
0076     typename parser_result<self_t, ScannerT>::type
0077     parse(ScannerT const& scan) const
0078     {
0079         return impl::refactor_unary_type<NestedT>::
0080             parse(*this, scan, binary, nested);
0081     }
0082 
0083 private:
0084     typename as_parser<BinaryT>::type::embed_t binary;
0085     typename NestedT::embed_t nested;
0086 };
0087 
0088 //////////////////////////////////
0089 template <typename NestedT>
0090 class refactor_unary_gen {
0091 
0092 public:
0093     typedef refactor_unary_gen<NestedT> embed_t;
0094 
0095     refactor_unary_gen(NestedT const& nested_ = non_nested_refactoring())
0096     : nested(nested_) {}
0097 
0098     template <typename ParserT>
0099     refactor_unary_parser<ParserT, NestedT>
0100     operator[](parser<ParserT> const& subject) const
0101     {
0102         return refactor_unary_parser<ParserT, NestedT>
0103             (subject.derived(), nested);
0104     }
0105 
0106 private:
0107     typename NestedT::embed_t nested;
0108 };
0109 
0110 const refactor_unary_gen<> refactor_unary_d = refactor_unary_gen<>();
0111 
0112 ///////////////////////////////////////////////////////////////////////////////
0113 //
0114 //  refactor_action_parser class
0115 //
0116 //      This helper template allows to attach an action taken from the left
0117 //      operand of the given binary parser to a newly constructed parser,
0118 //      which combines the subject of the left operand of the original binary
0119 //      parser with the right operand of the original binary parser by means of
0120 //      the original binary operator parser.
0121 //
0122 //      For instance the parser:
0123 //          some_parser[some_attached_functor] - another_parser
0124 //
0125 //      will be refactored to:
0126 //          (some_parser - another_parser)[some_attached_functor]
0127 //
0128 //      If the left operand to refactor is not an action parser, no refactoring
0129 //      is done at all.
0130 //
0131 //      The original parser should be a binary_parser_category parser,
0132 //      else the compilation will fail
0133 //
0134 ///////////////////////////////////////////////////////////////////////////////
0135 
0136 template <typename NestedT = non_nested_refactoring>
0137 class refactor_action_gen;
0138 
0139 template <typename BinaryT, typename NestedT = non_nested_refactoring>
0140 class refactor_action_parser :
0141     public parser<refactor_action_parser<BinaryT, NestedT> > {
0142 
0143 public:
0144     //  the parser to refactor has to be at least a binary_parser_category
0145     //  parser
0146     BOOST_STATIC_ASSERT((
0147         boost::is_convertible<typename BinaryT::parser_category_t,
0148             binary_parser_category>::value
0149     ));
0150 
0151     refactor_action_parser(BinaryT const& binary_, NestedT const& nested_)
0152     : binary(binary_), nested(nested_) {}
0153 
0154     typedef refactor_action_parser<BinaryT, NestedT> self_t;
0155     typedef refactor_action_gen<NestedT> parser_generator_t;
0156     typedef typename BinaryT::left_t::parser_category_t parser_category_t;
0157 
0158     template <typename ScannerT>
0159     typename parser_result<self_t, ScannerT>::type
0160     parse(ScannerT const& scan) const
0161     {
0162         return impl::refactor_action_type<NestedT>::
0163             parse(*this, scan, binary, nested);
0164     }
0165 
0166 private:
0167     typename as_parser<BinaryT>::type::embed_t binary;
0168     typename NestedT::embed_t nested;
0169 };
0170 
0171 //////////////////////////////////
0172 template <typename NestedT>
0173 class refactor_action_gen {
0174 
0175 public:
0176     typedef refactor_action_gen<NestedT> embed_t;
0177 
0178     refactor_action_gen(NestedT const& nested_ = non_nested_refactoring())
0179     : nested(nested_) {}
0180 
0181     template <typename ParserT>
0182     refactor_action_parser<ParserT, NestedT>
0183     operator[](parser<ParserT> const& subject) const
0184     {
0185         return refactor_action_parser<ParserT, NestedT>
0186             (subject.derived(), nested);
0187     }
0188 
0189 private:
0190     typename NestedT::embed_t nested;
0191 };
0192 
0193 const refactor_action_gen<> refactor_action_d = refactor_action_gen<>();
0194 
0195 ///////////////////////////////////////////////////////////////////////////////
0196 //
0197 //  attach_action_parser class
0198 //
0199 //      This helper template allows to attach an action given separately
0200 //      to all parsers, out of which the given parser is constructed and
0201 //      reconstructs a new parser having the same structure.
0202 //
0203 //      For instance the parser:
0204 //          (some_parser >> another_parser)[some_attached_functor]
0205 //
0206 //      will be refactored to:
0207 //          some_parser[some_attached_functor]
0208 //              >> another_parser[some_attached_functor]
0209 //
0210 //      The original parser should be a action_parser_category parser,
0211 //      else the compilation will fail.
0212 //
0213 //      If the parser, to which the action is attached is not an binary parser,
0214 //      no refactoring is done at all.
0215 //
0216 ///////////////////////////////////////////////////////////////////////////////
0217 
0218 template <typename NestedT = non_nested_refactoring>
0219 class attach_action_gen;
0220 
0221 template <typename ActionT, typename NestedT = non_nested_refactoring>
0222 class attach_action_parser :
0223     public parser<attach_action_parser<ActionT, NestedT> > {
0224 
0225 public:
0226     //  the parser to refactor has to be at least a action_parser_category
0227     //  parser
0228     BOOST_STATIC_ASSERT((
0229         boost::is_convertible<typename ActionT::parser_category_t,
0230             action_parser_category>::value
0231     ));
0232 
0233     attach_action_parser(ActionT const& actor_, NestedT const& nested_)
0234     : actor(actor_), nested(nested_) {}
0235 
0236     typedef attach_action_parser<ActionT, NestedT> self_t;
0237     typedef attach_action_gen<NestedT> parser_generator_t;
0238     typedef typename ActionT::parser_category_t parser_category_t;
0239 
0240     template <typename ScannerT>
0241     typename parser_result<self_t, ScannerT>::type
0242     parse(ScannerT const& scan) const
0243     {
0244         return impl::attach_action_type<NestedT>::
0245             parse(*this, scan, actor, nested);
0246     }
0247 
0248 private:
0249     typename as_parser<ActionT>::type::embed_t actor;
0250     typename NestedT::embed_t nested;
0251 };
0252 
0253 //////////////////////////////////
0254 template <typename NestedT>
0255 class attach_action_gen {
0256 
0257 public:
0258     typedef attach_action_gen<NestedT> embed_t;
0259 
0260     attach_action_gen(NestedT const& nested_ = non_nested_refactoring())
0261     : nested(nested_) {}
0262 
0263     template <typename ParserT, typename ActionT>
0264     attach_action_parser<action<ParserT, ActionT>, NestedT>
0265     operator[](action<ParserT, ActionT> const& actor) const
0266     {
0267         return attach_action_parser<action<ParserT, ActionT>, NestedT>
0268             (actor, nested);
0269     }
0270 
0271 private:
0272     typename NestedT::embed_t nested;
0273 };
0274 
0275 const attach_action_gen<> attach_action_d = attach_action_gen<>();
0276 
0277 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
0278 #pragma warning(pop)
0279 #endif
0280 
0281 ///////////////////////////////////////////////////////////////////////////////
0282 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
0283 
0284 }} // namespace BOOST_SPIRIT_CLASSIC_NS
0285 
0286 #endif // BOOST_SPIRIT_REFACTORING_HPP
0287