Merge pull request #12 from maxbrunsfeld/api-cleanup
Clean up API a bit after the addition of row/column tracking
This commit is contained in:
commit
27bce56ef2
23 changed files with 432 additions and 475 deletions
|
|
@ -1,7 +1,6 @@
|
|||
#include "tree_sitter/parser.h"
|
||||
#include "runtime/node.h"
|
||||
#include "runtime/tree.h"
|
||||
#include "runtime/length.h"
|
||||
#include "runtime/parser.h"
|
||||
#include "runtime/string_input.h"
|
||||
#include "runtime/document.h"
|
||||
|
|
@ -54,7 +53,7 @@ void ts_document_edit(TSDocument *self, TSInputEdit edit) {
|
|||
if (!self->tree)
|
||||
return;
|
||||
|
||||
size_t max_chars = ts_tree_total_size(self->tree).chars;
|
||||
size_t max_chars = ts_tree_total_chars(self->tree);
|
||||
if (edit.position > max_chars)
|
||||
edit.position = max_chars;
|
||||
if (edit.chars_removed > max_chars - edit.position)
|
||||
|
|
@ -85,7 +84,7 @@ void ts_document_invalidate(TSDocument *self) {
|
|||
}
|
||||
|
||||
TSNode ts_document_root_node(const TSDocument *self) {
|
||||
TSNode result = ts_node_make(self->tree, ts_length_zero(), 0);
|
||||
TSNode result = ts_node_make(self->tree, 0, 0, 0);
|
||||
while (result.data && !ts_tree_is_visible(result.data))
|
||||
result = ts_node_named_child(result, 0);
|
||||
return result;
|
||||
|
|
|
|||
|
|
@ -1,85 +1,72 @@
|
|||
#ifndef RUNTIME_LENGTH_H_
|
||||
#define RUNTIME_LENGTH_H_
|
||||
|
||||
#include "tree_sitter/runtime.h"
|
||||
#include "tree_sitter/parser.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static inline TSLength ts_length_add(TSLength len1, TSLength len2) {
|
||||
TSLength result;
|
||||
result.bytes = len1.bytes + len2.bytes;
|
||||
result.chars = len1.chars + len2.chars;
|
||||
|
||||
if ((len1.chars > 0 && len1.bytes == 0) || (len2.chars > 0 && len2.bytes == 0))
|
||||
if (ts_length_is_unknown(len1) || ts_length_is_unknown(len2)) {
|
||||
result.bytes = 0;
|
||||
result.rows = 0;
|
||||
result.columns = 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;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline TSLength ts_length_sub(TSLength len1, TSLength len2) {
|
||||
TSLength result;
|
||||
result.bytes = len1.bytes - len2.bytes;
|
||||
result.chars = len1.chars - len2.chars;
|
||||
|
||||
if ((len1.chars > 0 && len1.bytes == 0) || (len2.chars > 0 && len2.bytes == 0))
|
||||
if (ts_length_is_unknown(len1) || ts_length_is_unknown(len2)) {
|
||||
result.bytes = 0;
|
||||
result.rows = 0;
|
||||
result.columns = 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;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline TSPoint ts_point_make(size_t row, size_t column) {
|
||||
TSPoint point;
|
||||
point.row = row;
|
||||
point.column = column;
|
||||
return point;
|
||||
}
|
||||
|
||||
static inline TSPoint ts_point_add(TSPoint point1, TSPoint point2) {
|
||||
size_t row = point1.row + point2.row;
|
||||
|
||||
size_t column;
|
||||
if (point2.row == 0) {
|
||||
column = point1.column + point2.column;
|
||||
} else {
|
||||
column = point2.column;
|
||||
}
|
||||
|
||||
return ts_point_make(row, column);
|
||||
}
|
||||
|
||||
static inline TSPoint ts_point_sub(TSPoint point1, TSPoint point2) {
|
||||
size_t row, column;
|
||||
if (point1.row == point2.row) {
|
||||
row = 0;
|
||||
column = point1.column - point2.column;
|
||||
} else {
|
||||
row = point1.row - point2.row;
|
||||
column = point1.column;
|
||||
}
|
||||
|
||||
return ts_point_make(row, column);
|
||||
}
|
||||
|
||||
static inline TSLength ts_length_zero() {
|
||||
TSLength result;
|
||||
result.bytes = result.chars = 0;
|
||||
return result;
|
||||
return (TSLength){0, 0, 0, 0};
|
||||
}
|
||||
|
||||
static inline bool ts_length_eq(TSLength len1, TSLength len2) {
|
||||
return len1.bytes == len2.bytes && len1.chars == len2.chars;
|
||||
}
|
||||
|
||||
static inline TSPoint ts_point_zero() {
|
||||
TSPoint point;
|
||||
point.row = point.column = 0;
|
||||
return point;
|
||||
}
|
||||
|
||||
static inline TSLength ts_length_make(size_t bytes, size_t chars) {
|
||||
TSLength result;
|
||||
result.bytes = bytes;
|
||||
result.chars = chars;
|
||||
return result;
|
||||
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;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ static void ts_lexer__get_chunk(TSLexer *self) {
|
|||
TSInput input = self->input;
|
||||
if (!self->chunk ||
|
||||
self->current_position.bytes != self->chunk_start + self->chunk_size)
|
||||
input.seek_fn(input.payload, self->current_position);
|
||||
input.seek_fn(input.payload, self->current_position.chars, self->current_position.bytes);
|
||||
|
||||
self->chunk_start = self->current_position.bytes;
|
||||
self->chunk = input.read_fn(input.payload, &self->chunk_size);
|
||||
|
|
@ -51,10 +51,8 @@ static void ts_lexer__start(TSLexer *self, TSStateId lex_state) {
|
|||
}
|
||||
|
||||
static void ts_lexer__start_token(TSLexer *self) {
|
||||
LOG("start_token chars:%lu, rows:%lu, columns:%lu", self->current_position.chars, self->current_point.row, self->current_point.column);
|
||||
|
||||
LOG("start_token chars:%lu, rows:%lu, columns:%lu", self->current_position.chars, self->current_position.rows, self->current_position.columns);
|
||||
self->token_start_position = self->current_position;
|
||||
self->token_start_point = self->current_point;
|
||||
}
|
||||
|
||||
static bool ts_lexer__advance(TSLexer *self, TSStateId state) {
|
||||
|
|
@ -65,13 +63,13 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) {
|
|||
|
||||
if (self->lookahead_size) {
|
||||
self->current_position.bytes += self->lookahead_size;
|
||||
self->current_position.chars += 1;
|
||||
self->current_position.chars++;
|
||||
|
||||
if (self->lookahead == '\n') {
|
||||
self->current_point.row += 1;
|
||||
self->current_point.column = 0;
|
||||
self->current_position.rows++;
|
||||
self->current_position.columns = 0;
|
||||
} else {
|
||||
self->current_point.column += 1;
|
||||
self->current_position.columns++;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -85,22 +83,16 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) {
|
|||
static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol,
|
||||
TSSymbolMetadata metadata,
|
||||
const char *symbol_name) {
|
||||
TSLength size =
|
||||
ts_length_sub(self->current_position, self->token_start_position);
|
||||
TSLength padding =
|
||||
ts_length_sub(self->token_start_position, self->token_end_position);
|
||||
TSLength size = ts_length_sub(self->current_position, self->token_start_position);
|
||||
TSLength padding = ts_length_sub(self->token_start_position, self->token_end_position);
|
||||
self->token_end_position = self->current_position;
|
||||
|
||||
TSPoint size_point = ts_point_sub(self->current_point, self ->token_start_point);
|
||||
TSPoint padding_point = ts_point_sub(self->token_start_point, self->token_end_point);
|
||||
self->token_end_point = self->current_point;
|
||||
|
||||
if (symbol == ts_builtin_sym_error) {
|
||||
LOG("error_char");
|
||||
return ts_tree_make_error(size, padding, size_point, padding_point, self->lookahead);
|
||||
return ts_tree_make_error(size, padding, self->lookahead);
|
||||
} else {
|
||||
LOG("accept_token sym:%s", symbol_name);
|
||||
return ts_tree_make_leaf(symbol, padding, size, padding_point, size_point, metadata);
|
||||
return ts_tree_make_leaf(symbol, padding, size, metadata);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -119,19 +111,15 @@ TSLexer ts_lexer_make() {
|
|||
.chunk_start = 0,
|
||||
.debugger = ts_debugger_null(),
|
||||
};
|
||||
ts_lexer_reset(&result, ts_length_zero(), ts_point_zero());
|
||||
ts_lexer_reset(&result, ts_length_zero());
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline void ts_lexer__reset(TSLexer *self, TSLength position, TSPoint point) {
|
||||
static inline void ts_lexer__reset(TSLexer *self, TSLength position) {
|
||||
self->token_start_position = position;
|
||||
self->token_end_position = position;
|
||||
self->current_position = position;
|
||||
|
||||
self->token_start_point = point;
|
||||
self->token_end_point = point;
|
||||
self->current_point = point;
|
||||
|
||||
self->chunk = 0;
|
||||
self->chunk_start = 0;
|
||||
self->chunk_size = 0;
|
||||
|
|
@ -141,11 +129,11 @@ static inline void ts_lexer__reset(TSLexer *self, TSLength position, TSPoint poi
|
|||
|
||||
void ts_lexer_set_input(TSLexer *self, TSInput input) {
|
||||
self->input = input;
|
||||
ts_lexer__reset(self, ts_length_zero(), ts_point_zero());
|
||||
ts_lexer__reset(self, ts_length_zero());
|
||||
}
|
||||
|
||||
void ts_lexer_reset(TSLexer *self, TSLength position, TSPoint point) {
|
||||
void ts_lexer_reset(TSLexer *self, TSLength position) {
|
||||
if (!ts_length_eq(position, self->current_position))
|
||||
ts_lexer__reset(self, position, point);
|
||||
ts_lexer__reset(self, position);
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ extern "C" {
|
|||
|
||||
TSLexer ts_lexer_make();
|
||||
void ts_lexer_set_input(TSLexer *, TSInput);
|
||||
void ts_lexer_reset(TSLexer *, TSLength, TSPoint);
|
||||
void ts_lexer_reset(TSLexer *, TSLength);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
#include <stdbool.h>
|
||||
#include "runtime/node.h"
|
||||
#include "runtime/length.h"
|
||||
#include "runtime/tree.h"
|
||||
#include "runtime/document.h"
|
||||
|
||||
TSNode ts_node_make(const TSTree *tree, TSLength offset, size_t row) {
|
||||
return (TSNode){.data = tree, .offset = offset, .row = row };
|
||||
TSNode ts_node_make(const TSTree *tree, size_t chars, size_t byte, size_t row) {
|
||||
return (TSNode){.data = tree, .offset = {chars, byte, row} };
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -13,57 +12,83 @@ TSNode ts_node_make(const TSTree *tree, TSLength offset, size_t row) {
|
|||
*/
|
||||
|
||||
static inline TSNode ts_node__null() {
|
||||
return ts_node_make(NULL, ts_length_zero(), 0);
|
||||
return ts_node_make(NULL, 0, 0, 0);
|
||||
}
|
||||
|
||||
static inline const TSTree *ts_node__tree(TSNode self) {
|
||||
return self.data;
|
||||
}
|
||||
|
||||
static inline TSLength ts_node__offset(TSNode self) {
|
||||
return self.offset;
|
||||
static inline size_t ts_node__offset_char(TSNode self) {
|
||||
return self.offset[0];
|
||||
}
|
||||
|
||||
static inline bool ts_tree__is_relevant(const TSTree *tree, bool include_anonymous) {
|
||||
static inline size_t ts_node__offset_byte(TSNode self) {
|
||||
return self.offset[1];
|
||||
}
|
||||
|
||||
static inline size_t ts_node__offset_row(TSNode self) {
|
||||
return self.offset[2];
|
||||
}
|
||||
|
||||
static inline bool ts_node__is_relevant(TSNode self, bool include_anonymous) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
return include_anonymous ? tree->options.visible : tree->options.named;
|
||||
}
|
||||
|
||||
static inline size_t ts_tree__relevant_child_count(const TSTree *tree,
|
||||
bool include_anonymous) {
|
||||
static inline size_t ts_node__relevant_child_count(TSNode self, bool include_anonymous) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
return include_anonymous ? tree->visible_child_count : tree->named_child_count;
|
||||
}
|
||||
|
||||
static inline TSNode ts_node__direct_parent(TSNode self, size_t *index) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
*index = tree->context.index;
|
||||
return ts_node_make(
|
||||
tree->context.parent,
|
||||
ts_node__offset_char(self) - tree->context.offset.chars,
|
||||
ts_node__offset_byte(self) - tree->context.offset.bytes,
|
||||
ts_node__offset_row(self) - tree->context.offset.rows
|
||||
);
|
||||
}
|
||||
|
||||
static inline TSNode ts_node__direct_child(TSNode self, size_t i) {
|
||||
const TSTree *child_tree = ts_node__tree(self)->children[i];
|
||||
return ts_node_make(
|
||||
child_tree,
|
||||
ts_node__offset_char(self) + child_tree->context.offset.chars,
|
||||
ts_node__offset_byte(self) + child_tree->context.offset.bytes,
|
||||
ts_node__offset_row(self) + child_tree->context.offset.rows
|
||||
);
|
||||
}
|
||||
|
||||
static inline TSNode ts_node__child(TSNode self, size_t child_index,
|
||||
bool include_anonymous) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
TSLength position = ts_node__offset(self);
|
||||
size_t offset_row = self.row;
|
||||
|
||||
TSNode result = self;
|
||||
bool did_descend = true;
|
||||
|
||||
while (did_descend) {
|
||||
did_descend = false;
|
||||
|
||||
size_t index = 0;
|
||||
for (size_t i = 0; i < tree->child_count; i++) {
|
||||
TSTree *child = tree->children[i];
|
||||
if (ts_tree__is_relevant(child, include_anonymous)) {
|
||||
for (size_t i = 0; i < ts_node__tree(result)->child_count; i++) {
|
||||
TSNode child = ts_node__direct_child(result, i);
|
||||
if (ts_node__is_relevant(child, include_anonymous)) {
|
||||
if (index == child_index)
|
||||
return ts_node_make(child, position, offset_row);
|
||||
return child;
|
||||
index++;
|
||||
} else {
|
||||
size_t grandchild_index = child_index - index;
|
||||
size_t grandchild_count =
|
||||
ts_tree__relevant_child_count(child, include_anonymous);
|
||||
ts_node__relevant_child_count(child, include_anonymous);
|
||||
if (grandchild_index < grandchild_count) {
|
||||
did_descend = true;
|
||||
tree = child;
|
||||
result = child;
|
||||
child_index = grandchild_index;
|
||||
break;
|
||||
}
|
||||
index += grandchild_count;
|
||||
}
|
||||
position = ts_length_add(position, ts_tree_total_size(child));
|
||||
offset_row += child->padding_point.row + child->size_point.row;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -71,119 +96,110 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index,
|
|||
}
|
||||
|
||||
static inline TSNode ts_node__prev_sibling(TSNode self, bool include_anonymous) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
TSLength offset = ts_node__offset(self);
|
||||
size_t offset_row = self.row;
|
||||
TSNode result = self;
|
||||
|
||||
do {
|
||||
size_t index = tree->context.index;
|
||||
offset = ts_length_sub(offset, tree->context.offset);
|
||||
offset_row -= tree->context.offset_point.row;
|
||||
tree = tree->context.parent;
|
||||
if (!tree)
|
||||
size_t index;
|
||||
result = ts_node__direct_parent(result, &index);
|
||||
if (!result.data)
|
||||
break;
|
||||
|
||||
for (size_t i = index - 1; i + 1 > 0; i--) {
|
||||
const TSTree *child = tree->children[i];
|
||||
TSLength child_offset = ts_length_add(offset, child->context.offset);
|
||||
size_t child_offset_row = offset_row + child->context.offset_point.row;
|
||||
if (ts_tree__is_relevant(child, include_anonymous))
|
||||
return ts_node_make(child, child_offset, child_offset_row);
|
||||
size_t grandchild_count = ts_tree__relevant_child_count(child, include_anonymous);
|
||||
TSNode child = ts_node__direct_child(result, i);
|
||||
if (ts_node__is_relevant(child, include_anonymous))
|
||||
return child;
|
||||
size_t grandchild_count = ts_node__relevant_child_count(child, include_anonymous);
|
||||
if (grandchild_count > 0)
|
||||
return ts_node__child(ts_node_make(child, child_offset, child_offset_row),
|
||||
grandchild_count - 1, include_anonymous);
|
||||
return ts_node__child(child, grandchild_count - 1, include_anonymous);
|
||||
}
|
||||
} while (!ts_tree_is_visible(tree));
|
||||
} while (!ts_tree_is_visible(ts_node__tree(result)));
|
||||
|
||||
return ts_node__null();
|
||||
}
|
||||
|
||||
static inline TSNode ts_node__next_sibling(TSNode self, bool include_anonymous) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
TSLength offset = ts_node__offset(self);
|
||||
size_t offset_row = self.row;
|
||||
TSNode result = self;
|
||||
|
||||
do {
|
||||
size_t index = tree->context.index;
|
||||
offset = ts_length_sub(offset, tree->context.offset);
|
||||
offset_row -= tree->context.offset_point.row;
|
||||
tree = tree->context.parent;
|
||||
if (!tree)
|
||||
size_t index;
|
||||
result = ts_node__direct_parent(result, &index);
|
||||
if (!result.data)
|
||||
break;
|
||||
|
||||
for (size_t i = index + 1; i < tree->child_count; i++) {
|
||||
const TSTree *child = tree->children[i];
|
||||
TSLength child_offset = ts_length_add(offset, child->context.offset);
|
||||
size_t child_offset_row = offset_row + child->context.offset_point.row;
|
||||
if (ts_tree__is_relevant(child, include_anonymous))
|
||||
return ts_node_make(child, child_offset, child_offset_row);
|
||||
size_t grandchild_count = ts_tree__relevant_child_count(child, include_anonymous);
|
||||
for (size_t i = index + 1; i < ts_node__tree(result)->child_count; i++) {
|
||||
TSNode child = ts_node__direct_child(result, i);
|
||||
if (ts_node__is_relevant(child, include_anonymous))
|
||||
return child;
|
||||
size_t grandchild_count = ts_node__relevant_child_count(child, include_anonymous);
|
||||
if (grandchild_count > 0)
|
||||
return ts_node__child(ts_node_make(child, child_offset, child_offset_row), 0,
|
||||
include_anonymous);
|
||||
return ts_node__child(child, 0, include_anonymous);
|
||||
}
|
||||
} while (!ts_tree_is_visible(tree));
|
||||
} while (!ts_tree_is_visible(ts_node__tree(result)));
|
||||
|
||||
return ts_node__null();
|
||||
}
|
||||
|
||||
static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min,
|
||||
static inline TSNode ts_node__descendant_for_range(TSNode self, size_t min,
|
||||
size_t max,
|
||||
bool include_anonymous) {
|
||||
const TSTree *tree = ts_node__tree(self), *last_visible_tree = tree;
|
||||
TSLength offset = ts_node__offset(self), last_visible_position = offset;
|
||||
size_t offset_row = self.row, last_visible_row = offset_row;
|
||||
TSNode node = self;
|
||||
TSNode last_visible_node = self;
|
||||
|
||||
bool did_descend = true;
|
||||
while (did_descend) {
|
||||
did_descend = false;
|
||||
|
||||
for (size_t i = 0; i < tree->child_count; i++) {
|
||||
const TSTree *child = tree->children[i];
|
||||
if (offset.chars + child->padding.chars > min)
|
||||
for (size_t i = 0; i < ts_node__tree(node)->child_count; i++) {
|
||||
TSNode child = ts_node__direct_child(node, i);
|
||||
if (ts_node_start_char(child) > min)
|
||||
break;
|
||||
if (offset.chars + child->padding.chars + child->size.chars > max) {
|
||||
tree = child;
|
||||
if (ts_tree__is_relevant(child, include_anonymous)) {
|
||||
last_visible_tree = tree;
|
||||
last_visible_position = offset;
|
||||
last_visible_row = offset_row;
|
||||
}
|
||||
if (ts_node_end_char(child) > max) {
|
||||
node = child;
|
||||
if (ts_node__is_relevant(node, include_anonymous))
|
||||
last_visible_node = node;
|
||||
did_descend = true;
|
||||
break;
|
||||
}
|
||||
offset = ts_length_add(offset, ts_tree_total_size(child));
|
||||
offset_row += child->padding_point.row + child->size_point.row;
|
||||
}
|
||||
}
|
||||
|
||||
return ts_node_make(last_visible_tree, last_visible_position, last_visible_row);
|
||||
return last_visible_node;
|
||||
}
|
||||
|
||||
/*
|
||||
* Public
|
||||
*/
|
||||
|
||||
TSLength ts_node_pos(TSNode self) {
|
||||
return ts_length_add(ts_node__offset(self), ts_node__tree(self)->padding);
|
||||
size_t ts_node_start_char(TSNode self) {
|
||||
return ts_node__offset_char(self) + ts_node__tree(self)->padding.chars;
|
||||
}
|
||||
|
||||
TSLength ts_node_size(TSNode self) {
|
||||
return ts_node__tree(self)->size;
|
||||
size_t ts_node_end_char(TSNode self) {
|
||||
return ts_node_start_char(self) + ts_node__tree(self)->size.chars;
|
||||
}
|
||||
|
||||
TSPoint ts_node_size_point(TSNode self) {
|
||||
return ts_node__tree(self)->size_point;
|
||||
size_t ts_node_start_byte(TSNode self) {
|
||||
return ts_node__offset_byte(self) + ts_node__tree(self)->padding.bytes;
|
||||
}
|
||||
|
||||
size_t ts_node_end_byte(TSNode self) {
|
||||
return ts_node_start_byte(self) + ts_node__tree(self)->size.bytes;
|
||||
}
|
||||
|
||||
TSPoint ts_node_start_point(TSNode self) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
return ts_point_make(self.row + tree->padding_point.row, ts_tree_offset_column(tree));
|
||||
return (TSPoint){
|
||||
ts_node__offset_row(self) + tree->padding.rows,
|
||||
ts_tree_start_column(tree)
|
||||
};
|
||||
}
|
||||
|
||||
TSPoint ts_node_end_point(TSNode self) {
|
||||
return ts_point_add(ts_node_start_point(self), ts_node_size_point(self));
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
return (TSPoint){
|
||||
ts_node__offset_row(self) + tree->padding.rows + tree->size.rows,
|
||||
ts_tree_end_column(tree)
|
||||
};
|
||||
}
|
||||
|
||||
TSSymbol ts_node_symbol(TSNode self) {
|
||||
|
|
@ -201,8 +217,9 @@ const char *ts_node_string(TSNode self, const TSDocument *document) {
|
|||
|
||||
bool ts_node_eq(TSNode self, TSNode other) {
|
||||
return ts_tree_eq(ts_node__tree(self), ts_node__tree(other)) &&
|
||||
ts_length_eq(ts_node__offset(self), ts_node__offset(other)) &&
|
||||
self.row == other.row;
|
||||
self.offset[0] == other.offset[0] &&
|
||||
self.offset[1] == other.offset[1] &&
|
||||
self.offset[2] == other.offset[2];
|
||||
}
|
||||
|
||||
bool ts_node_is_named(TSNode self) {
|
||||
|
|
@ -214,20 +231,16 @@ bool ts_node_has_changes(TSNode self) {
|
|||
}
|
||||
|
||||
TSNode ts_node_parent(TSNode self) {
|
||||
const TSTree *tree = ts_node__tree(self);
|
||||
TSLength offset = ts_node__offset(self);
|
||||
size_t offset_row = self.row;
|
||||
TSNode result = self;
|
||||
size_t index;
|
||||
|
||||
do {
|
||||
offset = ts_length_sub(offset, tree->context.offset);
|
||||
offset_row -= tree->context.offset_point.row;
|
||||
|
||||
tree = tree->context.parent;
|
||||
if (!tree)
|
||||
result = ts_node__direct_parent(result, &index);
|
||||
if (!result.data)
|
||||
return ts_node__null();
|
||||
} while (!ts_tree_is_visible(tree));
|
||||
} while (!ts_tree_is_visible(result.data));
|
||||
|
||||
return ts_node_make(tree, offset, offset_row);
|
||||
return result;
|
||||
}
|
||||
|
||||
TSNode ts_node_child(TSNode self, size_t child_index) {
|
||||
|
|
@ -262,10 +275,10 @@ TSNode ts_node_prev_named_sibling(TSNode self) {
|
|||
return ts_node__prev_sibling(self, false);
|
||||
}
|
||||
|
||||
TSNode ts_node_descendent_for_range(TSNode self, size_t min, size_t max) {
|
||||
return ts_node__descendent_for_range(self, min, max, true);
|
||||
TSNode ts_node_descendant_for_range(TSNode self, size_t min, size_t max) {
|
||||
return ts_node__descendant_for_range(self, min, max, true);
|
||||
}
|
||||
|
||||
TSNode ts_node_named_descendent_for_range(TSNode self, size_t min, size_t max) {
|
||||
return ts_node__descendent_for_range(self, min, max, false);
|
||||
TSNode ts_node_named_descendant_for_range(TSNode self, size_t min, size_t max) {
|
||||
return ts_node__descendant_for_range(self, min, max, false);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,9 +2,7 @@
|
|||
#define RUNTIME_NODE_H_
|
||||
|
||||
#include "tree_sitter/parser.h"
|
||||
#include "runtime/length.h"
|
||||
#include "runtime/tree.h"
|
||||
|
||||
TSNode ts_node_make(const TSTree *, TSLength, size_t);
|
||||
TSNode ts_node_make(const TSTree *, size_t character, size_t byte, size_t row);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -58,8 +58,7 @@ static bool ts_parser__breakdown_reusable_subtree(LookaheadState *state) {
|
|||
* NULL if no right neighbor exists.
|
||||
*/
|
||||
static void ts_parser__pop_reusable_subtree(LookaheadState *state) {
|
||||
state->reusable_subtree_pos +=
|
||||
ts_tree_total_size(state->reusable_subtree).chars;
|
||||
state->reusable_subtree_pos += ts_tree_total_chars(state->reusable_subtree);
|
||||
|
||||
while (state->reusable_subtree) {
|
||||
TSTree *parent = state->reusable_subtree->context.parent;
|
||||
|
|
@ -304,10 +303,8 @@ static void ts_parser__reduce_error(TSParser *self, int head,
|
|||
TSTree **parent = vector_back(&self->reduce_parents);
|
||||
StackEntry *stack_entry = ts_stack_head(self->stack, head);
|
||||
stack_entry->position = ts_length_add(stack_entry->position, lookahead->padding);
|
||||
stack_entry->position_point = ts_point_add(stack_entry->position_point, lookahead->padding_point);
|
||||
(*parent)->size = ts_length_add((*parent)->size, lookahead->padding);
|
||||
lookahead->padding = ts_length_zero();
|
||||
lookahead->padding_point = ts_point_zero();
|
||||
ts_tree_set_fragile_left(*parent);
|
||||
ts_tree_set_fragile_right(*parent);
|
||||
}
|
||||
|
|
@ -546,7 +543,6 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) {
|
|||
for (int head = 0; head < ts_stack_head_count(self->stack);) {
|
||||
StackEntry *entry = ts_stack_head(self->stack, head);
|
||||
TSLength position = entry ? entry->position : ts_length_zero();
|
||||
TSPoint position_point = entry ? entry->position_point : ts_point_zero();
|
||||
|
||||
LOG("process head:%d, head_count:%d, state:%d, pos:%lu", head,
|
||||
ts_stack_head_count(self->stack),
|
||||
|
|
@ -559,7 +555,7 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) {
|
|||
lookahead = reused_lookahead;
|
||||
} else {
|
||||
last_position = position;
|
||||
ts_lexer_reset(&self->lexer, position, position_point);
|
||||
ts_lexer_reset(&self->lexer, position);
|
||||
TSStateId parse_state = ts_stack_top_state(self->stack, head);
|
||||
TSStateId lex_state = self->language->lex_states[parse_state];
|
||||
lookahead = self->language->lex_fn(&self->lexer, lex_state);
|
||||
|
|
@ -567,7 +563,7 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input, TSTree *previous_tree) {
|
|||
}
|
||||
|
||||
LOG("lookahead sym:%s, size:%lu", SYM_NAME(lookahead->symbol),
|
||||
ts_tree_total_size(lookahead).chars);
|
||||
ts_tree_total_chars(lookahead));
|
||||
|
||||
switch (ts_parser__consume_lookahead(self, head, lookahead)) {
|
||||
case ConsumeResultRemoved:
|
||||
|
|
|
|||
|
|
@ -126,18 +126,15 @@ static StackNode *stack_node_new(StackNode *next, TSStateId state, TSTree *tree)
|
|||
ts_tree_retain(tree);
|
||||
stack_node_retain(next);
|
||||
TSLength position = ts_tree_total_size(tree);
|
||||
TSPoint position_point = ts_tree_total_size_point(tree);
|
||||
if (next) {
|
||||
if (next)
|
||||
position = ts_length_add(next->entry.position, position);
|
||||
position_point = ts_point_add(next->entry.position_point, position_point);
|
||||
}
|
||||
*self = (StackNode){
|
||||
.ref_count = 1,
|
||||
.successor_count = 1,
|
||||
.successors = { next, NULL, NULL },
|
||||
.entry =
|
||||
{
|
||||
.state = state, .tree = tree, .position = position, .position_point = position_point,
|
||||
.state = state, .tree = tree, .position = position
|
||||
},
|
||||
};
|
||||
return self;
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ typedef struct {
|
|||
TSTree *tree;
|
||||
TSStateId state;
|
||||
TSLength position;
|
||||
TSPoint position_point;
|
||||
} StackEntry;
|
||||
|
||||
typedef struct {
|
||||
|
|
|
|||
|
|
@ -19,10 +19,10 @@ const char *ts_string_input_read(void *payload, size_t *bytes_read) {
|
|||
return input->string + previous_position;
|
||||
}
|
||||
|
||||
int ts_string_input_seek(void *payload, TSLength position) {
|
||||
int ts_string_input_seek(void *payload, size_t character, size_t byte) {
|
||||
TSStringInput *input = (TSStringInput *)payload;
|
||||
input->position = position.bytes;
|
||||
return (position.bytes < input->length);
|
||||
input->position = byte;
|
||||
return (byte < input->length);
|
||||
}
|
||||
|
||||
TSInput ts_string_input_make(const char *string) {
|
||||
|
|
|
|||
|
|
@ -7,8 +7,6 @@
|
|||
#include "runtime/length.h"
|
||||
|
||||
TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size,
|
||||
TSPoint padding_point,
|
||||
TSPoint size_point,
|
||||
TSSymbolMetadata metadata) {
|
||||
TSTree *result = malloc(sizeof(TSTree));
|
||||
*result = (TSTree){
|
||||
|
|
@ -20,8 +18,6 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size,
|
|||
.named_child_count = 0,
|
||||
.children = NULL,
|
||||
.padding = padding,
|
||||
.padding_point = padding_point,
|
||||
.size_point = size_point,
|
||||
.options =
|
||||
{
|
||||
.visible = metadata.visible, .named = metadata.named,
|
||||
|
|
@ -36,33 +32,26 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size,
|
|||
return result;
|
||||
}
|
||||
|
||||
TSTree *ts_tree_make_error(TSLength size, TSLength padding,
|
||||
TSPoint size_point,
|
||||
TSPoint padding_point,
|
||||
char lookahead_char) {
|
||||
TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char) {
|
||||
TSTree *result =
|
||||
ts_tree_make_leaf(ts_builtin_sym_error, padding, size, padding_point,
|
||||
size_point, (TSSymbolMetadata){
|
||||
.visible = true, .named = true,
|
||||
});
|
||||
ts_tree_make_leaf(ts_builtin_sym_error, padding, size, (TSSymbolMetadata){
|
||||
.visible = true, .named = true,
|
||||
});
|
||||
result->lookahead_char = lookahead_char;
|
||||
return result;
|
||||
}
|
||||
|
||||
void ts_tree_assign_parents(TSTree *self) {
|
||||
TSLength offset = ts_length_zero();
|
||||
TSPoint offset_point = ts_point_zero();
|
||||
for (size_t i = 0; i < self->child_count; i++) {
|
||||
TSTree *child = self->children[i];
|
||||
if (child->context.parent != self) {
|
||||
child->context.parent = self;
|
||||
child->context.index = i;
|
||||
child->context.offset = offset;
|
||||
child->context.offset_point = offset_point;
|
||||
ts_tree_assign_parents(child);
|
||||
}
|
||||
offset = ts_length_add(offset, ts_tree_total_size(child));
|
||||
offset_point = ts_point_add(offset_point, ts_tree_total_size_point(child));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -78,12 +67,8 @@ void ts_tree_set_children(TSTree *self, size_t child_count, TSTree **children) {
|
|||
if (i == 0) {
|
||||
self->padding = child->padding;
|
||||
self->size = child->size;
|
||||
self->padding_point = child->padding_point;
|
||||
self->size_point = child->size_point;
|
||||
} else {
|
||||
self->size =
|
||||
ts_length_add(ts_length_add(self->size, child->padding), child->size);
|
||||
self->size_point = ts_point_add(ts_point_add(self->size_point, child->padding_point), child->size_point);
|
||||
self->size = ts_length_add(self->size, ts_tree_total_size(child));
|
||||
}
|
||||
|
||||
if (child->options.visible) {
|
||||
|
|
@ -107,7 +92,7 @@ void ts_tree_set_children(TSTree *self, size_t child_count, TSTree **children) {
|
|||
TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count,
|
||||
TSTree **children, TSSymbolMetadata metadata) {
|
||||
TSTree *result =
|
||||
ts_tree_make_leaf(symbol, ts_length_zero(), ts_length_zero(), ts_point_zero(), ts_point_zero(), metadata);
|
||||
ts_tree_make_leaf(symbol, ts_length_zero(), ts_length_zero(), metadata);
|
||||
ts_tree_set_children(result, child_count, children);
|
||||
return result;
|
||||
}
|
||||
|
|
@ -129,32 +114,23 @@ void ts_tree_release(TSTree *self) {
|
|||
}
|
||||
}
|
||||
|
||||
size_t ts_tree_offset_column(const TSTree *self) {
|
||||
size_t column = self->padding_point.column;
|
||||
|
||||
if (self->padding_point.row > 0) {
|
||||
size_t ts_tree_start_column(const TSTree *self) {
|
||||
size_t column = self->padding.columns;
|
||||
if (self->padding.rows > 0)
|
||||
return column;
|
||||
for (const TSTree *tree = self; tree != NULL; tree = tree->context.parent) {
|
||||
column += tree->context.offset.columns;
|
||||
if (tree->context.offset.rows > 0)
|
||||
break;
|
||||
}
|
||||
|
||||
const TSTree *parent = self;
|
||||
TSPoint offset_point;
|
||||
do {
|
||||
offset_point = parent->context.offset_point;
|
||||
column += offset_point.column;
|
||||
|
||||
parent = parent->context.parent;
|
||||
if (!parent) break;
|
||||
} while (offset_point.row == 0);
|
||||
|
||||
return column;
|
||||
}
|
||||
|
||||
TSLength ts_tree_total_size(const TSTree *self) {
|
||||
return ts_length_add(self->padding, self->size);
|
||||
}
|
||||
|
||||
TSPoint ts_tree_total_size_point(const TSTree *self) {
|
||||
return ts_point_add(self->padding_point, self->size_point);
|
||||
size_t ts_tree_end_column(const TSTree *self) {
|
||||
size_t result = self->size.columns;
|
||||
if (self->size.rows == 0)
|
||||
result += ts_tree_start_column(self);
|
||||
return result;
|
||||
}
|
||||
|
||||
bool ts_tree_eq(const TSTree *self, const TSTree *other) {
|
||||
|
|
@ -274,26 +250,26 @@ void ts_tree_edit(TSTree *self, TSInputEdit edit) {
|
|||
size_t start = edit.position;
|
||||
size_t new_end = edit.position + edit.chars_inserted;
|
||||
size_t old_end = edit.position + edit.chars_removed;
|
||||
assert(old_end <= ts_tree_total_size(self).chars);
|
||||
assert(old_end <= ts_tree_total_chars(self));
|
||||
|
||||
self->options.has_changes = true;
|
||||
|
||||
if (start < self->padding.chars) {
|
||||
self->padding.bytes = 0;
|
||||
ts_length_set_unknown(&self->padding);
|
||||
long remaining_padding = self->padding.chars - old_end;
|
||||
if (remaining_padding >= 0) {
|
||||
self->padding.chars = new_end + remaining_padding;
|
||||
} else {
|
||||
self->padding.chars = new_end;
|
||||
self->size.chars += remaining_padding;
|
||||
self->size.bytes = 0;
|
||||
ts_length_set_unknown(&self->size);
|
||||
}
|
||||
} else if (start == self->padding.chars && edit.chars_removed == 0) {
|
||||
self->padding.bytes = 0;
|
||||
self->padding.chars += edit.chars_inserted;
|
||||
ts_length_set_unknown(&self->padding);
|
||||
} else {
|
||||
self->size.bytes = 0;
|
||||
self->size.chars += (edit.chars_inserted - edit.chars_removed);
|
||||
ts_length_set_unknown(&self->size);
|
||||
}
|
||||
|
||||
bool found_first_child = false;
|
||||
|
|
@ -301,7 +277,7 @@ void ts_tree_edit(TSTree *self, TSInputEdit edit) {
|
|||
size_t child_left = 0, child_right = 0;
|
||||
for (size_t i = 0; i < self->child_count; i++) {
|
||||
TSTree *child = self->children[i];
|
||||
size_t child_size = ts_tree_total_size(child).chars;
|
||||
size_t child_size = ts_tree_total_chars(child);
|
||||
child_left = child_right;
|
||||
child_right += child_size;
|
||||
|
||||
|
|
|
|||
|
|
@ -7,13 +7,13 @@ extern "C" {
|
|||
|
||||
#include <stdbool.h>
|
||||
#include "tree_sitter/parser.h"
|
||||
#include "runtime/length.h"
|
||||
|
||||
struct TSTree {
|
||||
struct {
|
||||
struct TSTree *parent;
|
||||
size_t index;
|
||||
TSLength offset;
|
||||
TSPoint offset_point;
|
||||
} context;
|
||||
size_t child_count;
|
||||
size_t visible_child_count;
|
||||
|
|
@ -25,9 +25,6 @@ struct TSTree {
|
|||
TSLength padding;
|
||||
TSLength size;
|
||||
|
||||
TSPoint padding_point;
|
||||
TSPoint size_point;
|
||||
|
||||
TSSymbol symbol;
|
||||
|
||||
struct {
|
||||
|
|
@ -41,12 +38,9 @@ struct TSTree {
|
|||
unsigned short int ref_count;
|
||||
};
|
||||
|
||||
TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, TSPoint,
|
||||
TSPoint, TSSymbolMetadata);
|
||||
TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, TSSymbolMetadata);
|
||||
TSTree *ts_tree_make_node(TSSymbol, size_t, TSTree **, TSSymbolMetadata);
|
||||
TSTree *ts_tree_make_error(TSLength size, TSLength padding,
|
||||
TSPoint padding_point,
|
||||
TSPoint size_point, char lookahead_char);
|
||||
TSTree *ts_tree_make_error(TSLength, TSLength, char);
|
||||
void ts_tree_retain(TSTree *tree);
|
||||
void ts_tree_release(TSTree *tree);
|
||||
bool ts_tree_eq(const TSTree *tree1, const TSTree *tree2);
|
||||
|
|
@ -54,13 +48,27 @@ int ts_tree_compare(const TSTree *tree1, const TSTree *tree2);
|
|||
char *ts_tree_string(const TSTree *tree, const char **names,
|
||||
bool include_anonymous);
|
||||
|
||||
size_t ts_tree_offset_column(const TSTree *self);
|
||||
TSLength ts_tree_total_size(const TSTree *tree);
|
||||
TSPoint ts_tree_total_size_point(const TSTree *self);
|
||||
size_t ts_tree_start_column(const TSTree *self);
|
||||
size_t ts_tree_end_column(const TSTree *self);
|
||||
void ts_tree_set_children(TSTree *, size_t, TSTree **);
|
||||
void ts_tree_assign_parents(TSTree *);
|
||||
void ts_tree_edit(TSTree *, TSInputEdit);
|
||||
|
||||
static inline size_t ts_tree_total_chars(const TSTree *self) {
|
||||
return self->padding.chars + self->size.chars;
|
||||
}
|
||||
|
||||
static inline TSLength ts_tree_total_size(const TSTree *self) {
|
||||
return ts_length_add(self->padding, self->size);
|
||||
}
|
||||
|
||||
static inline TSPoint ts_tree_extent(const TSTree *tree) {
|
||||
TSPoint result;
|
||||
result.row = tree->size.rows;
|
||||
result.column = tree->size.columns;
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline bool ts_tree_is_extra(const TSTree *tree) {
|
||||
return tree->options.extra;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue