/Src/Dependencies/Boost/boost/spirit/home/classic/core/composite/impl/directives.ipp

http://hadesmem.googlecode.com/ · C++ Header · 374 lines · 299 code · 53 blank · 22 comment · 1 complexity · c6a03423d07efaae70af6fe8d1052506 MD5 · raw file

  1. /*=============================================================================
  2. Copyright (c) 1998-2003 Joel de Guzman
  3. Copyright (c) 2001 Daniel Nuffer
  4. Copyright (c) 2001 Bruce Florman
  5. Copyright (c) 2002 Raghavendra Satish
  6. http://spirit.sourceforge.net/
  7. Use, modification and distribution is subject to the Boost Software
  8. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. http://www.boost.org/LICENSE_1_0.txt)
  10. =============================================================================*/
  11. #if !defined(BOOST_SPIRIT_DIRECTIVES_IPP)
  12. #define BOOST_SPIRIT_DIRECTIVES_IPP
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #include <boost/spirit/home/classic/core/scanner/skipper.hpp>
  15. namespace boost { namespace spirit {
  16. BOOST_SPIRIT_CLASSIC_NAMESPACE_BEGIN
  17. template <typename BaseT>
  18. struct no_skipper_iteration_policy;
  19. template <typename BaseT>
  20. struct inhibit_case_iteration_policy;
  21. template <typename A, typename B>
  22. struct alternative;
  23. template <typename A, typename B>
  24. struct longest_alternative;
  25. template <typename A, typename B>
  26. struct shortest_alternative;
  27. namespace impl
  28. {
  29. template <typename RT, typename ST, typename ScannerT, typename BaseT>
  30. inline RT
  31. contiguous_parser_parse(
  32. ST const& s,
  33. ScannerT const& scan,
  34. skipper_iteration_policy<BaseT> const&)
  35. {
  36. typedef scanner_policies<
  37. no_skipper_iteration_policy<
  38. BOOST_DEDUCED_TYPENAME ScannerT::iteration_policy_t>,
  39. BOOST_DEDUCED_TYPENAME ScannerT::match_policy_t,
  40. BOOST_DEDUCED_TYPENAME ScannerT::action_policy_t
  41. > policies_t;
  42. scan.skip(scan);
  43. RT hit = s.parse(scan.change_policies(policies_t(scan)));
  44. // We will not do a post skip!!!
  45. return hit;
  46. }
  47. template <typename RT, typename ST, typename ScannerT, typename BaseT>
  48. inline RT
  49. contiguous_parser_parse(
  50. ST const& s,
  51. ScannerT const& scan,
  52. no_skipper_iteration_policy<BaseT> const&)
  53. {
  54. return s.parse(scan);
  55. }
  56. template <typename RT, typename ST, typename ScannerT>
  57. inline RT
  58. contiguous_parser_parse(
  59. ST const& s,
  60. ScannerT const& scan,
  61. iteration_policy const&)
  62. {
  63. return s.parse(scan);
  64. }
  65. template <
  66. typename RT,
  67. typename ParserT,
  68. typename ScannerT,
  69. typename BaseT>
  70. inline RT
  71. implicit_lexeme_parse(
  72. ParserT const& p,
  73. ScannerT const& scan,
  74. skipper_iteration_policy<BaseT> const&)
  75. {
  76. typedef scanner_policies<
  77. no_skipper_iteration_policy<
  78. BOOST_DEDUCED_TYPENAME ScannerT::iteration_policy_t>,
  79. BOOST_DEDUCED_TYPENAME ScannerT::match_policy_t,
  80. BOOST_DEDUCED_TYPENAME ScannerT::action_policy_t
  81. > policies_t;
  82. scan.skip(scan);
  83. RT hit = p.parse_main(scan.change_policies(policies_t(scan)));
  84. // We will not do a post skip!!!
  85. return hit;
  86. }
  87. template <
  88. typename RT,
  89. typename ParserT,
  90. typename ScannerT,
  91. typename BaseT>
  92. inline RT
  93. implicit_lexeme_parse(
  94. ParserT const& p,
  95. ScannerT const& scan,
  96. no_skipper_iteration_policy<BaseT> const&)
  97. {
  98. return p.parse_main(scan);
  99. }
  100. template <typename RT, typename ParserT, typename ScannerT>
  101. inline RT
  102. implicit_lexeme_parse(
  103. ParserT const& p,
  104. ScannerT const& scan,
  105. iteration_policy const&)
  106. {
  107. return p.parse_main(scan);
  108. }
  109. template <typename RT, typename ST, typename ScannerT>
  110. inline RT
  111. inhibit_case_parser_parse(
  112. ST const& s,
  113. ScannerT const& scan,
  114. iteration_policy const&)
  115. {
  116. typedef scanner_policies<
  117. inhibit_case_iteration_policy<
  118. BOOST_DEDUCED_TYPENAME ScannerT::iteration_policy_t>,
  119. BOOST_DEDUCED_TYPENAME ScannerT::match_policy_t,
  120. BOOST_DEDUCED_TYPENAME ScannerT::action_policy_t
  121. > policies_t;
  122. return s.parse(scan.change_policies(policies_t(scan)));
  123. }
  124. template <typename RT, typename ST, typename ScannerT, typename BaseT>
  125. inline RT
  126. inhibit_case_parser_parse(
  127. ST const& s,
  128. ScannerT const& scan,
  129. inhibit_case_iteration_policy<BaseT> const&)
  130. {
  131. return s.parse(scan);
  132. }
  133. #if defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  134. ///////////////////////////////////////////////////////////////////////
  135. //
  136. // from spirit 1.1 (copyright (c) 2001 Bruce Florman)
  137. // various workarounds to support longest and shortest directives
  138. //
  139. ///////////////////////////////////////////////////////////////////////
  140. template <typename T>
  141. struct is_alternative
  142. {
  143. // Determine at compile time (without partial specialization)
  144. // whether a given type is an instance of the alternative<A,B>
  145. static T t();
  146. template <typename A, typename B>
  147. static char test_(alternative<A, B> const&); // no implementation
  148. static int test_(...); // no implementation
  149. enum { r = sizeof(char) == sizeof(test_(t())) };
  150. typedef mpl::bool_<r> value;
  151. };
  152. template <typename T> struct select_to_longest;
  153. template <typename T>
  154. struct to_longest_alternative
  155. {
  156. typedef typename select_to_longest<T>::result_t result_t;
  157. typedef typename select_to_longest<T>::plain_t plain_t;
  158. typedef typename select_to_longest<T>::choose_t choose_t;
  159. static result_t convert(T const& a);
  160. };
  161. template <typename T>
  162. struct to_longest_generic
  163. {
  164. typedef T const& result_t;
  165. typedef T plain_t;
  166. typedef mpl::false_ choose_t;
  167. };
  168. template <typename T>
  169. inline T const&
  170. to_longest_convert(T const& a, mpl::false_)
  171. { return a; }
  172. template <typename T>
  173. struct to_longest_recursive
  174. {
  175. typedef typename to_longest_alternative<
  176. typename T::left_t>::plain_t a_t;
  177. typedef typename to_longest_alternative<
  178. typename T::right_t>::plain_t b_t;
  179. typedef longest_alternative<a_t, b_t> result_t;
  180. typedef result_t plain_t;
  181. typedef mpl::true_ choose_t;
  182. };
  183. template <typename A, typename B>
  184. inline typename to_longest_alternative<alternative<A, B> >::result_t
  185. to_longest_convert(alternative<A, B> const& alt, mpl::true_)
  186. {
  187. typedef typename to_longest_alternative<
  188. alternative<A, B> >::result_t result_t;
  189. return result_t(
  190. to_longest_alternative<A>::convert(alt.left()),
  191. to_longest_alternative<B>::convert(alt.right()));
  192. }
  193. template <typename T>
  194. inline typename to_longest_alternative<T>::result_t
  195. to_longest_alternative<T>::convert(T const& a)
  196. {
  197. return to_longest_convert(
  198. a, to_longest_alternative<T>::choose_t());
  199. }
  200. template <typename T>
  201. struct select_to_longest
  202. {
  203. typedef typename mpl::if_<
  204. is_alternative<T> // IF
  205. , to_longest_recursive<T> // THEN
  206. , to_longest_generic<T> // ELSE
  207. >::type type;
  208. typedef typename select_to_longest::type::result_t result_t;
  209. typedef typename select_to_longest::type::plain_t plain_t;
  210. typedef typename select_to_longest::type::choose_t choose_t;
  211. };
  212. template <typename T> struct select_to_shortest;
  213. template <typename T>
  214. struct to_shortest_alternative
  215. {
  216. typedef typename select_to_shortest<T>::result_t result_t;
  217. typedef typename select_to_shortest<T>::plain_t plain_t;
  218. typedef typename select_to_shortest<T>::choose_t choose_t;
  219. static result_t convert(T const& a);
  220. };
  221. template <typename T>
  222. struct to_shortest_generic
  223. {
  224. typedef T const& result_t;
  225. typedef T plain_t;
  226. typedef mpl::false_ choose_t;
  227. };
  228. template <typename T>
  229. inline T const&
  230. to_shortest_convert(T const& a, mpl::false_) { return a; }
  231. template <typename T>
  232. struct to_shortest_recursive
  233. {
  234. typedef typename to_shortest_alternative<
  235. typename T::left_t>::plain_t a_t;
  236. typedef typename to_shortest_alternative<
  237. typename T::right_t>::plain_t b_t;
  238. typedef shortest_alternative<a_t, b_t> result_t;
  239. typedef result_t plain_t;
  240. typedef mpl::true_ choose_t;
  241. };
  242. template <typename A, typename B>
  243. inline typename to_shortest_alternative<alternative<A, B> >::result_t
  244. to_shortest_convert(alternative<A, B> const& alt, mpl::true_)
  245. {
  246. typedef typename to_shortest_alternative<
  247. alternative<A, B> >::result_t result_t;
  248. return result_t(
  249. to_shortest_alternative<A>::convert(alt.left()),
  250. to_shortest_alternative<B>::convert(alt.right()));
  251. }
  252. template <typename T>
  253. inline typename to_shortest_alternative<T>::result_t
  254. to_shortest_alternative<T>::convert(T const& a)
  255. {
  256. return to_shortest_convert(
  257. a, to_shortest_alternative<T>::choose_t());
  258. }
  259. template <typename T>
  260. struct select_to_shortest
  261. {
  262. typedef typename mpl::if_<
  263. is_alternative<T> // IF
  264. , to_shortest_recursive<T> // THEN
  265. , to_shortest_generic<T> // ELSE
  266. >::type type;
  267. typedef typename select_to_shortest::type::result_t result_t;
  268. typedef typename select_to_shortest::type::plain_t plain_t;
  269. typedef typename select_to_shortest::type::choose_t choose_t;
  270. };
  271. #else
  272. template <typename T>
  273. struct to_longest_alternative
  274. {
  275. typedef T result_t;
  276. static result_t const&
  277. convert(T const& a) // Special (end) case
  278. { return a; }
  279. };
  280. template <typename A, typename B>
  281. struct to_longest_alternative<alternative<A, B> >
  282. {
  283. typedef typename to_longest_alternative<A>::result_t a_t;
  284. typedef typename to_longest_alternative<B>::result_t b_t;
  285. typedef longest_alternative<a_t, b_t> result_t;
  286. static result_t
  287. convert(alternative<A, B> const& alt) // Recursive case
  288. {
  289. return result_t(
  290. to_longest_alternative<A>::convert(alt.left()),
  291. to_longest_alternative<B>::convert(alt.right()));
  292. }
  293. };
  294. template <typename T>
  295. struct to_shortest_alternative
  296. {
  297. typedef T result_t;
  298. static result_t const&
  299. convert(T const& a) // Special (end) case
  300. { return a; }
  301. };
  302. template <typename A, typename B>
  303. struct to_shortest_alternative<alternative<A, B> >
  304. {
  305. typedef typename to_shortest_alternative<A>::result_t a_t;
  306. typedef typename to_shortest_alternative<B>::result_t b_t;
  307. typedef shortest_alternative<a_t, b_t> result_t;
  308. static result_t
  309. convert(alternative<A, B> const& alt) // Recursive case
  310. {
  311. return result_t(
  312. to_shortest_alternative<A>::convert(alt.left()),
  313. to_shortest_alternative<B>::convert(alt.right()));
  314. }
  315. };
  316. #endif
  317. }
  318. BOOST_SPIRIT_CLASSIC_NAMESPACE_END
  319. }} // namespace boost::spirit
  320. #endif