1 /*=============================================================================
2 Copyright (c) 2001-2014 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_X3_SIMPLE_TRACE_DECEMBER_06_2008_1102AM)
9 #define BOOST_SPIRIT_X3_SIMPLE_TRACE_DECEMBER_06_2008_1102AM
11 #include <boost/spirit/home/x3/support/unused.hpp>
12 #include <boost/spirit/home/x3/support/traits/print_token.hpp>
13 #include <boost/spirit/home/x3/support/traits/print_attribute.hpp>
14 #include <boost/spirit/home/x3/nonterminal/debug_handler_state.hpp>
15 #include <boost/fusion/include/out.hpp>
16 #include <boost/type_traits/is_same.hpp>
19 // The stream to use for debug output
20 #if !defined(BOOST_SPIRIT_X3_DEBUG_OUT)
21 #define BOOST_SPIRIT_X3_DEBUG_OUT std::cerr
24 // number of tokens to print while debugging
25 #if !defined(BOOST_SPIRIT_X3_DEBUG_PRINT_SOME)
26 #define BOOST_SPIRIT_X3_DEBUG_PRINT_SOME 20
29 // number of spaces to indent
30 #if !defined(BOOST_SPIRIT_X3_DEBUG_INDENT)
31 #define BOOST_SPIRIT_X3_DEBUG_INDENT 2
34 namespace boost { namespace spirit { namespace x3
38 template <typename Char>
39 inline void token_printer(std::ostream& o, Char c)
41 // allow customization of the token printer routine
42 x3::traits::print_token(o, c);
46 template <int IndentSpaces = 2, int CharsToPrint = 20>
49 simple_trace(std::ostream& out)
50 : out(out), indent(0) {}
52 void print_indent(int n) const
55 for (int i = 0; i != n; ++i)
59 template <typename Iterator>
62 , Iterator first, Iterator const& last) const
65 out << '<' << tag << '>';
66 int const n = CharsToPrint;
67 for (int i = 0; first != last && i != n && *first; ++i, ++first)
68 detail::token_printer(out, *first);
69 out << "</" << tag << '>' << std::endl;
71 // $$$ FIXME convert invalid xml characters (e.g. '<') to valid
72 // character entities. $$$
75 template <typename Iterator, typename Attribute, typename State>
78 , Iterator const& last
79 , Attribute const& attr
81 , std::string const& rule_name) const
86 print_indent(indent++);
88 << '<' << rule_name << '>'
90 print_some("try", first, last);
93 case successful_parse:
94 print_some("success", first, last);
95 if (!is_same<Attribute, unused_type>::value)
100 traits::print_attribute(out, attr);
105 //~ if (!fusion::empty(context.locals))
108 //~ << context.locals
110 print_indent(--indent);
112 << "</" << rule_name << '>'
117 print_indent(indent);
118 out << "<fail/>" << std::endl;
119 print_indent(--indent);
121 << "</" << rule_name << '>'
133 typedef simple_trace<
134 BOOST_SPIRIT_X3_DEBUG_INDENT, BOOST_SPIRIT_X3_DEBUG_PRINT_SOME>
137 inline simple_trace_type&
140 static simple_trace_type tracer(BOOST_SPIRIT_X3_DEBUG_OUT);