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/detail/iterator.hpp> // for boost::detail::iterator_traits
30 #include <boost/assert.hpp>
32 #if defined(BOOST_SPIRIT_DEBUG) && \
33 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
35 #include <boost/spirit/home/classic/debug/debug_node.hpp>
38 #include <boost/spirit/home/classic/tree/common_fwd.hpp>
40 namespace boost { namespace spirit {
42 BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
45 void swap(tree_node<T>& a, tree_node<T>& b);
47 template <typename T, typename V>
48 void swap(node_iter_data<T, V>& a, node_iter_data<T, V>& b);
52 inline void cp_swap(T& t1, T& t2);
58 typedef T parse_node_t;
60 #if !defined(BOOST_SPIRIT_USE_BOOST_ALLOCATOR_FOR_TREES)
61 typedef std::allocator<tree_node<T> > allocator_type;
62 #elif !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
63 typedef boost::pool_allocator<tree_node<T> > allocator_type;
65 typedef boost::fast_pool_allocator<tree_node<T> > allocator_type;
68 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
69 typedef std::vector<tree_node<T>, allocator_type> children_t;
71 typedef std::list<tree_node<T>, allocator_type> children_t;
72 #endif // BOOST_SPIRIT_USE_LIST_FOR_TREES
74 typedef typename children_t::iterator tree_iterator;
75 typedef typename children_t::const_iterator const_tree_iterator;
85 explicit tree_node(T const& v)
90 tree_node(T const& v, children_t const& c)
95 void swap(tree_node<T>& x)
97 impl::cp_swap(value, x.value);
98 impl::cp_swap(children, x.children);
101 // Intel V5.0.1 has a problem without this explicit operator=
102 tree_node &operator= (tree_node const &rhs)
104 tree_node(rhs).swap(*this);
109 #if defined(BOOST_SPIRIT_DEBUG) && \
110 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
111 template <typename T>
113 operator<<(std::ostream& o, tree_node<T> const& n)
115 static int depth = 0;
117 for (int i = 0; i <= depth; ++i)
121 o << "(depth = " << depth++ << " value = " << n.value;
123 for (typename tree_node<T>::children_t::const_iterator it = n.children.begin();
124 it != n.children.end(); ++it)
126 o << " children[" << c++ << "] = " << *it;
134 //////////////////////////////////
135 template <typename IteratorT, typename ValueT>
136 struct node_iter_data
138 typedef IteratorT iterator_t;
139 typedef IteratorT /*const*/ const_iterator_t;
142 : first(), last(), is_root_(false), parser_id_(), value_()
145 node_iter_data(IteratorT const& _first, IteratorT const& _last)
146 : first(_first), last(_last), is_root_(false), parser_id_(), value_()
149 void swap(node_iter_data& x)
151 impl::cp_swap(first, x.first);
152 impl::cp_swap(last, x.last);
153 impl::cp_swap(parser_id_, x.parser_id_);
154 impl::cp_swap(is_root_, x.is_root_);
155 impl::cp_swap(value_, x.value_);
163 IteratorT const& begin() const
173 IteratorT const& end() const
198 ValueT const& value() const
203 void value(ValueT const& v)
208 IteratorT first, last;
210 parser_id parser_id_;
216 #if defined(BOOST_SPIRIT_DEBUG) && \
217 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
218 // value is default nil_t, so provide an operator<< for nil_t
220 operator<<(std::ostream& o, nil_t const&)
225 template <typename IteratorT, typename ValueT>
227 operator<<(std::ostream& o, node_iter_data<IteratorT, ValueT> const& n)
229 o << "(id = " << n.id() << " text = \"";
230 typedef typename node_iter_data<IteratorT, ValueT>::const_iterator_t
232 for (iterator_t it = n.begin(); it != n.end(); ++it)
233 impl::token_printer(o, *it);
234 o << "\" is_root = " << n.is_root()
235 << /*" value = " << n.value() << */")";
240 //////////////////////////////////
241 template <typename IteratorT = char const*, typename ValueT = nil_t>
245 typename boost::detail::iterator_traits<IteratorT>::value_type
248 #if !defined(BOOST_SPIRIT_USE_BOOST_ALLOCATOR_FOR_TREES)
249 typedef std::allocator<value_type> allocator_type;
250 #elif !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
251 typedef boost::pool_allocator<value_type> allocator_type;
253 typedef boost::fast_pool_allocator<value_type> allocator_type;
256 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
257 typedef std::vector<value_type, allocator_type> container_t;
259 typedef std::list<value_type, allocator_type> container_t;
262 typedef typename container_t::iterator iterator_t;
263 typedef typename container_t::const_iterator const_iterator_t;
266 : text(), is_root_(false), parser_id_(), value_()
269 #if defined(BOOST_NO_TEMPLATED_ITERATOR_CONSTRUCTORS)
270 node_val_data(IteratorT const& _first, IteratorT const& _last)
271 : text(), is_root_(false), parser_id_(), value_()
273 std::copy(_first, _last, std::inserter(text, text.end()));
276 // This constructor is for building text out of iterators
277 template <typename IteratorT2>
278 node_val_data(IteratorT2 const& _first, IteratorT2 const& _last)
279 : text(), is_root_(false), parser_id_(), value_()
281 std::copy(_first, _last, std::inserter(text, text.end()));
284 node_val_data(IteratorT const& _first, IteratorT const& _last)
285 : text(_first, _last), is_root_(false), parser_id_(), value_()
288 // This constructor is for building text out of iterators
289 template <typename IteratorT2>
290 node_val_data(IteratorT2 const& _first, IteratorT2 const& _last)
291 : text(_first, _last), is_root_(false), parser_id_(), value_()
295 void swap(node_val_data& x)
297 impl::cp_swap(text, x.text);
298 impl::cp_swap(is_root_, x.is_root_);
299 impl::cp_swap(parser_id_, x.parser_id_);
300 impl::cp_swap(value_, x.value_);
303 typename container_t::iterator begin()
308 typename container_t::const_iterator begin() const
313 typename container_t::iterator end()
318 typename container_t::const_iterator end() const
343 ValueT const& value() const
348 void value(ValueT const& v)
356 parser_id parser_id_;
360 #if defined(BOOST_SPIRIT_DEBUG) && \
361 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
362 template <typename IteratorT, typename ValueT>
364 operator<<(std::ostream& o, node_val_data<IteratorT, ValueT> const& n)
366 o << "(id = " << n.id() << " text = \"";
367 typedef typename node_val_data<IteratorT, ValueT>::const_iterator_t
369 for (iterator_t it = n.begin(); it != n.end(); ++it)
370 impl::token_printer(o, *it);
371 o << "\" is_root = " << n.is_root()
372 << " value = " << n.value() << ")";
377 template <typename T>
379 swap(tree_node<T>& a, tree_node<T>& b)
384 template <typename T, typename V>
386 swap(node_iter_data<T, V>& a, node_iter_data<T, V>& b)
391 //////////////////////////////////
392 template <typename ValueT>
393 class node_iter_data_factory
396 // This inner class is so that node_iter_data_factory can simulate
397 // a template template parameter
398 template <typename IteratorT>
402 typedef IteratorT iterator_t;
403 typedef node_iter_data<iterator_t, ValueT> node_t;
405 static node_t create_node(iterator_t const& first, iterator_t const& last,
406 bool /*is_leaf_node*/)
408 return node_t(first, last);
411 static node_t empty_node()
416 // precondition: ContainerT contains a tree_node<node_t>. And all
417 // iterators in the container point to the same sequence.
418 template <typename ContainerT>
419 static node_t group_nodes(ContainerT const& nodes)
421 return node_t(nodes.begin()->value.begin(),
422 nodes.back().value.end());
427 //////////////////////////////////
428 template <typename ValueT>
429 class node_val_data_factory
432 // This inner class is so that node_val_data_factory can simulate
433 // a template template parameter
434 template <typename IteratorT>
438 typedef IteratorT iterator_t;
439 typedef node_val_data<iterator_t, ValueT> node_t;
441 static node_t create_node(iterator_t const& first, iterator_t const& last,
445 return node_t(first, last);
450 static node_t empty_node()
455 template <typename ContainerT>
456 static node_t group_nodes(ContainerT const& nodes)
458 typename node_t::container_t c;
459 typename ContainerT::const_iterator i_end = nodes.end();
460 // copy all the nodes text into a new one
461 for (typename ContainerT::const_iterator i = nodes.begin();
464 // See docs: reduced_node_d cannot be used with a
465 // rule inside the [].
466 BOOST_ASSERT(i->children.size() == 0);
467 c.insert(c.end(), i->value.begin(), i->value.end());
469 return node_t(c.begin(), c.end());
474 //////////////////////////////////
475 template <typename ValueT>
476 class node_all_val_data_factory
479 // This inner class is so that node_all_val_data_factory can simulate
480 // a template template parameter
481 template <typename IteratorT>
485 typedef IteratorT iterator_t;
486 typedef node_val_data<iterator_t, ValueT> node_t;
488 static node_t create_node(iterator_t const& first, iterator_t const& last,
489 bool /*is_leaf_node*/)
491 return node_t(first, last);
494 static node_t empty_node()
499 template <typename ContainerT>
500 static node_t group_nodes(ContainerT const& nodes)
502 typename node_t::container_t c;
503 typename ContainerT::const_iterator i_end = nodes.end();
504 // copy all the nodes text into a new one
505 for (typename ContainerT::const_iterator i = nodes.begin();
508 BOOST_ASSERT(i->children.size() == 0);
509 c.insert(c.end(), i->value.begin(), i->value.end());
511 return node_t(c.begin(), c.end());
518 ///////////////////////////////////////////////////////////////////////////
519 // can't call unqualified swap from within classname::swap
520 // as Koenig lookup rules will find only the classname::swap
521 // member function not the global declaration, so use cp_swap
522 // as a forwarding function (JM):
523 template <typename T>
524 inline void cp_swap(T& t1, T& t2)
527 using BOOST_SPIRIT_CLASSIC_NS::swap;
533 //////////////////////////////////
534 template <typename IteratorT, typename NodeFactoryT, typename T>
535 class tree_match : public match<T>
539 typedef typename NodeFactoryT::template factory<IteratorT> node_factory_t;
540 typedef typename node_factory_t::node_t parse_node_t;
541 typedef tree_node<parse_node_t> node_t;
542 typedef typename node_t::children_t container_t;
543 typedef typename container_t::iterator tree_iterator;
544 typedef typename container_t::const_iterator const_tree_iterator;
547 typedef typename boost::call_traits<T>::param_type param_type;
548 typedef typename boost::call_traits<T>::reference reference;
549 typedef typename boost::call_traits<T>::const_reference const_reference;
552 : match<T>(), trees()
556 tree_match(std::size_t length_)
557 : match<T>(length_), trees()
560 tree_match(std::size_t length_, parse_node_t const& n)
561 : match<T>(length_), trees()
563 trees.push_back(node_t(n));
566 tree_match(std::size_t length_, param_type val, parse_node_t const& n)
567 : match<T>(length_, val), trees()
569 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
570 trees.reserve(10); // this is more or less an arbitrary number...
572 trees.push_back(node_t(n));
575 // attention, these constructors will change the second parameter!
576 tree_match(std::size_t length_, container_t& c)
577 : match<T>(length_), trees()
579 impl::cp_swap(trees, c);
582 tree_match(std::size_t length_, param_type val, container_t& c)
583 : match<T>(length_, val), trees()
585 impl::cp_swap(trees, c);
588 template <typename T2>
589 tree_match(match<T2> const& other)
590 : match<T>(other), trees()
593 template <typename T2, typename T3, typename T4>
594 tree_match(tree_match<T2, T3, T4> const& other)
595 : match<T>(other), trees()
596 { impl::cp_swap(trees, other.trees); }
598 template <typename T2>
600 operator=(match<T2> const& other)
602 match<T>::operator=(other);
606 template <typename T2, typename T3, typename T4>
608 operator=(tree_match<T2, T3, T4> const& other)
610 match<T>::operator=(other);
611 impl::cp_swap(trees, other.trees);
615 tree_match(tree_match const& x)
616 : match<T>(x), trees()
618 // use auto_ptr like ownership for the trees data member
619 impl::cp_swap(trees, x.trees);
622 tree_match& operator=(tree_match const& x)
629 void swap(tree_match& x)
632 impl::cp_swap(trees, x.trees);
635 mutable container_t trees;
638 #if defined(BOOST_SPIRIT_DEBUG) && \
639 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
640 template <typename IteratorT, typename NodeFactoryT, typename T>
642 operator<<(std::ostream& o, tree_match<IteratorT, NodeFactoryT, T> const& m)
645 typename tree_match<IteratorT, NodeFactoryT, T>::container_t::iterator
648 o << "(length = " << (int)m.length();
650 for (iterator i = m.trees.begin(); i != m.trees.end(); ++i)
652 o << " trees[" << c++ << "] = " << *i;
659 //////////////////////////////////
662 template <typename FunctorT, typename MatchT>
663 static void apply_op_to_match(FunctorT const& /*op*/, MatchT& /*m*/)
666 template <typename MatchT, typename Iterator1T, typename Iterator2T>
667 static void group_match(MatchT& /*m*/, parser_id const& /*id*/,
668 Iterator1T const& /*first*/, Iterator2T const& /*last*/)
671 template <typename MatchT>
672 static void concat(MatchT& /*a*/, MatchT const& /*b*/)
676 //////////////////////////////////
678 typename MatchPolicyT,
680 typename NodeFactoryT,
681 typename TreePolicyT,
684 struct common_tree_match_policy : public match_policy
686 common_tree_match_policy()
690 template <typename PolicyT>
691 common_tree_match_policy(PolicyT const & policies)
692 : match_policy((match_policy const &)policies)
696 template <typename U>
697 struct result { typedef tree_match<IteratorT, NodeFactoryT, U> type; };
699 typedef tree_match<IteratorT, NodeFactoryT, T> match_t;
700 typedef IteratorT iterator_t;
701 typedef TreePolicyT tree_policy_t;
702 typedef NodeFactoryT factory_t;
704 static const match_t no_match() { return match_t(); }
705 static const match_t empty_match()
706 { return match_t(0, tree_policy_t::empty_node()); }
708 template <typename AttrT, typename Iterator1T, typename Iterator2T>
709 static tree_match<IteratorT, NodeFactoryT, AttrT> create_match(
712 Iterator1T const& first,
713 Iterator2T const& last)
715 #if defined(BOOST_SPIRIT_DEBUG) && \
716 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
718 BOOST_SPIRIT_DEBUG_OUT << "\n>>> create_node(begin) <<<\n"
719 "creating node text: \"";
720 for (Iterator1T it = first; it != last; ++it)
721 impl::token_printer(BOOST_SPIRIT_DEBUG_OUT, *it);
722 BOOST_SPIRIT_DEBUG_OUT << "\"\n";
723 BOOST_SPIRIT_DEBUG_OUT << ">>> create_node(end) <<<\n\n";
725 return tree_match<IteratorT, NodeFactoryT, AttrT>(length, val,
726 tree_policy_t::create_node(length, first, last, true));
729 template <typename Match1T, typename Match2T>
730 static void concat_match(Match1T& a, Match2T const& b)
732 #if defined(BOOST_SPIRIT_DEBUG) && \
733 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
735 BOOST_SPIRIT_DEBUG_OUT << "\n>>> concat_match(begin) <<<\n";
736 BOOST_SPIRIT_DEBUG_OUT << "tree a:\n" << a << "\n";
737 BOOST_SPIRIT_DEBUG_OUT << "tree b:\n" << b << "\n";
738 BOOST_SPIRIT_DEBUG_OUT << ">>> concat_match(end) <<<\n\n";
740 BOOST_SPIRIT_ASSERT(a && b);
746 else if (b.length() == 0
747 #ifdef BOOST_SPIRIT_NO_TREE_NODE_COLLAPSING
748 && !b.trees.begin()->value.id().to_long()
755 tree_policy_t::concat(a, b);
758 template <typename MatchT, typename IteratorT2>
763 IteratorT2 const& first,
764 IteratorT2 const& last) const
768 #if defined(BOOST_SPIRIT_DEBUG) && \
769 (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_TREES)
771 BOOST_SPIRIT_DEBUG_OUT << "\n>>> group_match(begin) <<<\n"
772 "new node(" << id << ") \"";
773 for (IteratorT2 it = first; it != last; ++it)
774 impl::token_printer(BOOST_SPIRIT_DEBUG_OUT, *it);
775 BOOST_SPIRIT_DEBUG_OUT << "\"\n";
776 BOOST_SPIRIT_DEBUG_OUT << "new child tree (before grouping):\n" << m << "\n";
778 tree_policy_t::group_match(m, id, first, last);
780 BOOST_SPIRIT_DEBUG_OUT << "new child tree (after grouping):\n" << m << "\n";
781 BOOST_SPIRIT_DEBUG_OUT << ">>> group_match(end) <<<\n\n";
783 tree_policy_t::group_match(m, id, first, last);
788 //////////////////////////////////
789 template <typename MatchPolicyT, typename NodeFactoryT>
790 struct common_tree_tree_policy
792 typedef typename MatchPolicyT::iterator_t iterator_t;
793 typedef typename MatchPolicyT::match_t match_t;
794 typedef typename NodeFactoryT::template factory<iterator_t> factory_t;
795 typedef typename factory_t::node_t node_t;
797 template <typename Iterator1T, typename Iterator2T>
799 create_node(std::size_t /*length*/, Iterator1T const& first,
800 Iterator2T const& last, bool leaf_node)
802 return factory_t::create_node(first, last, leaf_node);
808 return factory_t::empty_node();
811 template <typename FunctorT>
812 static void apply_op_to_match(FunctorT const& op, match_t& m)
818 //////////////////////////////////
819 // directives to modify how the parse tree is generated
821 struct no_tree_gen_node_parser_gen;
823 template <typename T>
824 struct no_tree_gen_node_parser
825 : public unary<T, parser<no_tree_gen_node_parser<T> > >
827 typedef no_tree_gen_node_parser<T> self_t;
828 typedef no_tree_gen_node_parser_gen parser_generator_t;
829 typedef unary_parser_category parser_category_t;
831 no_tree_gen_node_parser(T const& a)
832 : unary<T, parser<no_tree_gen_node_parser<T> > >(a) {}
834 template <typename ScannerT>
835 typename parser_result<self_t, ScannerT>::type
836 parse(ScannerT const& scanner) const
838 typedef typename ScannerT::iteration_policy_t iteration_policy_t;
839 typedef match_policy match_policy_t;
840 typedef typename ScannerT::action_policy_t action_policy_t;
841 typedef scanner_policies<
847 return this->subject().parse(scanner.change_policies(policies_t(scanner)));
851 struct no_tree_gen_node_parser_gen
853 template <typename T>
856 typedef no_tree_gen_node_parser<T> type;
859 template <typename T>
860 static no_tree_gen_node_parser<T>
861 generate(parser<T> const& s)
863 return no_tree_gen_node_parser<T>(s.derived());
866 template <typename T>
867 no_tree_gen_node_parser<T>
868 operator[](parser<T> const& s) const
870 return no_tree_gen_node_parser<T>(s.derived());
874 const no_tree_gen_node_parser_gen no_node_d = no_tree_gen_node_parser_gen();
876 //////////////////////////////////
878 struct leaf_node_parser_gen;
881 struct leaf_node_parser
882 : public unary<T, parser<leaf_node_parser<T> > >
884 typedef leaf_node_parser<T> self_t;
885 typedef leaf_node_parser_gen parser_generator_t;
886 typedef unary_parser_category parser_category_t;
888 leaf_node_parser(T const& a)
889 : unary<T, parser<leaf_node_parser<T> > >(a) {}
891 template <typename ScannerT>
892 typename parser_result<self_t, ScannerT>::type
893 parse(ScannerT const& scanner) const
895 typedef scanner_policies< typename ScannerT::iteration_policy_t,
896 match_policy, typename ScannerT::action_policy_t > policies_t;
898 typedef typename ScannerT::iterator_t iterator_t;
899 typedef typename parser_result<self_t, ScannerT>::type result_t;
900 typedef typename result_t::node_factory_t factory_t;
902 iterator_t from = scanner.first;
903 result_t hit = impl::contiguous_parser_parse<result_t>(this->subject(),
904 scanner.change_policies(policies_t(scanner,match_policy(),scanner)),
908 return result_t(hit.length(),
909 factory_t::create_node(from, scanner.first, true));
911 return result_t(hit.length());
915 struct leaf_node_parser_gen
917 template <typename T>
920 typedef leaf_node_parser<T> type;
923 template <typename T>
924 static leaf_node_parser<T>
925 generate(parser<T> const& s)
927 return leaf_node_parser<T>(s.derived());
930 template <typename T>
932 operator[](parser<T> const& s) const
934 return leaf_node_parser<T>(s.derived());
938 const leaf_node_parser_gen leaf_node_d = leaf_node_parser_gen();
939 const leaf_node_parser_gen token_node_d = leaf_node_parser_gen();
941 //////////////////////////////////
944 template <typename MatchPolicyT>
945 struct tree_policy_selector
947 typedef tree_policy type;
952 //////////////////////////////////
953 template <typename NodeParserT>
954 struct node_parser_gen;
956 template <typename T, typename NodeParserT>
958 : public unary<T, parser<node_parser<T, NodeParserT> > >
960 typedef node_parser<T, NodeParserT> self_t;
961 typedef node_parser_gen<NodeParserT> parser_generator_t;
962 typedef unary_parser_category parser_category_t;
964 node_parser(T const& a)
965 : unary<T, parser<node_parser<T, NodeParserT> > >(a) {}
967 template <typename ScannerT>
970 typedef typename parser_result<T, ScannerT>::type type;
973 template <typename ScannerT>
974 typename parser_result<self_t, ScannerT>::type
975 parse(ScannerT const& scanner) const
977 typename parser_result<self_t, ScannerT>::type hit = this->subject().parse(scanner);
980 impl::tree_policy_selector<typename ScannerT::match_policy_t>::type::apply_op_to_match(NodeParserT(), hit);
986 template <typename NodeParserT>
987 struct node_parser_gen
989 template <typename T>
992 typedef node_parser<T, NodeParserT> type;
995 template <typename T>
996 static node_parser<T, NodeParserT>
997 generate(parser<T> const& s)
999 return node_parser<T, NodeParserT>(s.derived());
1002 template <typename T>
1003 node_parser<T, NodeParserT>
1004 operator[](parser<T> const& s) const
1006 return node_parser<T, NodeParserT>(s.derived());
1009 //////////////////////////////////
1010 struct reduced_node_op
1012 template <typename MatchT>
1013 void operator()(MatchT& m) const
1015 if (m.trees.size() == 1)
1017 m.trees.begin()->children.clear();
1019 else if (m.trees.size() > 1)
1021 typedef typename MatchT::node_factory_t node_factory_t;
1022 m = MatchT(m.length(), node_factory_t::group_nodes(m.trees));
1027 const node_parser_gen<reduced_node_op> reduced_node_d =
1028 node_parser_gen<reduced_node_op>();
1031 struct discard_node_op
1033 template <typename MatchT>
1034 void operator()(MatchT& m) const
1040 const node_parser_gen<discard_node_op> discard_node_d =
1041 node_parser_gen<discard_node_op>();
1043 struct infix_node_op
1045 template <typename MatchT>
1046 void operator()(MatchT& m) const
1048 typedef typename MatchT::container_t container_t;
1049 typedef typename MatchT::container_t::iterator iter_t;
1050 typedef typename MatchT::container_t::value_type value_t;
1054 using BOOST_SPIRIT_CLASSIC_NS::swap;
1056 // copying the tree nodes is expensive, since it may copy a whole
1057 // tree. swapping them is cheap, so swap the nodes we want into
1058 // a new container of children.
1059 container_t new_children;
1060 std::size_t length = 0;
1061 std::size_t tree_size = m.trees.size();
1063 // the infix_node_d[] make no sense for nodes with no subnodes
1064 BOOST_SPIRIT_ASSERT(tree_size >= 1);
1067 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
1068 new_children.reserve((tree_size+1)/2);
1070 iter_t i_end = m.trees.end();
1071 for (iter_t i = m.trees.begin(); i != i_end; ++i)
1074 // adjust the length
1075 length += std::distance((*i).value.begin(), (*i).value.end());
1077 // move the child node
1078 new_children.push_back(value_t());
1079 swap(new_children.back(), *i);
1083 // ignore this child node
1088 m = MatchT(length, new_children);
1092 const node_parser_gen<infix_node_op> infix_node_d =
1093 node_parser_gen<infix_node_op>();
1095 struct discard_first_node_op
1097 template <typename MatchT>
1098 void operator()(MatchT& m) const
1100 typedef typename MatchT::container_t container_t;
1101 typedef typename MatchT::container_t::iterator iter_t;
1102 typedef typename MatchT::container_t::value_type value_t;
1106 using BOOST_SPIRIT_CLASSIC_NS::swap;
1108 // copying the tree nodes is expensive, since it may copy a whole
1109 // tree. swapping them is cheap, so swap the nodes we want into
1110 // a new container of children, instead of saying
1111 // m.trees.erase(m.trees.begin()) because, on a container_t that will
1112 // cause all the nodes afterwards to be copied into the previous
1114 container_t new_children;
1115 std::size_t length = 0;
1116 std::size_t tree_size = m.trees.size();
1118 // the discard_first_node_d[] make no sense for nodes with no subnodes
1119 BOOST_SPIRIT_ASSERT(tree_size >= 1);
1121 if (tree_size > 1) {
1122 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
1123 new_children.reserve(tree_size - 1);
1125 iter_t i = m.trees.begin(), i_end = m.trees.end();
1126 for (++i; i != i_end; ++i)
1128 // adjust the length
1129 length += std::distance((*i).value.begin(), (*i).value.end());
1131 // move the child node
1132 new_children.push_back(value_t());
1133 swap(new_children.back(), *i);
1137 // if there was a tree and now there isn't any, insert an empty node
1138 iter_t i = m.trees.begin();
1140 // This isn't entirely correct, since the empty node will reference
1141 // the end of the discarded node, but I currently don't see any way to
1142 // get at the begin of the node following this subnode.
1143 // This should be safe anyway because the it shouldn't get dereferenced
1144 // under any circumstances.
1145 typedef typename value_t::parse_node_t::iterator_t iterator_type;
1146 iterator_type it = (*i).value.end();
1148 new_children.push_back(
1149 value_t(typename value_t::parse_node_t(it, it)));
1152 m = MatchT(length, new_children);
1156 const node_parser_gen<discard_first_node_op> discard_first_node_d =
1157 node_parser_gen<discard_first_node_op>();
1159 struct discard_last_node_op
1161 template <typename MatchT>
1162 void operator()(MatchT& m) const
1164 typedef typename MatchT::container_t container_t;
1165 typedef typename MatchT::container_t::iterator iter_t;
1166 typedef typename MatchT::container_t::value_type value_t;
1170 using BOOST_SPIRIT_CLASSIC_NS::swap;
1172 // copying the tree nodes is expensive, since it may copy a whole
1173 // tree. swapping them is cheap, so swap the nodes we want into
1174 // a new container of children, instead of saying
1175 // m.trees.erase(m.trees.begin()) because, on a container_t that will
1176 // cause all the nodes afterwards to be copied into the previous
1178 container_t new_children;
1179 std::size_t length = 0;
1180 std::size_t tree_size = m.trees.size();
1182 // the discard_last_node_d[] make no sense for nodes with no subnodes
1183 BOOST_SPIRIT_ASSERT(tree_size >= 1);
1185 if (tree_size > 1) {
1187 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
1188 new_children.reserve(tree_size - 1);
1190 iter_t i_end = m.trees.end();
1191 for (iter_t i = m.trees.begin(); i != i_end; ++i)
1193 // adjust the length
1194 length += std::distance((*i).value.begin(), (*i).value.end());
1196 // move the child node
1197 new_children.push_back(value_t());
1198 swap(new_children.back(), *i);
1202 // if there was a tree and now there isn't any, insert an empty node
1203 iter_t i = m.trees.begin();
1205 typedef typename value_t::parse_node_t::iterator_t iterator_type;
1206 iterator_type it = (*i).value.begin();
1208 new_children.push_back(
1209 value_t(typename value_t::parse_node_t(it, it)));
1212 m = MatchT(length, new_children);
1216 const node_parser_gen<discard_last_node_op> discard_last_node_d =
1217 node_parser_gen<discard_last_node_op>();
1219 struct inner_node_op
1221 template <typename MatchT>
1222 void operator()(MatchT& m) const
1224 typedef typename MatchT::container_t container_t;
1225 typedef typename MatchT::container_t::iterator iter_t;
1226 typedef typename MatchT::container_t::value_type value_t;
1230 using BOOST_SPIRIT_CLASSIC_NS::swap;
1232 // copying the tree nodes is expensive, since it may copy a whole
1233 // tree. swapping them is cheap, so swap the nodes we want into
1234 // a new container of children, instead of saying
1235 // m.trees.erase(m.trees.begin()) because, on a container_t that will
1236 // cause all the nodes afterwards to be copied into the previous
1238 container_t new_children;
1239 std::size_t length = 0;
1240 std::size_t tree_size = m.trees.size();
1242 // the inner_node_d[] make no sense for nodes with less then 2 subnodes
1243 BOOST_SPIRIT_ASSERT(tree_size >= 2);
1245 if (tree_size > 2) {
1246 m.trees.pop_back(); // erase the last element
1247 #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
1248 new_children.reserve(tree_size - 1);
1250 iter_t i = m.trees.begin(); // skip over the first element
1251 iter_t i_end = m.trees.end();
1252 for (++i; i != i_end; ++i)
1254 // adjust the length
1255 length += std::distance((*i).value.begin(), (*i).value.end());
1257 // move the child node
1258 new_children.push_back(value_t());
1259 swap(new_children.back(), *i);
1263 // if there was a tree and now there isn't any, insert an empty node
1264 iter_t i = m.trees.begin(); // skip over the first element
1266 typedef typename value_t::parse_node_t::iterator_t iterator_type;
1267 iterator_type it = (*++i).value.begin();
1269 new_children.push_back(
1270 value_t(typename value_t::parse_node_t(it, it)));
1273 m = MatchT(length, new_children);
1277 const node_parser_gen<inner_node_op> inner_node_d =
1278 node_parser_gen<inner_node_op>();
1281 //////////////////////////////////
1282 // action_directive_parser and action_directive_parser_gen
1283 // are meant to be used as a template to create directives that
1284 // generate action classes. For example access_match and
1285 // access_node. The ActionParserT template parameter must be
1286 // a class that has an innter class called action that is templated
1287 // on the parser type and the action type.
1288 template <typename ActionParserT>
1289 struct action_directive_parser_gen;
1291 template <typename T, typename ActionParserT>
1292 struct action_directive_parser
1293 : public unary<T, parser<action_directive_parser<T, ActionParserT> > >
1295 typedef action_directive_parser<T, ActionParserT> self_t;
1296 typedef action_directive_parser_gen<ActionParserT> parser_generator_t;
1297 typedef unary_parser_category parser_category_t;
1299 action_directive_parser(T const& a)
1300 : unary<T, parser<action_directive_parser<T, ActionParserT> > >(a) {}
1302 template <typename ScannerT>
1305 typedef typename parser_result<T, ScannerT>::type type;
1308 template <typename ScannerT>
1309 typename parser_result<self_t, ScannerT>::type
1310 parse(ScannerT const& scanner) const
1312 return this->subject().parse(scanner);
1315 template <typename ActionT>
1316 typename ActionParserT::template action<action_directive_parser<T, ActionParserT>, ActionT>
1317 operator[](ActionT const& actor) const
1320 ActionParserT::template action<action_directive_parser, ActionT>
1322 return action_t(*this, actor);
1326 //////////////////////////////////
1327 template <typename ActionParserT>
1328 struct action_directive_parser_gen
1330 template <typename T>
1333 typedef action_directive_parser<T, ActionParserT> type;
1336 template <typename T>
1337 static action_directive_parser<T, ActionParserT>
1338 generate(parser<T> const& s)
1340 return action_directive_parser<T, ActionParserT>(s.derived());
1343 template <typename T>
1344 action_directive_parser<T, ActionParserT>
1345 operator[](parser<T> const& s) const
1347 return action_directive_parser<T, ActionParserT>(s.derived());
1351 //////////////////////////////////
1352 // Calls the attached action passing it the match from the parser
1353 // and the first and last iterators.
1354 // The inner template class is used to simulate template-template parameters
1355 // (declared in common_fwd.hpp).
1356 template <typename ParserT, typename ActionT>
1357 struct access_match_action::action
1358 : public unary<ParserT, parser<access_match_action::action<ParserT, ActionT> > >
1360 typedef action_parser_category parser_category;
1361 typedef action<ParserT, ActionT> self_t;
1363 template <typename ScannerT>
1366 typedef typename parser_result<ParserT, ScannerT>::type type;
1369 action( ParserT const& subject,
1370 ActionT const& actor_);
1372 template <typename ScannerT>
1373 typename parser_result<self_t, ScannerT>::type
1374 parse(ScannerT const& scanner) const;
1376 ActionT const &predicate() const;
1382 //////////////////////////////////
1383 template <typename ParserT, typename ActionT>
1384 access_match_action::action<ParserT, ActionT>::action(
1385 ParserT const& subject,
1386 ActionT const& actor_)
1387 : unary<ParserT, parser<access_match_action::action<ParserT, ActionT> > >(subject)
1391 //////////////////////////////////
1392 template <typename ParserT, typename ActionT>
1393 template <typename ScannerT>
1394 typename parser_result<access_match_action::action<ParserT, ActionT>, ScannerT>::type
1395 access_match_action::action<ParserT, ActionT>::
1396 parse(ScannerT const& scan) const
1398 typedef typename ScannerT::iterator_t iterator_t;
1399 typedef typename parser_result<self_t, ScannerT>::type result_t;
1402 iterator_t save = scan.first;
1403 result_t hit = this->subject().parse(scan);
1404 actor(hit, save, scan.first);
1407 return scan.no_match();
1410 //////////////////////////////////
1411 template <typename ParserT, typename ActionT>
1412 ActionT const &access_match_action::action<ParserT, ActionT>::predicate() const
1417 //////////////////////////////////
1418 const action_directive_parser_gen<access_match_action> access_match_d
1419 = action_directive_parser_gen<access_match_action>();
1423 //////////////////////////////////
1424 // Calls the attached action passing it the node from the parser
1425 // and the first and last iterators
1426 // The inner template class is used to simulate template-template parameters
1427 // (declared in common_fwd.hpp).
1428 template <typename ParserT, typename ActionT>
1429 struct access_node_action::action
1430 : public unary<ParserT, parser<access_node_action::action<ParserT, ActionT> > >
1432 typedef action_parser_category parser_category;
1433 typedef action<ParserT, ActionT> self_t;
1435 template <typename ScannerT>
1438 typedef typename parser_result<ParserT, ScannerT>::type type;
1441 action( ParserT const& subject,
1442 ActionT const& actor_);
1444 template <typename ScannerT>
1445 typename parser_result<self_t, ScannerT>::type
1446 parse(ScannerT const& scanner) const;
1448 ActionT const &predicate() const;
1454 //////////////////////////////////
1455 template <typename ParserT, typename ActionT>
1456 access_node_action::action<ParserT, ActionT>::action(
1457 ParserT const& subject,
1458 ActionT const& actor_)
1459 : unary<ParserT, parser<access_node_action::action<ParserT, ActionT> > >(subject)
1463 //////////////////////////////////
1464 template <typename ParserT, typename ActionT>
1465 template <typename ScannerT>
1466 typename parser_result<access_node_action::action<ParserT, ActionT>, ScannerT>::type
1467 access_node_action::action<ParserT, ActionT>::
1468 parse(ScannerT const& scan) const
1470 typedef typename ScannerT::iterator_t iterator_t;
1471 typedef typename parser_result<self_t, ScannerT>::type result_t;
1474 iterator_t save = scan.first;
1475 result_t hit = this->subject().parse(scan);
1476 if (hit && hit.trees.size() > 0)
1477 actor(*hit.trees.begin(), save, scan.first);
1480 return scan.no_match();
1483 //////////////////////////////////
1484 template <typename ParserT, typename ActionT>
1485 ActionT const &access_node_action::action<ParserT, ActionT>::predicate() const
1490 //////////////////////////////////
1491 const action_directive_parser_gen<access_node_action> access_node_d
1492 = action_directive_parser_gen<access_node_action>();
1496 //////////////////////////////////
1498 ///////////////////////////////////////////////////////////////////////////////
1502 // Results returned by the tree parse functions:
1504 // stop: points to the final parse position (i.e parsing
1505 // processed the input up to this point).
1507 // match: true if parsing is successful. This may be full:
1508 // the parser consumed all the input, or partial:
1509 // the parser consumed only a portion of the input.
1511 // full: true when we have a full match (i.e the parser
1512 // consumed all the input.
1514 // length: The number of characters consumed by the parser.
1515 // This is valid only if we have a successful match
1516 // (either partial or full). A negative value means
1517 // that the match is unsucessful.
1519 // trees: Contains the root node(s) of the tree.
1521 ///////////////////////////////////////////////////////////////////////////////
1524 typename NodeFactoryT,
1527 struct tree_parse_info
1533 typename tree_match<IteratorT, NodeFactoryT, T>::container_t trees;
1543 template <typename IteratorT2>
1544 tree_parse_info(tree_parse_info<IteratorT2> const& pi)
1553 using BOOST_SPIRIT_CLASSIC_NS::swap;
1555 // use auto_ptr like ownership for the trees data member
1556 swap(trees, pi.trees);
1563 std::size_t length_,
1564 typename tree_match<IteratorT, NodeFactoryT, T>::container_t trees_)
1573 using BOOST_SPIRIT_CLASSIC_NS::swap;
1575 // use auto_ptr like ownership for the trees data member
1576 swap(trees, trees_);
1580 BOOST_SPIRIT_CLASSIC_NAMESPACE_END
1582 }} // namespace BOOST_SPIRIT_CLASSIC_NS