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