9
3

debug_handler.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. // Copyright (c) 2001-2011 Joel de Guzman
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #if !defined(BOOST_SPIRIT_KARMA_DEBUG_HANDLER_APR_21_2010_0148PM)
  7. #define BOOST_SPIRIT_KARMA_DEBUG_HANDLER_APR_21_2010_0148PM
  8. #if defined(_MSC_VER)
  9. #pragma once
  10. #endif
  11. #include <boost/spirit/home/support/unused.hpp>
  12. #include <boost/spirit/home/karma/nonterminal/rule.hpp>
  13. #include <boost/spirit/home/karma/nonterminal/debug_handler_state.hpp>
  14. #include <boost/function.hpp>
  15. #include <boost/fusion/include/at.hpp>
  16. #include <boost/fusion/include/vector.hpp>
  17. #include <boost/fusion/include/out.hpp>
  18. #include <iostream>
  19. namespace boost { namespace spirit { namespace karma
  20. {
  21. template <
  22. typename OutputIterator, typename Context, typename Delimiter
  23. , typename Properties, typename F>
  24. struct debug_handler
  25. {
  26. typedef detail::output_iterator<OutputIterator, Properties>
  27. output_iterator;
  28. typedef detail::enable_buffering<output_iterator> buffer_type;
  29. typedef function<bool(output_iterator&, Context&, Delimiter const&)>
  30. function_type;
  31. debug_handler(function_type subject, F f, std::string const& rule_name)
  32. : subject(subject)
  33. , f(f)
  34. , rule_name(rule_name)
  35. {}
  36. bool operator()(output_iterator& sink, Context& context
  37. , Delimiter const& delim) const
  38. {
  39. buffer_type buffer(sink);
  40. bool r = false;
  41. f (sink, context, pre_generate, rule_name, buffer);
  42. {
  43. detail::disable_counting<output_iterator> nocount(sink);
  44. r = subject(sink, context, delim);
  45. }
  46. if (r)
  47. {
  48. f (sink, context, successful_generate, rule_name, buffer);
  49. buffer.buffer_copy();
  50. return true;
  51. }
  52. f (sink, context, failed_generate, rule_name, buffer);
  53. return false;
  54. }
  55. function_type subject;
  56. F f;
  57. std::string rule_name;
  58. };
  59. template <typename OutputIterator
  60. , typename T1, typename T2, typename T3, typename T4, typename F>
  61. void debug(rule<OutputIterator, T1, T2, T3, T4>& r, F f)
  62. {
  63. typedef rule<OutputIterator, T1, T2, T3, T4> rule_type;
  64. typedef
  65. debug_handler<
  66. OutputIterator
  67. , typename rule_type::context_type
  68. , typename rule_type::delimiter_type
  69. , typename rule_type::properties
  70. , F>
  71. debug_handler;
  72. r.f = debug_handler(r.f, f, r.name());
  73. }
  74. struct simple_trace;
  75. namespace detail
  76. {
  77. // This class provides an extra level of indirection through a
  78. // template to produce the simple_trace type. This way, the use
  79. // of simple_trace below is hidden behind a dependent type, so
  80. // that compilers eagerly type-checking template definitions
  81. // won't complain that simple_trace is incomplete.
  82. template<typename T>
  83. struct get_simple_trace
  84. {
  85. typedef simple_trace type;
  86. };
  87. }
  88. template <typename OutputIterator
  89. , typename T1, typename T2, typename T3, typename T4>
  90. void debug(rule<OutputIterator, T1, T2, T3, T4>& r)
  91. {
  92. typedef rule<OutputIterator, T1, T2, T3, T4> rule_type;
  93. typedef
  94. debug_handler<
  95. OutputIterator
  96. , typename rule_type::context_type
  97. , typename rule_type::delimiter_type
  98. , typename rule_type::properties
  99. , simple_trace>
  100. debug_handler;
  101. typedef typename karma::detail::get_simple_trace<OutputIterator>::type
  102. trace;
  103. r.f = debug_handler(r.f, trace(), r.name());
  104. }
  105. }}}
  106. ///////////////////////////////////////////////////////////////////////////////
  107. // Utility macro for easy enabling of rule and grammar debugging
  108. #if !defined(BOOST_SPIRIT_DEBUG_NODE)
  109. #if defined(BOOST_SPIRIT_KARMA_DEBUG)
  110. #define BOOST_SPIRIT_DEBUG_NODE(r) r.name(#r); debug(r)
  111. #else
  112. #define BOOST_SPIRIT_DEBUG_NODE(r) r.name(#r);
  113. #endif
  114. #endif
  115. #endif