2014-09-26 16:15:07 -07:00
|
|
|
#ifndef RUNTIME_LENGTH_H_
|
|
|
|
|
#define RUNTIME_LENGTH_H_
|
|
|
|
|
|
2015-12-04 10:45:30 -08:00
|
|
|
#include "tree_sitter/parser.h"
|
2014-09-26 16:15:07 -07:00
|
|
|
#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};
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-22 18:02:11 -07:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-22 18:02:11 -07:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-04 20:20:29 -08:00
|
|
|
static inline bool ts_length_is_unknown(TSLength self) {
|
2016-09-13 13:08:52 -07:00
|
|
|
return self.bytes > 0 && self.chars == 0;
|
2015-12-04 20:20:29 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void ts_length_set_unknown(TSLength *self) {
|
2016-09-13 13:08:52 -07:00
|
|
|
self->chars = 0;
|
2015-12-04 20:20:29 -08:00
|
|
|
}
|
|
|
|
|
|
2016-09-03 23:40:57 -07:00
|
|
|
static inline TSLength ts_length_min(TSLength len1, TSLength len2) {
|
|
|
|
|
return (len1.chars < len2.chars) ? len1 : len2;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 16:15:07 -07:00
|
|
|
static inline TSLength ts_length_add(TSLength len1, TSLength len2) {
|
2014-09-26 16:31:30 -07:00
|
|
|
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
|
|
|
|
2015-12-04 20:20:29 -08:00
|
|
|
if (ts_length_is_unknown(len1) || ts_length_is_unknown(len2)) {
|
2016-09-13 13:08:52 -07:00
|
|
|
result.chars = 0;
|
2015-12-04 20:20:29 -08:00
|
|
|
} else {
|
2016-09-13 13:08:52 -07:00
|
|
|
result.chars = len1.chars + len2.chars;
|
2015-12-04 20:20:29 -08:00
|
|
|
}
|
2015-09-15 16:00:16 -07:00
|
|
|
|
2014-09-26 16:31:30 -07:00
|
|
|
return result;
|
2014-09-26 16:15:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline TSLength ts_length_sub(TSLength len1, TSLength len2) {
|
2014-09-26 16:31:30 -07:00
|
|
|
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
|
|
|
|
2015-12-04 20:20:29 -08: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
|
|
|
}
|
2015-11-18 16:34:50 -08:00
|
|
|
|
2014-09-26 16:31:30 -07:00
|
|
|
return result;
|
2014-09-26 16:15:07 -07:00
|
|
|
}
|
|
|
|
|
|
2015-12-04 20:20:29 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2015-12-04 20:20:29 -08: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;
|
2014-09-26 16:31:30 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 16:15:07 -07:00
|
|
|
#endif
|