os/ossrv/ossrv_pub/boost_apis/boost/spirit/utility/confix.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*=============================================================================
     2     Copyright (c) 2002-2003 Hartmut Kaiser
     3     http://spirit.sourceforge.net/
     4 
     5     Use, modification and distribution is subject to the Boost Software
     6     License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
     7     http://www.boost.org/LICENSE_1_0.txt)
     8 =============================================================================*/
     9 #ifndef BOOST_SPIRIT_CONFIX_HPP
    10 #define BOOST_SPIRIT_CONFIX_HPP
    11 
    12 ///////////////////////////////////////////////////////////////////////////////
    13 #include <boost/config.hpp>
    14 #include <boost/spirit/meta/as_parser.hpp>
    15 #include <boost/spirit/core/composite/operators.hpp>
    16 
    17 #include <boost/spirit/utility/confix_fwd.hpp>
    18 #include <boost/spirit/utility/impl/confix.ipp>
    19 
    20 ///////////////////////////////////////////////////////////////////////////////
    21 namespace boost { namespace spirit {
    22 
    23 ///////////////////////////////////////////////////////////////////////////////
    24 //
    25 //  confix_parser class
    26 //
    27 //      Parses a sequence of 3 sub-matches. This class may
    28 //      be used to parse structures, where the opening part is possibly
    29 //      contained in the expression part and the whole sequence is only
    30 //      parsed after seeing the closing part matching the first opening
    31 //      subsequence. Example: C-comments:
    32 //
    33 //      /* This is a C-comment */
    34 //
    35 ///////////////////////////////////////////////////////////////////////////////
    36 
    37 template<typename NestedT = non_nested, typename LexemeT = non_lexeme>
    38 struct confix_parser_gen;
    39 
    40 template <
    41     typename OpenT, typename ExprT, typename CloseT, typename CategoryT,
    42     typename NestedT, typename LexemeT
    43 >
    44 struct confix_parser :
    45     public parser<
    46         confix_parser<OpenT, ExprT, CloseT, CategoryT, NestedT, LexemeT>
    47     >
    48 {
    49     typedef
    50         confix_parser<OpenT, ExprT, CloseT, CategoryT, NestedT, LexemeT>
    51         self_t;
    52 
    53     confix_parser(OpenT const &open_, ExprT const &expr_, CloseT const &close_)
    54     : open(open_), expr(expr_), close(close_)
    55     {}
    56 
    57     template <typename ScannerT>
    58     typename parser_result<self_t, ScannerT>::type
    59     parse(ScannerT const& scan) const
    60     {
    61         return impl::confix_parser_type<CategoryT>::
    62             parse(NestedT(), LexemeT(), *this, scan, open, expr, close);
    63     }
    64 
    65 private:
    66 
    67     typename as_parser<OpenT>::type::embed_t open;
    68     typename as_parser<ExprT>::type::embed_t expr;
    69     typename as_parser<CloseT>::type::embed_t close;
    70 };
    71 
    72 ///////////////////////////////////////////////////////////////////////////////
    73 //
    74 //  Confix parser generator template
    75 //
    76 //      This is a helper for generating a correct confix_parser<> from
    77 //      auxiliary parameters. There are the following types supported as
    78 //      parameters yet: parsers, single characters and strings (see
    79 //      as_parser).
    80 //
    81 //      If the body parser is an action_parser_category type parser (a parser
    82 //      with an attached semantic action) we have to do something special. This
    83 //      happens, if the user wrote something like:
    84 //
    85 //          confix_p(open, body[f], close)
    86 //
    87 //      where 'body' is the parser matching the body of the confix sequence
    88 //      and 'f' is a functor to be called after matching the body. If we would
    89 //      do nothing, the resulting code would parse the sequence as follows:
    90 //
    91 //          start >> (body[f] - close) >> close
    92 //
    93 //      what in most cases is not what the user expects.
    94 //      (If this _is_ what you've expected, then please use the confix_p
    95 //      generator function 'direct()', which will inhibit
    96 //      re-attaching the actor to the body parser).
    97 //
    98 //      To make the confix parser behave as expected:
    99 //
   100 //          start >> (body - close)[f] >> close
   101 //
   102 //      the actor attached to the 'body' parser has to be re-attached to the
   103 //      (body - close) parser construct, which will make the resulting confix
   104 //      parser 'do the right thing'. This refactoring is done by the help of
   105 //      the refactoring parsers (see the files refactoring.[hi]pp).
   106 //
   107 //      Additionally special care must be taken, if the body parser is a
   108 //      unary_parser_category type parser as
   109 //
   110 //          confix_p(open, *anychar_p, close)
   111 //
   112 //      which without any refactoring would result in
   113 //
   114 //          start >> (*anychar_p - close) >> close
   115 //
   116 //      and will not give the expected result (*anychar_p will eat up all the
   117 //      input up to the end of the input stream). So we have to refactor this
   118 //      into:
   119 //
   120 //          start >> *(anychar_p - close) >> close
   121 //
   122 //      what will give the correct result.
   123 //
   124 //      The case, where the body parser is a combination of the two mentioned
   125 //      problems (i.e. the body parser is a unary parser  with an attached
   126 //      action), is handled accordingly too:
   127 //
   128 //          confix_p(start, (*anychar_p)[f], end)
   129 //
   130 //      will be parsed as expected:
   131 //
   132 //          start >> (*(anychar_p - end))[f] >> end.
   133 //
   134 ///////////////////////////////////////////////////////////////////////////////
   135 
   136 template<typename NestedT, typename LexemeT>
   137 struct confix_parser_gen
   138 {
   139     // Generic generator function for creation of concrete confix parsers
   140 
   141     template<typename StartT, typename ExprT, typename EndT>
   142     struct paren_op_result_type
   143     {
   144         typedef confix_parser<
   145             typename as_parser<StartT>::type,
   146             typename as_parser<ExprT>::type,
   147             typename as_parser<EndT>::type,
   148             typename as_parser<ExprT>::type::parser_category_t,
   149             NestedT,
   150             LexemeT
   151         > type;
   152     };
   153   
   154     template<typename StartT, typename ExprT, typename EndT>
   155     typename paren_op_result_type<StartT, ExprT, EndT>::type 
   156     operator()(StartT const &start_, ExprT const &expr_, EndT const &end_) const
   157     {
   158         typedef typename paren_op_result_type<StartT,ExprT,EndT>::type 
   159             return_t;
   160 
   161         return return_t(
   162             as_parser<StartT>::convert(start_),
   163             as_parser<ExprT>::convert(expr_),
   164             as_parser<EndT>::convert(end_)
   165         );
   166     }
   167 
   168     // Generic generator function for creation of concrete confix parsers
   169     // which have an action directly attached to the ExprT part of the
   170     // parser (see comment above, no automatic refactoring)
   171 
   172     template<typename StartT, typename ExprT, typename EndT>
   173     struct direct_result_type
   174     {
   175         typedef confix_parser<
   176             typename as_parser<StartT>::type,
   177             typename as_parser<ExprT>::type,
   178             typename as_parser<EndT>::type,
   179             plain_parser_category,   // do not re-attach action
   180             NestedT,
   181             LexemeT
   182         > type;
   183     };
   184 
   185     template<typename StartT, typename ExprT, typename EndT>
   186     typename direct_result_type<StartT,ExprT,EndT>::type
   187     direct(StartT const &start_, ExprT const &expr_, EndT const &end_) const
   188     {
   189         typedef typename direct_result_type<StartT,ExprT,EndT>::type
   190             return_t;
   191 
   192         return return_t(
   193             as_parser<StartT>::convert(start_),
   194             as_parser<ExprT>::convert(expr_),
   195             as_parser<EndT>::convert(end_)
   196         );
   197     }
   198 };
   199 
   200 ///////////////////////////////////////////////////////////////////////////////
   201 //
   202 //  Predefined non_nested confix parser generators
   203 //
   204 ///////////////////////////////////////////////////////////////////////////////
   205 
   206 const confix_parser_gen<non_nested, non_lexeme> confix_p =
   207     confix_parser_gen<non_nested, non_lexeme>();
   208 
   209 ///////////////////////////////////////////////////////////////////////////////
   210 //
   211 //  Comments are special types of confix parsers
   212 //
   213 //      Comment parser generator template. This is a helper for generating a
   214 //      correct confix_parser<> from auxiliary parameters, which is able to
   215 //      parse comment constructs: (StartToken >> Comment text >> EndToken).
   216 //
   217 //      There are the following types supported as parameters yet: parsers,
   218 //      single characters and strings (see as_parser).
   219 //
   220 //      There are two diffenerent predefined comment parser generators
   221 //      (comment_p and comment_nest_p, see below), which may be used for
   222 //      creating special comment parsers in two different ways.
   223 //
   224 //      If these are used with one parameter, a comment starting with the given
   225 //      first parser parameter up to the end of the line is matched. So for
   226 //      instance the following parser matches C++ style comments:
   227 //
   228 //          comment_p("//").
   229 //
   230 //      If these are used with two parameters, a comment starting with the
   231 //      first parser parameter up to the second parser parameter is matched.
   232 //      For instance a C style comment parser should be constrcuted as:
   233 //
   234 //          comment_p("/*", "*/").
   235 //
   236 //      Please note, that a comment is parsed implicitly as if the whole
   237 //      comment_p(...) statement were embedded into a lexeme_d[] directive.
   238 //
   239 ///////////////////////////////////////////////////////////////////////////////
   240 
   241 template<typename NestedT>
   242 struct comment_parser_gen
   243 {
   244     // Generic generator function for creation of concrete comment parsers
   245     // from an open token. The newline parser eol_p is used as the
   246     // closing token.
   247 
   248     template<typename StartT>
   249     struct paren_op1_result_type
   250     {
   251         typedef confix_parser<
   252             typename as_parser<StartT>::type,
   253             kleene_star<anychar_parser>,
   254             alternative<eol_parser, end_parser>,
   255             unary_parser_category,          // there is no action to re-attach
   256             NestedT,
   257             is_lexeme                       // insert implicit lexeme_d[]
   258         >
   259         type;
   260     };
   261 
   262     template<typename StartT>
   263     typename paren_op1_result_type<StartT>::type 
   264     operator() (StartT const &start_) const
   265     {
   266         typedef typename paren_op1_result_type<StartT>::type
   267             return_t;
   268 
   269         return return_t(
   270             as_parser<StartT>::convert(start_),
   271             *anychar_p,
   272             eol_p | end_p
   273         );
   274     }
   275 
   276     // Generic generator function for creation of concrete comment parsers
   277     // from an open and a close tokens.
   278 
   279     template<typename StartT, typename EndT>
   280     struct paren_op2_result_type
   281     {
   282         typedef confix_parser<
   283             typename as_parser<StartT>::type,
   284             kleene_star<anychar_parser>,
   285             typename as_parser<EndT>::type,
   286             unary_parser_category,          // there is no action to re-attach
   287             NestedT,
   288             is_lexeme                       // insert implicit lexeme_d[]
   289         > type;
   290     };
   291 
   292     template<typename StartT, typename EndT>
   293     typename paren_op2_result_type<StartT,EndT>::type
   294     operator() (StartT const &start_, EndT const &end_) const
   295     {
   296         typedef typename paren_op2_result_type<StartT,EndT>::type
   297             return_t;
   298 
   299         return return_t(
   300             as_parser<StartT>::convert(start_),
   301             *anychar_p,
   302             as_parser<EndT>::convert(end_)
   303         );
   304     }
   305 };
   306 
   307 ///////////////////////////////////////////////////////////////////////////////
   308 //
   309 //  Predefined non_nested comment parser generator
   310 //
   311 ///////////////////////////////////////////////////////////////////////////////
   312 
   313 const comment_parser_gen<non_nested> comment_p =
   314     comment_parser_gen<non_nested>();
   315 
   316 ///////////////////////////////////////////////////////////////////////////////
   317 //
   318 //  comment_nest_parser class
   319 //
   320 //      Parses a nested comments.
   321 //      Example: nested PASCAL-comments:
   322 //
   323 //      { This is a { nested } PASCAL-comment }
   324 //
   325 ///////////////////////////////////////////////////////////////////////////////
   326 
   327 template<typename OpenT, typename CloseT>
   328 struct comment_nest_parser:
   329     public parser<comment_nest_parser<OpenT, CloseT> >
   330 {
   331     typedef comment_nest_parser<OpenT, CloseT> self_t;
   332 
   333     comment_nest_parser(OpenT const &open_, CloseT const &close_):
   334         open(open_), close(close_)
   335     {}
   336 
   337     template<typename ScannerT>
   338     typename parser_result<self_t, ScannerT>::type
   339         parse(ScannerT const &scan) const
   340     {
   341         return do_parse(
   342             open >> *(*this | (anychar_p - close)) >> close,
   343             scan);
   344     }
   345 
   346 private:
   347     template<typename ParserT, typename ScannerT>
   348     typename parser_result<self_t, ScannerT>::type
   349         do_parse(ParserT const &p, ScannerT const &scan) const
   350     {
   351         return
   352             impl::contiguous_parser_parse<
   353                 typename parser_result<ParserT, ScannerT>::type
   354             >(p, scan, scan);
   355     }
   356 
   357     typename as_parser<OpenT>::type::embed_t open;
   358     typename as_parser<CloseT>::type::embed_t close;
   359 };
   360 
   361 ///////////////////////////////////////////////////////////////////////////////
   362 //
   363 //  Predefined nested comment parser generator
   364 //
   365 ///////////////////////////////////////////////////////////////////////////////
   366 
   367 template<typename OpenT, typename CloseT>
   368 struct comment_nest_p_result
   369 {
   370     typedef comment_nest_parser<
   371         typename as_parser<OpenT>::type,
   372         typename as_parser<CloseT>::type
   373     > type;
   374 };
   375 
   376 template<typename OpenT, typename CloseT>
   377 inline typename comment_nest_p_result<OpenT,CloseT>::type 
   378 comment_nest_p(OpenT const &open, CloseT const &close)
   379 {
   380     typedef typename comment_nest_p_result<OpenT,CloseT>::type
   381         result_t;
   382 
   383     return result_t(
   384         as_parser<OpenT>::convert(open),
   385         as_parser<CloseT>::convert(close)
   386     );
   387 }
   388 
   389 ///////////////////////////////////////////////////////////////////////////////
   390 }} // namespace boost::spirit
   391 
   392 #endif