2017-04-05 08:06:18 +00:00
|
|
|
#include "position.h"
|
2018-01-08 01:41:24 +00:00
|
|
|
#include "serializers/msgpack.h"
|
2017-04-05 08:06:18 +00:00
|
|
|
|
2017-04-07 06:10:17 +00:00
|
|
|
namespace {
|
|
|
|
// Skips until the character immediately following |skip_after|.
|
|
|
|
const char* SkipAfter(const char* input, char skip_after) {
|
|
|
|
while (*input && *input != skip_after)
|
|
|
|
++input;
|
|
|
|
++input;
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2018-01-14 19:39:29 +00:00
|
|
|
Position::Position() : line(0), column(0) {}
|
2017-04-05 08:06:18 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
Position::Position(int16_t line, int16_t column) : line(line), column(column) {}
|
2017-04-05 08:06:18 +00:00
|
|
|
|
|
|
|
Position::Position(const char* encoded) {
|
|
|
|
assert(encoded);
|
2018-01-14 19:39:29 +00:00
|
|
|
line = (int16_t)atoi(encoded) - 1;
|
2017-04-05 08:06:18 +00:00
|
|
|
|
2017-04-07 06:10:17 +00:00
|
|
|
encoded = SkipAfter(encoded, ':');
|
2017-04-05 08:06:18 +00:00
|
|
|
assert(encoded);
|
2018-01-14 19:39:29 +00:00
|
|
|
column = (int16_t)atoi(encoded) - 1;
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Position::ToString() {
|
|
|
|
// Output looks like this:
|
|
|
|
//
|
2017-04-07 06:10:17 +00:00
|
|
|
// 1:2
|
2017-04-05 08:06:18 +00:00
|
|
|
//
|
|
|
|
// 1 => line
|
|
|
|
// 2 => column
|
|
|
|
|
|
|
|
std::string result;
|
2018-01-14 19:39:29 +00:00
|
|
|
result += std::to_string(line + 1);
|
2017-04-05 08:06:18 +00:00
|
|
|
result += ':';
|
2018-01-14 19:39:29 +00:00
|
|
|
result += std::to_string(column + 1);
|
2017-04-05 08:06:18 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string Position::ToPrettyString(const std::string& filename) {
|
|
|
|
// Output looks like this:
|
|
|
|
//
|
2017-04-07 06:10:17 +00:00
|
|
|
// 1:2:3
|
2017-04-05 08:06:18 +00:00
|
|
|
//
|
2017-04-07 06:10:17 +00:00
|
|
|
// 1 => filename
|
|
|
|
// 2 => line
|
|
|
|
// 3 => column
|
2017-04-05 08:06:18 +00:00
|
|
|
|
|
|
|
std::string result;
|
|
|
|
result += filename;
|
|
|
|
result += ':';
|
2018-01-14 19:39:29 +00:00
|
|
|
result += std::to_string(line + 1);
|
2017-04-05 08:06:18 +00:00
|
|
|
result += ':';
|
2018-01-14 19:39:29 +00:00
|
|
|
result += std::to_string(column + 1);
|
2017-04-05 08:06:18 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Position::operator==(const Position& that) const {
|
|
|
|
return line == that.line && column == that.column;
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
bool Position::operator!=(const Position& that) const {
|
|
|
|
return !(*this == that);
|
|
|
|
}
|
2017-04-05 08:06:18 +00:00
|
|
|
|
|
|
|
bool Position::operator<(const Position& that) const {
|
2018-01-08 04:10:16 +00:00
|
|
|
if (line != that.line)
|
|
|
|
return line < that.line;
|
|
|
|
return column < that.column;
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Range::Range() {}
|
|
|
|
|
2017-05-15 03:51:53 +00:00
|
|
|
Range::Range(Position position) : Range(position, position) {}
|
|
|
|
|
2017-04-19 05:28:33 +00:00
|
|
|
Range::Range(Position start, Position end) : start(start), end(end) {}
|
2017-04-05 08:06:18 +00:00
|
|
|
|
2017-04-07 06:10:17 +00:00
|
|
|
Range::Range(const char* encoded) {
|
2018-01-07 23:52:46 +00:00
|
|
|
char* p = const_cast<char*>(encoded);
|
2018-01-14 19:39:29 +00:00
|
|
|
start.line = int16_t(strtol(p, &p, 10)) - 1;
|
2018-01-07 23:52:46 +00:00
|
|
|
assert(*p == ':');
|
|
|
|
p++;
|
2018-01-14 19:39:29 +00:00
|
|
|
start.column = int16_t(strtol(p, &p, 10)) - 1;
|
2018-01-07 23:52:46 +00:00
|
|
|
assert(*p == '-');
|
|
|
|
p++;
|
|
|
|
|
2018-01-14 19:39:29 +00:00
|
|
|
end.line = int16_t(strtol(p, &p, 10)) - 1;
|
2018-01-07 23:52:46 +00:00
|
|
|
assert(*p == ':');
|
|
|
|
p++;
|
2018-01-14 19:39:29 +00:00
|
|
|
end.column = int16_t(strtol(p, nullptr, 10)) - 1;
|
2017-04-05 08:06:18 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 05:43:01 +00:00
|
|
|
bool Range::Contains(int line, int column) const {
|
|
|
|
if (line == start.line && line == end.line)
|
2017-05-17 06:05:12 +00:00
|
|
|
return column >= start.column && column < end.column;
|
2017-04-11 05:43:01 +00:00
|
|
|
if (line == start.line)
|
|
|
|
return column >= start.column;
|
|
|
|
if (line == end.line)
|
2017-05-17 06:05:12 +00:00
|
|
|
return column < end.column;
|
2017-04-11 05:43:01 +00:00
|
|
|
if (line > start.line && line < end.line)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
std::string Range::ToString() {
|
2017-04-07 06:10:17 +00:00
|
|
|
// Output looks like this:
|
|
|
|
//
|
|
|
|
// *1:2-3:4
|
|
|
|
//
|
|
|
|
// * => if present, range is interesting
|
|
|
|
// 1 => start line
|
|
|
|
// 2 => start column
|
|
|
|
// 3 => end line
|
|
|
|
// 4 => end column
|
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
std::string output;
|
2017-04-05 08:29:15 +00:00
|
|
|
|
2018-01-14 19:39:29 +00:00
|
|
|
output += std::to_string(start.line + 1);
|
2017-04-07 06:10:17 +00:00
|
|
|
output += ':';
|
2018-01-14 19:39:29 +00:00
|
|
|
output += std::to_string(start.column + 1);
|
2017-04-07 06:10:17 +00:00
|
|
|
output += '-';
|
2018-01-14 19:39:29 +00:00
|
|
|
output += std::to_string(end.line + 1);
|
2017-04-07 06:10:17 +00:00
|
|
|
output += ':';
|
2018-01-14 19:39:29 +00:00
|
|
|
output += std::to_string(end.column + 1);
|
2017-04-05 08:29:15 +00:00
|
|
|
|
2017-04-05 08:06:18 +00:00
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Range::operator==(const Range& that) const {
|
|
|
|
return start == that.start && end == that.end;
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
bool Range::operator!=(const Range& that) const {
|
|
|
|
return !(*this == that);
|
|
|
|
}
|
2017-04-05 08:06:18 +00:00
|
|
|
|
|
|
|
bool Range::operator<(const Range& that) const {
|
2018-01-08 04:10:16 +00:00
|
|
|
if (start != that.start)
|
|
|
|
return start < that.start;
|
|
|
|
return end < that.end;
|
2017-05-19 07:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Position
|
|
|
|
void Reflect(Reader& visitor, Position& value) {
|
2018-01-08 01:41:24 +00:00
|
|
|
if (visitor.Format() == SerializeFormat::Json) {
|
2018-01-07 05:08:25 +00:00
|
|
|
std::string s = visitor.GetString();
|
|
|
|
value = Position(s.c_str());
|
2018-01-07 09:27:14 +00:00
|
|
|
} else {
|
2018-01-08 04:10:16 +00:00
|
|
|
Reflect(visitor, value.line);
|
|
|
|
Reflect(visitor, value.column);
|
2018-01-07 05:08:25 +00:00
|
|
|
}
|
2017-05-19 07:02:01 +00:00
|
|
|
}
|
|
|
|
void Reflect(Writer& visitor, Position& value) {
|
2018-01-07 09:27:14 +00:00
|
|
|
if (visitor.Format() == SerializeFormat::Json) {
|
|
|
|
std::string output = value.ToString();
|
|
|
|
visitor.String(output.c_str(), output.size());
|
|
|
|
} else {
|
|
|
|
Reflect(visitor, value.line);
|
|
|
|
Reflect(visitor, value.column);
|
|
|
|
}
|
2017-05-19 07:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Range
|
|
|
|
void Reflect(Reader& visitor, Range& value) {
|
2018-01-08 01:41:24 +00:00
|
|
|
if (visitor.Format() == SerializeFormat::Json) {
|
2018-01-07 05:08:25 +00:00
|
|
|
std::string s = visitor.GetString();
|
|
|
|
value = Range(s.c_str());
|
2018-01-07 09:27:14 +00:00
|
|
|
} else {
|
2018-01-08 04:10:16 +00:00
|
|
|
Reflect(visitor, value.start.line);
|
|
|
|
Reflect(visitor, value.start.column);
|
|
|
|
Reflect(visitor, value.end.line);
|
|
|
|
Reflect(visitor, value.end.column);
|
2018-01-07 05:08:25 +00:00
|
|
|
}
|
2017-05-19 07:02:01 +00:00
|
|
|
}
|
|
|
|
void Reflect(Writer& visitor, Range& value) {
|
2018-01-07 09:27:14 +00:00
|
|
|
if (visitor.Format() == SerializeFormat::Json) {
|
|
|
|
std::string output = value.ToString();
|
|
|
|
visitor.String(output.c_str(), output.size());
|
|
|
|
} else {
|
|
|
|
Reflect(visitor, value.start.line);
|
|
|
|
Reflect(visitor, value.start.column);
|
|
|
|
Reflect(visitor, value.end.line);
|
|
|
|
Reflect(visitor, value.end.column);
|
|
|
|
}
|
2018-01-06 21:46:41 +00:00
|
|
|
}
|