comparison DEPENDENCIES/generic/include/boost/spirit/home/x3/nonterminal/simple_trace.hpp @ 102:f46d142149f5

Whoops, finish that update
author Chris Cannam
date Mon, 07 Sep 2015 11:13:41 +0100
parents
children
comparison
equal deleted inserted replaced
101:c530137014c0 102:f46d142149f5
1 /*=============================================================================
2 Copyright (c) 2001-2014 Joel de Guzman
3 Copyright (c) 2001-2011 Hartmut Kaiser
4
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
10
11 #if defined(_MSC_VER)
12 #pragma once
13 #endif
14
15 #include <boost/spirit/home/x3/support/unused.hpp>
16 #include <boost/spirit/home/x3/support/traits/print_token.hpp>
17 #include <boost/spirit/home/x3/support/traits/print_attribute.hpp>
18 #include <boost/spirit/home/x3/nonterminal/debug_handler_state.hpp>
19 #include <boost/fusion/include/out.hpp>
20 #include <boost/type_traits/is_same.hpp>
21 #include <ostream>
22
23 // The stream to use for debug output
24 #if !defined(BOOST_SPIRIT_X3_DEBUG_OUT)
25 #define BOOST_SPIRIT_X3_DEBUG_OUT std::cerr
26 #endif
27
28 // number of tokens to print while debugging
29 #if !defined(BOOST_SPIRIT_X3_DEBUG_PRINT_SOME)
30 #define BOOST_SPIRIT_X3_DEBUG_PRINT_SOME 20
31 #endif
32
33 // number of spaces to indent
34 #if !defined(BOOST_SPIRIT_X3_DEBUG_INDENT)
35 #define BOOST_SPIRIT_X3_DEBUG_INDENT 2
36 #endif
37
38 namespace boost { namespace spirit { namespace x3
39 {
40 namespace detail
41 {
42 template <typename Char>
43 inline void token_printer(std::ostream& o, Char c)
44 {
45 // allow customization of the token printer routine
46 x3::traits::print_token(o, c);
47 }
48 }
49
50 template <int IndentSpaces = 2, int CharsToPrint = 20>
51 struct simple_trace
52 {
53 simple_trace(std::ostream& out)
54 : out(out), indent(0) {}
55
56 void print_indent(int n) const
57 {
58 n *= IndentSpaces;
59 for (int i = 0; i != n; ++i)
60 out << ' ';
61 }
62
63 template <typename Iterator>
64 void print_some(
65 char const* tag
66 , Iterator first, Iterator const& last) const
67 {
68 print_indent(indent);
69 out << '<' << tag << '>';
70 int const n = CharsToPrint;
71 for (int i = 0; first != last && i != n && *first; ++i, ++first)
72 detail::token_printer(out, *first);
73 out << "</" << tag << '>' << std::endl;
74
75 // $$$ FIXME convert invalid xml characters (e.g. '<') to valid
76 // character entities. $$$
77 }
78
79 template <typename Iterator, typename Attribute, typename State>
80 void operator()(
81 Iterator const& first
82 , Iterator const& last
83 , Attribute const& attr
84 , State state
85 , std::string const& rule_name) const
86 {
87 switch (state)
88 {
89 case pre_parse:
90 print_indent(indent++);
91 out
92 << '<' << rule_name << '>'
93 << std::endl;
94 print_some("try", first, last);
95 break;
96
97 case successful_parse:
98 print_some("success", first, last);
99 if (!is_same<Attribute, unused_type>::value)
100 {
101 print_indent(indent);
102 out
103 << "<attributes>";
104 traits::print_attribute(out, attr);
105 out
106 << "</attributes>";
107 out << std::endl;
108 }
109 //~ if (!fusion::empty(context.locals))
110 //~ out
111 //~ << "<locals>"
112 //~ << context.locals
113 //~ << "</locals>";
114 print_indent(--indent);
115 out
116 << "</" << rule_name << '>'
117 << std::endl;
118 break;
119
120 case failed_parse:
121 print_indent(indent);
122 out << "<fail/>" << std::endl;
123 print_indent(--indent);
124 out
125 << "</" << rule_name << '>'
126 << std::endl;
127 break;
128 }
129 }
130
131 std::ostream& out;
132 mutable int indent;
133 };
134
135 namespace detail
136 {
137 typedef simple_trace<
138 BOOST_SPIRIT_X3_DEBUG_INDENT, BOOST_SPIRIT_X3_DEBUG_PRINT_SOME>
139 simple_trace_type;
140
141 inline simple_trace_type&
142 get_simple_trace()
143 {
144 static simple_trace_type tracer(BOOST_SPIRIT_X3_DEBUG_OUT);
145 return tracer;
146 }
147 }
148 }}}
149
150 #endif