refactoring.ipp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*=============================================================================
  2. Copyright (c) 2002-2003 Hartmut Kaiser
  3. http://spirit.sourceforge.net/
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #ifndef BOOST_SPIRIT_REFACTORING_IPP
  9. #define BOOST_SPIRIT_REFACTORING_IPP
  10. ///////////////////////////////////////////////////////////////////////////////
  11. namespace boost { namespace spirit {
  12. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  13. ///////////////////////////////////////////////////////////////////////////////
  14. //
  15. // The struct 'self_nested_refactoring' is used to indicate, that the
  16. // refactoring algorithm should be 'self-nested'.
  17. //
  18. // The struct 'non_nested_refactoring' is used to indicate, that no nesting
  19. // of refactoring algorithms is reqired.
  20. //
  21. ///////////////////////////////////////////////////////////////////////////////
  22. struct non_nested_refactoring { typedef non_nested_refactoring embed_t; };
  23. struct self_nested_refactoring { typedef self_nested_refactoring embed_t; };
  24. ///////////////////////////////////////////////////////////////////////////////
  25. namespace impl {
  26. ///////////////////////////////////////////////////////////////////////////////
  27. //
  28. // Helper templates for refactoring parsers
  29. //
  30. ///////////////////////////////////////////////////////////////////////////////
  31. ///////////////////////////////////////////////////////////////////////////
  32. //
  33. // refactor the left unary operand of a binary parser
  34. //
  35. // The refactoring should be done only if the left operand is an
  36. // unary_parser_category parser.
  37. //
  38. ///////////////////////////////////////////////////////////////////////////
  39. ///////////////////////////////////////////////////////////////////////////
  40. template <typename CategoryT>
  41. struct refactor_unary_nested {
  42. template <
  43. typename ParserT, typename NestedT,
  44. typename ScannerT, typename BinaryT
  45. >
  46. static typename parser_result<ParserT, ScannerT>::type
  47. parse(ParserT const &, ScannerT const& scan, BinaryT const& binary,
  48. NestedT const& /*nested_d*/)
  49. {
  50. return binary.parse(scan);
  51. }
  52. };
  53. template <>
  54. struct refactor_unary_nested<unary_parser_category> {
  55. template <
  56. typename ParserT, typename ScannerT, typename BinaryT,
  57. typename NestedT
  58. >
  59. static typename parser_result<ParserT, ScannerT>::type
  60. parse(ParserT const &, ScannerT const& scan, BinaryT const& binary,
  61. NestedT const& nested_d)
  62. {
  63. typedef typename BinaryT::parser_generator_t op_t;
  64. typedef
  65. typename BinaryT::left_t::parser_generator_t
  66. unary_t;
  67. return
  68. unary_t::generate(
  69. nested_d[
  70. op_t::generate(binary.left().subject(), binary.right())
  71. ]
  72. ).parse(scan);
  73. }
  74. };
  75. ///////////////////////////////////////////////////////////////////////////
  76. template <typename CategoryT>
  77. struct refactor_unary_non_nested {
  78. template <typename ParserT, typename ScannerT, typename BinaryT>
  79. static typename parser_result<ParserT, ScannerT>::type
  80. parse(ParserT const &, ScannerT const& scan, BinaryT const& binary)
  81. {
  82. return binary.parse(scan);
  83. }
  84. };
  85. template <>
  86. struct refactor_unary_non_nested<unary_parser_category> {
  87. template <typename ParserT, typename ScannerT, typename BinaryT>
  88. static typename parser_result<ParserT, ScannerT>::type
  89. parse(ParserT const &, ScannerT const& scan, BinaryT const& binary)
  90. {
  91. typedef typename BinaryT::parser_generator_t op_t;
  92. typedef
  93. typename BinaryT::left_t::parser_generator_t
  94. unary_t;
  95. return unary_t::generate(
  96. op_t::generate(binary.left().subject(), binary.right())
  97. ).parse(scan);
  98. }
  99. };
  100. ///////////////////////////////////////////////////////////////////////////
  101. template <typename NestedT>
  102. struct refactor_unary_type {
  103. template <typename ParserT, typename ScannerT, typename BinaryT>
  104. static typename parser_result<ParserT, ScannerT>::type
  105. parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
  106. NestedT const& nested_d)
  107. {
  108. typedef
  109. typename BinaryT::left_t::parser_category_t
  110. parser_category_t;
  111. return refactor_unary_nested<parser_category_t>::
  112. parse(p, scan, binary, nested_d);
  113. }
  114. };
  115. template <>
  116. struct refactor_unary_type<non_nested_refactoring> {
  117. template <typename ParserT, typename ScannerT, typename BinaryT>
  118. static typename parser_result<ParserT, ScannerT>::type
  119. parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
  120. non_nested_refactoring const&)
  121. {
  122. typedef
  123. typename BinaryT::left_t::parser_category_t
  124. parser_category_t;
  125. return refactor_unary_non_nested<parser_category_t>::
  126. parse(p, scan, binary);
  127. }
  128. };
  129. template <>
  130. struct refactor_unary_type<self_nested_refactoring> {
  131. template <typename ParserT, typename ScannerT, typename BinaryT>
  132. static typename parser_result<ParserT, ScannerT>::type
  133. parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
  134. self_nested_refactoring const &nested_tag)
  135. {
  136. typedef
  137. typename BinaryT::left_t::parser_category_t
  138. parser_category_t;
  139. typedef typename ParserT::parser_generator_t parser_generator_t;
  140. parser_generator_t nested_d(nested_tag);
  141. return refactor_unary_nested<parser_category_t>::
  142. parse(p, scan, binary, nested_d);
  143. }
  144. };
  145. ///////////////////////////////////////////////////////////////////////////
  146. //
  147. // refactor the action on the left operand of a binary parser
  148. //
  149. // The refactoring should be done only if the left operand is an
  150. // action_parser_category parser.
  151. //
  152. ///////////////////////////////////////////////////////////////////////////
  153. ///////////////////////////////////////////////////////////////////////////
  154. template <typename CategoryT>
  155. struct refactor_action_nested {
  156. template <
  157. typename ParserT, typename ScannerT, typename BinaryT,
  158. typename NestedT
  159. >
  160. static typename parser_result<ParserT, ScannerT>::type
  161. parse(ParserT const &, ScannerT const& scan, BinaryT const& binary,
  162. NestedT const& nested_d)
  163. {
  164. return nested_d[binary].parse(scan);
  165. }
  166. };
  167. template <>
  168. struct refactor_action_nested<action_parser_category> {
  169. template <
  170. typename ParserT, typename ScannerT, typename BinaryT,
  171. typename NestedT
  172. >
  173. static typename parser_result<ParserT, ScannerT>::type
  174. parse(ParserT const &, ScannerT const& scan, BinaryT const& binary,
  175. NestedT const& nested_d)
  176. {
  177. typedef typename BinaryT::parser_generator_t binary_gen_t;
  178. return (
  179. nested_d[
  180. binary_gen_t::generate(
  181. binary.left().subject(),
  182. binary.right()
  183. )
  184. ][binary.left().predicate()]
  185. ).parse(scan);
  186. }
  187. };
  188. ///////////////////////////////////////////////////////////////////////////
  189. template <typename CategoryT>
  190. struct refactor_action_non_nested {
  191. template <typename ParserT, typename ScannerT, typename BinaryT>
  192. static typename parser_result<ParserT, ScannerT>::type
  193. parse(ParserT const &, ScannerT const& scan, BinaryT const& binary)
  194. {
  195. return binary.parse(scan);
  196. }
  197. };
  198. template <>
  199. struct refactor_action_non_nested<action_parser_category> {
  200. template <typename ParserT, typename ScannerT, typename BinaryT>
  201. static typename parser_result<ParserT, ScannerT>::type
  202. parse(ParserT const &, ScannerT const& scan, BinaryT const& binary)
  203. {
  204. typedef typename BinaryT::parser_generator_t binary_gen_t;
  205. return (
  206. binary_gen_t::generate(
  207. binary.left().subject(),
  208. binary.right()
  209. )[binary.left().predicate()]
  210. ).parse(scan);
  211. }
  212. };
  213. ///////////////////////////////////////////////////////////////////////////
  214. template <typename NestedT>
  215. struct refactor_action_type {
  216. template <typename ParserT, typename ScannerT, typename BinaryT>
  217. static typename parser_result<ParserT, ScannerT>::type
  218. parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
  219. NestedT const& nested_d)
  220. {
  221. typedef
  222. typename BinaryT::left_t::parser_category_t
  223. parser_category_t;
  224. return refactor_action_nested<parser_category_t>::
  225. parse(p, scan, binary, nested_d);
  226. }
  227. };
  228. template <>
  229. struct refactor_action_type<non_nested_refactoring> {
  230. template <typename ParserT, typename ScannerT, typename BinaryT>
  231. static typename parser_result<ParserT, ScannerT>::type
  232. parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
  233. non_nested_refactoring const&)
  234. {
  235. typedef
  236. typename BinaryT::left_t::parser_category_t
  237. parser_category_t;
  238. return refactor_action_non_nested<parser_category_t>::
  239. parse(p, scan, binary);
  240. }
  241. };
  242. template <>
  243. struct refactor_action_type<self_nested_refactoring> {
  244. template <typename ParserT, typename ScannerT, typename BinaryT>
  245. static typename parser_result<ParserT, ScannerT>::type
  246. parse(ParserT const &p, ScannerT const& scan, BinaryT const& binary,
  247. self_nested_refactoring const &nested_tag)
  248. {
  249. typedef typename ParserT::parser_generator_t parser_generator_t;
  250. typedef
  251. typename BinaryT::left_t::parser_category_t
  252. parser_category_t;
  253. parser_generator_t nested_d(nested_tag);
  254. return refactor_action_nested<parser_category_t>::
  255. parse(p, scan, binary, nested_d);
  256. }
  257. };
  258. ///////////////////////////////////////////////////////////////////////////
  259. //
  260. // refactor the action attached to a binary parser
  261. //
  262. // The refactoring should be done only if the given parser is an
  263. // binary_parser_category parser.
  264. //
  265. ///////////////////////////////////////////////////////////////////////////
  266. ///////////////////////////////////////////////////////////////////////////
  267. template <typename CategoryT>
  268. struct attach_action_nested {
  269. template <
  270. typename ParserT, typename ScannerT, typename ActionT,
  271. typename NestedT
  272. >
  273. static typename parser_result<ParserT, ScannerT>::type
  274. parse(ParserT const &, ScannerT const& scan, ActionT const &action,
  275. NestedT const& /*nested_d*/)
  276. {
  277. return action.parse(scan);
  278. }
  279. };
  280. template <>
  281. struct attach_action_nested<binary_parser_category> {
  282. template <
  283. typename ParserT, typename ScannerT, typename ActionT,
  284. typename NestedT
  285. >
  286. static typename parser_result<ParserT, ScannerT>::type
  287. parse(ParserT const &, ScannerT const& scan, ActionT const &action,
  288. NestedT const& nested_d)
  289. {
  290. typedef
  291. typename ActionT::subject_t::parser_generator_t
  292. binary_gen_t;
  293. return (
  294. binary_gen_t::generate(
  295. nested_d[action.subject().left()[action.predicate()]],
  296. nested_d[action.subject().right()[action.predicate()]]
  297. )
  298. ).parse(scan);
  299. }
  300. };
  301. ///////////////////////////////////////////////////////////////////////////
  302. template <typename CategoryT>
  303. struct attach_action_non_nested {
  304. template <typename ParserT, typename ScannerT, typename ActionT>
  305. static typename parser_result<ParserT, ScannerT>::type
  306. parse(ParserT const &, ScannerT const& scan, ActionT const &action)
  307. {
  308. return action.parse(scan);
  309. }
  310. };
  311. template <>
  312. struct attach_action_non_nested<binary_parser_category> {
  313. template <typename ParserT, typename ScannerT, typename ActionT>
  314. static typename parser_result<ParserT, ScannerT>::type
  315. parse(ParserT const &, ScannerT const& scan, ActionT const &action)
  316. {
  317. typedef
  318. typename ActionT::subject_t::parser_generator_t
  319. binary_gen_t;
  320. return (
  321. binary_gen_t::generate(
  322. action.subject().left()[action.predicate()],
  323. action.subject().right()[action.predicate()]
  324. )
  325. ).parse(scan);
  326. }
  327. };
  328. ///////////////////////////////////////////////////////////////////////////
  329. template <typename NestedT>
  330. struct attach_action_type {
  331. template <typename ParserT, typename ScannerT, typename ActionT>
  332. static typename parser_result<ParserT, ScannerT>::type
  333. parse(ParserT const &p, ScannerT const& scan, ActionT const& action,
  334. NestedT const& nested_d)
  335. {
  336. typedef
  337. typename ActionT::subject_t::parser_category_t
  338. parser_category_t;
  339. return attach_action_nested<parser_category_t>::
  340. parse(p, scan, action, nested_d);
  341. }
  342. };
  343. template <>
  344. struct attach_action_type<non_nested_refactoring> {
  345. template <typename ParserT, typename ScannerT, typename ActionT>
  346. static typename parser_result<ParserT, ScannerT>::type
  347. parse(ParserT const &p, ScannerT const& scan, ActionT const &action,
  348. non_nested_refactoring const&)
  349. {
  350. typedef
  351. typename ActionT::subject_t::parser_category_t
  352. parser_category_t;
  353. return attach_action_non_nested<parser_category_t>::
  354. parse(p, scan, action);
  355. }
  356. };
  357. template <>
  358. struct attach_action_type<self_nested_refactoring> {
  359. template <typename ParserT, typename ScannerT, typename ActionT>
  360. static typename parser_result<ParserT, ScannerT>::type
  361. parse(ParserT const &p, ScannerT const& scan, ActionT const &action,
  362. self_nested_refactoring const& nested_tag)
  363. {
  364. typedef typename ParserT::parser_generator_t parser_generator_t;
  365. typedef
  366. typename ActionT::subject_t::parser_category_t
  367. parser_category_t;
  368. parser_generator_t nested_d(nested_tag);
  369. return attach_action_nested<parser_category_t>::
  370. parse(p, scan, action, nested_d);
  371. }
  372. };
  373. } // namespace impl
  374. ///////////////////////////////////////////////////////////////////////////////
  375. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  376. }} // namespace boost::spirit
  377. #endif