Chris@16: /*============================================================================= Chris@16: Copyright (c) 2001-2011 Joel de Guzman Chris@16: Copyright (c) 2001-2011 Hartmut Kaiser Chris@16: Chris@16: Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: ==============================================================================*/ Chris@16: #if !defined(BOOST_SPIRIT_SIMPLE_TRACE_DECEMBER_06_2008_1102AM) Chris@16: #define BOOST_SPIRIT_SIMPLE_TRACE_DECEMBER_06_2008_1102AM Chris@16: Chris@16: #if defined(_MSC_VER) Chris@16: #pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: // The stream to use for debug output Chris@16: #if !defined(BOOST_SPIRIT_DEBUG_OUT) Chris@16: #define BOOST_SPIRIT_DEBUG_OUT std::cerr Chris@16: #endif Chris@16: Chris@16: // number of tokens to print while debugging Chris@16: #if !defined(BOOST_SPIRIT_DEBUG_PRINT_SOME) Chris@16: #define BOOST_SPIRIT_DEBUG_PRINT_SOME 20 Chris@16: #endif Chris@16: Chris@16: // number of spaces to indent Chris@16: #if !defined(BOOST_SPIRIT_DEBUG_INDENT) Chris@16: #define BOOST_SPIRIT_DEBUG_INDENT 2 Chris@16: #endif Chris@16: Chris@16: namespace boost { namespace spirit { namespace qi Chris@16: { Chris@16: namespace detail Chris@16: { Chris@16: template Chris@16: inline void token_printer(std::ostream& o, Char c) Chris@16: { Chris@16: // allow to customize the token printer routine Chris@16: spirit::traits::print_token(o, c); Chris@16: } Chris@16: } Chris@16: Chris@16: struct simple_trace Chris@16: { Chris@16: int& get_indent() const Chris@16: { Chris@16: static int indent = 0; Chris@16: return indent; Chris@16: } Chris@16: Chris@16: void print_indent(int n) const Chris@16: { Chris@16: n *= BOOST_SPIRIT_DEBUG_INDENT; Chris@16: for (int i = 0; i != n; ++i) Chris@16: BOOST_SPIRIT_DEBUG_OUT << ' '; Chris@16: } Chris@16: Chris@16: template Chris@16: void print_some( Chris@16: char const* tag Chris@16: , int /*indent*/ Chris@16: , Iterator first, Iterator const& last) const Chris@16: { Chris@16: print_indent(get_indent()); Chris@16: BOOST_SPIRIT_DEBUG_OUT << '<' << tag << '>'; Chris@16: int const n = BOOST_SPIRIT_DEBUG_PRINT_SOME; Chris@16: for (int i = 0; first != last && i != n && *first; ++i, ++first) Chris@16: detail::token_printer(BOOST_SPIRIT_DEBUG_OUT, *first); Chris@16: BOOST_SPIRIT_DEBUG_OUT << "' << std::endl; Chris@16: Chris@16: // $$$ FIXME convert invalid xml characters (e.g. '<') to valid Chris@16: // character entities. $$$ Chris@16: } Chris@16: Chris@16: template Chris@16: void operator()( Chris@16: Iterator const& first Chris@16: , Iterator const& last Chris@16: , Context const& context Chris@16: , State state Chris@16: , std::string const& rule_name) const Chris@16: { Chris@16: switch (state) Chris@16: { Chris@16: case pre_parse: Chris@16: print_indent(get_indent()++); Chris@16: BOOST_SPIRIT_DEBUG_OUT Chris@16: << '<' << rule_name << '>' Chris@16: << std::endl; Chris@16: print_some("try", get_indent(), first, last); Chris@16: break; Chris@16: case successful_parse: Chris@16: print_some("success", get_indent(), first, last); Chris@16: print_indent(get_indent()); Chris@16: BOOST_SPIRIT_DEBUG_OUT Chris@16: << ""; Chris@16: traits::print_attribute( Chris@16: BOOST_SPIRIT_DEBUG_OUT, Chris@16: context.attributes Chris@16: ); Chris@16: BOOST_SPIRIT_DEBUG_OUT Chris@16: << ""; Chris@16: if (!fusion::empty(context.locals)) Chris@16: BOOST_SPIRIT_DEBUG_OUT Chris@16: << "" Chris@16: << context.locals Chris@16: << ""; Chris@16: BOOST_SPIRIT_DEBUG_OUT << std::endl; Chris@16: print_indent(--get_indent()); Chris@16: BOOST_SPIRIT_DEBUG_OUT Chris@16: << "' Chris@16: << std::endl; Chris@16: break; Chris@16: case failed_parse: Chris@16: print_indent(get_indent()); Chris@16: BOOST_SPIRIT_DEBUG_OUT << "" << std::endl; Chris@16: print_indent(--get_indent()); Chris@16: BOOST_SPIRIT_DEBUG_OUT Chris@16: << "' Chris@16: << std::endl; Chris@16: break; Chris@16: } Chris@16: } Chris@16: }; Chris@16: }}} Chris@16: Chris@16: #endif