switch_tests_eps_default.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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_default4
  30. : public grammar<switch_grammar_direct_default4>
  31. {
  32. template <typename ScannerT>
  33. struct definition
  34. {
  35. definition(switch_grammar_direct_default4 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
  42. ];
  43. }
  44. rule<ScannerT> r;
  45. rule<ScannerT> const& start() const { return r; }
  46. };
  47. };
  48. struct switch_grammar_direct_default5
  49. : public grammar<switch_grammar_direct_default5>
  50. {
  51. template <typename ScannerT>
  52. struct definition
  53. {
  54. definition(switch_grammar_direct_default5 const& /*self*/)
  55. {
  56. r = switch_p [
  57. case_p<'a'>(int_p),
  58. case_p<'b'>(ch_p(',')),
  59. default_p,
  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_default6
  68. : public grammar<switch_grammar_direct_default6>
  69. {
  70. template <typename ScannerT>
  71. struct definition
  72. {
  73. definition(switch_grammar_direct_default6 const& /*self*/)
  74. {
  75. r = switch_p [
  76. default_p,
  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_default4
  89. : public grammar<switch_grammar_parser_default4>
  90. {
  91. template <typename ScannerT>
  92. struct definition
  93. {
  94. definition(switch_grammar_parser_default4 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
  101. ];
  102. }
  103. rule<ScannerT> r;
  104. rule<ScannerT> const& start() const { return r; }
  105. };
  106. };
  107. struct switch_grammar_parser_default5
  108. : public grammar<switch_grammar_parser_default5>
  109. {
  110. template <typename ScannerT>
  111. struct definition
  112. {
  113. definition(switch_grammar_parser_default5 const& /*self*/)
  114. {
  115. r = switch_p(anychar_p) [
  116. case_p<'a'>(int_p),
  117. case_p<'b'>(ch_p(',')),
  118. default_p,
  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_default6
  127. : public grammar<switch_grammar_parser_default6>
  128. {
  129. template <typename ScannerT>
  130. struct definition
  131. {
  132. definition(switch_grammar_parser_default6 const& /*self*/)
  133. {
  134. r = switch_p(anychar_p) [
  135. default_p,
  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. struct switch_grammar_actor_default4
  152. : public grammar<switch_grammar_actor_default4>
  153. {
  154. template <typename ScannerT>
  155. struct definition
  156. {
  157. definition(switch_grammar_actor_default4 const& /*self*/)
  158. {
  159. using phoenix::arg1;
  160. r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >>
  161. switch_p(r.val) [
  162. case_p<0>(int_p),
  163. case_p<1>(ch_p(',')),
  164. case_p<2>(str_p("bcd")),
  165. default_p
  166. ];
  167. }
  168. rule<ScannerT, select_result::context_t> r;
  169. rule<ScannerT, select_result::context_t> const&
  170. start() const { return r; }
  171. };
  172. };
  173. struct switch_grammar_actor_default5
  174. : public grammar<switch_grammar_actor_default5>
  175. {
  176. template <typename ScannerT>
  177. struct definition
  178. {
  179. definition(switch_grammar_actor_default5 const& /*self*/)
  180. {
  181. using phoenix::arg1;
  182. r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >>
  183. switch_p(r.val) [
  184. case_p<0>(int_p),
  185. case_p<1>(ch_p(',')),
  186. default_p,
  187. case_p<2>(str_p("bcd"))
  188. ];
  189. }
  190. rule<ScannerT, select_result::context_t> r;
  191. rule<ScannerT, select_result::context_t> const&
  192. start() const { return r; }
  193. };
  194. };
  195. struct switch_grammar_actor_default6
  196. : public grammar<switch_grammar_actor_default6>
  197. {
  198. template <typename ScannerT>
  199. struct definition
  200. {
  201. definition(switch_grammar_actor_default6 const& /*self*/)
  202. {
  203. using phoenix::arg1;
  204. r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >>
  205. switch_p(r.val) [
  206. default_p,
  207. case_p<0>(int_p),
  208. case_p<1>(ch_p(',')),
  209. case_p<2>(str_p("bcd"))
  210. ];
  211. }
  212. rule<ScannerT, select_result::context_t> r;
  213. rule<ScannerT, select_result::context_t> const&
  214. start() const { return r; }
  215. };
  216. };
  217. } // namespace test_grammars
  218. ///////////////////////////////////////////////////////////////////////////////
  219. namespace tests {
  220. // Tests for known (to the grammars) sequences
  221. struct check_grammar_known {
  222. template <typename GrammarT>
  223. void operator()(GrammarT)
  224. {
  225. GrammarT g;
  226. BOOST_TEST(parse("a1", g).full);
  227. BOOST_TEST(!parse("a,", g).hit);
  228. BOOST_TEST(!parse("abcd", g).hit);
  229. BOOST_TEST(parse("a 1", g, space_p).full);
  230. BOOST_TEST(!parse("a ,", g, space_p).hit);
  231. BOOST_TEST(!parse("a bcd", g, space_p).hit);
  232. BOOST_TEST(!parse("b1", g).hit);
  233. BOOST_TEST(parse("b,", g).full);
  234. BOOST_TEST(!parse("bbcd", g).hit);
  235. BOOST_TEST(!parse("b 1", g, space_p).hit);
  236. BOOST_TEST(parse("b ,", g, space_p).full);
  237. BOOST_TEST(!parse("b bcd", g, space_p).hit);
  238. BOOST_TEST(!parse("c1", g).hit);
  239. BOOST_TEST(!parse("c,", g).hit);
  240. BOOST_TEST(parse("cbcd", g).full);
  241. BOOST_TEST(!parse("c 1", g, space_p).hit);
  242. BOOST_TEST(!parse("c ,", g, space_p).hit);
  243. BOOST_TEST(parse("c bcd", g, space_p).full);
  244. }
  245. };
  246. // Tests for known (to the grammars) sequences
  247. // Tests for the default branches (without parsers) of the grammars
  248. struct check_grammar_default_plain {
  249. template <typename GrammarT>
  250. void operator()(GrammarT)
  251. {
  252. GrammarT g;
  253. BOOST_TEST(parse("d", g).full);
  254. BOOST_TEST(parse(" d", g, space_p).full); // JDG 10-18-2005 removed trailing ' ' to
  255. // avoid post skip problems
  256. }
  257. };
  258. } // namespace tests
  259. int
  260. main()
  261. {
  262. // Test switch_p parsers containing special (epsilon) default_p case
  263. // branches
  264. typedef boost::mpl::list<
  265. // switch_p syntax
  266. test_grammars::switch_grammar_direct_default4,
  267. test_grammars::switch_grammar_direct_default5,
  268. test_grammars::switch_grammar_direct_default6,
  269. // switch_p(parser) syntax
  270. test_grammars::switch_grammar_parser_default4,
  271. test_grammars::switch_grammar_parser_default5,
  272. test_grammars::switch_grammar_parser_default6,
  273. // switch_p(actor) syntax
  274. test_grammars::switch_grammar_actor_default4,
  275. test_grammars::switch_grammar_actor_default5,
  276. test_grammars::switch_grammar_actor_default6
  277. > default_epsilon_list_t;
  278. boost::mpl::for_each<default_epsilon_list_t>(tests::check_grammar_known());
  279. boost::mpl::for_each<default_epsilon_list_t>(
  280. tests::check_grammar_default_plain());
  281. return boost::report_errors();
  282. }