ast.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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_AST_HPP
  9. #define BOOST_SPIRIT_TREE_AST_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/ast_fwd.hpp>
  14. ///////////////////////////////////////////////////////////////////////////////
  15. namespace boost { namespace spirit {
  16. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  17. //////////////////////////////////
  18. // ast_match_policy is simply an id so the correct specialization of
  19. // tree_policy can be found.
  20. template <
  21. typename IteratorT,
  22. typename NodeFactoryT,
  23. typename T
  24. >
  25. struct ast_match_policy :
  26. public common_tree_match_policy<
  27. ast_match_policy<IteratorT, NodeFactoryT, T>,
  28. IteratorT,
  29. NodeFactoryT,
  30. ast_tree_policy<
  31. ast_match_policy<IteratorT, NodeFactoryT, T>,
  32. NodeFactoryT,
  33. T
  34. >,
  35. T
  36. >
  37. {
  38. typedef
  39. common_tree_match_policy<
  40. ast_match_policy<IteratorT, NodeFactoryT, T>,
  41. IteratorT,
  42. NodeFactoryT,
  43. ast_tree_policy<
  44. ast_match_policy<IteratorT, NodeFactoryT, T>,
  45. NodeFactoryT,
  46. T
  47. >,
  48. T
  49. >
  50. common_tree_match_policy_;
  51. ast_match_policy()
  52. {
  53. }
  54. template <typename PolicyT>
  55. ast_match_policy(PolicyT const & policies)
  56. : common_tree_match_policy_(policies)
  57. {
  58. }
  59. };
  60. //////////////////////////////////
  61. template <typename MatchPolicyT, typename NodeFactoryT, typename T>
  62. struct ast_tree_policy :
  63. public common_tree_tree_policy<MatchPolicyT, NodeFactoryT>
  64. {
  65. typedef typename MatchPolicyT::match_t match_t;
  66. typedef typename MatchPolicyT::iterator_t iterator_t;
  67. template<typename MatchAT, typename MatchBT>
  68. static void concat(MatchAT& a, MatchBT const& b)
  69. {
  70. BOOST_SPIRIT_ASSERT(a && b);
  71. #if defined(BOOST_SPIRIT_DEBUG) && \
  72. (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
  73. BOOST_SPIRIT_DEBUG_OUT << "\n>>>AST concat. a = " << a <<
  74. "\n\tb = " << b << "<<<\n";
  75. #endif
  76. typedef typename tree_match<iterator_t, NodeFactoryT, T>::container_t
  77. container_t;
  78. // test for size() is nessecary, because no_tree_gen_node leaves a.trees
  79. // and/or b.trees empty
  80. if (0 != b.trees.size() && b.trees.begin()->value.is_root())
  81. {
  82. BOOST_SPIRIT_ASSERT(b.trees.size() == 1);
  83. container_t tmp;
  84. std::swap(a.trees, tmp); // save a into tmp
  85. std::swap(b.trees, a.trees); // make b.trees[0] be new root (a.trees[0])
  86. container_t* pnon_root_trees = &a.trees;
  87. while (pnon_root_trees->size() > 0 &&
  88. pnon_root_trees->begin()->value.is_root())
  89. {
  90. pnon_root_trees = & pnon_root_trees->begin()->children;
  91. }
  92. pnon_root_trees->insert(pnon_root_trees->begin(),
  93. tmp.begin(), tmp.end());
  94. }
  95. else if (0 != a.trees.size() && a.trees.begin()->value.is_root())
  96. {
  97. BOOST_SPIRIT_ASSERT(a.trees.size() == 1);
  98. #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
  99. a.trees.begin()->children.reserve(a.trees.begin()->children.size() + b.trees.size());
  100. #endif
  101. std::copy(b.trees.begin(),
  102. b.trees.end(),
  103. std::back_insert_iterator<container_t>(
  104. a.trees.begin()->children));
  105. }
  106. else
  107. {
  108. #if !defined(BOOST_SPIRIT_USE_LIST_FOR_TREES)
  109. a.trees.reserve(a.trees.size() + b.trees.size());
  110. #endif
  111. std::copy(b.trees.begin(),
  112. b.trees.end(),
  113. std::back_insert_iterator<container_t>(a.trees));
  114. }
  115. #if defined(BOOST_SPIRIT_DEBUG) && \
  116. (BOOST_SPIRIT_DEBUG_FLAGS & BOOST_SPIRIT_DEBUG_FLAGS_NODES)
  117. BOOST_SPIRIT_DEBUG_OUT << ">>>after AST concat. a = " << a << "<<<\n\n";
  118. #endif
  119. return;
  120. }
  121. template <typename MatchT, typename Iterator1T, typename Iterator2T>
  122. static void group_match(MatchT& m, parser_id const& id,
  123. Iterator1T const& first, Iterator2T const& last)
  124. {
  125. if (!m)
  126. return;
  127. typedef typename tree_match<iterator_t, NodeFactoryT, T>::container_t
  128. container_t;
  129. typedef typename container_t::iterator cont_iterator_t;
  130. typedef typename NodeFactoryT::template factory<iterator_t> factory_t;
  131. if (m.trees.size() == 1
  132. #ifdef BOOST_SPIRIT_NO_TREE_NODE_COLLAPSING
  133. && !(id.to_long() && m.trees.begin()->value.id().to_long())
  134. #endif
  135. )
  136. {
  137. // set rule_id's. There may have been multiple nodes created.
  138. // Because of root_node[] they may be left-most children of the top
  139. // node.
  140. container_t* punset_id = &m.trees;
  141. while (punset_id->size() > 0 &&
  142. punset_id->begin()->value.id() == 0)
  143. {
  144. punset_id->begin()->value.id(id);
  145. punset_id = &punset_id->begin()->children;
  146. }
  147. m.trees.begin()->value.is_root(false);
  148. }
  149. else
  150. {
  151. match_t newmatch(m.length(),
  152. m.trees.empty() ?
  153. factory_t::empty_node() :
  154. factory_t::create_node(first, last, false));
  155. std::swap(newmatch.trees.begin()->children, m.trees);
  156. // set this node and all it's unset children's rule_id
  157. newmatch.trees.begin()->value.id(id);
  158. for (cont_iterator_t i = newmatch.trees.begin();
  159. i != newmatch.trees.end();
  160. ++i)
  161. {
  162. if (i->value.id() == 0)
  163. i->value.id(id);
  164. }
  165. m = newmatch;
  166. }
  167. }
  168. template <typename FunctorT, typename MatchT>
  169. static void apply_op_to_match(FunctorT const& op, MatchT& m)
  170. {
  171. op(m);
  172. }
  173. };
  174. namespace impl {
  175. template <typename IteratorT, typename NodeFactoryT, typename T>
  176. struct tree_policy_selector<ast_match_policy<IteratorT, NodeFactoryT, T> >
  177. {
  178. typedef ast_tree_policy<
  179. ast_match_policy<IteratorT, NodeFactoryT, T>,
  180. NodeFactoryT,
  181. T
  182. > type;
  183. };
  184. } // namespace impl
  185. //////////////////////////////////
  186. struct gen_ast_node_parser_gen;
  187. template <typename T>
  188. struct gen_ast_node_parser
  189. : public unary<T, parser<gen_ast_node_parser<T> > >
  190. {
  191. typedef gen_ast_node_parser<T> self_t;
  192. typedef gen_ast_node_parser_gen parser_generator_t;
  193. typedef unary_parser_category parser_category_t;
  194. gen_ast_node_parser(T const& a)
  195. : unary<T, parser<gen_ast_node_parser<T> > >(a) {}
  196. template <typename ScannerT>
  197. typename parser_result<self_t, ScannerT>::type
  198. parse(ScannerT const& scan) const
  199. {
  200. typedef typename ScannerT::iteration_policy_t iteration_policy_t;
  201. typedef typename ScannerT::match_policy_t::iterator_t iterator_t;
  202. typedef typename ScannerT::match_policy_t::factory_t factory_t;
  203. typedef ast_match_policy<iterator_t, factory_t> match_policy_t;
  204. typedef typename ScannerT::action_policy_t action_policy_t;
  205. typedef scanner_policies<
  206. iteration_policy_t,
  207. match_policy_t,
  208. action_policy_t
  209. > policies_t;
  210. return this->subject().parse(scan.change_policies(policies_t(scan)));
  211. }
  212. };
  213. //////////////////////////////////
  214. struct gen_ast_node_parser_gen
  215. {
  216. template <typename T>
  217. struct result {
  218. typedef gen_ast_node_parser<T> type;
  219. };
  220. template <typename T>
  221. static gen_ast_node_parser<T>
  222. generate(parser<T> const& s)
  223. {
  224. return gen_ast_node_parser<T>(s.derived());
  225. }
  226. template <typename T>
  227. gen_ast_node_parser<T>
  228. operator[](parser<T> const& s) const
  229. {
  230. return gen_ast_node_parser<T>(s.derived());
  231. }
  232. };
  233. //////////////////////////////////
  234. const gen_ast_node_parser_gen gen_ast_node_d = gen_ast_node_parser_gen();
  235. //////////////////////////////////
  236. struct root_node_op
  237. {
  238. template <typename MatchT>
  239. void operator()(MatchT& m) const
  240. {
  241. BOOST_SPIRIT_ASSERT(m.trees.size() > 0);
  242. m.trees.begin()->value.is_root(true);
  243. }
  244. };
  245. const node_parser_gen<root_node_op> root_node_d =
  246. node_parser_gen<root_node_op>();
  247. ///////////////////////////////////////////////////////////////////////////////
  248. //
  249. // Parse functions for ASTs
  250. //
  251. ///////////////////////////////////////////////////////////////////////////////
  252. template <
  253. typename AstFactoryT, typename IteratorT, typename ParserT,
  254. typename SkipT
  255. >
  256. inline tree_parse_info<IteratorT, AstFactoryT>
  257. ast_parse(
  258. IteratorT const& first_,
  259. IteratorT const& last_,
  260. parser<ParserT> const& parser,
  261. SkipT const& skip_,
  262. AstFactoryT const & /*dummy_*/ = AstFactoryT())
  263. {
  264. typedef skip_parser_iteration_policy<SkipT> it_policy_t;
  265. typedef ast_match_policy<IteratorT, AstFactoryT> ast_match_policy_t;
  266. typedef
  267. scanner_policies<it_policy_t, ast_match_policy_t>
  268. scan_policies_t;
  269. typedef scanner<IteratorT, scan_policies_t> scanner_t;
  270. it_policy_t iter_policy(skip_);
  271. scan_policies_t policies(iter_policy);
  272. IteratorT first = first_;
  273. scanner_t scan(first, last_, policies);
  274. tree_match<IteratorT, AstFactoryT> hit = parser.derived().parse(scan);
  275. return tree_parse_info<IteratorT, AstFactoryT>(
  276. first, hit, hit && (first == last_), hit.length(), hit.trees);
  277. }
  278. //////////////////////////////////
  279. template <typename IteratorT, typename ParserT, typename SkipT>
  280. inline tree_parse_info<IteratorT>
  281. ast_parse(
  282. IteratorT const& first_,
  283. IteratorT const& last_,
  284. parser<ParserT> const& parser,
  285. SkipT const& skip_)
  286. {
  287. typedef node_val_data_factory<nil_t> default_factory_t;
  288. return ast_parse(first_, last_, parser, skip_, default_factory_t());
  289. }
  290. //////////////////////////////////
  291. template <typename IteratorT, typename ParserT>
  292. inline tree_parse_info<IteratorT>
  293. ast_parse(
  294. IteratorT const& first_,
  295. IteratorT const& last,
  296. parser<ParserT> const& parser)
  297. {
  298. typedef ast_match_policy<IteratorT> ast_match_policy_t;
  299. IteratorT first = first_;
  300. scanner<
  301. IteratorT,
  302. scanner_policies<iteration_policy, ast_match_policy_t>
  303. > scan(first, last);
  304. tree_match<IteratorT> hit = parser.derived().parse(scan);
  305. return tree_parse_info<IteratorT>(
  306. first, hit, hit && (first == last), hit.length(), hit.trees);
  307. }
  308. //////////////////////////////////
  309. template <typename CharT, typename ParserT, typename SkipT>
  310. inline tree_parse_info<CharT const*>
  311. ast_parse(
  312. CharT const* str,
  313. parser<ParserT> const& parser,
  314. SkipT const& skip)
  315. {
  316. CharT const* last = str;
  317. while (*last)
  318. last++;
  319. return ast_parse(str, last, parser, skip);
  320. }
  321. //////////////////////////////////
  322. template <typename CharT, typename ParserT>
  323. inline tree_parse_info<CharT const*>
  324. ast_parse(
  325. CharT const* str,
  326. parser<ParserT> const& parser)
  327. {
  328. CharT const* last = str;
  329. while (*last)
  330. {
  331. last++;
  332. }
  333. return ast_parse(str, last, parser);
  334. }
  335. ///////////////////////////////////////////////////////////////////////////////
  336. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  337. }} // namespace BOOST_SPIRIT_CLASSIC_NS
  338. #endif