common.hpp
来自「CGAL is a collaborative effort of severa」· HPP 代码 · 共 1,335 行 · 第 1/3 页
HPP
1,335 行
class node_all_val_data_factory{public: // This inner class is so that node_all_val_data_factory can simluate // a template template parameter template <typename IteratorT> class factory { public: typedef IteratorT iterator_t; typedef node_val_data<iterator_t, ValueT> node_t; static node_t create_node(iterator_t const& first, iterator_t const& last, bool /*is_leaf_node*/) { return node_t(first, last); } static node_t empty_node() { return node_t(); } template <typename ContainerT> static node_t group_nodes(ContainerT const& nodes) { typename node_t::container_t c; // copy all the nodes text into a new one for (typename ContainerT::const_iterator i = nodes.begin(); i != nodes.end(); ++i) { // See docs: token_node_d or leaf_node_d cannot be used with a // rule inside the []. assert(i->children.size() == 0); c.insert(c.end(), i->value.begin(), i->value.end()); } return node_t(c.begin(), c.end()); } };};// forward declarationtemplate < typename IteratorT, typename NodeFactoryT = node_val_data_factory<nil_t>, typename T = nil_t>class tree_match;namespace impl { /////////////////////////////////////////////////////////////////////////// // can't call unqualified swap from within classname::swap // as Koenig lookup rules will find only the classname::swap // member function not the global declaration, so use cp_swap // as a forwarding function (JM):#if __GNUC__ == 2 using ::std::swap;#endif template <typename T> inline void cp_swap(T& t1, T& t2) { using std::swap; using boost::spirit::swap; using boost::swap; swap(t1, t2); }}//////////////////////////////////template <typename IteratorT, typename NodeFactoryT, typename T>class tree_match : public match<T>{public: typedef typename NodeFactoryT::template factory<IteratorT> node_factory_t; typedef typename node_factory_t::node_t parse_node_t; typedef tree_node<parse_node_t> node_t; typedef typename node_t::children_t container_t; typedef typename container_t::iterator tree_iterator; typedef typename container_t::const_iterator const_tree_iterator; typedef T attr_t; typedef typename boost::call_traits<T>::param_type param_type; typedef typename boost::call_traits<T>::reference reference; typedef typename boost::call_traits<T>::const_reference const_reference; tree_match() : match<T>(), trees() {} explicit tree_match(std::size_t length) : match<T>(length), trees() {} tree_match(std::size_t length, parse_node_t const& n) : match<T>(length), trees() { trees.push_back(node_t(n)); } tree_match(std::size_t length, param_type val, parse_node_t const& n) : match<T>(length, val), trees() { trees.push_back(node_t(n)); } template <typename T2> tree_match(match<T2> const& other) : match<T>(other), trees() {} template <typename T2, typename T3, typename T4> tree_match(tree_match<T2, T3, T4> const& other) : match<T>(other), trees() { impl::cp_swap(trees, other.trees); } template <typename T2> tree_match& operator=(match<T2> const& other) { match<T>::operator=(other); return *this; } template <typename T2, typename T3, typename T4> tree_match& operator=(tree_match<T2, T3, T4> const& other) { match<T>::operator=(other); impl::cp_swap(trees, other.trees); return *this; } tree_match(tree_match const& x) : match<T>(x), trees() { // use auto_ptr like ownership for the trees data member impl::cp_swap(trees, x.trees); } tree_match& operator=(tree_match const& x) { tree_match tmp(x); this->swap(tmp); return *this; } void swap(tree_match& x) { match<T>::swap(x); impl::cp_swap(trees, x.trees); } mutable container_t trees;};#if defined(BOOST_SPIRIT_DEBUG) && \ (BOOST_SPIRIT_DEBUG_FLAGS_NODES & BOOST_SPIRIT_DEBUG_FLAGS_TREES)template <typename IteratorT, typename NodeFactoryT, typename T>inline std::ostream&operator<<(std::ostream& o, tree_match<IteratorT, NodeFactoryT, T> const& m){ typedef typename tree_match<IteratorT, NodeFactoryT, T>::container_t::iterator iterator; o << "(length = " << m.length(); int c = 0; for (iterator i = m.trees.begin(); i != m.trees.end(); ++i) { o << " trees[" << c++ << "] = " << *i; } o << ")"; return o;}#endif//////////////////////////////////struct tree_policy{ template <typename FunctorT, typename MatchT> static void apply_op_to_match(FunctorT const& /*op*/, MatchT& /*m*/) {} template <typename MatchT, typename Iterator1T, typename Iterator2T> static void group_match(MatchT& /*m*/, parser_id const& /*id*/, Iterator1T const& /*first*/, Iterator2T const& /*last*/) {} template <typename MatchT> static void concat(MatchT& /*a*/, MatchT const& /*b*/) {}};//////////////////////////////////template < typename MatchPolicyT, typename IteratorT, typename NodeFactoryT, typename TreePolicyT>struct common_tree_match_policy : public match_policy{ template <typename T> struct result { typedef tree_match<IteratorT, NodeFactoryT, T> type; }; typedef tree_match<IteratorT, NodeFactoryT> match_t; typedef IteratorT iterator_t; typedef TreePolicyT tree_policy_t; typedef NodeFactoryT factory_t; static const match_t no_match() { return match_t(); } static const match_t empty_match() { return match_t(0, tree_policy_t::empty_node()); } template <typename AttrT, typename Iterator1T, typename Iterator2T> static tree_match<IteratorT, NodeFactoryT, AttrT> create_match( std::size_t length, AttrT const& val, Iterator1T const& first, Iterator2T const& last) {#if defined(BOOST_SPIRIT_DEBUG) && \ (BOOST_SPIRIT_DEBUG_FLAGS_NODES & BOOST_SPIRIT_DEBUG_FLAGS_TREES) BOOST_SPIRIT_DEBUG_OUT << ">>> create_node(begin) <<<\n" "creating node text: \""; for (Iterator1T it = first; it != last; ++it) impl::token_printer(BOOST_SPIRIT_DEBUG_OUT, *it); BOOST_SPIRIT_DEBUG_OUT << "\"\n"; BOOST_SPIRIT_DEBUG_OUT << ">>> create_node(end) <<<\n"; #endif return tree_match<IteratorT, NodeFactoryT, AttrT>(length, val, tree_policy_t::create_node(length, first, last, true)); } template <typename Match1T, typename Match2T> static void concat_match(Match1T& a, Match2T const& b) {#if defined(BOOST_SPIRIT_DEBUG) && \ (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_TREES) BOOST_SPIRIT_DEBUG_OUT << ">>> concat_match(begin) <<<\n"; BOOST_SPIRIT_DEBUG_OUT << "tree a:\n" << a << "\n"; BOOST_SPIRIT_DEBUG_OUT << "tree b:\n" << b << "\n"; BOOST_SPIRIT_DEBUG_OUT << ">>> concat_match(end) <<<\n";#endif BOOST_SPIRIT_ASSERT(a && b); if (a.length() == 0) { a = b; return; } else if (b.length() == 0) { return; } a.concat(b); tree_policy_t::concat(a, b); } template <typename MatchT, typename IteratorT2> void group_match( MatchT& m, parser_id const& id, IteratorT2 const& first, IteratorT2 const& last) const { if (!m) return; #if defined(BOOST_SPIRIT_DEBUG) && \ (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_TREES) BOOST_SPIRIT_DEBUG_OUT << ">>> group_match(begin) <<<\n" "new node(" << id << ") \""; for (IteratorT2 it = first; it != last; ++it) impl::token_printer(BOOST_SPIRIT_DEBUG_OUT, *it); BOOST_SPIRIT_DEBUG_OUT << "\"\n"; BOOST_SPIRIT_DEBUG_OUT << "new child tree (before grouping):\n" << m << "\n"; tree_policy_t::group_match(m, id, first, last); BOOST_SPIRIT_DEBUG_OUT << "new child tree (after grouping):\n" << m << "\n"; BOOST_SPIRIT_DEBUG_OUT << ">>> group_match(end) <<<\n";#else tree_policy_t::group_match(m, id, first, last);#endif }};//////////////////////////////////template <typename MatchPolicyT, typename NodeFactoryT>struct common_tree_tree_policy{ typedef typename MatchPolicyT::iterator_t iterator_t; typedef typename MatchPolicyT::match_t match_t; typedef typename NodeFactoryT::template factory<iterator_t> factory_t; typedef typename factory_t::node_t node_t; template <typename Iterator1T, typename Iterator2T> static node_t create_node(std::size_t /*length*/, Iterator1T const& first, Iterator2T const& last, bool leaf_node) { return factory_t::create_node(first, last, leaf_node); } static node_t empty_node() { return factory_t::empty_node(); } template <typename FunctorT> static void apply_op_to_match(FunctorT const& op, match_t& m) { op(m); }};//////////////////////////////////// directives to modify how the parse tree is generatedstruct no_tree_gen_node_parser_gen;template <typename T>struct no_tree_gen_node_parser: public unary<T, parser<no_tree_gen_node_parser<T> > >{ typedef no_tree_gen_node_parser<T> self_t; typedef no_tree_gen_node_parser_gen parser_generator_t; typedef unary_parser_category parser_category_t;// typedef no_tree_gen_node_parser<T> const &embed_t; no_tree_gen_node_parser(T const& a) : unary<T, parser<no_tree_gen_node_parser<T> > >(a) {} template <typename ScannerT> typename parser_result<self_t, ScannerT>::type parse(ScannerT const& scanner) const { typedef typename ScannerT::iteration_policy_t iteration_policy_t; typedef match_policy match_policy_t; typedef typename ScannerT::action_policy_t action_policy_t; typedef scanner_policies< iteration_policy_t, match_policy_t, action_policy_t > policies_t; return this->subject().parse(scanner.change_policies(policies_t(scanner))); }};//////////////////////////////////struct no_tree_gen_node_parser_gen{ template <typename T> struct result { typedef no_tree_gen_node_parser<T> type; }; template <typename T> static no_tree_gen_node_parser<T> generate(parser<T> const& s) { return no_tree_gen_node_parser<T>(s.derived()); } template <typename T> no_tree_gen_node_parser<T> operator[](parser<T> const& s) const { return no_tree_gen_node_parser<T>(s.derived()); }};//////////////////////////////////const no_tree_gen_node_parser_gen no_node_d = no_tree_gen_node_parser_gen();//////////////////////////////////namespace impl {template <typename MatchPolicyT>struct tree_policy_selector{ typedef tree_policy type;};} // namespace impl//////////////////////////////////template <typename NodeParserT>struct node_parser_gen;template <typename T, typename NodeParserT>struct node_parser: public unary<T, parser<node_parser<T, NodeParserT> > >{ typedef node_parser<T, NodeParserT> self_t; typedef node_parser_gen<NodeParserT> parser_generator_t; typedef unary_parser_category parser_category_t;// typedef node_parser<T, NodeParserT> const &embed_t; node_parser(T const& a) : unary<T, parser<node_parser<T, NodeParserT> > >(a) {} template <typename ScannerT> typename parser_result<self_t, ScannerT>::type parse(ScannerT const& scanner) const { typename parser_result<self_t, ScannerT>::type hit = this->subject().parse(scanner); if (hit) { impl::tree_policy_selector<typename ScannerT::match_policy_t>::type::apply_op_to_match(NodeParserT(), hit); } return hit; }};//////////////////////////////////template <typename NodeParserT>struct node_parser_gen{ template <typename T> struct result { typedef node_parser<T, NodeParserT> type; }; template <typename T> static node_parser<T, NodeParserT> generate(parser<T> const& s) { return node_parser<T, NodeParserT>(s.derived()); } template <typename T> node_parser<T, NodeParserT> operator[](parser<T> const& s) const { return node_parser<T, NodeParserT>(s.derived()); }};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?