Update contrib.
1 /*=============================================================================
2 Copyright (c) 2001-2003 Daniel Nuffer
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_TREE_AST_HPP
10 #define BOOST_SPIRIT_TREE_AST_HPP
12 #include <boost/spirit/tree/common.hpp>
13 #include <boost/spirit/core/scanner/scanner.hpp>
15 #include <boost/spirit/tree/ast_fwd.hpp>
17 ///////////////////////////////////////////////////////////////////////////////
18 namespace boost { namespace spirit {
21 //////////////////////////////////
22 // ast_match_policy is simply an id so the correct specialization of
23 // tree_policy can be found.
28 struct ast_match_policy :
29 public common_tree_match_policy<
30 ast_match_policy<IteratorT, NodeFactoryT>,
34 ast_match_policy<IteratorT, NodeFactoryT>,
40 common_tree_match_policy<
41 ast_match_policy<IteratorT, NodeFactoryT>,
45 ast_match_policy<IteratorT, NodeFactoryT>,
49 common_tree_match_policy_;
55 template <typename PolicyT>
56 ast_match_policy(PolicyT const & policies)
57 : common_tree_match_policy_(policies)
62 //////////////////////////////////
63 template <typename MatchPolicyT, typename NodeFactoryT>
64 struct ast_tree_policy :
65 public common_tree_tree_policy<MatchPolicyT, NodeFactoryT>
68 typename common_tree_tree_policy<MatchPolicyT, NodeFactoryT>::match_t
70 typedef typename MatchPolicyT::iterator_t iterator_t;
72 static void concat(match_t& a, match_t const& b)
74 BOOST_SPIRIT_ASSERT(a && b);
76 #if defined(BOOST_SPIRIT_DEBUG) && \
77 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
78 BOOST_SPIRIT_DEBUG_OUT << "\n>>>AST concat. a = " << a <<
79 "\n\tb = " << b << "<<<\n";
81 typedef typename tree_match<iterator_t, NodeFactoryT>::container_t
84 // test for size() is nessecary, because no_tree_gen_node leaves a.trees
85 // and/or b.trees empty
86 if (0 != b.trees.size() && b.trees.begin()->value.is_root())
88 BOOST_SPIRIT_ASSERT(b.trees.size() == 1);
91 std::swap(a.trees, tmp); // save a into tmp
92 std::swap(b.trees, a.trees); // make b.trees[0] be new root (a.trees[0])
93 container_t* pnon_root_trees = &a.trees;
94 while (pnon_root_trees->size() > 0 &&
95 pnon_root_trees->begin()->value.is_root())
97 pnon_root_trees = & pnon_root_trees->begin()->children;
99 pnon_root_trees->insert(pnon_root_trees->begin(),
100 tmp.begin(), tmp.end());
102 else if (0 != a.trees.size() && a.trees.begin()->value.is_root())
104 BOOST_SPIRIT_ASSERT(a.trees.size() == 1);
106 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
107 a.trees.begin()->children.reserve(a.trees.begin()->children.size() + b.trees.size());
109 std::copy(b.trees.begin(),
111 std::back_insert_iterator<container_t>(
112 a.trees.begin()->children));
116 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
117 a.trees.reserve(a.trees.size() + b.trees.size());
119 std::copy(b.trees.begin(),
121 std::back_insert_iterator<container_t>(a.trees));
124 #if defined(BOOST_SPIRIT_DEBUG) && \
125 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
126 BOOST_SPIRIT_DEBUG_OUT << ">>>after AST concat. a = " << a << "<<<\n\n";
132 template <typename MatchT, typename Iterator1T, typename Iterator2T>
133 static void group_match(MatchT& m, parser_id const& id,
134 Iterator1T const& first, Iterator2T const& last)
139 typedef typename tree_match<iterator_t, NodeFactoryT>::container_t
141 typedef typename container_t::iterator cont_iterator_t;
142 typedef typename NodeFactoryT::template factory<iterator_t> factory_t;
144 if (m.trees.size() == 1
145 #ifdef BOOST_SPIRIT_NO_TREE_NODE_COLLAPSING
146 && !(id.to_long() && m.trees.begin()->value.id().to_long())
150 // set rule_id's. There may have been multiple nodes created.
151 // Because of root_node[] they may be left-most children of the top
153 container_t* punset_id = &m.trees;
154 while (punset_id->size() > 0 &&
155 punset_id->begin()->value.id() == 0)
157 punset_id->begin()->value.id(id);
158 punset_id = &punset_id->begin()->children;
161 m.trees.begin()->value.is_root(false);
165 match_t newmatch(m.length(),
167 factory_t::empty_node() :
168 factory_t::create_node(first, last, false));
170 std::swap(newmatch.trees.begin()->children, m.trees);
171 // set this node and all it's unset children's rule_id
172 newmatch.trees.begin()->value.id(id);
173 for (cont_iterator_t i = newmatch.trees.begin();
174 i != newmatch.trees.end();
177 if (i->value.id() == 0)
184 template <typename FunctorT>
185 static void apply_op_to_match(FunctorT const& op, match_t& m)
193 template <typename IteratorT, typename NodeFactoryT>
194 struct tree_policy_selector<ast_match_policy<IteratorT, NodeFactoryT> >
196 typedef ast_tree_policy<
197 ast_match_policy<IteratorT, NodeFactoryT>, NodeFactoryT> type;
203 //////////////////////////////////
204 struct gen_ast_node_parser_gen;
206 template <typename T>
207 struct gen_ast_node_parser
208 : public unary<T, parser<gen_ast_node_parser<T> > >
210 typedef gen_ast_node_parser<T> self_t;
211 typedef gen_ast_node_parser_gen parser_generator_t;
212 typedef unary_parser_category parser_category_t;
213 // typedef gen_ast_node_parser<T> const &embed_t;
215 gen_ast_node_parser(T const& a)
216 : unary<T, parser<gen_ast_node_parser<T> > >(a) {}
218 template <typename ScannerT>
219 typename parser_result<self_t, ScannerT>::type
220 parse(ScannerT const& scan) const
222 typedef typename ScannerT::iteration_policy_t iteration_policy_t;
223 typedef typename ScannerT::match_policy_t::iterator_t iterator_t;
224 typedef typename ScannerT::match_policy_t::factory_t factory_t;
225 typedef ast_match_policy<iterator_t, factory_t> match_policy_t;
226 typedef typename ScannerT::action_policy_t action_policy_t;
227 typedef scanner_policies<
233 return this->subject().parse(scan.change_policies(policies_t(scan)));
237 //////////////////////////////////
238 struct gen_ast_node_parser_gen
240 template <typename T>
243 typedef gen_ast_node_parser<T> type;
246 template <typename T>
247 static gen_ast_node_parser<T>
248 generate(parser<T> const& s)
250 return gen_ast_node_parser<T>(s.derived());
253 template <typename T>
254 gen_ast_node_parser<T>
255 operator[](parser<T> const& s) const
257 return gen_ast_node_parser<T>(s.derived());
261 //////////////////////////////////
262 const gen_ast_node_parser_gen gen_ast_node_d = gen_ast_node_parser_gen();
265 //////////////////////////////////
268 template <typename MatchT>
269 void operator()(MatchT& m) const
271 BOOST_SPIRIT_ASSERT(m.trees.size() > 0);
272 m.trees.begin()->value.is_root(true);
276 const node_parser_gen<root_node_op> root_node_d =
277 node_parser_gen<root_node_op>();
279 ///////////////////////////////////////////////////////////////////////////////
281 // Parse functions for ASTs
283 ///////////////////////////////////////////////////////////////////////////////
285 typename AstFactoryT, typename IteratorT, typename ParserT,
288 inline tree_parse_info<IteratorT, AstFactoryT>
290 IteratorT const& first_,
291 IteratorT const& last_,
292 parser<ParserT> const& parser,
294 AstFactoryT const & /*dummy_*/ = AstFactoryT())
296 typedef skip_parser_iteration_policy<SkipT> iter_policy_t;
297 typedef ast_match_policy<IteratorT, AstFactoryT> ast_match_policy_t;
299 scanner_policies<iter_policy_t, ast_match_policy_t>
301 typedef scanner<IteratorT, scanner_policies_t> scanner_t;
303 iter_policy_t iter_policy(skip_);
304 scanner_policies_t policies(iter_policy);
305 IteratorT first = first_;
306 scanner_t scan(first, last_, policies);
307 tree_match<IteratorT, AstFactoryT> hit = parser.derived().parse(scan);
309 return tree_parse_info<IteratorT, AstFactoryT>(
310 first, hit, hit && (first == last_), hit.length(), hit.trees);
313 //////////////////////////////////
314 template <typename IteratorT, typename ParserT, typename SkipT>
315 inline tree_parse_info<IteratorT>
317 IteratorT const& first_,
318 IteratorT const& last_,
319 parser<ParserT> const& parser,
322 typedef node_val_data_factory<nil_t> default_factory_t;
323 return ast_parse(first_, last_, parser, skip_, default_factory_t());
326 //////////////////////////////////
327 template <typename IteratorT, typename ParserT>
328 inline tree_parse_info<IteratorT>
330 IteratorT const& first_,
331 IteratorT const& last,
332 parser<ParserT> const& parser)
334 typedef ast_match_policy<IteratorT> ast_match_policy_t;
335 IteratorT first = first_;
338 scanner_policies<iteration_policy, ast_match_policy_t>
340 tree_match<IteratorT> hit = parser.derived().parse(scan);
341 return tree_parse_info<IteratorT>(
342 first, hit, hit && (first == last), hit.length(), hit.trees);
345 //////////////////////////////////
346 template <typename CharT, typename ParserT, typename SkipT>
347 inline tree_parse_info<CharT const*>
350 parser<ParserT> const& parser,
353 CharT const* last = str;
356 return ast_parse(str, last, parser, skip);
359 //////////////////////////////////
360 template <typename CharT, typename ParserT>
361 inline tree_parse_info<CharT const*>
364 parser<ParserT> const& parser)
366 CharT const* last = str;
371 return ast_parse(str, last, parser);
374 ///////////////////////////////////////////////////////////////////////////////
375 }} // namespace boost::spirit