carl  24.04
Computer ARithmetic Library
SpiritHelper.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #define BOOST_SPIRIT_USE_PHOENIX_V3
6 #include <boost/spirit/include/qi.hpp>
7 #include <boost/spirit/include/support_line_pos_iterator.hpp>
8 
9 namespace carl::io {
10 namespace helper {
11  namespace spirit = boost::spirit;
12  namespace qi = boost::spirit::qi;
13 
14  struct ErrorHandler {
15  template<typename> struct result { using type = qi::error_handler_result; };
16  template<typename T1, typename T2>
17  qi::error_handler_result operator()(T1 b, T1 e, T1 where, T2 const& what) const {
18  static_assert(carl::is_instantiation_of<spirit::line_pos_iterator, T1>::value, "The iterators must be a line_pos_iterator.");
19  auto line_start = spirit::get_line_start(b, where);
20  auto line_end = std::find(where, e, '\n');
21  std::string line(++line_start, line_end);
22 
23  std::cout << "Parsing error at " << spirit::get_line(where) << ":" << spirit::get_column(line_start, where) << std::endl;
24  std::cout << "expected" << std::endl << "\t" << what.tag << ": " << what << std::endl;
25  std::cout << "but got" << std::endl << "\t" << std::string(where, line_end) << std::endl;
26  std::cout << "in line " << spirit::get_line(where) << std::endl << "\t" << line << std::endl;
27  return qi::fail;
28  }
29  };
30 
31 }
32 }
qi::error_handler_result operator()(T1 b, T1 e, T1 where, T2 const &what) const
Definition: SpiritHelper.h:17
qi::error_handler_result type
Definition: SpiritHelper.h:15