Vcsn  2.8
Be Rational
caret.cc
Go to the documentation of this file.
1 #include <lib/vcsn/rat/caret.hh>
2 
3 #include <istream>
4 #include <sstream>
5 #include <ostream>
6 
7 namespace vcsn
8 {
9  namespace detail
10  {
11  namespace
12  {
13  void print_caret_impl(std::istream& is,
14  std::ostream& os, const rat::location& loc)
15  {
16  const auto pos = is.tellg();
17  // If we hit EOF, the stream is bad. Clear that so that we
18  // can seekg etc.
19  is.clear();
20  is.seekg(0);
21  // Look for the right line.
22  auto buf = std::string{};
23  auto line = 1u;
24  while (getline(is, buf) && line != loc.begin.line)
25  ++line;
26  // If it's on several lines, just display the first one.
27  auto end_col
28  = loc.begin.line == loc.end.line
29  ? loc.end.column
30  : buf.size() + 1;
31  os << buf
32  << '\n'
33  << std::string(loc.begin.column - 1, ' ')
34  << std::string(std::max(1ul, end_col - loc.begin.column), '^');
35  // Restore position.
36  is.seekg(pos);
37  }
38  }
39 
40  void print_caret(std::istream& is,
41  std::ostream& os, const rat::location& loc)
42  {
43  os << '\n';
44  print_caret_impl(is, os, loc);
45  }
46 
47  std::string caret(std::istream& is, const rat::location& loc)
48  {
49  std::ostringstream os;
50  print_caret_impl(is, os, loc);
51  return os.str();
52  }
53  }
54 }
mutable_automaton< Context > u(const Context &ctx, unsigned n)
The Brzozowski universal witness.
Definition: u.hh:15
std::string caret(std::istream &is, const rat::location &loc)
Repeat a line with an error, underlining the error with carets.
Definition: caret.cc:47
Abstract a location.
Definition: location.hh:47
vcsn::rat::location location
Pairs of positions in a file/stream.
Definition: fwd.hh:36
std::ostringstream os
The output stream: the corresponding C++ snippet to compile.
Definition: translate.cc:404
void print_caret(std::istream &is, std::ostream &os, const rat::location &loc)
Repeat a line with an error, underlining the error with carets.
Definition: caret.cc:40
Definition: a-star.hh:8