OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
error_handler.hpp
Go to the documentation of this file.
1 /*=============================================================================
2  Copyright (c) 2001-2011 Joel de Guzman
3 
4  Distributed under the Boost Software License, Version 1.0. (See accompanying
5  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #if !defined(ERROR_HANDLER_HPP)
8 #define ERROR_HANDLER_HPP
9 
10 #include <iostream>
11 #include <string>
12 #include <vector>
13 
14 namespace client
15 {
17  // The error handler
19  template <typename Iterator>
21  {
22 #if BOOST_VERSION >= 106200
23  template <typename>
24 #else
25  template <typename, typename, typename>
26 #endif
27  struct result { typedef void type; };
28 
29  error_handler(Iterator first, Iterator last)
30  : first(first), last(last) {}
31 
32  template <typename Message, typename What>
33  void operator()(
34  Message const& message,
35  What const& what,
36  Iterator err_pos) const
37  {
38  int line;
39  Iterator line_start = get_pos(err_pos, line);
40  if (err_pos != last)
41  {
42  std::cout << message << what << " line " << line << ':' << std::endl;
43  std::cout << get_line(line_start) << std::endl;
44  for (; line_start != err_pos; ++line_start)
45  std::cout << ' ';
46  std::cout << '^' << std::endl;
47  }
48  else
49  {
50  std::cout << "Unexpected end of file. ";
51  std::cout << message << what << " line " << line << std::endl;
52  }
53  }
54 
55  Iterator get_pos(Iterator err_pos, int& line) const
56  {
57  line = 1;
58  Iterator i = first;
59  Iterator line_start = first;
60  while (i != err_pos)
61  {
62  bool eol = false;
63  if (i != err_pos && *i == '\r') // CR
64  {
65  eol = true;
66  line_start = ++i;
67  }
68  if (i != err_pos && *i == '\n') // LF
69  {
70  eol = true;
71  line_start = ++i;
72  }
73  if (eol)
74  ++line;
75  else
76  ++i;
77  }
78  return line_start;
79  }
80 
81  std::string get_line(Iterator err_pos) const
82  {
83  Iterator i = err_pos;
84  // position i to the next EOL
85  while (i != last && (*i != '\r' && *i != '\n'))
86  ++i;
87  return std::string(err_pos, i);
88  }
89 
90  Iterator first;
91  Iterator last;
92  std::vector<Iterator> iters;
93  };
94 }
95 
96 #endif
void operator()(Message const &message, What const &what, Iterator err_pos) const
Iterator get_pos(Iterator err_pos, int &line) const
std::string get_line(Iterator err_pos) const
error_handler(Iterator first, Iterator last)
The WHAT command.
Definition: What.h:28
std::vector< Iterator > iters
Inform & endl(Inform &inf)
Definition: Inform.cpp:42