test.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #if !defined(BOOST_SPIRIT_TEST_FEBRUARY_01_2007_0605PM)
  7. #define BOOST_SPIRIT_TEST_FEBRUARY_01_2007_0605PM
  8. #include <boost/spirit/include/qi_parse.hpp>
  9. #include <boost/spirit/include/qi_what.hpp>
  10. #include <boost/variant/apply_visitor.hpp>
  11. #include <iostream>
  12. namespace spirit_test
  13. {
  14. template <typename Char, typename Parser>
  15. bool test(Char const* in, Parser const& p, bool full_match = true)
  16. {
  17. // we don't care about the result of the "what" function.
  18. // we only care that all parsers have it:
  19. boost::spirit::qi::what(p);
  20. Char const* last = in;
  21. while (*last)
  22. last++;
  23. return boost::spirit::qi::parse(in, last, p)
  24. && (!full_match || (in == last));
  25. }
  26. template <typename Char, typename Parser, typename Skipper>
  27. bool test(Char const* in, Parser const& p
  28. , Skipper const& s, bool full_match = true)
  29. {
  30. // we don't care about the result of the "what" function.
  31. // we only care that all parsers have it:
  32. boost::spirit::qi::what(p);
  33. Char const* last = in;
  34. while (*last)
  35. last++;
  36. return boost::spirit::qi::phrase_parse(in, last, p, s)
  37. && (!full_match || (in == last));
  38. }
  39. template <typename Char, typename Parser>
  40. bool binary_test(Char const* in, std::size_t size, Parser const& p,
  41. bool full_match = true)
  42. {
  43. // we don't care about the result of the "what" function.
  44. // we only care that all parsers have it:
  45. boost::spirit::qi::what(p);
  46. Char const* last = in + size;
  47. return boost::spirit::qi::parse(in, last, p)
  48. && (!full_match || (in == last));
  49. }
  50. template <typename Char, typename Parser, typename Skipper>
  51. bool binary_test(Char const* in, std::size_t size, Parser const& p,
  52. Skipper const& s, bool full_match = true)
  53. {
  54. // we don't care about the result of the "what" function.
  55. // we only care that all parsers have it:
  56. boost::spirit::qi::what(p);
  57. Char const* last = in + size;
  58. return boost::spirit::qi::phrase_parse(in, last, p, s)
  59. && (!full_match || (in == last));
  60. }
  61. template <typename Char, typename Parser, typename Attr>
  62. bool test_attr(Char const* in, Parser const& p
  63. , Attr& attr, bool full_match = true)
  64. {
  65. // we don't care about the result of the "what" function.
  66. // we only care that all parsers have it:
  67. boost::spirit::qi::what(p);
  68. Char const* last = in;
  69. while (*last)
  70. last++;
  71. return boost::spirit::qi::parse(in, last, p, attr)
  72. && (!full_match || (in == last));
  73. }
  74. template <typename Char, typename Parser, typename Attr, typename Skipper>
  75. bool test_attr(Char const* in, Parser const& p
  76. , Attr& attr, Skipper const& s, bool full_match = true)
  77. {
  78. // we don't care about the result of the "what" function.
  79. // we only care that all parsers have it:
  80. boost::spirit::qi::what(p);
  81. Char const* last = in;
  82. while (*last)
  83. last++;
  84. return boost::spirit::qi::phrase_parse(in, last, p, s, attr)
  85. && (!full_match || (in == last));
  86. }
  87. template <typename Char, typename Parser, typename Attr>
  88. bool binary_test_attr(Char const* in, std::size_t size, Parser const& p,
  89. Attr& attr, bool full_match = true)
  90. {
  91. // we don't care about the result of the "what" function.
  92. // we only care that all parsers have it:
  93. boost::spirit::qi::what(p);
  94. Char const* last = in + size;
  95. return boost::spirit::qi::parse(in, last, p, attr)
  96. && (!full_match || (in == last));
  97. }
  98. template <typename Char, typename Parser, typename Attr, typename Skipper>
  99. bool binary_test_attr(Char const* in, std::size_t size, Parser const& p,
  100. Attr& attr, Skipper const& s, bool full_match = true)
  101. {
  102. // we don't care about the result of the "what" function.
  103. // we only care that all parsers have it:
  104. boost::spirit::qi::what(p);
  105. Char const* last = in + size;
  106. return boost::spirit::qi::phrase_parse(in, last, p, s, attr)
  107. && (!full_match || (in == last));
  108. }
  109. struct printer
  110. {
  111. typedef boost::spirit::utf8_string string;
  112. void element(string const& tag, string const& value, int depth) const
  113. {
  114. for (int i = 0; i < (depth*4); ++i) // indent to depth
  115. std::cout << ' ';
  116. std::cout << "tag: " << tag;
  117. if (value != "")
  118. std::cout << ", value: " << value;
  119. std::cout << std::endl;
  120. }
  121. };
  122. void print_info(boost::spirit::info const& what)
  123. {
  124. using boost::spirit::basic_info_walker;
  125. printer pr;
  126. basic_info_walker<printer> walker(pr, what.tag, 0);
  127. boost::apply_visitor(walker, what.value);
  128. }
  129. }
  130. #endif