parse_tree.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*=============================================================================
  2. Copyright (c) 2001-2003 Daniel Nuffer
  3. Copyright (c) 2001-2007 Hartmut Kaiser
  4. http://spirit.sourceforge.net/
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #ifndef BOOST_SPIRIT_TREE_PARSE_TREE_HPP
  9. #define BOOST_SPIRIT_TREE_PARSE_TREE_HPP
  10. #include <boost/spirit/home/classic/namespace.hpp>
  11. #include <boost/spirit/home/classic/tree/common.hpp>
  12. #include <boost/spirit/home/classic/core/scanner/scanner.hpp>
  13. #include <boost/spirit/home/classic/tree/parse_tree_fwd.hpp>
  14. ///////////////////////////////////////////////////////////////////////////////
  15. namespace boost { namespace spirit {
  16. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  17. //////////////////////////////////
  18. // pt_match_policy is simply an id so the correct specialization of tree_policy can be found.
  19. template <
  20. typename IteratorT,
  21. typename NodeFactoryT,
  22. typename T
  23. >
  24. struct pt_match_policy :
  25. public common_tree_match_policy<
  26. pt_match_policy<IteratorT, NodeFactoryT, T>,
  27. IteratorT,
  28. NodeFactoryT,
  29. pt_tree_policy<
  30. pt_match_policy<IteratorT, NodeFactoryT, T>,
  31. NodeFactoryT,
  32. T
  33. >,
  34. T
  35. >
  36. {
  37. typedef
  38. common_tree_match_policy<
  39. pt_match_policy<IteratorT, NodeFactoryT, T>,
  40. IteratorT,
  41. NodeFactoryT,
  42. pt_tree_policy<
  43. pt_match_policy<IteratorT, NodeFactoryT, T>,
  44. NodeFactoryT,
  45. T
  46. >,
  47. T
  48. >
  49. common_tree_match_policy_;
  50. pt_match_policy()
  51. {
  52. }
  53. template <typename PolicyT>
  54. pt_match_policy(PolicyT const & policies)
  55. : common_tree_match_policy_(policies)
  56. {
  57. }
  58. };
  59. //////////////////////////////////
  60. template <typename MatchPolicyT, typename NodeFactoryT, typename T>
  61. struct pt_tree_policy :
  62. public common_tree_tree_policy<MatchPolicyT, NodeFactoryT>
  63. {
  64. typedef typename MatchPolicyT::match_t match_t;
  65. typedef typename MatchPolicyT::iterator_t iterator_t;
  66. template<typename MatchAT, typename MatchBT>
  67. static void concat(MatchAT& a, MatchBT const& b)
  68. {
  69. BOOST_SPIRIT_ASSERT(a && b);
  70. std::copy(b.trees.begin(), b.trees.end(),
  71. std::back_insert_iterator<typename match_t::container_t>(a.trees));
  72. }
  73. template <typename MatchT, typename Iterator1T, typename Iterator2T>
  74. static void group_match(MatchT& m, parser_id const& id,
  75. Iterator1T const& first, Iterator2T const& last)
  76. {
  77. if (!m)
  78. return;
  79. typedef typename NodeFactoryT::template factory<iterator_t> factory_t;
  80. typedef typename tree_match<iterator_t, NodeFactoryT, T>::container_t
  81. container_t;
  82. typedef typename container_t::iterator cont_iterator_t;
  83. match_t newmatch(m.length(),
  84. factory_t::create_node(first, last, false));
  85. std::swap(newmatch.trees.begin()->children, m.trees);
  86. // set this node and all it's unset children's rule_id
  87. newmatch.trees.begin()->value.id(id);
  88. for (cont_iterator_t i = newmatch.trees.begin()->children.begin();
  89. i != newmatch.trees.begin()->children.end();
  90. ++i)
  91. {
  92. if (i->value.id() == 0)
  93. i->value.id(id);
  94. }
  95. m = newmatch;
  96. }
  97. template <typename FunctorT, typename MatchT>
  98. static void apply_op_to_match(FunctorT const& op, MatchT& m)
  99. {
  100. op(m);
  101. }
  102. };
  103. namespace impl {
  104. template <typename IteratorT, typename NodeFactoryT, typename T>
  105. struct tree_policy_selector<pt_match_policy<IteratorT, NodeFactoryT, T> >
  106. {
  107. typedef pt_tree_policy<
  108. pt_match_policy<IteratorT, NodeFactoryT, T>,
  109. NodeFactoryT,
  110. T
  111. > type;
  112. };
  113. } // namespace impl
  114. //////////////////////////////////
  115. struct gen_pt_node_parser_gen;
  116. template <typename T>
  117. struct gen_pt_node_parser
  118. : public unary<T, parser<gen_pt_node_parser<T> > >
  119. {
  120. typedef gen_pt_node_parser<T> self_t;
  121. typedef gen_pt_node_parser_gen parser_generator_t;
  122. typedef unary_parser_category parser_category_t;
  123. gen_pt_node_parser(T const& a)
  124. : unary<T, parser<gen_pt_node_parser<T> > >(a) {}
  125. template <typename ScannerT>
  126. typename parser_result<self_t, ScannerT>::type
  127. parse(ScannerT const& scan) const
  128. {
  129. typedef typename ScannerT::iteration_policy_t iteration_policy_t;
  130. typedef typename ScannerT::match_policy_t::iterator_t iterator_t;
  131. typedef typename ScannerT::match_policy_t::factory_t factory_t;
  132. typedef pt_match_policy<iterator_t, factory_t> match_policy_t;
  133. typedef typename ScannerT::action_policy_t action_policy_t;
  134. typedef scanner_policies<
  135. iteration_policy_t,
  136. match_policy_t,
  137. action_policy_t
  138. > policies_t;
  139. return this->subject().parse(scan.change_policies(policies_t(scan)));
  140. }
  141. };
  142. //////////////////////////////////
  143. struct gen_pt_node_parser_gen
  144. {
  145. template <typename T>
  146. struct result {
  147. typedef gen_pt_node_parser<T> type;
  148. };
  149. template <typename T>
  150. static gen_pt_node_parser<T>
  151. generate(parser<T> const& s)
  152. {
  153. return gen_pt_node_parser<T>(s.derived());
  154. }
  155. template <typename T>
  156. gen_pt_node_parser<T>
  157. operator[](parser<T> const& s) const
  158. {
  159. return gen_pt_node_parser<T>(s.derived());
  160. }
  161. };
  162. //////////////////////////////////
  163. const gen_pt_node_parser_gen gen_pt_node_d = gen_pt_node_parser_gen();
  164. ///////////////////////////////////////////////////////////////////////////////
  165. //
  166. // Parse functions for parse trees
  167. //
  168. ///////////////////////////////////////////////////////////////////////////////
  169. template <
  170. typename NodeFactoryT, typename IteratorT, typename ParserT,
  171. typename SkipT
  172. >
  173. inline tree_parse_info<IteratorT, NodeFactoryT>
  174. pt_parse(
  175. IteratorT const& first_,
  176. IteratorT const& last,
  177. parser<ParserT> const& p,
  178. SkipT const& skip,
  179. NodeFactoryT const& /*dummy_*/ = NodeFactoryT())
  180. {
  181. typedef skip_parser_iteration_policy<SkipT> it_policy_t;
  182. typedef pt_match_policy<IteratorT, NodeFactoryT> pt_match_policy_t;
  183. typedef
  184. scanner_policies<it_policy_t, pt_match_policy_t>
  185. scan_policies_t;
  186. typedef scanner<IteratorT, scan_policies_t> scanner_t;
  187. it_policy_t iter_policy(skip);
  188. scan_policies_t policies(iter_policy);
  189. IteratorT first = first_;
  190. scanner_t scan(first, last, policies);
  191. tree_match<IteratorT, NodeFactoryT> hit = p.derived().parse(scan);
  192. return tree_parse_info<IteratorT, NodeFactoryT>(
  193. first, hit, hit && (first == last), hit.length(), hit.trees);
  194. }
  195. template <typename IteratorT, typename ParserT, typename SkipT>
  196. inline tree_parse_info<IteratorT>
  197. pt_parse(
  198. IteratorT const& first,
  199. IteratorT const& last,
  200. parser<ParserT> const& p,
  201. SkipT const& skip)
  202. {
  203. typedef node_val_data_factory<nil_t> default_node_factory_t;
  204. return pt_parse(first, last, p, skip, default_node_factory_t());
  205. }
  206. //////////////////////////////////
  207. template <typename IteratorT, typename ParserT>
  208. inline tree_parse_info<IteratorT>
  209. pt_parse(
  210. IteratorT const& first_,
  211. IteratorT const& last,
  212. parser<ParserT> const& parser)
  213. {
  214. typedef pt_match_policy<IteratorT> pt_match_policy_t;
  215. IteratorT first = first_;
  216. scanner<
  217. IteratorT,
  218. scanner_policies<iteration_policy, pt_match_policy_t>
  219. > scan(first, last);
  220. tree_match<IteratorT> hit = parser.derived().parse(scan);
  221. return tree_parse_info<IteratorT>(
  222. first, hit, hit && (first == last), hit.length(), hit.trees);
  223. }
  224. //////////////////////////////////
  225. template <typename CharT, typename ParserT, typename SkipT>
  226. inline tree_parse_info<CharT const*>
  227. pt_parse(
  228. CharT const* str,
  229. parser<ParserT> const& p,
  230. SkipT const& skip)
  231. {
  232. CharT const* last = str;
  233. while (*last)
  234. last++;
  235. return pt_parse(str, last, p, skip);
  236. }
  237. //////////////////////////////////
  238. template <typename CharT, typename ParserT>
  239. inline tree_parse_info<CharT const*>
  240. pt_parse(
  241. CharT const* str,
  242. parser<ParserT> const& parser)
  243. {
  244. CharT const* last = str;
  245. while (*last)
  246. {
  247. last++;
  248. }
  249. return pt_parse(str, last, parser);
  250. }
  251. ///////////////////////////////////////////////////////////////////////////////
  252. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  253. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  254. #endif