error_reporting.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*=============================================================================
  2. Copyright (c) 2014 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(BOOST_SPIRIT_X3_ERROR_REPORTING_MAY_19_2014_00405PM)
  7. #define BOOST_SPIRIT_X3_ERROR_REPORTING_MAY_19_2014_00405PM
  8. #ifndef BOOST_SPIRIT_X3_NO_FILESYSTEM
  9. #include <boost/filesystem/path.hpp>
  10. #endif
  11. #include <boost/locale/encoding_utf.hpp>
  12. #include <boost/spirit/home/x3/support/ast/position_tagged.hpp>
  13. #include <ostream>
  14. // Clang-style error handling utilities
  15. namespace boost { namespace spirit { namespace x3
  16. {
  17. // tag used to get our error handler from the context
  18. struct error_handler_tag;
  19. template <typename Iterator>
  20. class error_handler
  21. {
  22. public:
  23. typedef Iterator iterator_type;
  24. error_handler(
  25. Iterator first, Iterator last, std::ostream& err_out
  26. , std::string file = "", int tabs = 4)
  27. : err_out(err_out)
  28. , file(file)
  29. , tabs(tabs)
  30. , pos_cache(first, last) {}
  31. typedef void result_type;
  32. void operator()(Iterator err_pos, std::string const& error_message) const;
  33. void operator()(Iterator err_first, Iterator err_last, std::string const& error_message) const;
  34. void operator()(position_tagged pos, std::string const& message) const
  35. {
  36. auto where = pos_cache.position_of(pos);
  37. (*this)(where.begin(), where.end(), message);
  38. }
  39. template <typename AST>
  40. void tag(AST& ast, Iterator first, Iterator last)
  41. {
  42. return pos_cache.annotate(ast, first, last);
  43. }
  44. boost::iterator_range<Iterator> position_of(position_tagged pos) const
  45. {
  46. return pos_cache.position_of(pos);
  47. }
  48. position_cache<std::vector<Iterator>> const& get_position_cache() const
  49. {
  50. return pos_cache;
  51. }
  52. private:
  53. void print_file_line(std::size_t line) const;
  54. void print_line(Iterator line_start, Iterator last) const;
  55. void print_indicator(Iterator& line_start, Iterator last, char ind) const;
  56. void skip_whitespace(Iterator& err_pos, Iterator last) const;
  57. void skip_non_whitespace(Iterator& err_pos, Iterator last) const;
  58. Iterator get_line_start(Iterator first, Iterator pos) const;
  59. std::size_t position(Iterator i) const;
  60. std::ostream& err_out;
  61. std::string file;
  62. int tabs;
  63. position_cache<std::vector<Iterator>> pos_cache;
  64. };
  65. template <typename Iterator>
  66. void error_handler<Iterator>::print_file_line(std::size_t line) const
  67. {
  68. if (file != "")
  69. {
  70. #ifdef BOOST_SPIRIT_X3_NO_FILESYSTEM
  71. err_out << "In file " << file << ", ";
  72. #else
  73. namespace fs = boost::filesystem;
  74. err_out << "In file " << fs::path(file).generic_string() << ", ";
  75. #endif
  76. }
  77. else
  78. {
  79. err_out << "In ";
  80. }
  81. err_out << "line " << line << ':' << std::endl;
  82. }
  83. template <typename Iterator>
  84. void error_handler<Iterator>::print_line(Iterator start, Iterator last) const
  85. {
  86. auto end = start;
  87. while (end != last)
  88. {
  89. auto c = *end;
  90. if (c == '\r' || c == '\n')
  91. break;
  92. else
  93. ++end;
  94. }
  95. typedef typename std::iterator_traits<Iterator>::value_type char_type;
  96. std::basic_string<char_type> line{start, end};
  97. err_out << locale::conv::utf_to_utf<char>(line) << std::endl;
  98. }
  99. template <typename Iterator>
  100. void error_handler<Iterator>::print_indicator(Iterator& start, Iterator last, char ind) const
  101. {
  102. for (; start != last; ++start)
  103. {
  104. auto c = *start;
  105. if (c == '\r' || c == '\n')
  106. break;
  107. else if (c == '\t')
  108. for (int i = 0; i < tabs; ++i)
  109. err_out << ind;
  110. else
  111. err_out << ind;
  112. }
  113. }
  114. template <typename Iterator>
  115. void error_handler<Iterator>::skip_whitespace(Iterator& err_pos, Iterator last) const
  116. {
  117. // make sure err_pos does not point to white space
  118. while (err_pos != last)
  119. {
  120. char c = *err_pos;
  121. if (std::isspace(c))
  122. ++err_pos;
  123. else
  124. break;
  125. }
  126. }
  127. template <typename Iterator>
  128. void error_handler<Iterator>::skip_non_whitespace(Iterator& err_pos, Iterator last) const
  129. {
  130. // make sure err_pos does not point to white space
  131. while (err_pos != last)
  132. {
  133. char c = *err_pos;
  134. if (std::isspace(c))
  135. break;
  136. else
  137. ++err_pos;
  138. }
  139. }
  140. template <class Iterator>
  141. inline Iterator error_handler<Iterator>::get_line_start(Iterator first, Iterator pos) const
  142. {
  143. Iterator latest = first;
  144. for (Iterator i = first; i != pos; ++i)
  145. if (*i == '\r' || *i == '\n')
  146. latest = i;
  147. return latest;
  148. }
  149. template <typename Iterator>
  150. std::size_t error_handler<Iterator>::position(Iterator i) const
  151. {
  152. std::size_t line { 1 };
  153. typename std::iterator_traits<Iterator>::value_type prev { 0 };
  154. for (Iterator pos = pos_cache.first(); pos != i; ++pos) {
  155. auto c = *pos;
  156. switch (c) {
  157. case '\n':
  158. if (prev != '\r') ++line;
  159. break;
  160. case '\r':
  161. ++line;
  162. break;
  163. default:
  164. break;
  165. }
  166. prev = c;
  167. }
  168. return line;
  169. }
  170. template <typename Iterator>
  171. void error_handler<Iterator>::operator()(
  172. Iterator err_pos, std::string const& error_message) const
  173. {
  174. Iterator first = pos_cache.first();
  175. Iterator last = pos_cache.last();
  176. // make sure err_pos does not point to white space
  177. skip_whitespace(err_pos, last);
  178. print_file_line(position(err_pos));
  179. err_out << error_message << std::endl;
  180. Iterator start = get_line_start(first, err_pos);
  181. if (start != first)
  182. ++start;
  183. print_line(start, last);
  184. print_indicator(start, err_pos, '_');
  185. err_out << "^_" << std::endl;
  186. }
  187. template <typename Iterator>
  188. void error_handler<Iterator>::operator()(
  189. Iterator err_first, Iterator err_last, std::string const& error_message) const
  190. {
  191. Iterator first = pos_cache.first();
  192. Iterator last = pos_cache.last();
  193. // make sure err_pos does not point to white space
  194. skip_whitespace(err_first, last);
  195. print_file_line(position(err_first));
  196. err_out << error_message << std::endl;
  197. Iterator start = get_line_start(first, err_first);
  198. if (start != first)
  199. ++start;
  200. print_line(start, last);
  201. print_indicator(start, err_first, ' ');
  202. print_indicator(start, err_last, '~');
  203. err_out << " <<-- Here" << std::endl;
  204. }
  205. }}}
  206. #endif