From b0f6bac3ab7745b62b8e2f153a3b69d9fabb5012 Mon Sep 17 00:00:00 2001 From: joshvera Date: Wed, 18 Nov 2015 16:34:50 -0800 Subject: [PATCH] replace start and end with padding and size --- include/tree_sitter/parser.h | 6 +++--- include/tree_sitter/runtime.h | 5 ++--- spec/runtime/tree_spec.cc | 16 +++++++-------- src/runtime/document.c | 2 +- src/runtime/length.h | 34 +++++++++++++++++++++++++++---- src/runtime/lexer.c | 31 ++++++++++++++++------------ src/runtime/lexer.h | 2 +- src/runtime/node.c | 38 +++++++++++++++++++++++------------ src/runtime/node.h | 2 +- src/runtime/parser.c | 9 ++++++--- src/runtime/tree.c | 22 +++++++++++--------- src/runtime/tree.h | 17 ++++++++++------ 12 files changed, 119 insertions(+), 65 deletions(-) diff --git a/include/tree_sitter/parser.h b/include/tree_sitter/parser.h index 9aa4939f..722c5208 100644 --- a/include/tree_sitter/parser.h +++ b/include/tree_sitter/parser.h @@ -38,9 +38,9 @@ typedef struct TSLexer { TSLength token_end_position; TSLength token_start_position; - TSSourceInfo current_source_info; - TSSourceInfo token_end_source_info; - TSSourceInfo token_start_source_info; + TSPoint current_point; + TSPoint token_end_point; + TSPoint token_start_point; size_t lookahead_size; int32_t lookahead; diff --git a/include/tree_sitter/runtime.h b/include/tree_sitter/runtime.h index c358f437..fc041596 100644 --- a/include/tree_sitter/runtime.h +++ b/include/tree_sitter/runtime.h @@ -38,13 +38,12 @@ typedef struct { typedef struct { size_t line; size_t column; -} TSSourceInfo; +} TSPoint; typedef struct { const void *data; TSLength offset; - TSSourceInfo start; - TSSourceInfo end; + TSPoint point; } TSNode; typedef unsigned short TSSymbol; diff --git a/spec/runtime/tree_spec.cc b/spec/runtime/tree_spec.cc index 33e344e2..99125dbe 100644 --- a/spec/runtime/tree_spec.cc +++ b/spec/runtime/tree_spec.cc @@ -29,8 +29,8 @@ describe("Tree", []() { TSTree *tree1, *tree2, *parent1; before_each([&]() { - tree1 = ts_tree_make_leaf(cat, {2, 1}, {5, 4}, ts_source_info_zero(), ts_source_info_zero(), TSNodeTypeNamed); - tree2 = ts_tree_make_leaf(cat, {1, 1}, {3, 3}, ts_source_info_zero(), ts_source_info_zero(), TSNodeTypeNamed); + tree1 = ts_tree_make_leaf(cat, {2, 1}, {5, 4}, ts_point_zero(), ts_point_zero(), TSNodeTypeNamed); + tree2 = ts_tree_make_leaf(cat, {1, 1}, {3, 3}, ts_point_zero(), ts_point_zero(), TSNodeTypeNamed); parent1 = ts_tree_make_node(dog, 2, tree_array({ tree1, tree2, @@ -55,8 +55,8 @@ describe("Tree", []() { TSTree *error_tree = ts_tree_make_error( ts_length_zero(), ts_length_zero(), - ts_source_info_zero(), - ts_source_info_zero(), + ts_point_zero(), + ts_point_zero(), 'z'); AssertThat(ts_tree_is_fragile_left(error_tree), IsTrue()); @@ -291,8 +291,8 @@ describe("Tree", []() { tree1->symbol + 1, tree1->padding, tree1->size, - tree1->start_source_info, - tree1->end_source_info, + tree1->padding_point, + tree1->size_point, TSNodeTypeNamed); AssertThat(ts_tree_eq(tree1, different_tree), IsFalse()); @@ -304,8 +304,8 @@ describe("Tree", []() { tree1->symbol + 1, tree1->padding, tree1->size, - tree1->start_source_info, - tree1->end_source_info, + tree1->padding_point, + tree1->size_point, TSNodeTypeNamed); TSTree *different_parent = ts_tree_make_node(dog, 2, tree_array({ diff --git a/src/runtime/document.c b/src/runtime/document.c index 091bda23..7042516b 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -85,7 +85,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()); + TSNode result = ts_node_make(self->tree, ts_length_zero(), ts_point_zero()); while (result.data && !ts_tree_is_visible(result.data)) result = ts_node_named_child(result, 0); return result; diff --git a/src/runtime/length.h b/src/runtime/length.h index 482e49a1..562e626d 100644 --- a/src/runtime/length.h +++ b/src/runtime/length.h @@ -26,6 +26,32 @@ static inline TSLength ts_length_sub(TSLength len1, TSLength len2) { return result; } +static inline TSPoint ts_point_add(TSPoint point1, TSPoint point2) { + size_t line = point1.line + point2.line; + + size_t column; + if (point2.line == 0) { + column = point1.column + point2.column; + } else { + column = point2.column; + } + + return (TSPoint){ .line = line, .column = column }; +} + +static inline TSPoint ts_point_sub(TSPoint point1, TSPoint point2) { + size_t line, column; + if (point1.line == point2.line) { + line = 0; + column = point1.column - point2.column; + } else { + line = point1.line - point2.line; + column = point1.column; + } + + return (TSPoint){ .line = line, .column = column }; +} + static inline TSLength ts_length_zero() { TSLength result; result.bytes = result.chars = 0; @@ -36,12 +62,12 @@ static inline bool ts_length_eq(TSLength len1, TSLength len2) { return len1.bytes == len2.bytes && len1.chars == len2.chars; } -static inline TSSourceInfo ts_source_info_zero() { - return (TSSourceInfo){ .line = 1, .column = 1 }; +static inline TSPoint ts_point_zero() { + return (TSPoint){ .line = 1, .column = 1 }; } -static inline TSSourceInfo ts_source_info_make(size_t line, size_t column) { - return (TSSourceInfo){ .line = line, .column = column }; +static inline TSPoint ts_point_make(size_t line, size_t column) { + return (TSPoint){ .line = line, .column = column }; } static inline TSLength ts_length_make(size_t bytes, size_t chars) { diff --git a/src/runtime/lexer.c b/src/runtime/lexer.c index 2e31accf..50fddf88 100644 --- a/src/runtime/lexer.c +++ b/src/runtime/lexer.c @@ -54,9 +54,9 @@ static void ts_lexer__start_token(TSLexer *self) { DEBUG("start_token chars:%lu", self->current_position.chars); self->token_start_position = self->current_position; - DEBUG("start_token line:%lu", self->current_source_info.line); - DEBUG("start_token column:%lu", self->current_source_info.column); - self->token_start_source_info = self->current_source_info; + DEBUG("start_token line:%lu", self->current_point.line); + DEBUG("start_token column:%lu", self->current_point.column); + self->token_start_point = self->current_point; } static bool ts_lexer__advance(TSLexer *self, TSStateId state) { @@ -70,10 +70,10 @@ static bool ts_lexer__advance(TSLexer *self, TSStateId state) { self->current_position.chars += 1; if (self->lookahead == '\n') { - self->current_source_info.line += 1; - self->current_source_info.column = 0; + self->current_point.line += 1; + self->current_point.column = 0; } else { - self->current_source_info.column += 1; + self->current_point.column += 1; } } @@ -92,17 +92,17 @@ static TSTree *ts_lexer__accept(TSLexer *self, TSSymbol symbol, ts_length_sub(self->token_start_position, self->token_end_position); self->token_end_position = self->current_position; - self->token_end_source_info = self->current_source_info; + self->token_end_point = self->current_point; if (symbol == ts_builtin_sym_error) { DEBUG("error_char"); - return ts_tree_make_error(size, padding, self->token_start_source_info, - self->token_end_source_info, self->lookahead); + return ts_tree_make_error(size, padding, self->token_start_point, + self->token_end_point, self->lookahead); } else { DEBUG("accept_token sym:%s", symbol_name); return ts_tree_make_leaf(symbol, padding, size, - self->token_start_source_info, - self->token_end_source_info, node_type); + self->token_start_point, + self->token_end_point, node_type); } } @@ -121,14 +121,19 @@ TSLexer ts_lexer_make() { .chunk_start = 0, .debugger = ts_debugger_null(), }; - ts_lexer_reset(&result, ts_length_zero()); + ts_lexer_reset(&result, ts_length_zero(), ts_point_zero()); return result; } -void ts_lexer_reset(TSLexer *self, TSLength position) { +void ts_lexer_reset(TSLexer *self, TSLength position, TSPoint point) { 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; diff --git a/src/runtime/lexer.h b/src/runtime/lexer.h index b127d483..2c4b5812 100644 --- a/src/runtime/lexer.h +++ b/src/runtime/lexer.h @@ -8,7 +8,7 @@ extern "C" { #include "tree_sitter/parser.h" TSLexer ts_lexer_make(); -void ts_lexer_reset(TSLexer *, TSLength); +void ts_lexer_reset(TSLexer *, TSLength, TSPoint); #ifdef __cplusplus } diff --git a/src/runtime/node.c b/src/runtime/node.c index a4968b24..a36105c5 100644 --- a/src/runtime/node.c +++ b/src/runtime/node.c @@ -4,11 +4,8 @@ #include "runtime/tree.h" #include "runtime/document.h" -TSNode ts_node_make(const TSTree *tree, TSLength offset) { - TSSourceInfo start = tree != NULL ? tree->start_source_info : ts_source_info_zero(); - TSSourceInfo end = tree != NULL ? tree->end_source_info : ts_source_info_zero(); - - return (TSNode){.data = tree, .offset = offset, .start = start, .end = end }; +TSNode ts_node_make(const TSTree *tree, TSLength offset, TSPoint point) { + return (TSNode){.data = tree, .offset = offset, .point = point }; } /* @@ -16,7 +13,7 @@ TSNode ts_node_make(const TSTree *tree, TSLength offset) { */ static inline TSNode ts_node__null() { - return ts_node_make(NULL, ts_length_zero()); + return ts_node_make(NULL, ts_length_zero(), ts_point_zero()); } static inline const TSTree *ts_node__tree(TSNode self) { @@ -27,10 +24,16 @@ static inline TSLength ts_node__offset(TSNode self) { return self.offset; } + +static inline TSPoint ts_node__point(TSNode self) { + return self.point; +} + static inline TSNode ts_node__child(TSNode self, size_t child_index, TSNodeType type) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); + TSPoint point = ts_node__point(self); bool did_descend = true; while (did_descend) { @@ -41,7 +44,7 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index, TSTree *child = tree->children[i]; if (child->options.type >= type) { if (index == child_index) - return ts_node_make(child, position); + return ts_node_make(child, position, point); index++; } else { size_t grandchild_index = child_index - index; @@ -66,10 +69,12 @@ static inline TSNode ts_node__child(TSNode self, size_t child_index, static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); + TSPoint point = ts_node__point(self); do { size_t index = tree->context.index; position = ts_length_sub(position, tree->context.offset); + point = ts_point_sub(point, tree->context.offset_point); tree = tree->context.parent; if (!tree) break; @@ -77,13 +82,14 @@ static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { for (size_t i = index - 1; i + 1 > 0; i--) { const TSTree *child = tree->children[i]; TSLength child_position = ts_length_add(position, child->context.offset); + TSPoint child_point = ts_point_add(point, child->context.offset_point); if (child->options.type >= type) - return ts_node_make(child, child_position); + return ts_node_make(child, child_position, child_point); size_t grandchild_count = (type == TSNodeTypeNamed) ? child->named_child_count : child->visible_child_count; if (grandchild_count > 0) - return ts_node__child(ts_node_make(child, child_position), + return ts_node__child(ts_node_make(child, child_position, child_point), grandchild_count - 1, type); } } while (!ts_tree_is_visible(tree)); @@ -94,6 +100,7 @@ static inline TSNode ts_node__prev_sibling(TSNode self, TSNodeType type) { static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); + TSPoint point = ts_node__point(self); do { size_t index = tree->context.index; @@ -105,13 +112,14 @@ static inline TSNode ts_node__next_sibling(TSNode self, TSNodeType type) { for (size_t i = index + 1; i < tree->child_count; i++) { const TSTree *child = tree->children[i]; TSLength child_position = ts_length_add(position, child->context.offset); + TSPoint child_point = ts_point_add(point, child->context.offset_point); if (child->options.type >= type) - return ts_node_make(child, child_position); + return ts_node_make(child, child_position, child_point); size_t grandchild_count = (type == TSNodeTypeNamed) ? child->named_child_count : child->visible_child_count; if (grandchild_count > 0) - return ts_node__child(ts_node_make(child, child_position), 0, type); + return ts_node__child(ts_node_make(child, child_position, child_point), 0, type); } } while (!ts_tree_is_visible(tree)); @@ -122,6 +130,7 @@ static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min, size_t max, TSNodeType type) { const TSTree *tree = ts_node__tree(self), *last_visible_tree = tree; TSLength position = ts_node__offset(self), last_visible_position = position; + TSPoint point = ts_node__point(self), last_visible_point = point; bool did_descend = true; while (did_descend) { @@ -144,7 +153,7 @@ static inline TSNode ts_node__descendent_for_range(TSNode self, size_t min, } } - return ts_node_make(last_visible_tree, last_visible_position); + return ts_node_make(last_visible_tree, last_visible_position, last_visible_point); } /* @@ -188,15 +197,18 @@ bool ts_node_has_changes(TSNode self) { TSNode ts_node_parent(TSNode self) { const TSTree *tree = ts_node__tree(self); TSLength position = ts_node__offset(self); + TSPoint point = ts_node__point(self); do { position = ts_length_sub(position, tree->context.offset); + point = ts_point_sub(point, tree->context.offset_point); + tree = tree->context.parent; if (!tree) return ts_node__null(); } while (!ts_tree_is_visible(tree)); - return ts_node_make(tree, position); + return ts_node_make(tree, position, point); } TSNode ts_node_child(TSNode self, size_t child_index) { diff --git a/src/runtime/node.h b/src/runtime/node.h index f1ea67b3..3b269887 100644 --- a/src/runtime/node.h +++ b/src/runtime/node.h @@ -5,6 +5,6 @@ #include "runtime/length.h" #include "runtime/tree.h" -TSNode ts_node_make(const TSTree *, TSLength); +TSNode ts_node_make(const TSTree *, TSLength, TSPoint); #endif diff --git a/src/runtime/parser.c b/src/runtime/parser.c index b338adaf..7e06ec76 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -119,18 +119,21 @@ static void ts_parser__get_next_lookahead(TSParser *self) { self->lookahead = self->reusable_subtree; TSLength size = ts_tree_total_size(self->lookahead); + TSPoint point_size = ts_tree_total_size_point(self->lookahead); DEBUG("reuse sym:%s size:%lu extra:%d", SYM_NAME(self->lookahead->symbol), size.chars, self->lookahead->options.extra); ts_lexer_reset(&self->lexer, - ts_length_add(self->lexer.current_position, size)); + ts_length_add(self->lexer.current_position, size), + ts_point_add(self->lexer.current_point, point_size)); ts_parser__pop_reusable_subtree(self); return; } TSLength position = self->lexer.current_position; + TSPoint point = self->lexer.current_point; for (size_t i = 0, count = ts_stack_head_count(self->stack); i < count; i++) { if (i > 0) { - ts_lexer_reset(&self->lexer, position); + ts_lexer_reset(&self->lexer, position, point); ts_tree_release(self->lookahead); } @@ -285,7 +288,7 @@ static void ts_parser__start(TSParser *self, TSInput input, } self->lexer.input = input; - ts_lexer_reset(&self->lexer, ts_length_zero()); + ts_lexer_reset(&self->lexer, ts_length_zero(), ts_point_zero()); ts_stack_clear(self->stack); self->reusable_subtree = previous_tree; diff --git a/src/runtime/tree.c b/src/runtime/tree.c index eeb9f9b9..16117adb 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -7,8 +7,8 @@ #include "runtime/length.h" TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size, - TSSourceInfo start_source_info, - TSSourceInfo end_source_info, + TSPoint padding_point, + TSPoint size_point, TSNodeType node_type) { TSTree *result = malloc(sizeof(TSTree)); *result = (TSTree){ @@ -20,8 +20,8 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size, .named_child_count = 0, .children = NULL, .padding = padding, - .start_source_info = start_source_info, - .end_source_info = end_source_info, + .padding_point = padding_point, + .size_point = size_point, .options = {.type = node_type }, }; @@ -34,12 +34,12 @@ TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size, } TSTree *ts_tree_make_error(TSLength size, TSLength padding, - TSSourceInfo start_source_info, - TSSourceInfo end_source_info, + TSPoint start_point, + TSPoint end_point, char lookahead_char) { TSTree *result = - ts_tree_make_leaf(ts_builtin_sym_error, padding, size, start_source_info, - end_source_info, TSNodeTypeNamed); + ts_tree_make_leaf(ts_builtin_sym_error, padding, size, start_point, + end_point, TSNodeTypeNamed); result->lookahead_char = lookahead_char; return result; } @@ -90,7 +90,7 @@ static void ts_tree__set_children(TSTree *self, TSTree **children, TSTree *ts_tree_make_node(TSSymbol symbol, size_t child_count, TSTree **children, TSNodeType node_type) { TSTree *result = - ts_tree_make_leaf(symbol, ts_length_zero(), ts_length_zero(), ts_source_info_zero(), ts_source_info_zero(), node_type); + ts_tree_make_leaf(symbol, ts_length_zero(), ts_length_zero(), ts_point_zero(), ts_point_zero(), node_type); ts_tree__set_children(result, children, child_count); return result; } @@ -116,6 +116,10 @@ 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); +} + bool ts_tree_eq(const TSTree *self, const TSTree *other) { if (self) { if (!other) diff --git a/src/runtime/tree.h b/src/runtime/tree.h index 263f9bc2..9a51a022 100644 --- a/src/runtime/tree.h +++ b/src/runtime/tree.h @@ -13,6 +13,7 @@ struct TSTree { struct TSTree *parent; size_t index; TSLength offset; + TSPoint offset_point; } context; size_t child_count; size_t visible_child_count; @@ -23,9 +24,12 @@ struct TSTree { }; TSLength padding; TSLength size; + + TSPoint padding_point; + TSPoint size_point; + TSSymbol symbol; - TSSourceInfo start_source_info; - TSSourceInfo end_source_info; + struct { TSNodeType type : 2; bool extra : 1; @@ -36,18 +40,19 @@ struct TSTree { unsigned short int ref_count; }; -TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, TSSourceInfo, - TSSourceInfo, TSNodeType); +TSTree *ts_tree_make_leaf(TSSymbol, TSLength, TSLength, TSPoint, + TSPoint, TSNodeType); TSTree *ts_tree_make_node(TSSymbol, size_t, TSTree **, TSNodeType); TSTree *ts_tree_make_error(TSLength size, TSLength padding, - TSSourceInfo start_source_info, - TSSourceInfo end_source_info, char lookahead_char); + TSPoint padding_point, + TSPoint size_point, char lookahead_char); void ts_tree_retain(TSTree *tree); void ts_tree_release(TSTree *tree); bool ts_tree_eq(const TSTree *tree1, const TSTree *tree2); char *ts_tree_string(const TSTree *tree, const char **names, bool include_anonymous); TSLength ts_tree_total_size(const TSTree *tree); +TSPoint ts_tree_total_size_point(const TSTree *self); void ts_tree_prepend_children(TSTree *, size_t, TSTree **); void ts_tree_edit(TSTree *, TSInputEdit);