Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / spirit / home / x3 / support / traits / print_token.hpp
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_PRINT_TOKEN_JANUARY_20_2013_0814AM)
9 #define BOOST_SPIRIT_X3_PRINT_TOKEN_JANUARY_20_2013_0814AM
10
11 #if defined(_MSC_VER)
12 #pragma once
13 #endif
14
15 #include <boost/mpl/if.hpp>
16 #include <boost/mpl/and.hpp>
17 #include <boost/type_traits/is_convertible.hpp>
18 #include <cctype>
19
20 namespace boost { namespace spirit { namespace x3 { namespace traits
21 {
22     ///////////////////////////////////////////////////////////////////////////
23     // generate debug output for lookahead token (character) stream
24     namespace detail
25     {
26         struct token_printer_debug_for_chars
27         {
28             template<typename Out, typename Char>
29             static void print(Out& o, Char c)
30             {
31                 using namespace std;    // allow for ADL to find the proper iscntrl
32
33                 switch (c)
34                 {
35                     case '\a': o << "\\a"; break;
36                     case '\b': o << "\\b"; break;
37                     case '\f': o << "\\f"; break;
38                     case '\n': o << "\\n"; break;
39                     case '\r': o << "\\r"; break;
40                     case '\t': o << "\\t"; break;
41                     case '\v': o << "\\v"; break;
42                     default:
43                         if (c >= 0 && c < 127 && iscntrl(c))
44                             o << "\\" << std::oct << int(c);
45                         else
46                             o << Char(c);
47                 }
48             }
49         };
50
51         // for token types where the comparison with char constants wouldn't work
52         struct token_printer_debug
53         {
54             template<typename Out, typename T>
55             static void print(Out& o, T const& val)
56             {
57                 o << val;
58             }
59         };
60     }
61
62     template <typename T, typename Enable = void>
63     struct token_printer_debug
64       : mpl::if_<
65             mpl::and_<
66                 is_convertible<T, char>, is_convertible<char, T> >
67           , detail::token_printer_debug_for_chars
68           , detail::token_printer_debug>::type
69     {};
70
71     template <typename Out, typename T>
72     inline void print_token(Out& out, T const& val)
73     {
74         // allow to customize the token printer routine
75         token_printer_debug<T>::print(out, val);
76     }
77 }}}}
78
79 #endif