debug_parsing_error.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #ifndef BOOST_METAPARSE_V1_DEBUG_PARSING_ERROR_HPP
  2. #define BOOST_METAPARSE_V1_DEBUG_PARSING_ERROR_HPP
  3. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2011.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <boost/metaparse/v1/fwd/build_parser.hpp>
  8. #include <boost/metaparse/v1/start.hpp>
  9. #include <boost/metaparse/v1/is_error.hpp>
  10. #include <boost/metaparse/v1/get_remaining.hpp>
  11. #include <boost/mpl/if.hpp>
  12. #include <boost/mpl/string.hpp>
  13. #include <iostream>
  14. #include <cstdlib>
  15. namespace boost
  16. {
  17. namespace metaparse
  18. {
  19. namespace v1
  20. {
  21. template <class P, class S>
  22. class debug_parsing_error
  23. {
  24. public:
  25. debug_parsing_error()
  26. {
  27. using std::cout;
  28. using std::endl;
  29. using boost::mpl::c_str;
  30. typedef display<typename P::template apply<S, start>::type> runner;
  31. cout << "Compile-time parsing results" << endl;
  32. cout << "----------------------------" << endl;
  33. cout << "Input text:" << endl;
  34. cout << c_str<S>::type::value << endl;
  35. cout << endl;
  36. runner::run();
  37. std::exit(0);
  38. }
  39. typedef debug_parsing_error type;
  40. private:
  41. template <class Result>
  42. struct display_error
  43. {
  44. static void run()
  45. {
  46. typedef typename Result::type R;
  47. std::cout
  48. << "Parsing failed:" << std::endl
  49. << "line " << get_line<typename R::source_position>::type::value
  50. << ", col " << get_col<typename R::source_position>::type::value
  51. << ": "
  52. << R::message::type::get_value() << std::endl;
  53. }
  54. };
  55. template <class Result>
  56. struct display_no_error
  57. {
  58. static void run()
  59. {
  60. using std::cout;
  61. using std::endl;
  62. using boost::mpl::c_str;
  63. typedef typename get_remaining<Result>::type remaining_string;
  64. cout
  65. << "Parsing was successful. Remaining string is:" << endl
  66. << c_str<remaining_string>::type::value << endl;
  67. }
  68. };
  69. template <class Result>
  70. struct display :
  71. boost::mpl::if_<
  72. typename is_error<Result>::type,
  73. display_error<Result>,
  74. display_no_error<Result>
  75. >::type
  76. {};
  77. };
  78. // Special case to handle when DebugParsingError is used with build_parser
  79. // (it shouldn't be)
  80. template <class P, class S>
  81. class debug_parsing_error<build_parser<P>, S> :
  82. debug_parsing_error<P, S>
  83. {};
  84. }
  85. }
  86. }
  87. #endif