File indexing completed on 2025-01-31 10:02:36
0001
0002
0003
0004
0005
0006
0007
0008 #if !defined(BOOST_SPIRIT_X3_SIMPLE_TRACE_DECEMBER_06_2008_1102AM)
0009 #define BOOST_SPIRIT_X3_SIMPLE_TRACE_DECEMBER_06_2008_1102AM
0010
0011 #include <boost/spirit/home/x3/support/unused.hpp>
0012 #include <boost/spirit/home/x3/support/traits/print_token.hpp>
0013 #include <boost/spirit/home/x3/support/traits/print_attribute.hpp>
0014 #include <boost/spirit/home/x3/nonterminal/debug_handler_state.hpp>
0015 #include <boost/type_traits/is_same.hpp>
0016 #include <iostream>
0017
0018
0019 #if !defined(BOOST_SPIRIT_X3_DEBUG_OUT)
0020 #define BOOST_SPIRIT_X3_DEBUG_OUT std::cerr
0021 #endif
0022
0023
0024 #if !defined(BOOST_SPIRIT_X3_DEBUG_PRINT_SOME)
0025 #define BOOST_SPIRIT_X3_DEBUG_PRINT_SOME 20
0026 #endif
0027
0028
0029 #if !defined(BOOST_SPIRIT_X3_DEBUG_INDENT)
0030 #define BOOST_SPIRIT_X3_DEBUG_INDENT 2
0031 #endif
0032
0033 namespace boost { namespace spirit { namespace x3
0034 {
0035 namespace detail
0036 {
0037 template <typename Char>
0038 inline void token_printer(std::ostream& o, Char c)
0039 {
0040
0041 x3::traits::print_token(o, c);
0042 }
0043 }
0044
0045 template <int IndentSpaces = 2, int CharsToPrint = 20>
0046 struct simple_trace
0047 {
0048 simple_trace(std::ostream& out)
0049 : out(out), indent(0) {}
0050
0051 void print_indent(int n) const
0052 {
0053 n *= IndentSpaces;
0054 for (int i = 0; i != n; ++i)
0055 out << ' ';
0056 }
0057
0058 template <typename Iterator>
0059 void print_some(
0060 char const* tag
0061 , Iterator first, Iterator const& last) const
0062 {
0063 print_indent(indent);
0064 out << '<' << tag << '>';
0065 int const n = CharsToPrint;
0066 for (int i = 0; first != last && i != n && *first; ++i, ++first)
0067 detail::token_printer(out, *first);
0068 out << "</" << tag << '>' << std::endl;
0069
0070
0071
0072 }
0073
0074 template <typename Iterator, typename Attribute, typename State>
0075 void operator()(
0076 Iterator const& first
0077 , Iterator const& last
0078 , Attribute const& attr
0079 , State state
0080 , std::string const& rule_name) const
0081 {
0082 switch (state)
0083 {
0084 case pre_parse:
0085 print_indent(indent++);
0086 out
0087 << '<' << rule_name << '>'
0088 << std::endl;
0089 print_some("try", first, last);
0090 break;
0091
0092 case successful_parse:
0093 print_some("success", first, last);
0094 if (!is_same<Attribute, unused_type>::value)
0095 {
0096 print_indent(indent);
0097 out
0098 << "<attributes>";
0099 traits::print_attribute(out, attr);
0100 out
0101 << "</attributes>";
0102 out << std::endl;
0103 }
0104 print_indent(--indent);
0105 out
0106 << "</" << rule_name << '>'
0107 << std::endl;
0108 break;
0109
0110 case failed_parse:
0111 print_indent(indent);
0112 out << "<fail/>" << std::endl;
0113 print_indent(--indent);
0114 out
0115 << "</" << rule_name << '>'
0116 << std::endl;
0117 break;
0118 }
0119 }
0120
0121 std::ostream& out;
0122 mutable int indent;
0123 };
0124
0125 namespace detail
0126 {
0127 typedef simple_trace<
0128 BOOST_SPIRIT_X3_DEBUG_INDENT, BOOST_SPIRIT_X3_DEBUG_PRINT_SOME>
0129 simple_trace_type;
0130
0131 inline simple_trace_type&
0132 get_simple_trace()
0133 {
0134 static simple_trace_type tracer(BOOST_SPIRIT_X3_DEBUG_OUT);
0135 return tracer;
0136 }
0137 }
0138 }}}
0139
0140 #endif