Spicy
location.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <functional>
6 #include <string>
7 #include <utility>
8 
9 #include <hilti/rt/filesystem.h>
10 
11 #include <hilti/base/util.h>
12 
13 namespace hilti {
14 
18 class Location {
19 public:
30  Location(hilti::rt::filesystem::path file = "", int from_line = -1, int to_line = -1, int from_character = -1,
31  int to_character = -1)
32  : _file(std::move(file)),
33  _from_line(from_line),
34  _to_line(to_line),
35  _from_character(from_character),
36  _to_character(to_character) {}
37 
38  Location(const Location&) = default;
39  Location(Location&&) = default;
40  Location& operator=(const Location&) = default;
41  Location& operator=(Location&&) = default;
42  ~Location() = default;
43 
44  auto file() const { return _file.generic_string(); }
45  auto from() const { return _from_line; }
46  auto to() const { return _to_line; }
47 
53  std::string render(bool no_path = false) const;
54 
59  explicit operator bool() const;
60 
62  operator std::string() const { return render(); }
63 
64  bool operator<(const Location& other) const {
65  return std::tie(_file, _from_line, _from_character, _to_line, _to_character) <
66  std::tie(other._file, other._from_line, other._from_character, other._to_line, other._to_character);
67  }
68 
69  bool operator==(const Location& other) const {
70  return std::tie(_file, _from_line, _from_character, _to_line, _to_character) ==
71  std::tie(other._file, other._from_line, other._from_character, other._to_line, other._to_character);
72  }
73 
74 private:
75  hilti::rt::filesystem::path _file;
76  int _from_line = -1;
77  int _to_line = -1;
78 
79  int _from_character = -1;
80  int _to_character = -1;
81 
82  friend struct std::hash<Location>;
83 };
84 
86 inline auto to_string(const Location& l) { return l.render(); }
87 
89 inline std::ostream& operator<<(std::ostream& out, const Location& l) {
90  out << l.render();
91  return out;
92 }
93 
94 namespace location {
96 extern const Location None;
97 } // namespace location
98 
99 } // namespace hilti
100 
101 namespace std {
102 template<>
103 struct hash<hilti::Location> {
104  size_t operator()(const hilti::Location& x) const {
105  return hilti::rt::hashCombine(std::hash<std::string>()(x._file), x._from_line, x._to_line, x._from_character,
106  x._to_character);
107  }
108 };
109 } // namespace std
Definition: optional.h:79
Location(hilti::rt::filesystem::path file="", int from_line=-1, int to_line=-1, int from_character=-1, int to_character=-1)
Definition: location.h:30
std::string render(bool no_path=false) const
Definition: location.cc:12
Definition: location.h:18
Definition: location.h:94