1
/*=============================================================================
3
Copyright (c) 2001-2003 Daniel Nuffer
4
http://spirit.sourceforge.net/
6
Permission to copy, use, modify, sell and distribute this software is
7
granted provided this copyright notice appears in all copies. This
8
software is provided "as is" without express or implied warranty, and
9
with no claim as to its suitability for any purpose.
10
=============================================================================*/
11
#ifndef BOOST_SPIRIT_TREE_AST_HPP
12
#define BOOST_SPIRIT_TREE_AST_HPP
14
#include "boost/spirit/tree/common.hpp"
15
#include "boost/spirit/core/scanner/scanner.hpp"
17
///////////////////////////////////////////////////////////////////////////////
18
namespace boost { namespace spirit {
20
template <typename MatchPolicyT, typename NodeFactoryT>
21
struct ast_tree_policy;
23
//////////////////////////////////
24
// ast_match_policy is simply an id so the correct specialization of
25
// tree_policy can be found.
28
typename NodeFactoryT = node_val_data_factory<nil_t>
30
struct ast_match_policy :
31
public common_tree_match_policy<
32
ast_match_policy<IteratorT, NodeFactoryT>,
36
ast_match_policy<IteratorT, NodeFactoryT>,
43
//////////////////////////////////
44
template <typename MatchPolicyT, typename NodeFactoryT>
45
struct ast_tree_policy :
46
public common_tree_tree_policy<MatchPolicyT, NodeFactoryT>
49
typename common_tree_tree_policy<MatchPolicyT, NodeFactoryT>::match_t
51
typedef typename MatchPolicyT::iterator_t iterator_t;
53
static void concat(match_t& a, match_t const& b)
55
BOOST_SPIRIT_ASSERT(a && b);
57
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS_NODES & BOOST_SPIRIT_DEBUG_FLAGS_TREES)
58
BOOST_SPIRIT_DEBUG_OUT << "concat. a = " << a <<
59
"\n\tb = " << b << std::endl;
61
typedef typename tree_match<iterator_t, NodeFactoryT>::container_t
64
// test for size() is nessecary, because no_tree_gen_node leaves a.trees
65
// and/or b.trees empty
66
if (0 != b.trees.size() && b.trees.begin()->value.is_root())
68
BOOST_SPIRIT_ASSERT(b.trees.size() == 1);
71
std::swap(a.trees, tmp); // save a into tmp
72
std::swap(b.trees, a.trees); // make b.trees[0] be new root (a.trees[0])
73
container_t* pnon_root_trees = &a.trees;
74
while (pnon_root_trees->size() > 0 &&
75
pnon_root_trees->begin()->value.is_root())
77
pnon_root_trees = & pnon_root_trees->begin()->children;
79
pnon_root_trees->insert(pnon_root_trees->begin(),
80
tmp.begin(), tmp.end());
82
else if (0 != a.trees.size() && a.trees.begin()->value.is_root())
84
BOOST_SPIRIT_ASSERT(a.trees.size() == 1);
85
std::copy(b.trees.begin(),
87
std::back_insert_iterator<container_t>(
88
a.trees.begin()->children));
92
std::copy(b.trees.begin(),
94
std::back_insert_iterator<container_t>(a.trees));
97
#if defined(BOOST_SPIRIT_DEBUG) && (BOOST_SPIRIT_DEBUG_FLAGS_NODES & BOOST_SPIRIT_DEBUG_FLAGS_TREES)
98
BOOST_SPIRIT_DEBUG_OUT << "after concat. a = " << a << std::endl;
104
template <typename Iterator1T, typename Iterator2T>
105
static void group_match(match_t& m, parser_id const& id,
106
Iterator1T const& first, Iterator2T const& last)
111
typedef typename tree_match<iterator_t, NodeFactoryT>::container_t
113
typedef typename container_t::iterator cont_iterator_t;
114
typedef typename NodeFactoryT::template factory<iterator_t> factory_t;
115
// only one node, so don't make a new level
116
if (m.trees.size() == 1)
118
// set rule_id's. There may have been multiple nodes created.
119
// Because of root_node[] they may be left-most children of the top
121
container_t* punset_id = &m.trees;
122
while (punset_id->size() > 0 &&
123
punset_id->begin()->value.id() == 0)
125
punset_id->begin()->value.id(id);
126
punset_id = &punset_id->begin()->children;
129
m.trees.begin()->value.is_root(false);
133
match_t newmatch(m.length(),
134
factory_t::create_node(first, last, false));
136
std::swap(newmatch.trees.begin()->children, m.trees);
137
// set this node and all it's unset children's rule_id
138
newmatch.trees.begin()->value.id(id);
139
for (cont_iterator_t i = newmatch.trees.begin();
140
i != newmatch.trees.end();
143
if (i->value.id() == 0)
150
template <typename FunctorT>
151
static void apply_op_to_match(FunctorT const& op, match_t& m)
159
template <typename IteratorT, typename NodeFactoryT>
160
struct tree_policy_selector<ast_match_policy<IteratorT, NodeFactoryT> >
162
typedef ast_tree_policy<
163
ast_match_policy<IteratorT, NodeFactoryT>, NodeFactoryT> type;
169
//////////////////////////////////
170
struct gen_ast_node_parser_gen;
172
template <typename T>
173
struct gen_ast_node_parser
174
: public unary<T, parser<gen_ast_node_parser<T> > >
176
typedef gen_ast_node_parser<T> self_t;
177
typedef gen_ast_node_parser_gen parser_generator_t;
178
typedef unary_parser_category parser_category_t;
179
// typedef gen_ast_node_parser<T> const &embed_t;
181
gen_ast_node_parser(T const& a)
182
: unary<T, parser<gen_ast_node_parser<T> > >(a) {}
184
template <typename ScannerT>
185
typename parser_result<self_t, ScannerT>::type
186
parse(ScannerT const& scan) const
188
typedef typename ScannerT::iteration_policy_t iteration_policy_t;
189
typedef typename ScannerT::match_policy_t::iterator_t iterator_t;
190
typedef typename ScannerT::match_policy_t::factory_t factory_t;
191
typedef ast_match_policy<iterator_t, factory_t> match_policy_t;
192
typedef typename ScannerT::action_policy_t action_policy_t;
193
typedef scanner_policies<
199
return this->subject().parse(scan.change_policies(policies_t()));
203
//////////////////////////////////
204
struct gen_ast_node_parser_gen
206
template <typename T>
209
typedef gen_ast_node_parser<T> type;
212
template <typename T>
213
static gen_ast_node_parser<T>
214
generate(parser<T> const& s)
216
return gen_ast_node_parser<T>(s.derived());
219
template <typename T>
220
gen_ast_node_parser<T>
221
operator[](parser<T> const& s) const
223
return gen_ast_node_parser<T>(s.derived());
227
//////////////////////////////////
228
const gen_ast_node_parser_gen gen_ast_node_d = gen_ast_node_parser_gen();
231
//////////////////////////////////
234
template <typename MatchT>
235
void operator()(MatchT& m) const
237
BOOST_SPIRIT_ASSERT(m.trees.size() > 0);
238
m.trees.begin()->value.is_root(true);
242
const node_parser_gen<root_node_op> root_node_d =
243
node_parser_gen<root_node_op>();
245
///////////////////////////////////////////////////////////////////////////////
247
// Parse functions for ASTs
249
///////////////////////////////////////////////////////////////////////////////
251
typename AstFactoryT, typename IteratorT, typename ParserT,
254
inline tree_parse_info<IteratorT, AstFactoryT>
256
IteratorT const& first_,
257
IteratorT const& last_,
258
parser<ParserT> const& parser,
260
AstFactoryT const & dummy_ = AstFactoryT())
262
typedef skip_parser_iteration_policy<SkipT> iter_policy_t;
263
typedef ast_match_policy<IteratorT, AstFactoryT> ast_match_policy_t;
265
scanner_policies<iter_policy_t, ast_match_policy_t>
267
typedef scanner<IteratorT, scanner_policies_t> scanner_t;
269
iter_policy_t iter_policy(skip_);
270
scanner_policies_t policies(iter_policy);
271
IteratorT first = first_;
272
scanner_t scan(first, last_, policies);
273
tree_match<IteratorT, AstFactoryT> hit = parser.derived().parse(scan);
275
return tree_parse_info<IteratorT, AstFactoryT>(
276
first, hit, hit && (first == last_), hit.length(), hit.trees);
279
//////////////////////////////////
280
template <typename IteratorT, typename ParserT, typename SkipT>
281
inline tree_parse_info<IteratorT>
283
IteratorT const& first_,
284
IteratorT const& last_,
285
parser<ParserT> const& parser,
288
typedef node_val_data_factory<nil_t> default_factory_t;
289
return ast_parse(first_, last_, parser, skip_, default_factory_t());
292
//////////////////////////////////
293
template <typename IteratorT, typename ParserT>
294
inline tree_parse_info<IteratorT>
296
IteratorT const& first_,
297
IteratorT const& last,
298
parser<ParserT> const& parser)
300
IteratorT first = first_;
303
scanner_policies<iteration_policy, ast_match_policy<IteratorT> >
305
tree_match<IteratorT> hit = parser.derived().parse(scan);
306
return tree_parse_info<IteratorT>(first, hit, hit && (first == last),
307
hit.length(), hit.trees);
310
//////////////////////////////////
311
template <typename CharT, typename ParserT, typename SkipT>
312
inline tree_parse_info<CharT const*>
315
parser<ParserT> const& parser,
318
CharT const* last = str;
321
return ast_parse(str, last, parser, skip);
324
//////////////////////////////////
325
template <typename CharT, typename ParserT>
326
inline tree_parse_info<CharT const*>
329
parser<ParserT> const& parser)
331
CharT const* last = str;
336
return ast_parse(str, last, parser);
339
///////////////////////////////////////////////////////////////////////////////
340
}} // namespace boost::spirit