switch_tests_wo_default.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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
  29. struct switch_grammar_direct : public grammar<switch_grammar_direct>
  30. {
  31. template <typename ScannerT>
  32. struct definition
  33. {
  34. definition(switch_grammar_direct const& /*self*/)
  35. {
  36. r = switch_p [
  37. case_p<'a'>(int_p),
  38. case_p<'b'>(ch_p(',')),
  39. case_p<'c'>(str_p("bcd")),
  40. case_p<'d'>(eps_p)
  41. ];
  42. }
  43. rule<ScannerT> r;
  44. rule<ScannerT> const& start() const { return r; }
  45. };
  46. };
  47. ///////////////////////////////////////////////////////////////////////////////
  48. // Test the switch_p usage given a parser as the switch condition
  49. struct switch_grammar_parser : public grammar<switch_grammar_parser>
  50. {
  51. template <typename ScannerT>
  52. struct definition
  53. {
  54. definition(switch_grammar_parser const& /*self*/)
  55. {
  56. r = switch_p(anychar_p) [
  57. case_p<'a'>(int_p),
  58. case_p<'b'>(ch_p(',')),
  59. case_p<'c'>(str_p("bcd")),
  60. case_p<'d'>(eps_p)
  61. ];
  62. }
  63. rule<ScannerT> r;
  64. rule<ScannerT> const& start() const { return r; }
  65. };
  66. };
  67. ///////////////////////////////////////////////////////////////////////////////
  68. // Test the switch_p usage given an actor as the switch condition
  69. struct select_result : public BOOST_SPIRIT_CLASSIC_NS::closure<select_result, int>
  70. {
  71. member1 val;
  72. };
  73. struct switch_grammar_actor : public grammar<switch_grammar_actor>
  74. {
  75. template <typename ScannerT>
  76. struct definition
  77. {
  78. definition(switch_grammar_actor const& /*self*/)
  79. {
  80. using phoenix::arg1;
  81. r = select_p('a', 'b', 'c', 'd')[r.val = arg1] >>
  82. switch_p(r.val) [
  83. case_p<0>(int_p),
  84. case_p<1>(ch_p(',')),
  85. case_p<2>(str_p("bcd")),
  86. case_p<3>(eps_p)
  87. ];
  88. }
  89. rule<ScannerT, select_result::context_t> r;
  90. rule<ScannerT, select_result::context_t> const&
  91. start() const { return r; }
  92. };
  93. };
  94. } // namespace test_grammars
  95. ///////////////////////////////////////////////////////////////////////////////
  96. namespace tests {
  97. // Tests for known (to the grammars) sequences
  98. struct check_grammar_known {
  99. template <typename GrammarT>
  100. void operator()(GrammarT)
  101. {
  102. GrammarT g;
  103. BOOST_TEST(parse("a1", g).full);
  104. BOOST_TEST(!parse("a,", g).hit);
  105. BOOST_TEST(!parse("abcd", g).hit);
  106. BOOST_TEST(!parse("a", g).hit);
  107. BOOST_TEST(parse("a 1", g, space_p).full);
  108. BOOST_TEST(!parse("a ,", g, space_p).hit);
  109. BOOST_TEST(!parse("a bcd", g, space_p).hit);
  110. BOOST_TEST(!parse("a ", g, space_p).hit);
  111. BOOST_TEST(!parse("b1", g).hit);
  112. BOOST_TEST(parse("b,", g).full);
  113. BOOST_TEST(!parse("bbcd", g).hit);
  114. BOOST_TEST(!parse("b", g).hit);
  115. BOOST_TEST(!parse("b 1", g, space_p).hit);
  116. BOOST_TEST(parse("b ,", g, space_p).full);
  117. BOOST_TEST(!parse("b bcd", g, space_p).hit);
  118. BOOST_TEST(!parse("b ", g, space_p).hit);
  119. BOOST_TEST(!parse("c1", g).hit);
  120. BOOST_TEST(!parse("c,", g).hit);
  121. BOOST_TEST(parse("cbcd", g).full);
  122. BOOST_TEST(!parse("c", g).hit);
  123. BOOST_TEST(!parse("c 1", g, space_p).hit);
  124. BOOST_TEST(!parse("c ,", g, space_p).hit);
  125. BOOST_TEST(parse("c bcd", g, space_p).full);
  126. BOOST_TEST(!parse("c ", g, space_p).hit);
  127. BOOST_TEST(parse("d1", g).hit);
  128. BOOST_TEST(parse("d,", g).hit);
  129. BOOST_TEST(parse("dbcd", g).hit);
  130. BOOST_TEST(parse("d", g).full);
  131. BOOST_TEST(parse("d 1", g, space_p).hit);
  132. BOOST_TEST(parse("d ,", g, space_p).hit);
  133. BOOST_TEST(parse("d bcd", g, space_p).hit);
  134. BOOST_TEST(parse(" d", g, space_p).full); // JDG 10-18-2005 removed trailing ' ' to
  135. // avoid post skip problems
  136. BOOST_TEST(parse(" a 1 b , c bcd d", *g, space_p).full);
  137. // JDG 10-18-2005 removed trailing ' ' to avoid post skip problems
  138. }
  139. };
  140. // Tests for unknown (to the grammar) sequences
  141. struct check_grammar_unknown_default {
  142. template <typename GrammarT>
  143. void operator()(GrammarT)
  144. {
  145. GrammarT g;
  146. BOOST_TEST(!parse("x1", g).hit);
  147. BOOST_TEST(!parse("x,", g).hit);
  148. BOOST_TEST(!parse("xbcd", g).hit);
  149. BOOST_TEST(!parse("x 1", g, space_p).hit);
  150. BOOST_TEST(!parse("x ,", g, space_p).hit);
  151. BOOST_TEST(!parse("x bcd", g, space_p).hit);
  152. }
  153. };
  154. } // namespace tests
  155. int
  156. main()
  157. {
  158. // Test switch_p without any default_p case branches
  159. typedef boost::mpl::list<
  160. test_grammars::switch_grammar_direct,
  161. test_grammars::switch_grammar_parser,
  162. test_grammars::switch_grammar_actor
  163. > grammar_list_t;
  164. boost::mpl::for_each<grammar_list_t>(tests::check_grammar_known());
  165. boost::mpl::for_each<grammar_list_t>(tests::check_grammar_unknown_default());
  166. return boost::report_errors();
  167. }