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     Use, modification and distribution is subject to the Boost Software
0006     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0007     http://www.boost.org/LICENSE_1_0.txt)
0008 =============================================================================*/
0009 #ifndef BOOST_SPIRIT_REFACTORING_IPP
0010 #define BOOST_SPIRIT_REFACTORING_IPP
0011 
0012 ///////////////////////////////////////////////////////////////////////////////
0013 namespace boost { namespace spirit {
0014 
0015 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
0016 
0017 ///////////////////////////////////////////////////////////////////////////////
0018 //
0019 //  The struct 'self_nested_refactoring' is used to indicate, that the
0020 //  refactoring algorithm should be 'self-nested'.
0021 //
0022 //  The struct 'non_nested_refactoring' is used to indicate, that no nesting
0023 //  of refactoring algorithms is reqired.
0024 //
0025 ///////////////////////////////////////////////////////////////////////////////
0026 
0027 struct non_nested_refactoring { typedef non_nested_refactoring embed_t; };
0028 struct self_nested_refactoring { typedef self_nested_refactoring embed_t; };
0029 
0030 ///////////////////////////////////////////////////////////////////////////////
0031 namespace impl {
0032 
0033 ///////////////////////////////////////////////////////////////////////////////
0034 //
0035 //  Helper templates for refactoring parsers
0036 //
0037 ///////////////////////////////////////////////////////////////////////////////
0038 
0039     ///////////////////////////////////////////////////////////////////////////
0040     //
0041     //  refactor the left unary operand of a binary parser
0042     //
0043     //      The refactoring should be done only if the left operand is an
0044     //      unary_parser_category parser.
0045     //
0046     ///////////////////////////////////////////////////////////////////////////
0047 
0048     ///////////////////////////////////////////////////////////////////////////
0049     template <typename CategoryT>
0050     struct refactor_unary_nested {
0051 
0052         template <
0053             typename ParserT, typename NestedT,
0054             typename ScannerT, typename BinaryT
0055         >
0056         static typename parser_result<ParserT, ScannerT>::type
0057         parse(ParserT const &, ScannerT const& scan, BinaryT const& binary,
0058             NestedT const& /*nested_d*/)
0059         {
0060             return binary.parse(scan);
0061         }
0062     };
0063 
0064     template <>
0065     struct refactor_unary_nested<unary_parser_category> {
0066 
0067         template <
0068             typename ParserT, typename ScannerT, typename BinaryT,
0069             typename NestedT
0070         >
0071         static typename parser_result<ParserT, ScannerT>::type
0072         parse(ParserT const &, ScannerT const& scan, BinaryT const& binary,
0073             NestedT const& nested_d)
0074         {
0075             typedef typename BinaryT::parser_generator_t op_t;
0076             typedef
0077                 typename BinaryT::left_t::parser_generator_t
0078                 unary_t;
0079 
0080             return
0081                 unary_t::generate(
0082                     nested_d[
0083                         op_t::generate(binary.left().subject(), binary.right())
0084                     ]
0085                 ).parse(scan);
0086         }
0087     };
0088 
0089     ///////////////////////////////////////////////////////////////////////////
0090     template <typename CategoryT>
0091     struct refactor_unary_non_nested {
0092 
0093         template <typename ParserT, typename ScannerT, typename BinaryT>
0094         static typename parser_result<ParserT, ScannerT>::type
0095         parse(ParserT const &, ScannerT const& scan, BinaryT const& binary)
0096         {
0097             return binary.parse(scan);
0098         }
0099     };
0100 
0101     template <>
0102     struct refactor_unary_non_nested<unary_parser_category> {
0103 
0104         template <typename ParserT, typename ScannerT, typename BinaryT>
0105         static typename parser_result<ParserT, ScannerT>::type
0106         parse(ParserT const &, ScannerT const& scan, BinaryT const& binary)
0107         {
0108             typedef typename BinaryT::parser_generator_t op_t;
0109             typedef
0110                 typename BinaryT::left_t::parser_generator_t
0111                 unary_t;
0112 
0113             return unary_t::generate(
0114                 op_t::generate(binary.left().subject(), binary.right())
0115             ).parse(scan);
0116         }
0117     };
0118 
0119     ///////////////////////////////////////////////////////////////////////////
0120     template <typename NestedT>
0121     struct refactor_unary_type {
0122 
0123         template <typename ParserT, typename ScannerT, typename BinaryT>
0124         static typename parser_result<ParserT, ScannerT>::type
0125         parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
0126             NestedT const& nested_d)
0127         {
0128             typedef
0129                 typename BinaryT::left_t::parser_category_t
0130                 parser_category_t;
0131 
0132             return refactor_unary_nested<parser_category_t>::
0133                     parse(p, scan, binary, nested_d);
0134         }
0135     };
0136 
0137     template <>
0138     struct refactor_unary_type<non_nested_refactoring> {
0139 
0140         template <typename ParserT, typename ScannerT, typename BinaryT>
0141         static typename parser_result<ParserT, ScannerT>::type
0142         parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
0143             non_nested_refactoring const&)
0144         {
0145             typedef
0146                 typename BinaryT::left_t::parser_category_t
0147                 parser_category_t;
0148 
0149             return refactor_unary_non_nested<parser_category_t>::
0150                     parse(p, scan, binary);
0151         }
0152 
0153     };
0154 
0155     template <>
0156     struct refactor_unary_type<self_nested_refactoring> {
0157 
0158         template <typename ParserT, typename ScannerT, typename BinaryT>
0159         static typename parser_result<ParserT, ScannerT>::type
0160         parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
0161             self_nested_refactoring const &nested_tag)
0162         {
0163             typedef
0164                 typename BinaryT::left_t::parser_category_t
0165                 parser_category_t;
0166             typedef typename ParserT::parser_generator_t parser_generator_t;
0167 
0168             parser_generator_t nested_d(nested_tag);
0169             return refactor_unary_nested<parser_category_t>::
0170                     parse(p, scan, binary, nested_d);
0171         }
0172 
0173     };
0174 
0175     ///////////////////////////////////////////////////////////////////////////
0176     //
0177     //  refactor the action on the left operand of a binary parser
0178     //
0179     //      The refactoring should be done only if the left operand is an
0180     //      action_parser_category parser.
0181     //
0182     ///////////////////////////////////////////////////////////////////////////
0183 
0184     ///////////////////////////////////////////////////////////////////////////
0185     template <typename CategoryT>
0186     struct refactor_action_nested {
0187 
0188         template <
0189             typename ParserT, typename ScannerT, typename BinaryT,
0190             typename NestedT
0191         >
0192         static typename parser_result<ParserT, ScannerT>::type
0193         parse(ParserT const &, ScannerT const& scan, BinaryT const& binary,
0194             NestedT const& nested_d)
0195         {
0196             return nested_d[binary].parse(scan);
0197         }
0198     };
0199 
0200     template <>
0201     struct refactor_action_nested<action_parser_category> {
0202 
0203         template <
0204             typename ParserT, typename ScannerT, typename BinaryT,
0205             typename NestedT
0206         >
0207         static typename parser_result<ParserT, ScannerT>::type
0208         parse(ParserT const &, ScannerT const& scan, BinaryT const& binary,
0209             NestedT const& nested_d)
0210         {
0211             typedef typename BinaryT::parser_generator_t binary_gen_t;
0212 
0213             return (
0214                 nested_d[
0215                     binary_gen_t::generate(
0216                         binary.left().subject(),
0217                         binary.right()
0218                     )
0219                 ][binary.left().predicate()]
0220             ).parse(scan);
0221         }
0222     };
0223 
0224     ///////////////////////////////////////////////////////////////////////////
0225     template <typename CategoryT>
0226     struct refactor_action_non_nested {
0227 
0228         template <typename ParserT, typename ScannerT, typename BinaryT>
0229         static typename parser_result<ParserT, ScannerT>::type
0230         parse(ParserT const &, ScannerT const& scan, BinaryT const& binary)
0231         {
0232             return binary.parse(scan);
0233         }
0234     };
0235 
0236     template <>
0237     struct refactor_action_non_nested<action_parser_category> {
0238 
0239         template <typename ParserT, typename ScannerT, typename BinaryT>
0240         static typename parser_result<ParserT, ScannerT>::type
0241         parse(ParserT const &, ScannerT const& scan, BinaryT const& binary)
0242         {
0243             typedef typename BinaryT::parser_generator_t binary_gen_t;
0244 
0245             return (
0246                 binary_gen_t::generate(
0247                     binary.left().subject(),
0248                     binary.right()
0249                 )[binary.left().predicate()]
0250             ).parse(scan);
0251         }
0252     };
0253 
0254     ///////////////////////////////////////////////////////////////////////////
0255     template <typename NestedT>
0256     struct refactor_action_type {
0257 
0258         template <typename ParserT, typename ScannerT, typename BinaryT>
0259         static typename parser_result<ParserT, ScannerT>::type
0260         parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
0261             NestedT const& nested_d)
0262         {
0263             typedef
0264                 typename BinaryT::left_t::parser_category_t
0265                 parser_category_t;
0266 
0267             return refactor_action_nested<parser_category_t>::
0268                     parse(p, scan, binary, nested_d);
0269         }
0270     };
0271 
0272     template <>
0273     struct refactor_action_type<non_nested_refactoring> {
0274 
0275         template <typename ParserT, typename ScannerT, typename BinaryT>
0276         static typename parser_result<ParserT, ScannerT>::type
0277         parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
0278             non_nested_refactoring const&)
0279         {
0280             typedef
0281                 typename BinaryT::left_t::parser_category_t
0282                 parser_category_t;
0283 
0284             return refactor_action_non_nested<parser_category_t>::
0285                 parse(p, scan, binary);
0286         }
0287     };
0288 
0289     template <>
0290     struct refactor_action_type<self_nested_refactoring> {
0291 
0292         template <typename ParserT, typename ScannerT, typename BinaryT>
0293         static typename parser_result<ParserT, ScannerT>::type
0294         parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
0295             self_nested_refactoring const &nested_tag)
0296         {
0297             typedef typename ParserT::parser_generator_t parser_generator_t;
0298             typedef
0299                 typename BinaryT::left_t::parser_category_t
0300                 parser_category_t;
0301 
0302             parser_generator_t nested_d(nested_tag);
0303             return refactor_action_nested<parser_category_t>::
0304                     parse(p, scan, binary, nested_d);
0305         }
0306     };
0307 
0308     ///////////////////////////////////////////////////////////////////////////
0309     //
0310     //  refactor the action attached to a binary parser
0311     //
0312     //      The refactoring should be done only if the given parser is an
0313     //      binary_parser_category parser.
0314     //
0315     ///////////////////////////////////////////////////////////////////////////
0316 
0317     ///////////////////////////////////////////////////////////////////////////
0318     template <typename CategoryT>
0319     struct attach_action_nested {
0320 
0321         template <
0322             typename ParserT, typename ScannerT, typename ActionT,
0323             typename NestedT
0324         >
0325         static typename parser_result<ParserT, ScannerT>::type
0326         parse(ParserT const &, ScannerT const& scan, ActionT const &action,
0327             NestedT const& /*nested_d*/)
0328         {
0329             return action.parse(scan);
0330         }
0331     };
0332 
0333     template <>
0334     struct attach_action_nested<binary_parser_category> {
0335 
0336         template <
0337             typename ParserT, typename ScannerT, typename ActionT,
0338             typename NestedT
0339         >
0340         static typename parser_result<ParserT, ScannerT>::type
0341         parse(ParserT const &, ScannerT const& scan, ActionT const &action,
0342             NestedT const& nested_d)
0343         {
0344             typedef
0345                 typename ActionT::subject_t::parser_generator_t
0346                 binary_gen_t;
0347 
0348             return (
0349                 binary_gen_t::generate(
0350                     nested_d[action.subject().left()[action.predicate()]],
0351                     nested_d[action.subject().right()[action.predicate()]]
0352                 )
0353             ).parse(scan);
0354         }
0355     };
0356 
0357     ///////////////////////////////////////////////////////////////////////////
0358     template <typename CategoryT>
0359     struct attach_action_non_nested {
0360 
0361         template <typename ParserT, typename ScannerT, typename ActionT>
0362         static typename parser_result<ParserT, ScannerT>::type
0363         parse(ParserT const &, ScannerT const& scan, ActionT const &action)
0364         {
0365             return action.parse(scan);
0366         }
0367     };
0368 
0369     template <>
0370     struct attach_action_non_nested<binary_parser_category> {
0371 
0372         template <typename ParserT, typename ScannerT, typename ActionT>
0373         static typename parser_result<ParserT, ScannerT>::type
0374         parse(ParserT const &, ScannerT const& scan, ActionT const &action)
0375         {
0376             typedef
0377                 typename ActionT::subject_t::parser_generator_t
0378                 binary_gen_t;
0379 
0380             return (
0381                 binary_gen_t::generate(
0382                     action.subject().left()[action.predicate()],
0383                     action.subject().right()[action.predicate()]
0384                 )
0385             ).parse(scan);
0386         }
0387     };
0388 
0389     ///////////////////////////////////////////////////////////////////////////
0390     template <typename NestedT>
0391     struct attach_action_type {
0392 
0393         template <typename ParserT, typename ScannerT, typename ActionT>
0394         static typename parser_result<ParserT, ScannerT>::type
0395         parse(ParserT const &p, ScannerT const& scan, ActionT const& action,
0396             NestedT const& nested_d)
0397         {
0398             typedef
0399                 typename ActionT::subject_t::parser_category_t
0400                 parser_category_t;
0401 
0402             return attach_action_nested<parser_category_t>::
0403                     parse(p, scan, action, nested_d);
0404         }
0405     };
0406 
0407     template <>
0408     struct attach_action_type<non_nested_refactoring> {
0409 
0410         template <typename ParserT, typename ScannerT, typename ActionT>
0411         static typename parser_result<ParserT, ScannerT>::type
0412         parse(ParserT const &p, ScannerT const& scan, ActionT const &action,
0413             non_nested_refactoring const&)
0414         {
0415             typedef
0416                 typename ActionT::subject_t::parser_category_t
0417                 parser_category_t;
0418 
0419             return attach_action_non_nested<parser_category_t>::
0420                 parse(p, scan, action);
0421         }
0422     };
0423 
0424     template <>
0425     struct attach_action_type<self_nested_refactoring> {
0426 
0427         template <typename ParserT, typename ScannerT, typename ActionT>
0428         static typename parser_result<ParserT, ScannerT>::type
0429         parse(ParserT const &p, ScannerT const& scan, ActionT const &action,
0430             self_nested_refactoring const& nested_tag)
0431         {
0432             typedef typename ParserT::parser_generator_t parser_generator_t;
0433             typedef
0434                 typename ActionT::subject_t::parser_category_t
0435                 parser_category_t;
0436 
0437             parser_generator_t nested_d(nested_tag);
0438             return attach_action_nested<parser_category_t>::
0439                     parse(p, scan, action, nested_d);
0440         }
0441     };
0442 
0443 }   // namespace impl
0444 
0445 ///////////////////////////////////////////////////////////////////////////////
0446 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
0447 
0448 }} // namespace boost::spirit
0449 
0450 #endif
0451