1 /*=============================================================================
2 Copyright (c) 2002-2003 Hartmut Kaiser
3 http://spirit.sourceforge.net/
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 =============================================================================*/
8 #ifndef BOOST_SPIRIT_CONFIX_HPP
9 #define BOOST_SPIRIT_CONFIX_HPP
11 ///////////////////////////////////////////////////////////////////////////////
12 #include <boost/config.hpp>
13 #include <boost/spirit/home/classic/namespace.hpp>
14 #include <boost/spirit/home/classic/meta/as_parser.hpp>
15 #include <boost/spirit/home/classic/core/composite/operators.hpp>
17 #include <boost/spirit/home/classic/utility/confix_fwd.hpp>
18 #include <boost/spirit/home/classic/utility/impl/confix.ipp>
20 ///////////////////////////////////////////////////////////////////////////////
21 namespace boost { namespace spirit {
23 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
25 ///////////////////////////////////////////////////////////////////////////////
27 // confix_parser class
29 // Parses a sequence of 3 sub-matches. This class may
30 // be used to parse structures, where the opening part is possibly
31 // contained in the expression part and the whole sequence is only
32 // parsed after seeing the closing part matching the first opening
33 // subsequence. Example: C-comments:
35 // /* This is a C-comment */
37 ///////////////////////////////////////////////////////////////////////////////
39 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
41 #pragma warning(disable:4512) //assignment operator could not be generated
44 template<typename NestedT = non_nested, typename LexemeT = non_lexeme>
45 struct confix_parser_gen;
48 typename OpenT, typename ExprT, typename CloseT, typename CategoryT,
49 typename NestedT, typename LexemeT
51 struct confix_parser :
53 confix_parser<OpenT, ExprT, CloseT, CategoryT, NestedT, LexemeT>
57 confix_parser<OpenT, ExprT, CloseT, CategoryT, NestedT, LexemeT>
60 confix_parser(OpenT const &open_, ExprT const &expr_, CloseT const &close_)
61 : open(open_), expr(expr_), close(close_)
64 template <typename ScannerT>
65 typename parser_result<self_t, ScannerT>::type
66 parse(ScannerT const& scan) const
68 return impl::confix_parser_type<CategoryT>::
69 parse(NestedT(), LexemeT(), *this, scan, open, expr, close);
74 typename as_parser<OpenT>::type::embed_t open;
75 typename as_parser<ExprT>::type::embed_t expr;
76 typename as_parser<CloseT>::type::embed_t close;
79 #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
83 ///////////////////////////////////////////////////////////////////////////////
85 // Confix parser generator template
87 // This is a helper for generating a correct confix_parser<> from
88 // auxiliary parameters. There are the following types supported as
89 // parameters yet: parsers, single characters and strings (see
92 // If the body parser is an action_parser_category type parser (a parser
93 // with an attached semantic action) we have to do something special. This
94 // happens, if the user wrote something like:
96 // confix_p(open, body[f], close)
98 // where 'body' is the parser matching the body of the confix sequence
99 // and 'f' is a functor to be called after matching the body. If we would
100 // do nothing, the resulting code would parse the sequence as follows:
102 // start >> (body[f] - close) >> close
104 // what in most cases is not what the user expects.
105 // (If this _is_ what you've expected, then please use the confix_p
106 // generator function 'direct()', which will inhibit
107 // re-attaching the actor to the body parser).
109 // To make the confix parser behave as expected:
111 // start >> (body - close)[f] >> close
113 // the actor attached to the 'body' parser has to be re-attached to the
114 // (body - close) parser construct, which will make the resulting confix
115 // parser 'do the right thing'. This refactoring is done by the help of
116 // the refactoring parsers (see the files refactoring.[hi]pp).
118 // Additionally special care must be taken, if the body parser is a
119 // unary_parser_category type parser as
121 // confix_p(open, *anychar_p, close)
123 // which without any refactoring would result in
125 // start >> (*anychar_p - close) >> close
127 // and will not give the expected result (*anychar_p will eat up all the
128 // input up to the end of the input stream). So we have to refactor this
131 // start >> *(anychar_p - close) >> close
133 // what will give the correct result.
135 // The case, where the body parser is a combination of the two mentioned
136 // problems (i.e. the body parser is a unary parser with an attached
137 // action), is handled accordingly too:
139 // confix_p(start, (*anychar_p)[f], end)
141 // will be parsed as expected:
143 // start >> (*(anychar_p - end))[f] >> end.
145 ///////////////////////////////////////////////////////////////////////////////
147 template<typename NestedT, typename LexemeT>
148 struct confix_parser_gen
150 // Generic generator function for creation of concrete confix parsers
152 template<typename StartT, typename ExprT, typename EndT>
153 struct paren_op_result_type
155 typedef confix_parser<
156 typename as_parser<StartT>::type,
157 typename as_parser<ExprT>::type,
158 typename as_parser<EndT>::type,
159 typename as_parser<ExprT>::type::parser_category_t,
165 template<typename StartT, typename ExprT, typename EndT>
166 typename paren_op_result_type<StartT, ExprT, EndT>::type
167 operator()(StartT const &start_, ExprT const &expr_, EndT const &end_) const
169 typedef typename paren_op_result_type<StartT,ExprT,EndT>::type
173 as_parser<StartT>::convert(start_),
174 as_parser<ExprT>::convert(expr_),
175 as_parser<EndT>::convert(end_)
179 // Generic generator function for creation of concrete confix parsers
180 // which have an action directly attached to the ExprT part of the
181 // parser (see comment above, no automatic refactoring)
183 template<typename StartT, typename ExprT, typename EndT>
184 struct direct_result_type
186 typedef confix_parser<
187 typename as_parser<StartT>::type,
188 typename as_parser<ExprT>::type,
189 typename as_parser<EndT>::type,
190 plain_parser_category, // do not re-attach action
196 template<typename StartT, typename ExprT, typename EndT>
197 typename direct_result_type<StartT,ExprT,EndT>::type
198 direct(StartT const &start_, ExprT const &expr_, EndT const &end_) const
200 typedef typename direct_result_type<StartT,ExprT,EndT>::type
204 as_parser<StartT>::convert(start_),
205 as_parser<ExprT>::convert(expr_),
206 as_parser<EndT>::convert(end_)
211 ///////////////////////////////////////////////////////////////////////////////
213 // Predefined non_nested confix parser generators
215 ///////////////////////////////////////////////////////////////////////////////
217 const confix_parser_gen<non_nested, non_lexeme> confix_p =
218 confix_parser_gen<non_nested, non_lexeme>();
220 ///////////////////////////////////////////////////////////////////////////////
222 // Comments are special types of confix parsers
224 // Comment parser generator template. This is a helper for generating a
225 // correct confix_parser<> from auxiliary parameters, which is able to
226 // parse comment constructs: (StartToken >> Comment text >> EndToken).
228 // There are the following types supported as parameters yet: parsers,
229 // single characters and strings (see as_parser).
231 // There are two diffenerent predefined comment parser generators
232 // (comment_p and comment_nest_p, see below), which may be used for
233 // creating special comment parsers in two different ways.
235 // If these are used with one parameter, a comment starting with the given
236 // first parser parameter up to the end of the line is matched. So for
237 // instance the following parser matches C++ style comments:
241 // If these are used with two parameters, a comment starting with the
242 // first parser parameter up to the second parser parameter is matched.
243 // For instance a C style comment parser should be constrcuted as:
245 // comment_p("/*", "*/").
247 // Please note, that a comment is parsed implicitly as if the whole
248 // comment_p(...) statement were embedded into a lexeme_d[] directive.
250 ///////////////////////////////////////////////////////////////////////////////
252 template<typename NestedT>
253 struct comment_parser_gen
255 // Generic generator function for creation of concrete comment parsers
256 // from an open token. The newline parser eol_p is used as the
259 template<typename StartT>
260 struct paren_op1_result_type
262 typedef confix_parser<
263 typename as_parser<StartT>::type,
264 kleene_star<anychar_parser>,
265 alternative<eol_parser, end_parser>,
266 unary_parser_category, // there is no action to re-attach
268 is_lexeme // insert implicit lexeme_d[]
273 template<typename StartT>
274 typename paren_op1_result_type<StartT>::type
275 operator() (StartT const &start_) const
277 typedef typename paren_op1_result_type<StartT>::type
281 as_parser<StartT>::convert(start_),
287 // Generic generator function for creation of concrete comment parsers
288 // from an open and a close tokens.
290 template<typename StartT, typename EndT>
291 struct paren_op2_result_type
293 typedef confix_parser<
294 typename as_parser<StartT>::type,
295 kleene_star<anychar_parser>,
296 typename as_parser<EndT>::type,
297 unary_parser_category, // there is no action to re-attach
299 is_lexeme // insert implicit lexeme_d[]
303 template<typename StartT, typename EndT>
304 typename paren_op2_result_type<StartT,EndT>::type
305 operator() (StartT const &start_, EndT const &end_) const
307 typedef typename paren_op2_result_type<StartT,EndT>::type
311 as_parser<StartT>::convert(start_),
313 as_parser<EndT>::convert(end_)
318 ///////////////////////////////////////////////////////////////////////////////
320 // Predefined non_nested comment parser generator
322 ///////////////////////////////////////////////////////////////////////////////
324 const comment_parser_gen<non_nested> comment_p =
325 comment_parser_gen<non_nested>();
327 ///////////////////////////////////////////////////////////////////////////////
329 // comment_nest_parser class
331 // Parses a nested comments.
332 // Example: nested PASCAL-comments:
334 // { This is a { nested } PASCAL-comment }
336 ///////////////////////////////////////////////////////////////////////////////
338 template<typename OpenT, typename CloseT>
339 struct comment_nest_parser:
340 public parser<comment_nest_parser<OpenT, CloseT> >
342 typedef comment_nest_parser<OpenT, CloseT> self_t;
344 comment_nest_parser(OpenT const &open_, CloseT const &close_):
345 open(open_), close(close_)
348 template<typename ScannerT>
349 typename parser_result<self_t, ScannerT>::type
350 parse(ScannerT const &scan) const
353 open >> *(*this | (anychar_p - close)) >> close,
358 template<typename ParserT, typename ScannerT>
359 typename parser_result<self_t, ScannerT>::type
360 do_parse(ParserT const &p, ScannerT const &scan) const
363 impl::contiguous_parser_parse<
364 typename parser_result<ParserT, ScannerT>::type
368 typename as_parser<OpenT>::type::embed_t open;
369 typename as_parser<CloseT>::type::embed_t close;
372 ///////////////////////////////////////////////////////////////////////////////
374 // Predefined nested comment parser generator
376 ///////////////////////////////////////////////////////////////////////////////
378 template<typename OpenT, typename CloseT>
379 struct comment_nest_p_result
381 typedef comment_nest_parser<
382 typename as_parser<OpenT>::type,
383 typename as_parser<CloseT>::type
387 template<typename OpenT, typename CloseT>
388 inline typename comment_nest_p_result<OpenT,CloseT>::type
389 comment_nest_p(OpenT const &open, CloseT const &close)
391 typedef typename comment_nest_p_result<OpenT,CloseT>::type
395 as_parser<OpenT>::convert(open),
396 as_parser<CloseT>::convert(close)
400 ///////////////////////////////////////////////////////////////////////////////
401 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
403 }} // namespace BOOST_SPIRIT_CLASSIC_NS