debug.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*=============================================================================
  2. Copyright (c) 2001-2010 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_DEBUG
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/spirit/include/qi_operator.hpp>
  9. #include <boost/spirit/include/qi_char.hpp>
  10. #include <boost/spirit/include/qi_string.hpp>
  11. #include <boost/spirit/include/qi_numeric.hpp>
  12. #include <boost/spirit/include/qi_auxiliary.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/spirit/include/phoenix_object.hpp>
  18. #include <boost/spirit/include/phoenix_bind.hpp>
  19. #include <boost/fusion/include/std_pair.hpp>
  20. #include <string>
  21. #include <cstring>
  22. #include <iostream>
  23. #include "test.hpp"
  24. int
  25. main()
  26. {
  27. using spirit_test::test_attr;
  28. using spirit_test::test;
  29. using namespace boost::spirit::ascii;
  30. using namespace boost::spirit::qi::labels;
  31. using boost::spirit::qi::locals;
  32. using boost::spirit::qi::rule;
  33. using boost::spirit::qi::int_;
  34. using boost::spirit::qi::fail;
  35. using boost::spirit::qi::on_error;
  36. using boost::spirit::qi::debug;
  37. using boost::spirit::qi::alpha;
  38. namespace phx = boost::phoenix;
  39. { // basic tests
  40. rule<char const*> a, b, c, start;
  41. a = 'a';
  42. b = 'b';
  43. c = 'c';
  44. BOOST_SPIRIT_DEBUG_NODE(a);
  45. BOOST_SPIRIT_DEBUG_NODE(b);
  46. BOOST_SPIRIT_DEBUG_NODE(c);
  47. start = *(a | b | c);
  48. BOOST_SPIRIT_DEBUG_NODE(start);
  49. BOOST_TEST(test("abcabcacb", start));
  50. start = (a | b) >> (start | b);
  51. BOOST_SPIRIT_DEBUG_NODE(start);
  52. BOOST_TEST(test("aaaabababaaabbb", start));
  53. BOOST_TEST(test("aaaabababaaabba", start, false));
  54. // ignore the skipper!
  55. BOOST_TEST(test("aaaabababaaabba", start, space, false));
  56. }
  57. { // basic tests w/ skipper
  58. rule<char const*, space_type> a, b, c, start;
  59. a = 'a';
  60. b = 'b';
  61. c = 'c';
  62. BOOST_SPIRIT_DEBUG_NODE(a);
  63. BOOST_SPIRIT_DEBUG_NODE(b);
  64. BOOST_SPIRIT_DEBUG_NODE(c);
  65. start = *(a | b | c);
  66. BOOST_SPIRIT_DEBUG_NODE(start);
  67. BOOST_TEST(test(" a b c a b c a c b ", start, space));
  68. start = (a | b) >> (start | b);
  69. BOOST_SPIRIT_DEBUG_NODE(start);
  70. BOOST_TEST(test(" a a a a b a b a b a a a b b b ", start, space));
  71. BOOST_TEST(test(" a a a a b a b a b a a a b b a ", start, space, false));
  72. }
  73. { // std::container attributes
  74. typedef boost::fusion::vector<int, char> fs;
  75. rule<char const*, std::vector<fs>(), space_type> start;
  76. start = *(int_ >> alpha);
  77. BOOST_SPIRIT_DEBUG_NODE(start);
  78. BOOST_TEST(test("1 a 2 b 3 c", start, space));
  79. }
  80. { // error handling
  81. using namespace boost::spirit::ascii;
  82. using boost::phoenix::construct;
  83. using boost::phoenix::bind;
  84. rule<char const*> r;
  85. r = '(' > int_ > ',' > int_ > ')';
  86. on_error<fail>
  87. (
  88. r, std::cout
  89. << phx::val("Error! Expecting: ")
  90. << _4
  91. << phx::val(", got: \"")
  92. << construct<std::string>(_3, _2)
  93. << phx::val("\"")
  94. << std::endl
  95. );
  96. BOOST_SPIRIT_DEBUG_NODE(r);
  97. BOOST_TEST(test("(123,456)", r));
  98. BOOST_TEST(!test("(abc,def)", r));
  99. BOOST_TEST(!test("(123,456]", r));
  100. BOOST_TEST(!test("(123;456)", r));
  101. BOOST_TEST(!test("[123,456]", r));
  102. }
  103. return boost::report_errors();
  104. }