Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r56446 - trunk/boost/spirit/home/qi/nonterminal
From: hartmut.kaiser_at_[hidden]
Date: 2009-09-27 17:09:20


Author: hkaiser
Date: 2009-09-27 17:09:19 EDT (Sun, 27 Sep 2009)
New Revision: 56446
URL: http://svn.boost.org/trac/boost/changeset/56446

Log:
Spirit: added simple token printer for parser trace output
Text files modified:
   trunk/boost/spirit/home/qi/nonterminal/simple_trace.hpp | 63 +++++++++++++++++++++++++++++++++++++++
   1 files changed, 62 insertions(+), 1 deletions(-)

Modified: trunk/boost/spirit/home/qi/nonterminal/simple_trace.hpp
==============================================================================
--- trunk/boost/spirit/home/qi/nonterminal/simple_trace.hpp (original)
+++ trunk/boost/spirit/home/qi/nonterminal/simple_trace.hpp 2009-09-27 17:09:19 EDT (Sun, 27 Sep 2009)
@@ -32,6 +32,67 @@
 
 namespace boost { namespace spirit { namespace qi
 {
+ namespace detail
+ {
+ struct token_printer_aux_for_chars
+ {
+ template<typename Char>
+ static void print(std::ostream& o, Char c)
+ {
+ using namespace std; // allow for ADL to find the proper iscntrl
+
+ if (c == static_cast<Char>('\a'))
+ o << "\\a";
+ else if (c == static_cast<Char>('\b'))
+ o << "\\b";
+ else if (c == static_cast<Char>('\f'))
+ o << "\\f";
+ else if (c == static_cast<Char>('\n'))
+ o << "\\n";
+ else if (c == static_cast<Char>('\r'))
+ o << "\\r";
+ else if (c == static_cast<Char>('\t'))
+ o << "\\t";
+ else if (c == static_cast<Char>('\v'))
+ o << "\\v";
+ else if (iscntrl(c))
+ o << "\\" << std::oct << static_cast<int>(c);
+ else
+ o << static_cast<char>(c);
+ }
+ };
+
+ // for token types where the comparison with char constants wouldn't work
+ struct token_printer_aux_for_other_types
+ {
+ template<typename Char>
+ static void print(std::ostream& o, Char c)
+ {
+ o << c;
+ }
+ };
+
+ template <typename Char>
+ struct token_printer_aux
+ : mpl::if_<
+ mpl::and_<
+ is_convertible<Char, char>, is_convertible<char, Char> >
+ , token_printer_aux_for_chars
+ , token_printer_aux_for_other_types>::type
+ {};
+
+ template<typename Char>
+ inline void token_printer(std::ostream& o, Char c)
+ {
+ // allow to customize the token printer routine
+#if !defined(BOOST_SPIRIT_DEBUG_TOKEN_PRINTER)
+ token_printer_aux<Char>::print(o, c);
+#else
+ BOOST_SPIRIT_DEBUG_TOKEN_PRINTER(o, c);
+#endif
+ }
+ }
+
     struct simple_trace
     {
         void print_indent(int n) const
@@ -51,7 +112,7 @@
             BOOST_SPIRIT_DEBUG_OUT << '<' << tag << '>';
             int const n = BOOST_SPIRIT_DEBUG_PRINT_SOME;
             for (int i = 0; first != last && i != n && *first; ++i, ++first)
- BOOST_SPIRIT_DEBUG_OUT << *first;
+ detail::token_printer(BOOST_SPIRIT_DEBUG_OUT, *first);
             BOOST_SPIRIT_DEBUG_OUT << "</" << tag << '>' << std::endl;
         }
 


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk