terminal_ex.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*=============================================================================
  2. Copyright (c) 2008 Francois Barel
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. =============================================================================*/
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/type_traits/is_same.hpp>
  8. #include <boost/spirit/include/qi_operator.hpp>
  9. #include <boost/spirit/include/qi_char.hpp>
  10. #include <boost/spirit/include/phoenix_core.hpp>
  11. #include <boost/spirit/include/phoenix_operator.hpp>
  12. #include <iterator>
  13. #include "test.hpp"
  14. namespace testns
  15. {
  16. BOOST_SPIRIT_TERMINAL_NAME_EX( ops, ops_type )
  17. ///////////////////////////////////////////////////////////////////////////
  18. // Parsers
  19. ///////////////////////////////////////////////////////////////////////////
  20. template <typename T1>
  21. struct ops_1_parser
  22. : boost::spirit::qi::primitive_parser<ops_1_parser<T1> >
  23. {
  24. ops_1_parser(T1 t1)
  25. : t1(t1)
  26. {}
  27. template <typename Context, typename Iterator>
  28. struct attribute
  29. {
  30. typedef int type; // Number of parsed chars.
  31. };
  32. template <typename Iterator, typename Context
  33. , typename Skipper, typename Attribute>
  34. bool parse(Iterator& first, Iterator const& last
  35. , Context& /*context*/, Skipper const& skipper
  36. , Attribute& attr) const
  37. {
  38. boost::spirit::qi::skip_over(first, last, skipper);
  39. int count = 0;
  40. Iterator it = first;
  41. typedef typename std::iterator_traits<Iterator>::value_type Char;
  42. for (T1 t = 0; t < t1; t++, count++)
  43. if (it == last || *it++ != Char('+'))
  44. return false;
  45. boost::spirit::traits::assign_to(count, attr);
  46. first = it;
  47. return true;
  48. }
  49. template <typename Context>
  50. boost::spirit::qi::info what(Context& /*context*/) const
  51. {
  52. return boost::spirit::qi::info("ops_1");
  53. }
  54. const T1 t1;
  55. // silence MSVC warning C4512: assignment operator could not be generated
  56. BOOST_DELETED_FUNCTION(ops_1_parser& operator= (ops_1_parser const&));
  57. };
  58. template <typename T1, typename T2>
  59. struct ops_2_parser
  60. : boost::spirit::qi::primitive_parser<ops_2_parser<T1, T2> >
  61. {
  62. ops_2_parser(T1 t1, T2 t2)
  63. : t1(t1)
  64. , t2(t2)
  65. {}
  66. template <typename Context, typename Iterator>
  67. struct attribute
  68. {
  69. typedef int type; // Number of parsed chars.
  70. };
  71. template <typename Iterator, typename Context
  72. , typename Skipper, typename Attribute>
  73. bool parse(Iterator& first, Iterator const& last
  74. , Context& /*context*/, Skipper const& skipper
  75. , Attribute& attr) const
  76. {
  77. boost::spirit::qi::skip_over(first, last, skipper);
  78. int count = 0;
  79. Iterator it = first;
  80. typedef typename std::iterator_traits<Iterator>::value_type Char;
  81. for (T1 t = 0; t < t1; t++, count++)
  82. if (it == last || *it++ != Char('+'))
  83. return false;
  84. for (T2 t = 0; t < t2; t++, count++)
  85. if (it == last || *it++ != Char('-'))
  86. return false;
  87. boost::spirit::traits::assign_to(count, attr);
  88. first = it;
  89. return true;
  90. }
  91. template <typename Context>
  92. boost::spirit::qi::info what(Context& /*context*/) const
  93. {
  94. return boost::spirit::qi::info("ops_2");
  95. }
  96. const T1 t1;
  97. const T2 t2;
  98. // silence MSVC warning C4512: assignment operator could not be generated
  99. BOOST_DELETED_FUNCTION(ops_2_parser& operator= (ops_2_parser const&));
  100. };
  101. template <typename T1, typename T2, typename T3>
  102. struct ops_3_parser
  103. : boost::spirit::qi::primitive_parser<ops_3_parser<T1, T2, T3> >
  104. {
  105. ops_3_parser(T1 t1, T2 t2, T3 t3)
  106. : t1(t1)
  107. , t2(t2)
  108. , t3(t3)
  109. {}
  110. template <typename Context, typename Iterator>
  111. struct attribute
  112. {
  113. typedef int type; // Number of parsed chars.
  114. };
  115. template <typename Iterator, typename Context
  116. , typename Skipper, typename Attribute>
  117. bool parse(Iterator& first, Iterator const& last
  118. , Context& /*context*/, Skipper const& skipper
  119. , Attribute& attr) const
  120. {
  121. boost::spirit::qi::skip_over(first, last, skipper);
  122. int count = 0;
  123. Iterator it = first;
  124. typedef typename std::iterator_traits<Iterator>::value_type Char;
  125. for (T1 t = 0; t < t1; t++, count++)
  126. if (it == last || *it++ != Char('+'))
  127. return false;
  128. for (T2 t = 0; t < t2; t++, count++)
  129. if (it == last || *it++ != Char('-'))
  130. return false;
  131. for (T3 t = 0; t < t3; t++, count++)
  132. if (it == last || *it++ != Char('*'))
  133. return false;
  134. boost::spirit::traits::assign_to(count, attr);
  135. first = it;
  136. return true;
  137. }
  138. template <typename Context>
  139. boost::spirit::qi::info what(Context& /*context*/) const
  140. {
  141. return boost::spirit::qi::info("ops_3");
  142. }
  143. const T1 t1;
  144. const T2 t2;
  145. const T3 t3;
  146. // silence MSVC warning C4512: assignment operator could not be generated
  147. BOOST_DELETED_FUNCTION(ops_3_parser& operator= (ops_3_parser const&));
  148. };
  149. }
  150. namespace boost { namespace spirit
  151. {
  152. ///////////////////////////////////////////////////////////////////////////
  153. // Enablers
  154. ///////////////////////////////////////////////////////////////////////////
  155. template <typename T1>
  156. struct use_terminal<qi::domain
  157. , terminal_ex<testns::tag::ops, fusion::vector1<T1> > >
  158. : mpl::true_ {};
  159. template <typename T1, typename T2>
  160. struct use_terminal<qi::domain
  161. , terminal_ex<testns::tag::ops, fusion::vector2<T1, T2> > >
  162. : mpl::true_ {};
  163. template <typename T1, typename T2, typename T3>
  164. struct use_terminal<qi::domain
  165. , terminal_ex<testns::tag::ops, fusion::vector3<T1, T2, T3> > >
  166. : mpl::true_ {};
  167. template <>
  168. struct use_lazy_terminal<qi::domain, testns::tag::ops, 1>
  169. : mpl::true_ {};
  170. template <>
  171. struct use_lazy_terminal<qi::domain, testns::tag::ops, 2>
  172. : mpl::true_ {};
  173. template <>
  174. struct use_lazy_terminal<qi::domain, testns::tag::ops, 3>
  175. : mpl::true_ {};
  176. }}
  177. namespace boost { namespace spirit { namespace qi
  178. {
  179. ///////////////////////////////////////////////////////////////////////////
  180. // Parser generators: make_xxx function (objects)
  181. ///////////////////////////////////////////////////////////////////////////
  182. template <typename Modifiers, typename T1>
  183. struct make_primitive<
  184. terminal_ex<testns::tag::ops, fusion::vector1<T1> >
  185. , Modifiers>
  186. {
  187. typedef testns::ops_1_parser<T1> result_type;
  188. template <typename Terminal>
  189. result_type operator()(const Terminal& term, unused_type) const
  190. {
  191. return result_type(
  192. fusion::at_c<0>(term.args)
  193. );
  194. }
  195. };
  196. template <typename Modifiers, typename T1, typename T2>
  197. struct make_primitive<
  198. terminal_ex<testns::tag::ops, fusion::vector2<T1, T2> >
  199. , Modifiers>
  200. {
  201. typedef testns::ops_2_parser<T1, T2> result_type;
  202. template <typename Terminal>
  203. result_type operator()(const Terminal& term, unused_type) const
  204. {
  205. return result_type(
  206. fusion::at_c<0>(term.args)
  207. , fusion::at_c<1>(term.args)
  208. );
  209. }
  210. };
  211. template <typename Modifiers, typename T1, typename T2, typename T3>
  212. struct make_primitive<
  213. terminal_ex<testns::tag::ops, fusion::vector3<T1, T2, T3> >
  214. , Modifiers>
  215. {
  216. typedef testns::ops_3_parser<T1, T2, T3> result_type;
  217. template <typename Terminal>
  218. result_type operator()(const Terminal& term, unused_type) const
  219. {
  220. return result_type(
  221. fusion::at_c<0>(term.args)
  222. , fusion::at_c<1>(term.args)
  223. , fusion::at_c<2>(term.args)
  224. );
  225. }
  226. };
  227. }}}
  228. namespace testns
  229. {
  230. template <typename T1, typename T>
  231. void check_type_1(const T& /*t*/)
  232. {
  233. BOOST_STATIC_ASSERT(( boost::is_same<T
  234. , typename boost::spirit::terminal<testns::tag::ops>::result<T1>::type >::value ));
  235. }
  236. template <typename T1, typename T2, typename T>
  237. void check_type_2(const T& /*t*/)
  238. {
  239. BOOST_STATIC_ASSERT(( boost::is_same<T
  240. , typename boost::spirit::terminal<testns::tag::ops>::result<T1, T2>::type >::value ));
  241. }
  242. template <typename T1, typename T2, typename T3, typename T>
  243. void check_type_3(const T& /*t*/)
  244. {
  245. BOOST_STATIC_ASSERT(( boost::is_same<T
  246. , typename boost::spirit::terminal<testns::tag::ops>::result<T1, T2, T3>::type >::value ));
  247. }
  248. }
  249. int
  250. main()
  251. {
  252. using spirit_test::test_attr;
  253. using spirit_test::test;
  254. using testns::ops;
  255. using testns::check_type_1;
  256. using testns::check_type_2;
  257. using testns::check_type_3;
  258. { // immediate args
  259. int c = 0;
  260. #define IP1 ops(2)
  261. check_type_1<int>(IP1);
  262. BOOST_TEST(test_attr("++/", IP1 >> '/', c) && c == 2);
  263. c = 0;
  264. #define IP2 ops(2, 3)
  265. check_type_2<int, int>(IP2);
  266. BOOST_TEST(test_attr("++---/", IP2 >> '/', c) && c == 5);
  267. c = 0;
  268. #define IP3 ops(2, 3, 4)
  269. check_type_3<int, int, int>(IP3);
  270. BOOST_TEST(!test("++---***/", IP3 >> '/'));
  271. #define IP4 ops(2, 3, 4)
  272. check_type_3<int, int, int>(IP4);
  273. BOOST_TEST(test_attr("++---****/", IP4 >> '/', c) && c == 9);
  274. }
  275. using boost::phoenix::val;
  276. using boost::phoenix::actor;
  277. using boost::phoenix::expression::value;
  278. { // all lazy args
  279. int c = 0;
  280. #define LP1 ops(val(1))
  281. check_type_1<value<int>::type>(LP1);
  282. BOOST_TEST(test_attr("+/", LP1 >> '/', c) && c == 1);
  283. c = 0;
  284. #define LP2 ops(val(1), val(4))
  285. check_type_2<value<int>::type, value<int>::type>(LP2);
  286. BOOST_TEST(test_attr("+----/", LP2 >> '/', c) && c == 5);
  287. c = 0;
  288. #define LP3 ops(val((char)2), val(3.), val(4))
  289. check_type_3<value<char>::type, value<double>::type, value<int>::type>(LP3);
  290. BOOST_TEST(!test("++---***/", LP3 >> '/'));
  291. #define LP4 ops(val(1), val(2), val(3))
  292. check_type_3<value<int>::type, value<int>::type, value<int>::type>(LP4);
  293. BOOST_TEST(test_attr("+--***/", LP4 >> '/', c) && c == 6);
  294. }
  295. { // mixed immediate and lazy args
  296. namespace fusion = boost::fusion;
  297. namespace phx = boost::phoenix;
  298. int c = 0;
  299. #define MP1 ops(val(3), 2)
  300. check_type_2<value<int>::type, int>(MP1);
  301. BOOST_TEST(test_attr("+++--/", MP1 >> '/', c) && c == 5);
  302. c = 0;
  303. #define MP2 ops(4, val(1))
  304. check_type_2<int, value<int>::type>(MP2);
  305. BOOST_TEST(test_attr("++++-/", MP2 >> '/', c) && c == 5);
  306. c = 0;
  307. #define MP3 ops(2, val(2), val(2))
  308. check_type_3<int, value<int>::type, value<int>::type>(MP3);
  309. BOOST_TEST(!test("++-**/", MP3 >> '/'));
  310. #define MP4 ops(2, val(2), 2)
  311. check_type_3<int, value<int>::type, int>(MP4);
  312. BOOST_TEST(test_attr("++--**/", MP4 >> '/', c) && c == 6);
  313. c = 0;
  314. #define MP5 ops(val(5) - val(3), 2, val(2))
  315. check_type_3<phx::expression::minus<value<int>::type, value<int>::type>::type, int, value<int>::type>(MP5);
  316. BOOST_TEST(test_attr("++--**/", MP5 >> '/', c) && c == 6);
  317. }
  318. return boost::report_errors();
  319. }