| 1 |
/* |
| 2 |
Copyright 2015 Google Inc. All rights reserved. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
#ifndef JSONNET_STATIC_ERROR_H |
| 18 |
#define JSONNET_STATIC_ERROR_H |
| 19 |
|
| 20 |
#include <iostream> |
| 21 |
#include <sstream> |
| 22 |
|
| 23 |
namespace jsonnet::internal { |
| 24 |
|
| 25 |
struct Location { |
| 26 |
unsigned long line; |
| 27 |
unsigned long column; |
| 28 |
Location(void) : line(0), column(0) {} |
| 29 |
Location(unsigned long line, unsigned long column) : line(line), column(column) {} |
| 30 |
bool isSet(void) const |
| 31 |
{ |
| 32 |
return line != 0; |
| 33 |
} |
| 34 |
Location successor(void) const |
| 35 |
{ |
| 36 |
return Location(this->line, this->column + 1); |
| 37 |
} |
| 38 |
}; |
| 39 |
|
| 40 |
static inline std::ostream &operator<<(std::ostream &o, const Location &loc) |
| 41 |
{ |
| 42 |
o << loc.line << ":" << loc.column; |
| 43 |
return o; |
| 44 |
} |
| 45 |
|
| 46 |
struct LocationRange { |
| 47 |
std::string file; |
| 48 |
// [begin, end) |
| 49 |
Location begin, end; |
| 50 |
LocationRange(void) {} |
| 51 |
/** This is useful for special locations, e.g. manifestation entry point. */ |
| 52 |
LocationRange(const std::string &msg) : file(msg) {} |
| 53 |
LocationRange(const std::string &file, const Location &begin, const Location &end) |
| 54 |
: file(file), begin(begin), end(end) |
| 55 |
{ |
| 56 |
} |
| 57 |
bool isSet(void) const |
| 58 |
{ |
| 59 |
return begin.isSet(); |
| 60 |
} |
| 61 |
}; |
| 62 |
|
| 63 |
static inline std::ostream &operator<<(std::ostream &o, const LocationRange &loc) |
| 64 |
{ |
| 65 |
if (loc.file.length() > 0) |
| 66 |
o << loc.file; |
| 67 |
if (loc.isSet()) { |
| 68 |
if (loc.file.length() > 0) |
| 69 |
o << ":"; |
| 70 |
if (loc.begin.line == loc.end.line) { |
| 71 |
if (loc.begin.column == loc.end.column - 1) { |
| 72 |
o << loc.begin; |
| 73 |
} else { |
| 74 |
o << loc.begin << "-" << loc.end.column; |
| 75 |
} |
| 76 |
} else { |
| 77 |
o << "(" << loc.begin << ")-(" << loc.end << ")"; |
| 78 |
} |
| 79 |
} |
| 80 |
return o; |
| 81 |
} |
| 82 |
|
| 83 |
struct StaticError { |
| 84 |
LocationRange location; |
| 85 |
std::string msg; |
| 86 |
StaticError(const std::string &msg) : msg(msg) {} |
| 87 |
StaticError(const std::string &filename, const Location &location, const std::string &msg) |
| 88 |
: location(filename, location, location.successor()), msg(msg) |
| 89 |
{ |
| 90 |
} |
| 91 |
StaticError(const LocationRange &location, const std::string &msg) |
| 92 |
: location(location), msg(msg) |
| 93 |
{ |
| 94 |
} |
| 95 |
|
| 96 |
std::string toString() const |
| 97 |
{ |
| 98 |
std::stringstream ss; |
| 99 |
if (location.isSet()) { |
| 100 |
ss << location << ":"; |
| 101 |
} |
| 102 |
ss << " " << msg; |
| 103 |
return ss.str(); |
| 104 |
} |
| 105 |
}; |
| 106 |
|
| 107 |
static inline std::ostream &operator<<(std::ostream &o, const StaticError &err) |
| 108 |
{ |
| 109 |
o << err.toString(); |
| 110 |
return o; |
| 111 |
} |
| 112 |
|
| 113 |
} // namespace jsonnet::internal |
| 114 |
|
| 115 |
#endif // JSONNET_ERROR_H |