print_token.hpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*=============================================================================
  2. Copyright (c) 2001-2014 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ================================================_==============================*/
  7. #if !defined(BOOST_SPIRIT_X3_PRINT_TOKEN_JANUARY_20_2013_0814AM)
  8. #define BOOST_SPIRIT_X3_PRINT_TOKEN_JANUARY_20_2013_0814AM
  9. #include <boost/mpl/if.hpp>
  10. #include <boost/mpl/and.hpp>
  11. #include <boost/type_traits/is_convertible.hpp>
  12. #include <cctype>
  13. #include <ios>
  14. namespace boost { namespace spirit { namespace x3 { namespace traits
  15. {
  16. ///////////////////////////////////////////////////////////////////////////
  17. // generate debug output for lookahead token (character) stream
  18. namespace detail
  19. {
  20. struct token_printer_debug_for_chars
  21. {
  22. template<typename Out, typename Char>
  23. static void print(Out& o, Char c)
  24. {
  25. using namespace std; // allow for ADL to find the proper iscntrl
  26. switch (c)
  27. {
  28. case '\a': o << "\\a"; break;
  29. case '\b': o << "\\b"; break;
  30. case '\f': o << "\\f"; break;
  31. case '\n': o << "\\n"; break;
  32. case '\r': o << "\\r"; break;
  33. case '\t': o << "\\t"; break;
  34. case '\v': o << "\\v"; break;
  35. default:
  36. if (c >= 0 && c < 127)
  37. {
  38. if (iscntrl(c))
  39. o << "\\" << std::oct << int(c);
  40. else if (isprint(c))
  41. o << char(c);
  42. else
  43. o << "\\x" << std::hex << int(c);
  44. }
  45. else
  46. o << "\\x" << std::hex << int(c);
  47. }
  48. }
  49. };
  50. // for token types where the comparison with char constants wouldn't work
  51. struct token_printer_debug
  52. {
  53. template<typename Out, typename T>
  54. static void print(Out& o, T const& val)
  55. {
  56. o << val;
  57. }
  58. };
  59. }
  60. template <typename T, typename Enable = void>
  61. struct token_printer_debug
  62. : mpl::if_<
  63. mpl::and_<
  64. is_convertible<T, char>, is_convertible<char, T> >
  65. , detail::token_printer_debug_for_chars
  66. , detail::token_printer_debug>::type
  67. {};
  68. template <typename Out, typename T>
  69. inline void print_token(Out& out, T const& val)
  70. {
  71. // allow to customize the token printer routine
  72. token_printer_debug<T>::print(out, val);
  73. }
  74. }}}}
  75. #endif