Limbo 3.5.4
Loading...
Searching...
No Matches
ErrorHandler.h
Go to the documentation of this file.
1
12
13#ifndef _ERRORHANDLER_H
14#define _ERRORHANDLER_H
15
16#include <iostream>
17#include <string>
18#include <vector>
19
22template <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>
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
probably used as some kind of type traits
std::string get_line(Iterator err_pos) const
get line from iterator
ErrorHandler(Iterator first, Iterator last)
constructor
Iterator last
end iterator
void operator()(Message const &message, What const &what, Iterator err_pos) const
API to invoke the error handler.
std::vector< Iterator > iters
not sure what it is used and why it is here
Iterator get_pos(Iterator err_pos, int &line) const
get position of error
Iterator first
begin iterator