rule2.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/spirit/include/phoenix_core.hpp>
  16. #include <boost/spirit/include/phoenix_operator.hpp>
  17. #include <boost/fusion/include/std_pair.hpp>
  18. #include <string>
  19. #include <cstring>
  20. #include <iostream>
  21. #include "test.hpp"
  22. int
  23. main()
  24. {
  25. using spirit_test::test_attr;
  26. using spirit_test::test;
  27. using namespace boost::spirit::ascii;
  28. using namespace boost::spirit::qi::labels;
  29. using boost::spirit::qi::locals;
  30. using boost::spirit::qi::rule;
  31. using boost::spirit::qi::int_;
  32. using boost::spirit::qi::uint_;
  33. using boost::spirit::qi::fail;
  34. using boost::spirit::qi::on_error;
  35. using boost::spirit::qi::debug;
  36. using boost::spirit::qi::lit;
  37. namespace phx = boost::phoenix;
  38. { // test unassigned rule
  39. rule<char const*> a;
  40. BOOST_TEST(!test("x", a));
  41. }
  42. { // alias tests
  43. rule<char const*> a, b, c, d, start;
  44. a = 'a';
  45. b = 'b';
  46. c = 'c';
  47. d = start.alias(); // d will always track start
  48. start = *(a | b | c);
  49. BOOST_TEST(test("abcabcacb", d));
  50. start = (a | b) >> (start | b);
  51. BOOST_TEST(test("aaaabababaaabbb", d));
  52. }
  53. { // copy tests
  54. rule<char const*> a, b, c, start;
  55. a = 'a';
  56. b = 'b';
  57. c = 'c';
  58. // The FF is the dynamic equivalent of start = *(a | b | c);
  59. start = a;
  60. start = start.copy() | b;
  61. start = start.copy() | c;
  62. start = *(start.copy());
  63. BOOST_TEST(test("abcabcacb", start));
  64. // The FF is the dynamic equivalent of start = (a | b) >> (start | b);
  65. start = b;
  66. start = a | start.copy();
  67. start = start.copy() >> (start | b);
  68. BOOST_TEST(test("aaaabababaaabbb", start));
  69. BOOST_TEST(test("aaaabababaaabba", start, false));
  70. }
  71. { // context tests
  72. char ch;
  73. rule<char const*, char()> a;
  74. a = alpha[_val = _1];
  75. BOOST_TEST(test("x", a[phx::ref(ch) = _1]));
  76. BOOST_TEST(ch == 'x');
  77. BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
  78. BOOST_TEST(ch == 'z');
  79. }
  80. { // auto rules tests
  81. char ch = '\0';
  82. rule<char const*, char()> a;
  83. a %= alpha;
  84. BOOST_TEST(test("x", a[phx::ref(ch) = _1]));
  85. BOOST_TEST(ch == 'x');
  86. ch = '\0';
  87. BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
  88. BOOST_TEST(ch == 'z');
  89. a = alpha; // test deduced auto rule behavior
  90. ch = '\0';
  91. BOOST_TEST(test("x", a[phx::ref(ch) = _1]));
  92. BOOST_TEST(ch == 'x');
  93. ch = '\0';
  94. BOOST_TEST(test_attr("z", a, ch)); // attribute is given.
  95. BOOST_TEST(ch == 'z');
  96. }
  97. { // auto rules tests: allow stl containers as attributes to
  98. // sequences (in cases where attributes of the elements
  99. // are convertible to the value_type of the container or if
  100. // the element itself is an stl container with value_type
  101. // that is convertible to the value_type of the attribute).
  102. std::string s;
  103. rule<char const*, std::string()> r;
  104. r %= char_ >> *(',' >> char_);
  105. BOOST_TEST(test("a,b,c,d,e,f", r[phx::ref(s) = _1]));
  106. BOOST_TEST(s == "abcdef");
  107. r = char_ >> *(',' >> char_); // test deduced auto rule behavior
  108. s.clear();
  109. BOOST_TEST(test("a,b,c,d,e,f", r[phx::ref(s) = _1]));
  110. BOOST_TEST(s == "abcdef");
  111. r %= char_ >> char_ >> char_ >> char_ >> char_ >> char_;
  112. s.clear();
  113. BOOST_TEST(test("abcdef", r[phx::ref(s) = _1]));
  114. BOOST_TEST(s == "abcdef");
  115. r = char_ >> char_ >> char_ >> char_ >> char_ >> char_;
  116. s.clear();
  117. BOOST_TEST(test("abcdef", r[phx::ref(s) = _1]));
  118. BOOST_TEST(s == "abcdef");
  119. }
  120. return boost::report_errors();
  121. }