rule1.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  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/spirit/include/qi_operator.hpp>
  8. #include <boost/spirit/include/qi_char.hpp>
  9. #include <boost/spirit/include/qi_string.hpp>
  10. #include <boost/spirit/include/qi_numeric.hpp>
  11. #include <boost/spirit/include/qi_auxiliary.hpp>
  12. #include <boost/spirit/include/qi_directive.hpp>
  13. #include <boost/spirit/include/qi_nonterminal.hpp>
  14. #include <boost/spirit/include/qi_action.hpp>
  15. #include <boost/fusion/include/std_pair.hpp>
  16. #include <string>
  17. #include <cstring>
  18. #include <iostream>
  19. #include "test.hpp"
  20. int
  21. main()
  22. {
  23. using spirit_test::test_attr;
  24. using spirit_test::test;
  25. using namespace boost::spirit::ascii;
  26. using namespace boost::spirit::qi::labels;
  27. using boost::spirit::qi::locals;
  28. using boost::spirit::qi::rule;
  29. using boost::spirit::qi::int_;
  30. using boost::spirit::qi::uint_;
  31. using boost::spirit::qi::fail;
  32. using boost::spirit::qi::on_error;
  33. using boost::spirit::qi::debug;
  34. using boost::spirit::qi::lit;
  35. { // basic tests
  36. rule<char const*> a, b, c, start;
  37. a = 'a';
  38. b = 'b';
  39. c = 'c';
  40. a.name("a");
  41. b.name("b");
  42. c.name("c");
  43. start.name("start");
  44. debug(a);
  45. debug(b);
  46. debug(c);
  47. debug(start);
  48. start = *(a | b | c);
  49. BOOST_TEST(test("abcabcacb", start));
  50. start = (a | b) >> (start | b);
  51. BOOST_TEST(test("aaaabababaaabbb", start));
  52. BOOST_TEST(test("aaaabababaaabba", start, false));
  53. // ignore the skipper!
  54. BOOST_TEST(test("aaaabababaaabba", start, space, false));
  55. }
  56. { // basic tests with direct initialization
  57. rule<char const*> a ('a');
  58. rule<char const*> b ('b');
  59. rule<char const*> c ('c');
  60. #ifdef BOOST_CLANG
  61. # pragma clang diagnostic push
  62. // variable 'start' is uninitialized when used within its own initialization
  63. # pragma clang diagnostic ignored "-Wuninitialized"
  64. #endif
  65. rule<char const*> start = (a | b) >> (start | b);
  66. #ifdef BOOST_CLANG
  67. # pragma clang diagnostic pop
  68. #endif
  69. BOOST_TEST(test("aaaabababaaabbb", start));
  70. BOOST_TEST(test("aaaabababaaabba", start, false));
  71. // ignore the skipper!
  72. BOOST_TEST(test("aaaabababaaabba", start, space, false));
  73. }
  74. { // basic tests w/ skipper
  75. rule<char const*, space_type> a, b, c, start;
  76. a = 'a';
  77. b = 'b';
  78. c = 'c';
  79. a.name("a");
  80. b.name("b");
  81. c.name("c");
  82. start.name("start");
  83. debug(a);
  84. debug(b);
  85. debug(c);
  86. debug(start);
  87. start = *(a | b | c);
  88. BOOST_TEST(test(" a b c a b c a c b ", start, space));
  89. start = (a | b) >> (start | b);
  90. BOOST_TEST(test(" a a a a b a b a b a a a b b b ", start, space));
  91. BOOST_TEST(test(" a a a a b a b a b a a a b b a ", start, space, false));
  92. }
  93. { // basic tests w/ skipper but no final post-skip
  94. rule<char const*, space_type> a, b, c, start;
  95. a = 'a';
  96. b = 'b';
  97. c = 'c';
  98. a.name("a");
  99. b.name("b");
  100. c.name("c");
  101. start.name("start");
  102. debug(a);
  103. debug(b);
  104. debug(c);
  105. debug(start);
  106. start = *(a | b) >> c;
  107. using boost::spirit::qi::phrase_parse;
  108. using boost::spirit::qi::skip_flag;
  109. {
  110. char const *s1 = " a b a a b b a c ... "
  111. , *const e1 = s1 + std::strlen(s1);
  112. BOOST_TEST(phrase_parse(s1, e1, start, space, skip_flag::dont_postskip)
  113. && s1 == e1 - 5);
  114. }
  115. start = (a | b) >> (start | c);
  116. {
  117. char const *s1 = " a a a a b a b a b a a a b b b c "
  118. , *const e1 = s1 + std::strlen(s1);
  119. BOOST_TEST(phrase_parse(s1, e1, start, space, skip_flag::postskip)
  120. && s1 == e1);
  121. }
  122. {
  123. char const *s1 = " a a a a b a b a b a a a b b b c "
  124. , *const e1 = s1 + std::strlen(s1);
  125. BOOST_TEST(phrase_parse(s1, e1, start, space, skip_flag::dont_postskip)
  126. && s1 == e1 - 1);
  127. }
  128. }
  129. return boost::report_errors();
  130. }