debug_handler.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. namespace boost { namespace spirit { namespace karma
  16. {
  17. template <
  18. typename OutputIterator, typename Context, typename Delimiter
  19. , typename Properties, typename F>
  20. struct debug_handler
  21. {
  22. typedef detail::output_iterator<OutputIterator, Properties>
  23. output_iterator;
  24. typedef detail::enable_buffering<output_iterator> buffer_type;
  25. typedef function<bool(output_iterator&, Context&, Delimiter const&)>
  26. function_type;
  27. debug_handler(function_type subject, F f, std::string const& rule_name)
  28. : subject(subject)
  29. , f(f)
  30. , rule_name(rule_name)
  31. {}
  32. bool operator()(output_iterator& sink, Context& context
  33. , Delimiter const& delim) const
  34. {
  35. buffer_type buffer(sink);
  36. bool r = false;
  37. f (sink, context, pre_generate, rule_name, buffer);
  38. {
  39. detail::disable_counting<output_iterator> nocount(sink);
  40. r = subject(sink, context, delim);
  41. }
  42. if (r)
  43. {
  44. f (sink, context, successful_generate, rule_name, buffer);
  45. buffer.buffer_copy();
  46. return true;
  47. }
  48. f (sink, context, failed_generate, rule_name, buffer);
  49. return false;
  50. }
  51. function_type subject;
  52. F f;
  53. std::string rule_name;
  54. };
  55. template <typename OutputIterator
  56. , typename T1, typename T2, typename T3, typename T4, typename F>
  57. void debug(rule<OutputIterator, T1, T2, T3, T4>& r, F f)
  58. {
  59. typedef rule<OutputIterator, T1, T2, T3, T4> rule_type;
  60. typedef
  61. debug_handler<
  62. OutputIterator
  63. , typename rule_type::context_type
  64. , typename rule_type::delimiter_type
  65. , typename rule_type::properties
  66. , F>
  67. debug_handler;
  68. r.f = debug_handler(r.f, f, r.name());
  69. }
  70. struct simple_trace;
  71. namespace detail
  72. {
  73. // This class provides an extra level of indirection through a
  74. // template to produce the simple_trace type. This way, the use
  75. // of simple_trace below is hidden behind a dependent type, so
  76. // that compilers eagerly type-checking template definitions
  77. // won't complain that simple_trace is incomplete.
  78. template<typename T>
  79. struct get_simple_trace
  80. {
  81. typedef simple_trace type;
  82. };
  83. }
  84. template <typename OutputIterator
  85. , typename T1, typename T2, typename T3, typename T4>
  86. void debug(rule<OutputIterator, T1, T2, T3, T4>& r)
  87. {
  88. typedef rule<OutputIterator, T1, T2, T3, T4> rule_type;
  89. typedef
  90. debug_handler<
  91. OutputIterator
  92. , typename rule_type::context_type
  93. , typename rule_type::delimiter_type
  94. , typename rule_type::properties
  95. , simple_trace>
  96. debug_handler;
  97. typedef typename karma::detail::get_simple_trace<OutputIterator>::type
  98. trace;
  99. r.f = debug_handler(r.f, trace(), r.name());
  100. }
  101. }}}
  102. ///////////////////////////////////////////////////////////////////////////////
  103. // Utility macro for easy enabling of rule and grammar debugging
  104. #if !defined(BOOST_SPIRIT_DEBUG_NODE)
  105. #if defined(BOOST_SPIRIT_KARMA_DEBUG)
  106. #define BOOST_SPIRIT_DEBUG_NODE(r) r.name(#r); debug(r)
  107. #else
  108. #define BOOST_SPIRIT_DEBUG_NODE(r) r.name(#r);
  109. #endif
  110. #endif
  111. #endif