1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ==============================================================================*/
8 #if !defined(BOOST_SPIRIT_SIMPLE_TRACE_DECEMBER_06_2008_1102AM)
9 #define BOOST_SPIRIT_SIMPLE_TRACE_DECEMBER_06_2008_1102AM
15 #include <boost/spirit/home/support/unused.hpp>
16 #include <boost/spirit/home/qi/nonterminal/debug_handler_state.hpp>
17 #include <boost/fusion/include/out.hpp>
19 #include <boost/mpl/if.hpp>
20 #include <boost/mpl/and.hpp>
21 #include <boost/type_traits/is_convertible.hpp>
22 #include <boost/spirit/home/support/attributes.hpp>
24 // The stream to use for debug output
25 #if !defined(BOOST_SPIRIT_DEBUG_OUT)
26 #define BOOST_SPIRIT_DEBUG_OUT std::cerr
29 // number of tokens to print while debugging
30 #if !defined(BOOST_SPIRIT_DEBUG_PRINT_SOME)
31 #define BOOST_SPIRIT_DEBUG_PRINT_SOME 20
34 // number of spaces to indent
35 #if !defined(BOOST_SPIRIT_DEBUG_INDENT)
36 #define BOOST_SPIRIT_DEBUG_INDENT 2
39 namespace boost { namespace spirit { namespace qi
43 template<typename Char>
44 inline void token_printer(std::ostream& o, Char c)
46 // allow to customize the token printer routine
47 spirit::traits::print_token(o, c);
53 int& get_indent() const
55 static int indent = 0;
59 void print_indent(int n) const
61 n *= BOOST_SPIRIT_DEBUG_INDENT;
62 for (int i = 0; i != n; ++i)
63 BOOST_SPIRIT_DEBUG_OUT << ' ';
66 template <typename Iterator>
70 , Iterator first, Iterator const& last) const
72 print_indent(get_indent());
73 BOOST_SPIRIT_DEBUG_OUT << '<' << tag << '>';
74 int const n = BOOST_SPIRIT_DEBUG_PRINT_SOME;
75 for (int i = 0; first != last && i != n && *first; ++i, ++first)
76 detail::token_printer(BOOST_SPIRIT_DEBUG_OUT, *first);
77 BOOST_SPIRIT_DEBUG_OUT << "</" << tag << '>' << std::endl;
79 // $$$ FIXME convert invalid xml characters (e.g. '<') to valid
80 // character entities. $$$
83 template <typename Iterator, typename Context, typename State>
86 , Iterator const& last
87 , Context const& context
89 , std::string const& rule_name) const
94 print_indent(get_indent()++);
95 BOOST_SPIRIT_DEBUG_OUT
96 << '<' << rule_name << '>'
98 print_some("try", get_indent(), first, last);
100 case successful_parse:
101 print_some("success", get_indent(), first, last);
102 print_indent(get_indent());
103 BOOST_SPIRIT_DEBUG_OUT
105 traits::print_attribute(
106 BOOST_SPIRIT_DEBUG_OUT,
109 BOOST_SPIRIT_DEBUG_OUT
111 if (!fusion::empty(context.locals))
112 BOOST_SPIRIT_DEBUG_OUT
116 BOOST_SPIRIT_DEBUG_OUT << std::endl;
117 print_indent(--get_indent());
118 BOOST_SPIRIT_DEBUG_OUT
119 << "</" << rule_name << '>'
123 print_indent(get_indent());
124 BOOST_SPIRIT_DEBUG_OUT << "<fail/>" << std::endl;
125 print_indent(--get_indent());
126 BOOST_SPIRIT_DEBUG_OUT
127 << "</" << rule_name << '>'