tree-sitter/src/runtime/length.h

75 lines
1.9 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_length_is_unknown(TSLength self) {
return self.chars > 0 && self.bytes == 0;
}
static inline void ts_length_set_unknown(TSLength *self) {
self->bytes = 0;
2016-09-09 21:11:02 -07:00
self->extent = (TSPoint){0, 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;
2015-09-15 16:00:16 -07:00
if (ts_length_is_unknown(len1) || ts_length_is_unknown(len2)) {
2015-09-15 16:00:16 -07:00
result.bytes = 0;
2016-09-09 21:11:02 -07:00
result.extent = (TSPoint){0, result.chars};
} else {
result.bytes = len1.bytes + len2.bytes;
2016-09-09 21:11:02 -07:00
result.extent = ts_point_add(len1.extent, len2.extent);
}
2015-09-15 16:00:16 -07:00
return result;
}
static inline TSLength ts_length_sub(TSLength len1, TSLength len2) {
TSLength result;
result.chars = len1.chars - len2.chars;
2015-09-15 16:00:16 -07:00
if (ts_length_is_unknown(len1) || ts_length_is_unknown(len2)) {
2015-09-15 16:00:16 -07:00
result.bytes = 0;
2016-09-09 21:11:02 -07:00
result.extent = (TSPoint){0, result.chars};
2015-11-30 12:56:10 -05:00
} else {
result.bytes = len1.bytes - len2.bytes;
2016-09-09 21:11:02 -07:00
result.extent = ts_point_sub(len1.extent, len2.extent);
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