The Battle for Wesnoth  1.15.0+dev
exceptions.hpp
Go to the documentation of this file.
1 // (C) Copyright 2015 - 2017 Christopher Beck
2 
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
4 // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
5 
6 #ifndef SPIRIT_PO_EXCEPTIONS_HPP_INCLUDED
7 #define SPIRIT_PO_EXCEPTIONS_HPP_INCLUDED
8 
10 #include <boost/spirit/include/support_line_pos_iterator.hpp>
11 #include <string>
12 
13 namespace spirit_po {
14 
15 // Show the next 80 characters from some iterator position.
16 // Intended to be used for parser error messages
17 template <typename Iterator>
18 std::string iterator_context(Iterator & it, Iterator & end) {
19  std::string result;
20  std::size_t line_no = boost::spirit::get_line(it);
21  if (line_no != static_cast<std::size_t>(-1)) {
22  result = "Line " + std::to_string(line_no) + ":\n";
23  }
24 
25  uint count = 80;
26  while (it != end && count) {
27  result += *it;
28  ++it;
29  --count;
30  }
31  return result;
32 }
33 
34 // When the thing being parsed is a short string, we can give
35 // a better context report
36 inline std::string string_iterator_context(const std::string & str,
37  std::string::const_iterator it) {
38  std::string result{str};
39  result += "\n";
40 
41  for (auto temp = str.begin(); temp != it; ++temp) {
42  result += ' ';
43  }
44  result += "^\n";
45  return result;
46 }
47 
48 } // end namespace spirit_po
49 
50 
51 #ifdef SPIRIT_PO_NO_EXCEPTIONS
52 
53 #define SPIRIT_PO_CATALOG_FAIL(Message) \
54 do { \
55  error_message_ = (Message); \
56  return ; \
57 } while(0)
58 
59 #else // SPIRIT_PO_NO_EXCEPTIONS
60 
61 #include <stdexcept>
62 
63 namespace spirit_po {
64 
65 struct catalog_exception : std::runtime_error {
66  explicit catalog_exception(const char * what) : runtime_error(what) {}
67  explicit catalog_exception(const std::string & what) : runtime_error(what) {}
68 };
69 
70 } // end namespace spirit_po
71 
72 #define SPIRIT_PO_CATALOG_FAIL(Message) \
73 do { \
74  throw spirit_po::catalog_exception(( Message )); \
75 } while(0)
76 
77 
78 #endif // SPIRIT_PO_NO_EXCEPTIONS
79 
80 #endif // SPIRIT_PO_EXCEPTIONS_HPP_INCLUDED
std::string string_iterator_context(const std::string &str, std::string::const_iterator it)
Definition: exceptions.hpp:36
catalog_exception(const std::string &what)
Definition: exceptions.hpp:67
catalog_exception(const char *what)
Definition: exceptions.hpp:66
std::string iterator_context(Iterator &it, Iterator &end)
Definition: exceptions.hpp:18
unsigned int uint
Definition: catalog.hpp:39