tree-sitter/src/runtime/length.h

99 lines
2.5 KiB
C
Raw Normal View History

#ifndef RUNTIME_LENGTH_H_
#define RUNTIME_LENGTH_H_
#include "tree_sitter/parser.h"
#include <stdbool.h>
2016-09-09 21:11:02 -07:00
static inline TSPoint ts_point_add(TSPoint a, TSPoint b) {
if (b.row > 0)
return (TSPoint){a.row + b.row, b.column};
else
return (TSPoint){a.row, a.column + b.column};
}
static inline TSPoint ts_point_sub(TSPoint a, TSPoint b) {
if (a.row > b.row)
return (TSPoint){a.row - b.row, a.column};
else
return (TSPoint){0, a.column - b.column};
}
static inline bool ts_point_lte(TSPoint a, TSPoint b) {
return (a.row < b.row) || (a.row == b.row && a.column <= b.column);
}
static inline bool ts_point_lt(TSPoint a, TSPoint b) {
return (a.row < b.row) || (a.row == b.row && a.column < b.column);
}
static inline bool ts_point_eq(TSPoint a, TSPoint b) {
return a.row == b.row && a.column == b.column;
}
2016-09-13 13:08:52 -07:00
static inline TSPoint ts_point_min(TSPoint a, TSPoint b) {
if (a.row < b.row || (a.row == b.row && a.column < b.column))
return a;
else
return b;
}
static inline TSPoint ts_point_max(TSPoint a, TSPoint b) {
if (a.row > b.row || (a.row == b.row && a.column > b.column))
return a;
else
return b;
}
static inline bool ts_length_is_unknown(TSLength self) {
2016-09-13 13:08:52 -07:00
return self.bytes > 0 && self.chars == 0;
}
static inline void ts_length_set_unknown(TSLength *self) {
2016-09-13 13:08:52 -07:00
self->chars = 0;
}
static inline TSLength ts_length_min(TSLength len1, TSLength len2) {
return (len1.chars < len2.chars) ? len1 : len2;
}
static inline TSLength ts_length_add(TSLength len1, TSLength len2) {
TSLength result;
result.chars = len1.chars + len2.chars;
2016-09-13 13:08:52 -07:00
result.bytes = len1.bytes + len2.bytes;
result.extent = ts_point_add(len1.extent, len2.extent);
2015-09-15 16:00:16 -07:00
if (ts_length_is_unknown(len1) || ts_length_is_unknown(len2)) {
2016-09-13 13:08:52 -07:00
result.chars = 0;
} else {
2016-09-13 13:08:52 -07:00
result.chars = len1.chars + len2.chars;
}
2015-09-15 16:00:16 -07:00
return result;
}
static inline TSLength ts_length_sub(TSLength len1, TSLength len2) {
TSLength result;
2016-09-13 13:08:52 -07:00
result.bytes = len1.bytes - len2.bytes;
result.extent = ts_point_sub(len1.extent, len2.extent);
2015-09-15 16:00:16 -07:00
if (ts_length_is_unknown(len1) || ts_length_is_unknown(len2)) {
2016-09-13 13:08:52 -07:00
result.chars = 0;
2015-11-30 12:56:10 -05:00
} else {
2016-09-13 13:08:52 -07:00
result.chars = len1.chars - len2.chars;
2015-11-30 12:56:10 -05:00
}
return result;
}
static inline TSLength ts_length_zero() {
2016-09-09 21:11:02 -07:00
return (TSLength){ 0, 0, {0, 0} };
2015-11-12 15:32:53 -05:00
}
static inline bool ts_length_eq(TSLength self, TSLength other) {
2015-12-04 20:56:33 -08:00
return self.bytes == other.bytes && self.chars == other.chars &&
2016-09-09 21:11:02 -07:00
self.extent.row == other.extent.row &&
self.extent.column == other.extent.column;
}
#endif