Limbo
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ErrorHandler.h
Go to the documentation of this file.
1 
13 #ifndef _ERRORHANDLER_H
14 #define _ERRORHANDLER_H
15 
16 #include <iostream>
17 #include <string>
18 #include <vector>
19 
22 template <typename Iterator>
24 {
26  template <typename, typename, typename>
27  struct result
28  {
30  typedef void type;
32  };
33 
36  ErrorHandler(Iterator first, Iterator last)
37  : first(first), last(last) {}
38 
45  template <typename Message, typename What>
46  void operator()(
47  Message const& message,
48  What const& what,
49  Iterator err_pos) const
50  {
51  int line;
52  Iterator line_start = get_pos(err_pos, line);
53  if (err_pos != last)
54  {
55  std::cout << message << what << " line " << line << ':' << std::endl;
56  std::cout << get_line(line_start) << std::endl;
57  for (; line_start != err_pos; ++line_start)
58  std::cout << ' ';
59  std::cout << '^' << std::endl;
60  }
61  else
62  {
63  std::cout << "Unexpected end of file. ";
64  std::cout << message << what << " line " << line << std::endl;
65  }
66  }
67 
72  Iterator get_pos(Iterator err_pos, int& line) const
73  {
74  line = 1;
75  Iterator i = first;
76  Iterator line_start = first;
77  while (i != err_pos)
78  {
79  bool eol = false;
80  if (i != err_pos && *i == '\r') // CR
81  {
82  eol = true;
83  line_start = ++i;
84  }
85  if (i != err_pos && *i == '\n') // LF
86  {
87  eol = true;
88  line_start = ++i;
89  }
90  if (eol)
91  ++line;
92  else
93  ++i;
94  }
95  return line_start;
96  }
97 
101  std::string get_line(Iterator err_pos) const
102  {
103  Iterator i = err_pos;
104  // position i to the next EOL
105  while (i != last && (*i != '\r' && *i != '\n'))
106  ++i;
107  return std::string(err_pos, i);
108  }
109 
110  Iterator first;
111  Iterator last;
112  std::vector<Iterator> iters;
113 };
114 
115 #endif
std::string get_line(Iterator err_pos) const
get line from iterator
Definition: ErrorHandler.h:101
probably used as some kind of type traits
Definition: ErrorHandler.h:27
Iterator first
begin iterator
Definition: ErrorHandler.h:110
Iterator get_pos(Iterator err_pos, int &line) const
get position of error
Definition: ErrorHandler.h:72
ErrorHandler(Iterator first, Iterator last)
constructor
Definition: ErrorHandler.h:36
error handler
Definition: ErrorHandler.h:23
Iterator last
end iterator
Definition: ErrorHandler.h:111
void operator()(Message const &message, What const &what, Iterator err_pos) const
API to invoke the error handler.
Definition: ErrorHandler.h:46
std::vector< Iterator > iters
not sure what it is used and why it is here
Definition: ErrorHandler.h:112