debug.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*=============================================================================
  2. Copyright (c) 2001-2015 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. #define BOOST_SPIRIT_X3_DEBUG
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/spirit/home/x3.hpp>
  9. #include <boost/fusion/include/std_pair.hpp>
  10. #include <boost/fusion/include/vector.hpp>
  11. #include <vector>
  12. #include <string>
  13. #include <cstring>
  14. #include <iostream>
  15. #include "test.hpp"
  16. struct my_error_handler
  17. {
  18. template <typename Iterator, typename Exception, typename Context>
  19. boost::spirit::x3::error_handler_result
  20. operator()(Iterator&, Iterator const& last, Exception const& x, Context const&) const
  21. {
  22. std::cout
  23. << "Error! Expecting: "
  24. << x.which()
  25. << ", got: \""
  26. << std::string(x.where(), last)
  27. << "\""
  28. << std::endl;
  29. return boost::spirit::x3::error_handler_result::fail;
  30. }
  31. };
  32. struct my_attribute
  33. {
  34. bool alive = true;
  35. void access() const
  36. {
  37. BOOST_TEST(alive);
  38. }
  39. ~my_attribute()
  40. {
  41. alive = false;
  42. }
  43. friend std::ostream & operator << (std::ostream & os, my_attribute const & attr)
  44. {
  45. attr.access();
  46. return os << "my_attribute";
  47. }
  48. };
  49. int
  50. main()
  51. {
  52. using spirit_test::test_attr;
  53. using spirit_test::test;
  54. using namespace boost::spirit::x3::ascii;
  55. using boost::spirit::x3::rule;
  56. using boost::spirit::x3::symbols;
  57. using boost::spirit::x3::int_;
  58. using boost::spirit::x3::alpha;
  59. { // basic tests
  60. auto a = rule<class a>("a") = 'a';
  61. auto b = rule<class b>("b") = 'b';
  62. auto c = rule<class c>("c") = 'c';
  63. {
  64. auto start = *(a | b | c);
  65. BOOST_TEST(test("abcabcacb", start));
  66. }
  67. {
  68. rule<class start> start("start");
  69. auto start_def =
  70. start = (a | b) >> (start | b);
  71. BOOST_TEST(test("aaaabababaaabbb", start_def));
  72. BOOST_TEST(test("aaaabababaaabba", start_def, false));
  73. }
  74. }
  75. { // basic tests w/ skipper
  76. auto a = rule<class a>("a") = 'a';
  77. auto b = rule<class b>("b") = 'b';
  78. auto c = rule<class c>("c") = 'c';
  79. {
  80. auto start = *(a | b | c);
  81. BOOST_TEST(test(" a b c a b c a c b ", start, space));
  82. }
  83. {
  84. rule<class start> start("start");
  85. auto start_def =
  86. start = (a | b) >> (start | b);
  87. BOOST_TEST(test(" a a a a b a b a b a a a b b b ", start_def, space));
  88. BOOST_TEST(test(" a a a a b a b a b a a a b b a ", start_def, space, false));
  89. }
  90. }
  91. { // std::container attributes
  92. typedef boost::fusion::vector<int, char> fs;
  93. rule<class start, std::vector<fs>> start("start");
  94. auto start_def =
  95. start = *(int_ >> alpha);
  96. BOOST_TEST(test("1 a 2 b 3 c", start_def, space));
  97. }
  98. { // error handling
  99. auto r_def = '(' > int_ > ',' > int_ > ')';
  100. auto r = r_def.on_error(my_error_handler());
  101. BOOST_TEST(test("(123,456)", r));
  102. BOOST_TEST(!test("(abc,def)", r));
  103. BOOST_TEST(!test("(123,456]", r));
  104. BOOST_TEST(!test("(123;456)", r));
  105. BOOST_TEST(!test("[123,456]", r));
  106. }
  107. {
  108. symbols<my_attribute> a{{{ "a", my_attribute{} }}};
  109. auto b = rule<struct b, my_attribute>("b") = a;
  110. my_attribute attr;
  111. BOOST_TEST(test_attr("a", b, attr));
  112. }
  113. return boost::report_errors();
  114. }