Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:40:49

0001 #ifndef BOOST_METAPARSE_V1_GRAMMAR_HPP
0002 #define BOOST_METAPARSE_V1_GRAMMAR_HPP
0003 
0004 // Copyright Abel Sinkovics (abel@sinkovics.hu)  2012.
0005 // Distributed under the Boost Software License, Version 1.0.
0006 //    (See accompanying file LICENSE_1_0.txt or copy at
0007 //          http://www.boost.org/LICENSE_1_0.txt)
0008 
0009 #include <boost/metaparse/v1/repeated.hpp>
0010 #include <boost/metaparse/v1/repeated1.hpp>
0011 #include <boost/metaparse/v1/sequence.hpp>
0012 #include <boost/metaparse/v1/one_of.hpp>
0013 #include <boost/metaparse/v1/transform.hpp>
0014 #include <boost/metaparse/v1/lit.hpp>
0015 #include <boost/metaparse/v1/lit_c.hpp>
0016 #include <boost/metaparse/v1/token.hpp>
0017 #include <boost/metaparse/v1/keyword.hpp>
0018 #include <boost/metaparse/v1/middle_of.hpp>
0019 #include <boost/metaparse/v1/last_of.hpp>
0020 #include <boost/metaparse/v1/always.hpp>
0021 #include <boost/metaparse/v1/one_char_except_c.hpp>
0022 #include <boost/metaparse/v1/foldr1.hpp>
0023 #include <boost/metaparse/v1/foldl_start_with_parser.hpp>
0024 #include <boost/metaparse/v1/alphanum.hpp>
0025 #include <boost/metaparse/v1/build_parser.hpp>
0026 #include <boost/metaparse/v1/entire_input.hpp>
0027 #include <boost/metaparse/v1/string.hpp>
0028 #include <boost/metaparse/v1/impl/front_inserter.hpp>
0029 
0030 #include <boost/mpl/at.hpp>
0031 #include <boost/mpl/map.hpp>
0032 #include <boost/mpl/eval_if.hpp>
0033 #include <boost/mpl/has_key.hpp>
0034 #include <boost/mpl/lambda.hpp>
0035 #include <boost/mpl/front.hpp>
0036 #include <boost/mpl/back.hpp>
0037 #include <boost/mpl/pair.hpp>
0038 #include <boost/mpl/insert.hpp>
0039 
0040 /*
0041  * The grammar
0042  *
0043  * rule_definition ::= name_token define_token expression
0044  * expression ::= seq_expression (or_token seq_expression)*
0045  * seq_expression ::= repeated_expression+
0046  * repeated_expression ::= name_expression (repeated_token | repeated1_token)*
0047  * name_expression ::= char_token | name_token | bracket_expression
0048  * bracket_expression ::= open_bracket_token expression close_bracket_token
0049  */
0050 
0051 namespace boost
0052 {
0053   namespace metaparse
0054   {
0055     namespace v1
0056     {
0057       namespace grammar_util
0058       {
0059         template <char Op, class FState>
0060         struct repeated_apply_impl
0061         {
0062           typedef repeated_apply_impl type;
0063         
0064           template <class G>
0065           struct apply :
0066             repeated<typename FState::template apply<G>::type>
0067           {};
0068         };
0069 
0070         template <class FState>
0071         struct repeated_apply_impl<'+', FState>
0072         {
0073           typedef repeated_apply_impl type;
0074         
0075           template <class G>
0076           struct apply :
0077             repeated1<typename FState::template apply<G>::type>
0078           {};
0079         };
0080 
0081         struct build_repeated
0082         {
0083           typedef build_repeated type;
0084         
0085           template <class FState, class T>
0086           struct apply : repeated_apply_impl<T::type::value, FState> {};
0087         };
0088         
0089         struct build_sequence
0090         {
0091           typedef build_sequence type;
0092         
0093           template <class FState, class FP>
0094           struct apply_impl
0095           {
0096             typedef apply_impl type;
0097         
0098             template <class G>
0099             struct apply :
0100               sequence<
0101                 typename FState::template apply<G>::type,
0102                 typename FP::template apply<G>::type
0103               >
0104             {};
0105           };
0106         
0107           template <class FState, class FP>
0108           struct apply : apply_impl<FState, FP> {};
0109         };
0110         
0111         struct build_selection
0112         {
0113           typedef build_selection type;
0114         
0115           template <class FState, class FP>
0116           struct apply_impl
0117           {
0118             typedef apply_impl type;
0119         
0120             template <class G>
0121             struct apply :
0122               one_of<
0123                 typename FState::template apply<G>::type,
0124                 typename FP::template apply<G>::type
0125               >
0126             {};
0127           };
0128         
0129           template <class FState, class FP>
0130           struct apply : apply_impl<FState, FP> {};
0131         };
0132         
0133         template <class G, class Name>
0134         struct get_parser
0135         {
0136           typedef
0137             typename boost::mpl::at<typename G::rules, Name>::type
0138               ::template apply<G>
0139             p;
0140         
0141           template <class Actions>
0142           struct impl : transform<typename p::type, typename Actions::type> {};
0143         
0144           typedef
0145             typename boost::mpl::eval_if<
0146               typename boost::mpl::has_key<typename G::actions, Name>::type,
0147               impl<boost::mpl::at<typename G::actions, Name> >,
0148               p
0149             >::type
0150             type;
0151         };
0152         
0153         struct build_name
0154         {
0155           typedef build_name type;
0156         
0157           template <class Name>
0158           struct apply_impl
0159           {
0160             typedef apply_impl type;
0161         
0162             template <class G>
0163             struct apply : get_parser<G, Name> {};
0164           };
0165         
0166           template <class Name>
0167           struct apply : apply_impl<Name> {};
0168         };
0169         
0170         struct build_char
0171         {
0172           typedef build_char type;
0173         
0174           template <class C>
0175           struct apply_impl
0176           {
0177             typedef apply_impl type;
0178         
0179             template <class G>
0180             struct apply : lit<C> {};
0181           };
0182         
0183           template <class C>
0184           struct apply : apply_impl<C> {};
0185         };
0186         
0187         typedef token<lit_c<'*'> > repeated_token;
0188         typedef token<lit_c<'+'> > repeated1_token;
0189         typedef token<lit_c<'|'> > or_token;
0190         typedef token<lit_c<'('> > open_bracket_token;
0191         typedef token<lit_c<')'> > close_bracket_token;
0192         typedef token<keyword<string<':',':','='> > > define_token;
0193 
0194         typedef
0195           middle_of<
0196             lit_c<'\''>,
0197             one_of<
0198               last_of<
0199                 lit_c<'\\'>,
0200                 one_of<
0201                   always<lit_c<'n'>, boost::mpl::char_<'\n'> >,
0202                   always<lit_c<'r'>, boost::mpl::char_<'\r'> >,
0203                   always<lit_c<'t'>, boost::mpl::char_<'\t'> >,
0204                   lit_c<'\\'>,
0205                   lit_c<'\''>
0206                 >
0207               >,
0208               one_char_except_c<'\''>
0209             >,
0210             token<lit_c<'\''> >
0211           >
0212           char_token;
0213         
0214         typedef
0215           token<
0216             foldr1<
0217               one_of<alphanum, lit_c<'_'> >,
0218               string<>,
0219               impl::front_inserter
0220             >
0221           >
0222           name_token;
0223         
0224         struct expression;
0225         
0226         typedef
0227           middle_of<open_bracket_token, expression, close_bracket_token>
0228           bracket_expression;
0229 
0230         typedef
0231           one_of<
0232             transform<char_token, build_char>,
0233             transform<name_token, build_name>,
0234             bracket_expression
0235           >
0236           name_expression;
0237 
0238         typedef
0239           foldl_start_with_parser<
0240             one_of<repeated_token, repeated1_token>,
0241             name_expression,
0242             build_repeated
0243           >
0244           repeated_expression;
0245         
0246         typedef
0247           foldl_start_with_parser<
0248             repeated_expression,
0249             repeated_expression,
0250             build_sequence
0251           >
0252           seq_expression;
0253         
0254         struct expression :
0255           foldl_start_with_parser<
0256             last_of<or_token, seq_expression>,
0257             seq_expression,
0258             build_selection
0259           >
0260         {};
0261         
0262         typedef sequence<name_token, define_token, expression> rule_definition;
0263 
0264         typedef build_parser<entire_input<rule_definition> > parser_parser;
0265         
0266         template <class P>
0267         struct build_native_parser
0268         {
0269           typedef build_native_parser type;
0270         
0271           template <class G>
0272           struct apply
0273           {
0274             typedef P type;
0275           };
0276         };
0277         
0278         template <class S>
0279         struct build_parsed_parser
0280         {
0281           typedef typename parser_parser::apply<S>::type p;
0282           typedef typename boost::mpl::front<p>::type name;
0283           typedef typename boost::mpl::back<p>::type exp;
0284         
0285           struct the_parser
0286           {
0287             typedef the_parser type;
0288         
0289             template <class G>
0290             struct apply : exp::template apply<G> {};
0291           };
0292         
0293           typedef boost::mpl::pair<name, the_parser> type;
0294         };
0295         
0296         typedef build_parser<name_token> name_parser;
0297         
0298         template <class S>
0299         struct rebuild : name_parser::template apply<S> {};
0300         
0301         struct no_action;
0302         
0303         template <class G, class P, class F>
0304         struct add_rule;
0305         
0306         template <class G, class Name, class P>
0307         struct add_import;
0308         
0309         template <class Start, class Rules, class Actions>
0310         struct grammar_builder
0311         {
0312           typedef grammar_builder type;
0313           typedef Rules rules;
0314           typedef Actions actions;
0315         
0316           // Make it a parser
0317           template <class S, class Pos>
0318           struct apply :
0319             get_parser<
0320               grammar_builder,
0321               typename rebuild<Start>::type
0322             >::type::template apply<S, Pos>
0323           {};
0324         
0325           template <class Name, class P>
0326           struct import :
0327             add_import<grammar_builder, typename rebuild<Name>::type, P>
0328           {};
0329         
0330           template <class Def, class Action = no_action>
0331           struct rule :
0332             add_rule<grammar_builder, build_parsed_parser<Def>, Action>
0333           {};
0334         };
0335         
0336         template <class Start, class Rules, class Actions, class P>
0337         struct add_rule<grammar_builder<Start, Rules, Actions>, P, no_action> :
0338           grammar_builder<
0339             Start,
0340             typename boost::mpl::insert<Rules, typename P::type>::type,
0341             Actions
0342           >
0343         {};
0344         
0345         template <class Start, class Rules, class Actions, class P, class F>
0346         struct add_rule<grammar_builder<Start, Rules, Actions>, P, F> :
0347           grammar_builder<
0348             Start,
0349             typename boost::mpl::insert<Rules, typename P::type>::type,
0350             typename boost::mpl::insert<
0351               Actions,
0352               boost::mpl::pair<
0353                 typename P::name,
0354                 typename boost::mpl::lambda<F>::type
0355               >
0356             >
0357             ::type
0358           >
0359         {};
0360         
0361         template <class Start, class Rules, class Actions, class Name, class P>
0362         struct add_import<grammar_builder<Start, Rules, Actions>, Name, P> :
0363           grammar_builder<
0364             Start,
0365             typename boost::mpl::insert<
0366               Rules,
0367               boost::mpl::pair<Name, build_native_parser<P> >
0368             >::type,
0369             Actions
0370           >
0371         {};
0372       }
0373 
0374       template <class Start = string<'S'> >
0375       struct grammar :
0376         grammar_util::grammar_builder<
0377           Start,
0378           boost::mpl::map<>,
0379           boost::mpl::map<>
0380         >
0381       {};
0382     }
0383   }
0384 }
0385 
0386 #endif