First public contribution.
1 /*=============================================================================
2 Copyright (c) 2002-2003 Hartmut Kaiser
3 http://spirit.sourceforge.net/
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
12 ///////////////////////////////////////////////////////////////////////////////
13 #include <boost/config.hpp>
14 #include <boost/spirit/meta/as_parser.hpp>
15 #include <boost/spirit/core/composite/operators.hpp>
17 #include <boost/spirit/utility/confix_fwd.hpp>
18 #include <boost/spirit/utility/impl/confix.ipp>
20 ///////////////////////////////////////////////////////////////////////////////
21 namespace boost { namespace spirit {
23 ///////////////////////////////////////////////////////////////////////////////
25 // confix_parser class
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:
33 // /* This is a C-comment */
35 ///////////////////////////////////////////////////////////////////////////////
37 template<typename NestedT = non_nested, typename LexemeT = non_lexeme>
38 struct confix_parser_gen;
41 typename OpenT, typename ExprT, typename CloseT, typename CategoryT,
42 typename NestedT, typename LexemeT
44 struct confix_parser :
46 confix_parser<OpenT, ExprT, CloseT, CategoryT, NestedT, LexemeT>
50 confix_parser<OpenT, ExprT, CloseT, CategoryT, NestedT, LexemeT>
53 confix_parser(OpenT const &open_, ExprT const &expr_, CloseT const &close_)
54 : open(open_), expr(expr_), close(close_)
57 template <typename ScannerT>
58 typename parser_result<self_t, ScannerT>::type
59 parse(ScannerT const& scan) const
61 return impl::confix_parser_type<CategoryT>::
62 parse(NestedT(), LexemeT(), *this, scan, open, expr, close);
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;
72 ///////////////////////////////////////////////////////////////////////////////
74 // Confix parser generator template
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
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:
85 // confix_p(open, body[f], close)
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:
91 // start >> (body[f] - close) >> close
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).
98 // To make the confix parser behave as expected:
100 // start >> (body - close)[f] >> close
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).
107 // Additionally special care must be taken, if the body parser is a
108 // unary_parser_category type parser as
110 // confix_p(open, *anychar_p, close)
112 // which without any refactoring would result in
114 // start >> (*anychar_p - close) >> close
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
120 // start >> *(anychar_p - close) >> close
122 // what will give the correct result.
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:
128 // confix_p(start, (*anychar_p)[f], end)
130 // will be parsed as expected:
132 // start >> (*(anychar_p - end))[f] >> end.
134 ///////////////////////////////////////////////////////////////////////////////
136 template<typename NestedT, typename LexemeT>
137 struct confix_parser_gen
139 // Generic generator function for creation of concrete confix parsers
141 template<typename StartT, typename ExprT, typename EndT>
142 struct paren_op_result_type
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,
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
158 typedef typename paren_op_result_type<StartT,ExprT,EndT>::type
162 as_parser<StartT>::convert(start_),
163 as_parser<ExprT>::convert(expr_),
164 as_parser<EndT>::convert(end_)
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)
172 template<typename StartT, typename ExprT, typename EndT>
173 struct direct_result_type
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
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
189 typedef typename direct_result_type<StartT,ExprT,EndT>::type
193 as_parser<StartT>::convert(start_),
194 as_parser<ExprT>::convert(expr_),
195 as_parser<EndT>::convert(end_)
200 ///////////////////////////////////////////////////////////////////////////////
202 // Predefined non_nested confix parser generators
204 ///////////////////////////////////////////////////////////////////////////////
206 const confix_parser_gen<non_nested, non_lexeme> confix_p =
207 confix_parser_gen<non_nested, non_lexeme>();
209 ///////////////////////////////////////////////////////////////////////////////
211 // Comments are special types of confix parsers
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).
217 // There are the following types supported as parameters yet: parsers,
218 // single characters and strings (see as_parser).
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.
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:
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:
234 // comment_p("/*", "*/").
236 // Please note, that a comment is parsed implicitly as if the whole
237 // comment_p(...) statement were embedded into a lexeme_d[] directive.
239 ///////////////////////////////////////////////////////////////////////////////
241 template<typename NestedT>
242 struct comment_parser_gen
244 // Generic generator function for creation of concrete comment parsers
245 // from an open token. The newline parser eol_p is used as the
248 template<typename StartT>
249 struct paren_op1_result_type
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
257 is_lexeme // insert implicit lexeme_d[]
262 template<typename StartT>
263 typename paren_op1_result_type<StartT>::type
264 operator() (StartT const &start_) const
266 typedef typename paren_op1_result_type<StartT>::type
270 as_parser<StartT>::convert(start_),
276 // Generic generator function for creation of concrete comment parsers
277 // from an open and a close tokens.
279 template<typename StartT, typename EndT>
280 struct paren_op2_result_type
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
288 is_lexeme // insert implicit lexeme_d[]
292 template<typename StartT, typename EndT>
293 typename paren_op2_result_type<StartT,EndT>::type
294 operator() (StartT const &start_, EndT const &end_) const
296 typedef typename paren_op2_result_type<StartT,EndT>::type
300 as_parser<StartT>::convert(start_),
302 as_parser<EndT>::convert(end_)
307 ///////////////////////////////////////////////////////////////////////////////
309 // Predefined non_nested comment parser generator
311 ///////////////////////////////////////////////////////////////////////////////
313 const comment_parser_gen<non_nested> comment_p =
314 comment_parser_gen<non_nested>();
316 ///////////////////////////////////////////////////////////////////////////////
318 // comment_nest_parser class
320 // Parses a nested comments.
321 // Example: nested PASCAL-comments:
323 // { This is a { nested } PASCAL-comment }
325 ///////////////////////////////////////////////////////////////////////////////
327 template<typename OpenT, typename CloseT>
328 struct comment_nest_parser:
329 public parser<comment_nest_parser<OpenT, CloseT> >
331 typedef comment_nest_parser<OpenT, CloseT> self_t;
333 comment_nest_parser(OpenT const &open_, CloseT const &close_):
334 open(open_), close(close_)
337 template<typename ScannerT>
338 typename parser_result<self_t, ScannerT>::type
339 parse(ScannerT const &scan) const
342 open >> *(*this | (anychar_p - close)) >> close,
347 template<typename ParserT, typename ScannerT>
348 typename parser_result<self_t, ScannerT>::type
349 do_parse(ParserT const &p, ScannerT const &scan) const
352 impl::contiguous_parser_parse<
353 typename parser_result<ParserT, ScannerT>::type
357 typename as_parser<OpenT>::type::embed_t open;
358 typename as_parser<CloseT>::type::embed_t close;
361 ///////////////////////////////////////////////////////////////////////////////
363 // Predefined nested comment parser generator
365 ///////////////////////////////////////////////////////////////////////////////
367 template<typename OpenT, typename CloseT>
368 struct comment_nest_p_result
370 typedef comment_nest_parser<
371 typename as_parser<OpenT>::type,
372 typename as_parser<CloseT>::type
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)
380 typedef typename comment_nest_p_result<OpenT,CloseT>::type
384 as_parser<OpenT>::convert(open),
385 as_parser<CloseT>::convert(close)
389 ///////////////////////////////////////////////////////////////////////////////
390 }} // namespace boost::spirit