Represent Lengths in terms of Points
This commit is contained in:
parent
eb5dda75c4
commit
cc62fe0375
13 changed files with 106 additions and 100 deletions
|
|
@ -4,14 +4,27 @@
|
|||
#include "tree_sitter/parser.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
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;
|
||||
self->rows = 0;
|
||||
self->columns = 0;
|
||||
self->extent = (TSPoint){0, 0};
|
||||
}
|
||||
|
||||
static inline TSLength ts_length_min(TSLength len1, TSLength len2) {
|
||||
|
|
@ -24,17 +37,10 @@ static inline TSLength ts_length_add(TSLength len1, TSLength len2) {
|
|||
|
||||
if (ts_length_is_unknown(len1) || ts_length_is_unknown(len2)) {
|
||||
result.bytes = 0;
|
||||
result.rows = 0;
|
||||
result.columns = result.chars;
|
||||
result.extent = (TSPoint){0, result.chars};
|
||||
} else {
|
||||
result.bytes = len1.bytes + len2.bytes;
|
||||
if (len2.rows == 0) {
|
||||
result.rows = len1.rows;
|
||||
result.columns = len1.columns + len2.columns;
|
||||
} else {
|
||||
result.rows = len1.rows + len2.rows;
|
||||
result.columns = len2.columns;
|
||||
}
|
||||
result.extent = ts_point_add(len1.extent, len2.extent);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
@ -46,29 +52,23 @@ static inline TSLength ts_length_sub(TSLength len1, TSLength len2) {
|
|||
|
||||
if (ts_length_is_unknown(len1) || ts_length_is_unknown(len2)) {
|
||||
result.bytes = 0;
|
||||
result.rows = 0;
|
||||
result.columns = result.chars;
|
||||
result.extent = (TSPoint){0, result.chars};
|
||||
} else {
|
||||
result.bytes = len1.bytes - len2.bytes;
|
||||
if (len1.rows == len2.rows) {
|
||||
result.rows = 0;
|
||||
result.columns = len1.columns - len2.columns;
|
||||
} else {
|
||||
result.rows = len1.rows - len2.rows;
|
||||
result.columns = len1.columns;
|
||||
}
|
||||
result.extent = ts_point_sub(len1.extent, len2.extent);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline TSLength ts_length_zero() {
|
||||
return (TSLength){ 0, 0, 0, 0 };
|
||||
return (TSLength){ 0, 0, {0, 0} };
|
||||
}
|
||||
|
||||
static inline bool ts_length_eq(TSLength self, TSLength other) {
|
||||
return self.bytes == other.bytes && self.chars == other.chars &&
|
||||
self.rows == other.rows && self.columns == other.columns;
|
||||
self.extent.row == other.extent.row &&
|
||||
self.extent.column == other.extent.column;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue