switch_tests_general_def.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*=============================================================================
  2. Copyright (c) 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. #include <iostream>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #define BOOST_SPIRIT_SWITCH_CASE_LIMIT 6
  11. #define BOOST_SPIRIT_SELECT_LIMIT 6
  12. #define PHOENIX_LIMIT 6
  13. //#define BOOST_SPIRIT_DEBUG
  14. #include <boost/mpl/list.hpp>
  15. #include <boost/mpl/for_each.hpp>
  16. #include <boost/spirit/include/classic_primitives.hpp>
  17. #include <boost/spirit/include/classic_numerics.hpp>
  18. #include <boost/spirit/include/classic_actions.hpp>
  19. #include <boost/spirit/include/classic_operators.hpp>
  20. #include <boost/spirit/include/classic_rule.hpp>
  21. #include <boost/spirit/include/classic_grammar.hpp>
  22. #include <boost/spirit/include/classic_switch.hpp>
  23. #include <boost/spirit/include/classic_select.hpp>
  24. #include <boost/spirit/include/classic_closure.hpp>
  25. using namespace BOOST_SPIRIT_CLASSIC_NS;
  26. namespace test_grammars {
  27. ///////////////////////////////////////////////////////////////////////////////
  28. // Test the direct switch_p usage (with default_p)
  29. struct switch_grammar_direct_default1
  30. : public grammar<switch_grammar_direct_default1>
  31. {
  32. template <typename ScannerT>
  33. struct definition
  34. {
  35. definition(switch_grammar_direct_default1 const& /*self*/)
  36. {
  37. r = switch_p [
  38. case_p<'a'>(int_p),
  39. case_p<'b'>(ch_p(',')),
  40. case_p<'c'>(str_p("bcd")),
  41. default_p(str_p("default"))
  42. ];
  43. }
  44. rule<ScannerT> r;
  45. rule<ScannerT> const& start() const { return r; }
  46. };
  47. };
  48. struct switch_grammar_direct_default2
  49. : public grammar<switch_grammar_direct_default2>
  50. {
  51. template <typename ScannerT>
  52. struct definition
  53. {
  54. definition(switch_grammar_direct_default2 const& /*self*/)
  55. {
  56. r = switch_p [
  57. case_p<'a'>(int_p),
  58. case_p<'b'>(ch_p(',')),
  59. default_p(str_p("default")),
  60. case_p<'c'>(str_p("bcd"))
  61. ];
  62. }
  63. rule<ScannerT> r;
  64. rule<ScannerT> const& start() const { return r; }
  65. };
  66. };
  67. struct switch_grammar_direct_default3
  68. : public grammar<switch_grammar_direct_default3>
  69. {
  70. template <typename ScannerT>
  71. struct definition
  72. {
  73. definition(switch_grammar_direct_default3 const& /*self*/)
  74. {
  75. r = switch_p [
  76. default_p(str_p("default")),
  77. case_p<'a'>(int_p),
  78. case_p<'b'>(ch_p(',')),
  79. case_p<'c'>(str_p("bcd"))
  80. ];
  81. }
  82. rule<ScannerT> r;
  83. rule<ScannerT> const& start() const { return r; }
  84. };
  85. };
  86. ///////////////////////////////////////////////////////////////////////////////
  87. // Test the switch_p usage given a parser as the switch condition
  88. struct switch_grammar_parser_default1
  89. : public grammar<switch_grammar_parser_default1>
  90. {
  91. template <typename ScannerT>
  92. struct definition
  93. {
  94. definition(switch_grammar_parser_default1 const& /*self*/)
  95. {
  96. r = switch_p(anychar_p) [
  97. case_p<'a'>(int_p),
  98. case_p<'b'>(ch_p(',')),
  99. case_p<'c'>(str_p("bcd")),
  100. default_p(str_p("default"))
  101. ];
  102. }
  103. rule<ScannerT> r;
  104. rule<ScannerT> const& start() const { return r; }
  105. };
  106. };
  107. struct switch_grammar_parser_default2
  108. : public grammar<switch_grammar_parser_default2>
  109. {
  110. template <typename ScannerT>
  111. struct definition
  112. {
  113. definition(switch_grammar_parser_default2 const& /*self*/)
  114. {
  115. r = switch_p(anychar_p) [
  116. case_p<'a'>(int_p),
  117. case_p<'b'>(ch_p(',')),
  118. default_p(str_p("default")),
  119. case_p<'c'>(str_p("bcd"))
  120. ];
  121. }
  122. rule<ScannerT> r;
  123. rule<ScannerT> const& start() const { return r; }
  124. };
  125. };
  126. struct switch_grammar_parser_default3
  127. : public grammar<switch_grammar_parser_default3>
  128. {
  129. template <typename ScannerT>
  130. struct definition
  131. {
  132. definition(switch_grammar_parser_default3 const& /*self*/)
  133. {
  134. r = switch_p(anychar_p) [
  135. default_p(str_p("default")),
  136. case_p<'a'>(int_p),
  137. case_p<'b'>(ch_p(',')),
  138. case_p<'c'>(str_p("bcd"))
  139. ];
  140. }
  141. rule<ScannerT> r;
  142. rule<ScannerT> const& start() const { return r; }
  143. };
  144. };
  145. ///////////////////////////////////////////////////////////////////////////////
  146. // Test the switch_p usage given an actor as the switch condition
  147. struct select_result : public BOOST_SPIRIT_CLASSIC_NS::closure<select_result, int>
  148. {
  149. member1 val;
  150. };
  151. ///////////////////////////////////////////////////////////////////////////
  152. struct switch_grammar_actor_default1
  153. : public grammar<switch_grammar_actor_default1>
  154. {
  155. template <typename ScannerT>
  156. struct definition
  157. {
  158. definition(switch_grammar_actor_default1 const& /*self*/)
  159. {
  160. using phoenix::arg1;
  161. r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >>
  162. switch_p(r.val) [
  163. case_p<0>(int_p),
  164. case_p<1>(ch_p(',')),
  165. case_p<2>(str_p("bcd")),
  166. default_p(str_p("default"))
  167. ];
  168. }
  169. rule<ScannerT, select_result::context_t> r;
  170. rule<ScannerT, select_result::context_t> const&
  171. start() const { return r; }
  172. };
  173. };
  174. struct switch_grammar_actor_default2
  175. : public grammar<switch_grammar_actor_default2>
  176. {
  177. template <typename ScannerT>
  178. struct definition
  179. {
  180. definition(switch_grammar_actor_default2 const& /*self*/)
  181. {
  182. using phoenix::arg1;
  183. r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >>
  184. switch_p(r.val) [
  185. case_p<0>(int_p),
  186. case_p<1>(ch_p(',')),
  187. default_p(str_p("default")),
  188. case_p<2>(str_p("bcd"))
  189. ];
  190. }
  191. rule<ScannerT, select_result::context_t> r;
  192. rule<ScannerT, select_result::context_t> const&
  193. start() const { return r; }
  194. };
  195. };
  196. struct switch_grammar_actor_default3
  197. : public grammar<switch_grammar_actor_default3>
  198. {
  199. template <typename ScannerT>
  200. struct definition
  201. {
  202. definition(switch_grammar_actor_default3 const& /*self*/)
  203. {
  204. using phoenix::arg1;
  205. r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >>
  206. switch_p(r.val) [
  207. default_p(str_p("default")),
  208. case_p<0>(int_p),
  209. case_p<1>(ch_p(',')),
  210. case_p<2>(str_p("bcd"))
  211. ];
  212. }
  213. rule<ScannerT, select_result::context_t> r;
  214. rule<ScannerT, select_result::context_t> const&
  215. start() const { return r; }
  216. };
  217. };
  218. } // namespace test_grammars
  219. ///////////////////////////////////////////////////////////////////////////////
  220. namespace tests {
  221. // Tests for known (to the grammars) sequences
  222. struct check_grammar_known {
  223. template <typename GrammarT>
  224. void operator()(GrammarT)
  225. {
  226. GrammarT g;
  227. BOOST_TEST(parse("a1", g).full);
  228. BOOST_TEST(!parse("a,", g).hit);
  229. BOOST_TEST(!parse("abcd", g).hit);
  230. BOOST_TEST(parse("a 1", g, space_p).full);
  231. BOOST_TEST(!parse("a ,", g, space_p).hit);
  232. BOOST_TEST(!parse("a bcd", g, space_p).hit);
  233. BOOST_TEST(!parse("b1", g).hit);
  234. BOOST_TEST(parse("b,", g).full);
  235. BOOST_TEST(!parse("bbcd", g).hit);
  236. BOOST_TEST(!parse("b 1", g, space_p).hit);
  237. BOOST_TEST(parse("b ,", g, space_p).full);
  238. BOOST_TEST(!parse("b bcd", g, space_p).hit);
  239. BOOST_TEST(!parse("c1", g).hit);
  240. BOOST_TEST(!parse("c,", g).hit);
  241. BOOST_TEST(parse("cbcd", g).full);
  242. BOOST_TEST(!parse("c 1", g, space_p).hit);
  243. BOOST_TEST(!parse("c ,", g, space_p).hit);
  244. BOOST_TEST(parse("c bcd", g, space_p).full);
  245. }
  246. };
  247. // Tests for unknown (to the grammar) sequences
  248. struct check_grammar_unknown_default {
  249. template <typename GrammarT>
  250. void operator()(GrammarT)
  251. {
  252. GrammarT g;
  253. BOOST_TEST(!parse("d1", g).hit);
  254. BOOST_TEST(!parse("d,", g).hit);
  255. BOOST_TEST(!parse("dbcd", g).hit);
  256. BOOST_TEST(!parse("d 1", g, space_p).hit);
  257. BOOST_TEST(!parse("d ,", g, space_p).hit);
  258. BOOST_TEST(!parse("d bcd", g, space_p).hit);
  259. }
  260. };
  261. // Tests for the default branches (with parsers) of the grammars
  262. struct check_grammar_default {
  263. template <typename GrammarT>
  264. void operator()(GrammarT)
  265. {
  266. GrammarT g;
  267. BOOST_TEST(parse("ddefault", g).full);
  268. BOOST_TEST(parse("d default", g, space_p).full);
  269. }
  270. };
  271. } // namespace tests
  272. int
  273. main()
  274. {
  275. // Test switch_p parsers containing general default_p(...) case branches
  276. typedef boost::mpl::list<
  277. // switch_p syntax
  278. test_grammars::switch_grammar_direct_default1,
  279. test_grammars::switch_grammar_direct_default2,
  280. test_grammars::switch_grammar_direct_default3,
  281. // switch_p(parser) syntax
  282. test_grammars::switch_grammar_parser_default1,
  283. test_grammars::switch_grammar_parser_default2,
  284. test_grammars::switch_grammar_parser_default3,
  285. // switch_p(actor) syntax
  286. test_grammars::switch_grammar_actor_default1,
  287. test_grammars::switch_grammar_actor_default2,
  288. test_grammars::switch_grammar_actor_default3
  289. > default_list_t;
  290. boost::mpl::for_each<default_list_t>(tests::check_grammar_known());
  291. boost::mpl::for_each<default_list_t>(tests::check_grammar_unknown_default());
  292. boost::mpl::for_each<default_list_t>(tests::check_grammar_default());
  293. return boost::report_errors();
  294. }