print_token.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. namespace boost { namespace spirit { namespace x3 { namespace traits
  14. {
  15. ///////////////////////////////////////////////////////////////////////////
  16. // generate debug output for lookahead token (character) stream
  17. namespace detail
  18. {
  19. struct token_printer_debug_for_chars
  20. {
  21. template<typename Out, typename Char>
  22. static void print(Out& o, Char c)
  23. {
  24. using namespace std; // allow for ADL to find the proper iscntrl
  25. switch (c)
  26. {
  27. case '\a': o << "\\a"; break;
  28. case '\b': o << "\\b"; break;
  29. case '\f': o << "\\f"; break;
  30. case '\n': o << "\\n"; break;
  31. case '\r': o << "\\r"; break;
  32. case '\t': o << "\\t"; break;
  33. case '\v': o << "\\v"; break;
  34. default:
  35. if (c >= 0 && c < 127 && iscntrl(c))
  36. o << "\\" << std::oct << int(c);
  37. else
  38. o << Char(c);
  39. }
  40. }
  41. };
  42. // for token types where the comparison with char constants wouldn't work
  43. struct token_printer_debug
  44. {
  45. template<typename Out, typename T>
  46. static void print(Out& o, T const& val)
  47. {
  48. o << val;
  49. }
  50. };
  51. }
  52. template <typename T, typename Enable = void>
  53. struct token_printer_debug
  54. : mpl::if_<
  55. mpl::and_<
  56. is_convertible<T, char>, is_convertible<char, T> >
  57. , detail::token_printer_debug_for_chars
  58. , detail::token_printer_debug>::type
  59. {};
  60. template <typename Out, typename T>
  61. inline void print_token(Out& out, T const& val)
  62. {
  63. // allow to customize the token printer routine
  64. token_printer_debug<T>::print(out, val);
  65. }
  66. }}}}
  67. #endif