1 /*=============================================================================
2 Copyright (c) 2001-2003 Daniel Nuffer
3 Copyright (c) 2001-2007 Hartmut Kaiser
4 Revised 2007, Copyright (c) Tobias Schwinger
5 http://spirit.sourceforge.net/
7 Distributed under the Boost Software License, Version 1.0. (See accompanying
8 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 =============================================================================*/
10 #ifndef BOOST_SPIRIT_TREE_COMMON_HPP
11 #define BOOST_SPIRIT_TREE_COMMON_HPP
13 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
19 #if defined(BOOST_SPIRIT_USE_BOOST_ALLOCATOR_FOR_TREES)
20 #include <boost/pool/pool_alloc.hpp>
25 #include <boost/ref.hpp>
26 #include <boost/call_traits.hpp>
27 #include <boost/spirit/home/classic/namespace.hpp>
28 #include <boost/spirit/home/classic/core.hpp>
29 #include <boost/assert.hpp>
31 #if defined(BOOST_SPIRIT_DEBUG) && \
32 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
34 #include <boost/spirit/home/classic/debug/debug_node.hpp>
37 #include <boost/spirit/home/classic/tree/common_fwd.hpp>
39 #include <iterator> // for std::iterator_traits, std::distance
41 namespace boost { namespace spirit {
43 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
46 void swap(tree_node<T>& a, tree_node<T>& b);
48 template <typename T, typename V>
49 void swap(node_iter_data<T, V>& a, node_iter_data<T, V>& b);
53 inline void cp_swap(T& t1, T& t2);
59 typedef T parse_node_t;
61 #if !defined(BOOST_SPIRIT_USE_BOOST_ALLOCATOR_FOR_TREES)
62 typedef std::allocator<tree_node<T> > allocator_type;
63 #elif !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
64 typedef boost::pool_allocator<tree_node<T> > allocator_type;
66 typedef boost::fast_pool_allocator<tree_node<T> > allocator_type;
69 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
70 typedef std::vector<tree_node<T>, allocator_type> children_t;
72 typedef std::list<tree_node<T>, allocator_type> children_t;
73 #endif // BOOST_SPIRIT_USE_LIST_FOR_TREES
75 typedef typename children_t::iterator tree_iterator;
76 typedef typename children_t::const_iterator const_tree_iterator;
86 explicit tree_node(T const& v)
91 tree_node(T const& v, children_t const& c)
96 void swap(tree_node<T>& x)
98 impl::cp_swap(value, x.value);
99 impl::cp_swap(children, x.children);
103 #if defined(BOOST_SPIRIT_DEBUG) && \
104 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
105 template <typename T>
107 operator<<(std::ostream& o, tree_node<T> const& n)
109 static int depth = 0;
111 for (int i = 0; i <= depth; ++i)
115 o << "(depth = " << depth++ << " value = " << n.value;
117 for (typename tree_node<T>::children_t::const_iterator it = n.children.begin();
118 it != n.children.end(); ++it)
120 o << " children[" << c++ << "] = " << *it;
128 //////////////////////////////////
129 template <typename IteratorT, typename ValueT>
130 struct node_iter_data
132 typedef IteratorT iterator_t;
133 typedef IteratorT /*const*/ const_iterator_t;
136 : first(), last(), is_root_(false), parser_id_(), value_()
139 node_iter_data(IteratorT const& _first, IteratorT const& _last)
140 : first(_first), last(_last), is_root_(false), parser_id_(), value_()
143 void swap(node_iter_data& x)
145 impl::cp_swap(first, x.first);
146 impl::cp_swap(last, x.last);
147 impl::cp_swap(parser_id_, x.parser_id_);
148 impl::cp_swap(is_root_, x.is_root_);
149 impl::cp_swap(value_, x.value_);
157 IteratorT const& begin() const
167 IteratorT const& end() const
192 ValueT const& value() const
197 void value(ValueT const& v)
202 IteratorT first, last;
204 parser_id parser_id_;
210 #if defined(BOOST_SPIRIT_DEBUG) && \
211 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
212 // value is default nil_t, so provide an operator<< for nil_t
214 operator<<(std::ostream& o, nil_t const&)
219 template <typename IteratorT, typename ValueT>
221 operator<<(std::ostream& o, node_iter_data<IteratorT, ValueT> const& n)
223 o << "(id = " << n.id() << " text = \"";
224 typedef typename node_iter_data<IteratorT, ValueT>::const_iterator_t
226 for (iterator_t it = n.begin(); it != n.end(); ++it)
227 impl::token_printer(o, *it);
228 o << "\" is_root = " << n.is_root()
229 << /*" value = " << n.value() << */")";
234 //////////////////////////////////
235 template <typename IteratorT = char const*, typename ValueT = nil_t>
239 typename std::iterator_traits<IteratorT>::value_type
242 #if !defined(BOOST_SPIRIT_USE_BOOST_ALLOCATOR_FOR_TREES)
243 typedef std::allocator<value_type> allocator_type;
244 #elif !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
245 typedef boost::pool_allocator<value_type> allocator_type;
247 typedef boost::fast_pool_allocator<value_type> allocator_type;
250 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
251 typedef std::vector<value_type, allocator_type> container_t;
253 typedef std::list<value_type, allocator_type> container_t;
256 typedef typename container_t::iterator iterator_t;
257 typedef typename container_t::const_iterator const_iterator_t;
260 : text(), is_root_(false), parser_id_(), value_()
263 #if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)
264 node_val_data(IteratorT const& _first, IteratorT const& _last)
265 : text(), is_root_(false), parser_id_(), value_()
267 std::copy(_first, _last, std::inserter(text, text.end()));
270 // This constructor is for building text out of iterators
271 template <typename IteratorT2>
272 node_val_data(IteratorT2 const& _first, IteratorT2 const& _last)
273 : text(), is_root_(false), parser_id_(), value_()
275 std::copy(_first, _last, std::inserter(text, text.end()));
278 node_val_data(IteratorT const& _first, IteratorT const& _last)
279 : text(_first, _last), is_root_(false), parser_id_(), value_()
282 // This constructor is for building text out of iterators
283 template <typename IteratorT2>
284 node_val_data(IteratorT2 const& _first, IteratorT2 const& _last)
285 : text(_first, _last), is_root_(false), parser_id_(), value_()
289 void swap(node_val_data& x)
291 impl::cp_swap(text, x.text);
292 impl::cp_swap(is_root_, x.is_root_);
293 impl::cp_swap(parser_id_, x.parser_id_);
294 impl::cp_swap(value_, x.value_);
297 typename container_t::iterator begin()
302 typename container_t::const_iterator begin() const
307 typename container_t::iterator end()
312 typename container_t::const_iterator end() const
337 ValueT const& value() const
342 void value(ValueT const& v)
350 parser_id parser_id_;
354 #if defined(BOOST_SPIRIT_DEBUG) && \
355 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
356 template <typename IteratorT, typename ValueT>
358 operator<<(std::ostream& o, node_val_data<IteratorT, ValueT> const& n)
360 o << "(id = " << n.id() << " text = \"";
361 typedef typename node_val_data<IteratorT, ValueT>::const_iterator_t
363 for (iterator_t it = n.begin(); it != n.end(); ++it)
364 impl::token_printer(o, *it);
365 o << "\" is_root = " << n.is_root()
366 << " value = " << n.value() << ")";
371 template <typename T>
373 swap(tree_node<T>& a, tree_node<T>& b)
378 template <typename T, typename V>
380 swap(node_iter_data<T, V>& a, node_iter_data<T, V>& b)
385 //////////////////////////////////
386 template <typename ValueT>
387 class node_iter_data_factory
390 // This inner class is so that node_iter_data_factory can simulate
391 // a template template parameter
392 template <typename IteratorT>
396 typedef IteratorT iterator_t;
397 typedef node_iter_data<iterator_t, ValueT> node_t;
399 static node_t create_node(iterator_t const& first, iterator_t const& last,
400 bool /*is_leaf_node*/)
402 return node_t(first, last);
405 static node_t empty_node()
410 // precondition: ContainerT contains a tree_node<node_t>. And all
411 // iterators in the container point to the same sequence.
412 template <typename ContainerT>
413 static node_t group_nodes(ContainerT const& nodes)
415 return node_t(nodes.begin()->value.begin(),
416 nodes.back().value.end());
421 //////////////////////////////////
422 template <typename ValueT>
423 class node_val_data_factory
426 // This inner class is so that node_val_data_factory can simulate
427 // a template template parameter
428 template <typename IteratorT>
432 typedef IteratorT iterator_t;
433 typedef node_val_data<iterator_t, ValueT> node_t;
435 static node_t create_node(iterator_t const& first, iterator_t const& last,
439 return node_t(first, last);
444 static node_t empty_node()
449 template <typename ContainerT>
450 static node_t group_nodes(ContainerT const& nodes)
452 typename node_t::container_t c;
453 typename ContainerT::const_iterator i_end = nodes.end();
454 // copy all the nodes text into a new one
455 for (typename ContainerT::const_iterator i = nodes.begin();
458 // See docs: reduced_node_d cannot be used with a
459 // rule inside the [].
460 BOOST_ASSERT(i->children.size() == 0);
461 c.insert(c.end(), i->value.begin(), i->value.end());
463 return node_t(c.begin(), c.end());
468 //////////////////////////////////
469 template <typename ValueT>
470 class node_all_val_data_factory
473 // This inner class is so that node_all_val_data_factory can simulate
474 // a template template parameter
475 template <typename IteratorT>
479 typedef IteratorT iterator_t;
480 typedef node_val_data<iterator_t, ValueT> node_t;
482 static node_t create_node(iterator_t const& first, iterator_t const& last,
483 bool /*is_leaf_node*/)
485 return node_t(first, last);
488 static node_t empty_node()
493 template <typename ContainerT>
494 static node_t group_nodes(ContainerT const& nodes)
496 typename node_t::container_t c;
497 typename ContainerT::const_iterator i_end = nodes.end();
498 // copy all the nodes text into a new one
499 for (typename ContainerT::const_iterator i = nodes.begin();
502 BOOST_ASSERT(i->children.size() == 0);
503 c.insert(c.end(), i->value.begin(), i->value.end());
505 return node_t(c.begin(), c.end());
512 ///////////////////////////////////////////////////////////////////////////
513 // can't call unqualified swap from within classname::swap
514 // as Koenig lookup rules will find only the classname::swap
515 // member function not the global declaration, so use cp_swap
516 // as a forwarding function (JM):
517 template <typename T>
518 inline void cp_swap(T& t1, T& t2)
521 using BOOST_SPIRIT_CLASSIC_NS::swap;
527 //////////////////////////////////
528 template <typename IteratorT, typename NodeFactoryT, typename T>
529 class tree_match : public match<T>
533 typedef typename NodeFactoryT::template factory<IteratorT> node_factory_t;
534 typedef typename node_factory_t::node_t parse_node_t;
535 typedef tree_node<parse_node_t> node_t;
536 typedef typename node_t::children_t container_t;
537 typedef typename container_t::iterator tree_iterator;
538 typedef typename container_t::const_iterator const_tree_iterator;
541 typedef typename boost::call_traits<T>::param_type param_type;
542 typedef typename boost::call_traits<T>::reference reference;
543 typedef typename boost::call_traits<T>::const_reference const_reference;
546 : match<T>(), trees()
550 tree_match(std::size_t length_)
551 : match<T>(length_), trees()
554 tree_match(std::size_t length_, parse_node_t const& n)
555 : match<T>(length_), trees()
557 trees.push_back(node_t(n));
560 tree_match(std::size_t length_, param_type val, parse_node_t const& n)
561 : match<T>(length_, val), trees()
563 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
564 trees.reserve(10); // this is more or less an arbitrary number...
566 trees.push_back(node_t(n));
569 // attention, these constructors will change the second parameter!
570 tree_match(std::size_t length_, container_t& c)
571 : match<T>(length_), trees()
573 impl::cp_swap(trees, c);
576 tree_match(std::size_t length_, param_type val, container_t& c)
577 : match<T>(length_, val), trees()
579 impl::cp_swap(trees, c);
582 template <typename T2>
583 tree_match(match<T2> const& other)
584 : match<T>(other), trees()
587 template <typename T2, typename T3, typename T4>
588 tree_match(tree_match<T2, T3, T4> const& other)
589 : match<T>(other), trees()
590 { impl::cp_swap(trees, other.trees); }
592 template <typename T2>
594 operator=(match<T2> const& other)
596 match<T>::operator=(other);
600 template <typename T2, typename T3, typename T4>
602 operator=(tree_match<T2, T3, T4> const& other)
604 match<T>::operator=(other);
605 impl::cp_swap(trees, other.trees);
609 tree_match(tree_match const& x)
610 : match<T>(x), trees()
612 // use auto_ptr like ownership for the trees data member
613 impl::cp_swap(trees, x.trees);
616 tree_match& operator=(tree_match const& x)
623 void swap(tree_match& x)
626 impl::cp_swap(trees, x.trees);
629 mutable container_t trees;
632 #if defined(BOOST_SPIRIT_DEBUG) && \
633 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
634 template <typename IteratorT, typename NodeFactoryT, typename T>
636 operator<<(std::ostream& o, tree_match<IteratorT, NodeFactoryT, T> const& m)
639 typename tree_match<IteratorT, NodeFactoryT, T>::container_t::iterator
642 o << "(length = " << (int)m.length();
644 for (iterator i = m.trees.begin(); i != m.trees.end(); ++i)
646 o << " trees[" << c++ << "] = " << *i;
653 //////////////////////////////////
656 template <typename FunctorT, typename MatchT>
657 static void apply_op_to_match(FunctorT const& /*op*/, MatchT& /*m*/)
660 template <typename MatchT, typename Iterator1T, typename Iterator2T>
661 static void group_match(MatchT& /*m*/, parser_id const& /*id*/,
662 Iterator1T const& /*first*/, Iterator2T const& /*last*/)
665 template <typename MatchT>
666 static void concat(MatchT& /*a*/, MatchT const& /*b*/)
670 //////////////////////////////////
672 typename MatchPolicyT,
674 typename NodeFactoryT,
675 typename TreePolicyT,
678 struct common_tree_match_policy : public match_policy
680 common_tree_match_policy()
684 template <typename PolicyT>
685 common_tree_match_policy(PolicyT const & policies)
686 : match_policy((match_policy const &)policies)
690 template <typename U>
691 struct result { typedef tree_match<IteratorT, NodeFactoryT, U> type; };
693 typedef tree_match<IteratorT, NodeFactoryT, T> match_t;
694 typedef IteratorT iterator_t;
695 typedef TreePolicyT tree_policy_t;
696 typedef NodeFactoryT factory_t;
698 static const match_t no_match() { return match_t(); }
699 static const match_t empty_match()
700 { return match_t(0, tree_policy_t::empty_node()); }
702 template <typename AttrT, typename Iterator1T, typename Iterator2T>
703 static tree_match<IteratorT, NodeFactoryT, AttrT> create_match(
706 Iterator1T const& first,
707 Iterator2T const& last)
709 #if defined(BOOST_SPIRIT_DEBUG) && \
710 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
712 BOOST_SPIRIT_DEBUG_OUT << "\n>>> create_node(begin) <<<\n"
713 "creating node text: \"";
714 for (Iterator1T it = first; it != last; ++it)
715 impl::token_printer(BOOST_SPIRIT_DEBUG_OUT, *it);
716 BOOST_SPIRIT_DEBUG_OUT << "\"\n";
717 BOOST_SPIRIT_DEBUG_OUT << ">>> create_node(end) <<<\n\n";
719 return tree_match<IteratorT, NodeFactoryT, AttrT>(length, val,
720 tree_policy_t::create_node(length, first, last, true));
723 template <typename Match1T, typename Match2T>
724 static void concat_match(Match1T& a, Match2T const& b)
726 #if defined(BOOST_SPIRIT_DEBUG) && \
727 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
729 BOOST_SPIRIT_DEBUG_OUT << "\n>>> concat_match(begin) <<<\n";
730 BOOST_SPIRIT_DEBUG_OUT << "tree a:\n" << a << "\n";
731 BOOST_SPIRIT_DEBUG_OUT << "tree b:\n" << b << "\n";
732 BOOST_SPIRIT_DEBUG_OUT << ">>> concat_match(end) <<<\n\n";
734 BOOST_SPIRIT_ASSERT(a && b);
740 else if (b.length() == 0
741 #ifdef BOOST_SPIRIT_NO_TREE_NODE_COLLAPSING
742 && !b.trees.begin()->value.id().to_long()
749 tree_policy_t::concat(a, b);
752 template <typename MatchT, typename IteratorT2>
757 IteratorT2 const& first,
758 IteratorT2 const& last) const
762 #if defined(BOOST_SPIRIT_DEBUG) && \
763 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_TREES)
765 BOOST_SPIRIT_DEBUG_OUT << "\n>>> group_match(begin) <<<\n"
766 "new node(" << id << ") \"";
767 for (IteratorT2 it = first; it != last; ++it)
768 impl::token_printer(BOOST_SPIRIT_DEBUG_OUT, *it);
769 BOOST_SPIRIT_DEBUG_OUT << "\"\n";
770 BOOST_SPIRIT_DEBUG_OUT << "new child tree (before grouping):\n" << m << "\n";
772 tree_policy_t::group_match(m, id, first, last);
774 BOOST_SPIRIT_DEBUG_OUT << "new child tree (after grouping):\n" << m << "\n";
775 BOOST_SPIRIT_DEBUG_OUT << ">>> group_match(end) <<<\n\n";
777 tree_policy_t::group_match(m, id, first, last);
782 //////////////////////////////////
783 template <typename MatchPolicyT, typename NodeFactoryT>
784 struct common_tree_tree_policy
786 typedef typename MatchPolicyT::iterator_t iterator_t;
787 typedef typename MatchPolicyT::match_t match_t;
788 typedef typename NodeFactoryT::template factory<iterator_t> factory_t;
789 typedef typename factory_t::node_t node_t;
791 template <typename Iterator1T, typename Iterator2T>
793 create_node(std::size_t /*length*/, Iterator1T const& first,
794 Iterator2T const& last, bool leaf_node)
796 return factory_t::create_node(first, last, leaf_node);
802 return factory_t::empty_node();
805 template <typename FunctorT>
806 static void apply_op_to_match(FunctorT const& op, match_t& m)
812 //////////////////////////////////
813 // directives to modify how the parse tree is generated
815 struct no_tree_gen_node_parser_gen;
817 template <typename T>
818 struct no_tree_gen_node_parser
819 : public unary<T, parser<no_tree_gen_node_parser<T> > >
821 typedef no_tree_gen_node_parser<T> self_t;
822 typedef no_tree_gen_node_parser_gen parser_generator_t;
823 typedef unary_parser_category parser_category_t;
825 no_tree_gen_node_parser(T const& a)
826 : unary<T, parser<no_tree_gen_node_parser<T> > >(a) {}
828 template <typename ScannerT>
829 typename parser_result<self_t, ScannerT>::type
830 parse(ScannerT const& scanner) const
832 typedef typename ScannerT::iteration_policy_t iteration_policy_t;
833 typedef match_policy match_policy_t;
834 typedef typename ScannerT::action_policy_t action_policy_t;
835 typedef scanner_policies<
841 return this->subject().parse(scanner.change_policies(policies_t(scanner)));
845 struct no_tree_gen_node_parser_gen
847 template <typename T>
850 typedef no_tree_gen_node_parser<T> type;
853 template <typename T>
854 static no_tree_gen_node_parser<T>
855 generate(parser<T> const& s)
857 return no_tree_gen_node_parser<T>(s.derived());
860 template <typename T>
861 no_tree_gen_node_parser<T>
862 operator[](parser<T> const& s) const
864 return no_tree_gen_node_parser<T>(s.derived());
868 const no_tree_gen_node_parser_gen no_node_d = no_tree_gen_node_parser_gen();
870 //////////////////////////////////
872 struct leaf_node_parser_gen;
875 struct leaf_node_parser
876 : public unary<T, parser<leaf_node_parser<T> > >
878 typedef leaf_node_parser<T> self_t;
879 typedef leaf_node_parser_gen parser_generator_t;
880 typedef unary_parser_category parser_category_t;
882 leaf_node_parser(T const& a)
883 : unary<T, parser<leaf_node_parser<T> > >(a) {}
885 template <typename ScannerT>
886 typename parser_result<self_t, ScannerT>::type
887 parse(ScannerT const& scanner) const
889 typedef scanner_policies< typename ScannerT::iteration_policy_t,
890 match_policy, typename ScannerT::action_policy_t > policies_t;
892 typedef typename ScannerT::iterator_t iterator_t;
893 typedef typename parser_result<self_t, ScannerT>::type result_t;
894 typedef typename result_t::node_factory_t factory_t;
896 iterator_t from = scanner.first;
897 result_t hit = impl::contiguous_parser_parse<result_t>(this->subject(),
898 scanner.change_policies(policies_t(scanner,match_policy(),scanner)),
902 return result_t(hit.length(),
903 factory_t::create_node(from, scanner.first, true));
905 return result_t(hit.length());
909 struct leaf_node_parser_gen
911 template <typename T>
914 typedef leaf_node_parser<T> type;
917 template <typename T>
918 static leaf_node_parser<T>
919 generate(parser<T> const& s)
921 return leaf_node_parser<T>(s.derived());
924 template <typename T>
926 operator[](parser<T> const& s) const
928 return leaf_node_parser<T>(s.derived());
932 const leaf_node_parser_gen leaf_node_d = leaf_node_parser_gen();
933 const leaf_node_parser_gen token_node_d = leaf_node_parser_gen();
935 //////////////////////////////////
938 template <typename MatchPolicyT>
939 struct tree_policy_selector
941 typedef tree_policy type;
946 //////////////////////////////////
947 template <typename NodeParserT>
948 struct node_parser_gen;
950 template <typename T, typename NodeParserT>
952 : public unary<T, parser<node_parser<T, NodeParserT> > >
954 typedef node_parser<T, NodeParserT> self_t;
955 typedef node_parser_gen<NodeParserT> parser_generator_t;
956 typedef unary_parser_category parser_category_t;
958 node_parser(T const& a)
959 : unary<T, parser<node_parser<T, NodeParserT> > >(a) {}
961 template <typename ScannerT>
964 typedef typename parser_result<T, ScannerT>::type type;
967 template <typename ScannerT>
968 typename parser_result<self_t, ScannerT>::type
969 parse(ScannerT const& scanner) const
971 typename parser_result<self_t, ScannerT>::type hit = this->subject().parse(scanner);
974 impl::tree_policy_selector<typename ScannerT::match_policy_t>::type::apply_op_to_match(NodeParserT(), hit);
980 template <typename NodeParserT>
981 struct node_parser_gen
983 template <typename T>
986 typedef node_parser<T, NodeParserT> type;
989 template <typename T>
990 static node_parser<T, NodeParserT>
991 generate(parser<T> const& s)
993 return node_parser<T, NodeParserT>(s.derived());
996 template <typename T>
997 node_parser<T, NodeParserT>
998 operator[](parser<T> const& s) const
1000 return node_parser<T, NodeParserT>(s.derived());
1003 //////////////////////////////////
1004 struct reduced_node_op
1006 template <typename MatchT>
1007 void operator()(MatchT& m) const
1009 if (m.trees.size() == 1)
1011 m.trees.begin()->children.clear();
1013 else if (m.trees.size() > 1)
1015 typedef typename MatchT::node_factory_t node_factory_t;
1016 m = MatchT(m.length(), node_factory_t::group_nodes(m.trees));
1021 const node_parser_gen<reduced_node_op> reduced_node_d =
1022 node_parser_gen<reduced_node_op>();
1025 struct discard_node_op
1027 template <typename MatchT>
1028 void operator()(MatchT& m) const
1034 const node_parser_gen<discard_node_op> discard_node_d =
1035 node_parser_gen<discard_node_op>();
1037 struct infix_node_op
1039 template <typename MatchT>
1040 void operator()(MatchT& m) const
1042 typedef typename MatchT::container_t container_t;
1043 typedef typename MatchT::container_t::iterator iter_t;
1044 typedef typename MatchT::container_t::value_type value_t;
1048 using BOOST_SPIRIT_CLASSIC_NS::swap;
1050 // copying the tree nodes is expensive, since it may copy a whole
1051 // tree. swapping them is cheap, so swap the nodes we want into
1052 // a new container of children.
1053 container_t new_children;
1054 std::size_t length = 0;
1055 std::size_t tree_size = m.trees.size();
1057 // the infix_node_d[] make no sense for nodes with no subnodes
1058 BOOST_SPIRIT_ASSERT(tree_size >= 1);
1061 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
1062 new_children.reserve((tree_size+1)/2);
1064 iter_t i_end = m.trees.end();
1065 for (iter_t i = m.trees.begin(); i != i_end; ++i)
1068 // adjust the length
1069 length += std::distance((*i).value.begin(), (*i).value.end());
1071 // move the child node
1072 new_children.push_back(value_t());
1073 swap(new_children.back(), *i);
1077 // ignore this child node
1082 m = MatchT(length, new_children);
1086 const node_parser_gen<infix_node_op> infix_node_d =
1087 node_parser_gen<infix_node_op>();
1089 struct discard_first_node_op
1091 template <typename MatchT>
1092 void operator()(MatchT& m) const
1094 typedef typename MatchT::container_t container_t;
1095 typedef typename MatchT::container_t::iterator iter_t;
1096 typedef typename MatchT::container_t::value_type value_t;
1100 using BOOST_SPIRIT_CLASSIC_NS::swap;
1102 // copying the tree nodes is expensive, since it may copy a whole
1103 // tree. swapping them is cheap, so swap the nodes we want into
1104 // a new container of children, instead of saying
1105 // m.trees.erase(m.trees.begin()) because, on a container_t that will
1106 // cause all the nodes afterwards to be copied into the previous
1108 container_t new_children;
1109 std::size_t length = 0;
1110 std::size_t tree_size = m.trees.size();
1112 // the discard_first_node_d[] make no sense for nodes with no subnodes
1113 BOOST_SPIRIT_ASSERT(tree_size >= 1);
1115 if (tree_size > 1) {
1116 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
1117 new_children.reserve(tree_size - 1);
1119 iter_t i = m.trees.begin(), i_end = m.trees.end();
1120 for (++i; i != i_end; ++i)
1122 // adjust the length
1123 length += std::distance((*i).value.begin(), (*i).value.end());
1125 // move the child node
1126 new_children.push_back(value_t());
1127 swap(new_children.back(), *i);
1131 // if there was a tree and now there isn't any, insert an empty node
1132 iter_t i = m.trees.begin();
1134 // This isn't entirely correct, since the empty node will reference
1135 // the end of the discarded node, but I currently don't see any way to
1136 // get at the begin of the node following this subnode.
1137 // This should be safe anyway because the it shouldn't get dereferenced
1138 // under any circumstances.
1139 typedef typename value_t::parse_node_t::iterator_t iterator_type;
1140 iterator_type it = (*i).value.end();
1142 new_children.push_back(
1143 value_t(typename value_t::parse_node_t(it, it)));
1146 m = MatchT(length, new_children);
1150 const node_parser_gen<discard_first_node_op> discard_first_node_d =
1151 node_parser_gen<discard_first_node_op>();
1153 struct discard_last_node_op
1155 template <typename MatchT>
1156 void operator()(MatchT& m) const
1158 typedef typename MatchT::container_t container_t;
1159 typedef typename MatchT::container_t::iterator iter_t;
1160 typedef typename MatchT::container_t::value_type value_t;
1164 using BOOST_SPIRIT_CLASSIC_NS::swap;
1166 // copying the tree nodes is expensive, since it may copy a whole
1167 // tree. swapping them is cheap, so swap the nodes we want into
1168 // a new container of children, instead of saying
1169 // m.trees.erase(m.trees.begin()) because, on a container_t that will
1170 // cause all the nodes afterwards to be copied into the previous
1172 container_t new_children;
1173 std::size_t length = 0;
1174 std::size_t tree_size = m.trees.size();
1176 // the discard_last_node_d[] make no sense for nodes with no subnodes
1177 BOOST_SPIRIT_ASSERT(tree_size >= 1);
1179 if (tree_size > 1) {
1181 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
1182 new_children.reserve(tree_size - 1);
1184 iter_t i_end = m.trees.end();
1185 for (iter_t i = m.trees.begin(); i != i_end; ++i)
1187 // adjust the length
1188 length += std::distance((*i).value.begin(), (*i).value.end());
1190 // move the child node
1191 new_children.push_back(value_t());
1192 swap(new_children.back(), *i);
1196 // if there was a tree and now there isn't any, insert an empty node
1197 iter_t i = m.trees.begin();
1199 typedef typename value_t::parse_node_t::iterator_t iterator_type;
1200 iterator_type it = (*i).value.begin();
1202 new_children.push_back(
1203 value_t(typename value_t::parse_node_t(it, it)));
1206 m = MatchT(length, new_children);
1210 const node_parser_gen<discard_last_node_op> discard_last_node_d =
1211 node_parser_gen<discard_last_node_op>();
1213 struct inner_node_op
1215 template <typename MatchT>
1216 void operator()(MatchT& m) const
1218 typedef typename MatchT::container_t container_t;
1219 typedef typename MatchT::container_t::iterator iter_t;
1220 typedef typename MatchT::container_t::value_type value_t;
1224 using BOOST_SPIRIT_CLASSIC_NS::swap;
1226 // copying the tree nodes is expensive, since it may copy a whole
1227 // tree. swapping them is cheap, so swap the nodes we want into
1228 // a new container of children, instead of saying
1229 // m.trees.erase(m.trees.begin()) because, on a container_t that will
1230 // cause all the nodes afterwards to be copied into the previous
1232 container_t new_children;
1233 std::size_t length = 0;
1234 std::size_t tree_size = m.trees.size();
1236 // the inner_node_d[] make no sense for nodes with less then 2 subnodes
1237 BOOST_SPIRIT_ASSERT(tree_size >= 2);
1239 if (tree_size > 2) {
1240 m.trees.pop_back(); // erase the last element
1241 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
1242 new_children.reserve(tree_size - 1);
1244 iter_t i = m.trees.begin(); // skip over the first element
1245 iter_t i_end = m.trees.end();
1246 for (++i; i != i_end; ++i)
1248 // adjust the length
1249 length += std::distance((*i).value.begin(), (*i).value.end());
1251 // move the child node
1252 new_children.push_back(value_t());
1253 swap(new_children.back(), *i);
1257 // if there was a tree and now there isn't any, insert an empty node
1258 iter_t i = m.trees.begin(); // skip over the first element
1260 typedef typename value_t::parse_node_t::iterator_t iterator_type;
1261 iterator_type it = (*++i).value.begin();
1263 new_children.push_back(
1264 value_t(typename value_t::parse_node_t(it, it)));
1267 m = MatchT(length, new_children);
1271 const node_parser_gen<inner_node_op> inner_node_d =
1272 node_parser_gen<inner_node_op>();
1275 //////////////////////////////////
1276 // action_directive_parser and action_directive_parser_gen
1277 // are meant to be used as a template to create directives that
1278 // generate action classes. For example access_match and
1279 // access_node. The ActionParserT template parameter must be
1280 // a class that has an innter class called action that is templated
1281 // on the parser type and the action type.
1282 template <typename ActionParserT>
1283 struct action_directive_parser_gen;
1285 template <typename T, typename ActionParserT>
1286 struct action_directive_parser
1287 : public unary<T, parser<action_directive_parser<T, ActionParserT> > >
1289 typedef action_directive_parser<T, ActionParserT> self_t;
1290 typedef action_directive_parser_gen<ActionParserT> parser_generator_t;
1291 typedef unary_parser_category parser_category_t;
1293 action_directive_parser(T const& a)
1294 : unary<T, parser<action_directive_parser<T, ActionParserT> > >(a) {}
1296 template <typename ScannerT>
1299 typedef typename parser_result<T, ScannerT>::type type;
1302 template <typename ScannerT>
1303 typename parser_result<self_t, ScannerT>::type
1304 parse(ScannerT const& scanner) const
1306 return this->subject().parse(scanner);
1309 template <typename ActionT>
1310 typename ActionParserT::template action<action_directive_parser<T, ActionParserT>, ActionT>
1311 operator[](ActionT const& actor) const
1314 ActionParserT::template action<action_directive_parser, ActionT>
1316 return action_t(*this, actor);
1320 //////////////////////////////////
1321 template <typename ActionParserT>
1322 struct action_directive_parser_gen
1324 template <typename T>
1327 typedef action_directive_parser<T, ActionParserT> type;
1330 template <typename T>
1331 static action_directive_parser<T, ActionParserT>
1332 generate(parser<T> const& s)
1334 return action_directive_parser<T, ActionParserT>(s.derived());
1337 template <typename T>
1338 action_directive_parser<T, ActionParserT>
1339 operator[](parser<T> const& s) const
1341 return action_directive_parser<T, ActionParserT>(s.derived());
1345 //////////////////////////////////
1346 // Calls the attached action passing it the match from the parser
1347 // and the first and last iterators.
1348 // The inner template class is used to simulate template-template parameters
1349 // (declared in common_fwd.hpp).
1350 template <typename ParserT, typename ActionT>
1351 struct access_match_action::action
1352 : public unary<ParserT, parser<access_match_action::action<ParserT, ActionT> > >
1354 typedef action_parser_category parser_category;
1355 typedef action<ParserT, ActionT> self_t;
1357 template <typename ScannerT>
1360 typedef typename parser_result<ParserT, ScannerT>::type type;
1363 action( ParserT const& subject,
1364 ActionT const& actor_);
1366 template <typename ScannerT>
1367 typename parser_result<self_t, ScannerT>::type
1368 parse(ScannerT const& scanner) const;
1370 ActionT const &predicate() const;
1376 //////////////////////////////////
1377 template <typename ParserT, typename ActionT>
1378 access_match_action::action<ParserT, ActionT>::action(
1379 ParserT const& subject,
1380 ActionT const& actor_)
1381 : unary<ParserT, parser<access_match_action::action<ParserT, ActionT> > >(subject)
1385 //////////////////////////////////
1386 template <typename ParserT, typename ActionT>
1387 template <typename ScannerT>
1388 typename parser_result<access_match_action::action<ParserT, ActionT>, ScannerT>::type
1389 access_match_action::action<ParserT, ActionT>::
1390 parse(ScannerT const& scan) const
1392 typedef typename ScannerT::iterator_t iterator_t;
1393 typedef typename parser_result<self_t, ScannerT>::type result_t;
1396 iterator_t save = scan.first;
1397 result_t hit = this->subject().parse(scan);
1398 actor(hit, save, scan.first);
1401 return scan.no_match();
1404 //////////////////////////////////
1405 template <typename ParserT, typename ActionT>
1406 ActionT const &access_match_action::action<ParserT, ActionT>::predicate() const
1411 //////////////////////////////////
1412 const action_directive_parser_gen<access_match_action> access_match_d
1413 = action_directive_parser_gen<access_match_action>();
1417 //////////////////////////////////
1418 // Calls the attached action passing it the node from the parser
1419 // and the first and last iterators
1420 // The inner template class is used to simulate template-template parameters
1421 // (declared in common_fwd.hpp).
1422 template <typename ParserT, typename ActionT>
1423 struct access_node_action::action
1424 : public unary<ParserT, parser<access_node_action::action<ParserT, ActionT> > >
1426 typedef action_parser_category parser_category;
1427 typedef action<ParserT, ActionT> self_t;
1429 template <typename ScannerT>
1432 typedef typename parser_result<ParserT, ScannerT>::type type;
1435 action( ParserT const& subject,
1436 ActionT const& actor_);
1438 template <typename ScannerT>
1439 typename parser_result<self_t, ScannerT>::type
1440 parse(ScannerT const& scanner) const;
1442 ActionT const &predicate() const;
1448 //////////////////////////////////
1449 template <typename ParserT, typename ActionT>
1450 access_node_action::action<ParserT, ActionT>::action(
1451 ParserT const& subject,
1452 ActionT const& actor_)
1453 : unary<ParserT, parser<access_node_action::action<ParserT, ActionT> > >(subject)
1457 //////////////////////////////////
1458 template <typename ParserT, typename ActionT>
1459 template <typename ScannerT>
1460 typename parser_result<access_node_action::action<ParserT, ActionT>, ScannerT>::type
1461 access_node_action::action<ParserT, ActionT>::
1462 parse(ScannerT const& scan) const
1464 typedef typename ScannerT::iterator_t iterator_t;
1465 typedef typename parser_result<self_t, ScannerT>::type result_t;
1468 iterator_t save = scan.first;
1469 result_t hit = this->subject().parse(scan);
1470 if (hit && hit.trees.size() > 0)
1471 actor(*hit.trees.begin(), save, scan.first);
1474 return scan.no_match();
1477 //////////////////////////////////
1478 template <typename ParserT, typename ActionT>
1479 ActionT const &access_node_action::action<ParserT, ActionT>::predicate() const
1484 //////////////////////////////////
1485 const action_directive_parser_gen<access_node_action> access_node_d
1486 = action_directive_parser_gen<access_node_action>();
1490 //////////////////////////////////
1492 ///////////////////////////////////////////////////////////////////////////////
1496 // Results returned by the tree parse functions:
1498 // stop: points to the final parse position (i.e parsing
1499 // processed the input up to this point).
1501 // match: true if parsing is successful. This may be full:
1502 // the parser consumed all the input, or partial:
1503 // the parser consumed only a portion of the input.
1505 // full: true when we have a full match (i.e the parser
1506 // consumed all the input.
1508 // length: The number of characters consumed by the parser.
1509 // This is valid only if we have a successful match
1510 // (either partial or full). A negative value means
1511 // that the match is unsuccessful.
1513 // trees: Contains the root node(s) of the tree.
1515 ///////////////////////////////////////////////////////////////////////////////
1518 typename NodeFactoryT,
1521 struct tree_parse_info
1527 typename tree_match<IteratorT, NodeFactoryT, T>::container_t trees;
1537 template <typename IteratorT2>
1538 tree_parse_info(tree_parse_info<IteratorT2> const& pi)
1547 using BOOST_SPIRIT_CLASSIC_NS::swap;
1549 // use auto_ptr like ownership for the trees data member
1550 swap(trees, pi.trees);
1557 std::size_t length_,
1558 typename tree_match<IteratorT, NodeFactoryT, T>::container_t trees_)
1567 using BOOST_SPIRIT_CLASSIC_NS::swap;
1569 // use auto_ptr like ownership for the trees data member
1570 swap(trees, trees_);
1574 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
1576 }} // namespace BOOST_SPIRIT_CLASSIC_NS