diff --git a/lib/src/get_changed_ranges.c b/lib/src/get_changed_ranges.c index b8915544..18a42417 100644 --- a/lib/src/get_changed_ranges.c +++ b/lib/src/get_changed_ranges.c @@ -7,7 +7,11 @@ // #define DEBUG_GET_CHANGED_RANGES -static void ts_range_array_add(TSRangeArray *self, Length start, Length end) { +static void ts_range_array_add( + TSRangeArray *self, + Length start, + Length end +) { if (self->size > 0) { TSRange *last_range = array_back(self); if (start.bytes <= last_range->end_byte) { @@ -23,8 +27,12 @@ static void ts_range_array_add(TSRangeArray *self, Length start, Length end) { } } -bool ts_range_array_intersects(const TSRangeArray *self, unsigned start_index, - uint32_t start_byte, uint32_t end_byte) { +bool ts_range_array_intersects( + const TSRangeArray *self, + unsigned start_index, + uint32_t start_byte, + uint32_t end_byte +) { for (unsigned i = start_index; i < self->size; i++) { TSRange *range = &self->contents[i]; if (range->end_byte > start_byte) { @@ -102,9 +110,13 @@ typedef struct { bool in_padding; } Iterator; -static Iterator iterator_new(TreeCursor *cursor, const Subtree *tree, const TSLanguage *language) { +static Iterator iterator_new( + TreeCursor *cursor, + const Subtree *tree, + const TSLanguage *language +) { array_clear(&cursor->stack); - array_push(&cursor->stack, ((TreeCursorEntry){ + array_push(&cursor->stack, ((TreeCursorEntry) { .subtree = tree, .position = length_zero(), .child_index = 0, @@ -210,7 +222,7 @@ static bool iterator_descend(Iterator *self, uint32_t goal_position) { Length child_right = length_add(child_left, ts_subtree_size(*child)); if (child_right.bytes > goal_position) { - array_push(&self->cursor.stack, ((TreeCursorEntry){ + array_push(&self->cursor.stack, ((TreeCursorEntry) { .subtree = child, .position = position, .child_index = i, @@ -262,7 +274,7 @@ static void iterator_advance(Iterator *self) { if (!ts_subtree_extra(*entry.subtree)) structural_child_index++; const Subtree *next_child = &ts_subtree_children(*parent)[child_index]; - array_push(&self->cursor.stack, ((TreeCursorEntry){ + array_push(&self->cursor.stack, ((TreeCursorEntry) { .subtree = next_child, .position = position, .child_index = child_index, @@ -289,7 +301,10 @@ typedef enum { IteratorMatches, } IteratorComparison; -static IteratorComparison iterator_compare(const Iterator *old_iter, const Iterator *new_iter) { +static IteratorComparison iterator_compare( + const Iterator *old_iter, + const Iterator *new_iter +) { Subtree old_tree = NULL_SUBTREE; Subtree new_tree = NULL_SUBTREE; uint32_t old_start = 0; @@ -339,11 +354,13 @@ static inline void iterator_print_state(Iterator *self) { } #endif -unsigned ts_subtree_get_changed_ranges(const Subtree *old_tree, const Subtree *new_tree, - TreeCursor *cursor1, TreeCursor *cursor2, - const TSLanguage *language, - const TSRangeArray *included_range_differences, - TSRange **ranges) { +unsigned ts_subtree_get_changed_ranges( + const Subtree *old_tree, const Subtree *new_tree, + TreeCursor *cursor1, TreeCursor *cursor2, + const TSLanguage *language, + const TSRangeArray *included_range_differences, + TSRange **ranges +) { TSRangeArray results = array_new(); Iterator old_iter = iterator_new(cursor1, old_tree, language); diff --git a/lib/src/language.c b/lib/src/language.c index d1319e6a..d0b497d6 100644 --- a/lib/src/language.c +++ b/lib/src/language.c @@ -40,9 +40,9 @@ TSSymbolMetadata ts_language_symbol_metadata( TSSymbol symbol ) { if (symbol == ts_builtin_sym_error) { - return (TSSymbolMetadata){.visible = true, .named = true}; + return (TSSymbolMetadata) {.visible = true, .named = true}; } else if (symbol == ts_builtin_sym_error_repeat) { - return (TSSymbolMetadata){.visible = false, .named = false}; + return (TSSymbolMetadata) {.visible = false, .named = false}; } else { return self->symbol_metadata[symbol]; } diff --git a/lib/src/parser.c b/lib/src/parser.c index 95b258a7..7db1aceb 100644 --- a/lib/src/parser.c +++ b/lib/src/parser.c @@ -1017,7 +1017,7 @@ static bool ts_parser__do_all_potential_reductions( break; case TSParseActionTypeReduce: if (action.reduce.child_count > 0) - ts_reduce_action_set_add(&self->reduce_actions, (ReduceAction){ + ts_reduce_action_set_add(&self->reduce_actions, (ReduceAction) { .symbol = action.reduce.symbol, .count = action.reduce.child_count, .dynamic_precedence = action.reduce.dynamic_precedence, diff --git a/lib/src/stack.c b/lib/src/stack.c index c32704c4..1dc6895f 100644 --- a/lib/src/stack.c +++ b/lib/src/stack.c @@ -43,11 +43,6 @@ typedef struct { bool is_pending; } StackIterator; -typedef struct { - void *payload; - StackIterateCallback callback; -} StackIterateSession; - typedef Array(StackNode *) StackNodeArray; typedef enum { @@ -91,7 +86,11 @@ static void stack_node_retain(StackNode *self) { assert(self->ref_count != 0); } -static void stack_node_release(StackNode *self, StackNodeArray *pool, SubtreePool *subtree_pool) { +static void stack_node_release( + StackNode *self, + StackNodeArray *pool, + SubtreePool *subtree_pool +) { recur: assert(self->ref_count != 0); self->ref_count--; @@ -121,16 +120,25 @@ recur: } } -static StackNode *stack_node_new(StackNode *previous_node, Subtree subtree, - bool is_pending, TSStateId state, StackNodeArray *pool) { - StackNode *node = pool->size > 0 ? - array_pop(pool) : - ts_malloc(sizeof(StackNode)); - *node = (StackNode){.ref_count = 1, .link_count = 0, .state = state}; +static StackNode *stack_node_new( + StackNode *previous_node, + Subtree subtree, + bool is_pending, + TSStateId state, + StackNodeArray *pool +) { + StackNode *node = pool->size > 0 + ? array_pop(pool) + : ts_malloc(sizeof(StackNode)); + *node = (StackNode) { + .ref_count = 1, + .link_count = 0, + .state = state + }; if (previous_node) { node->link_count = 1; - node->links[0] = (StackLink){ + node->links[0] = (StackLink) { .node = previous_node, .subtree = subtree, .is_pending = is_pending, @@ -156,19 +164,29 @@ static StackNode *stack_node_new(StackNode *previous_node, Subtree subtree, } static bool stack__subtree_is_equivalent(Subtree left, Subtree right) { - return - left.ptr == right.ptr || - (left.ptr && right.ptr && - ts_subtree_symbol(left) == ts_subtree_symbol(right) && - ((ts_subtree_error_cost(left) > 0 && ts_subtree_error_cost(right) > 0) || - (ts_subtree_padding(left).bytes == ts_subtree_padding(right).bytes && - ts_subtree_size(left).bytes == ts_subtree_size(right).bytes && - ts_subtree_child_count(left) == ts_subtree_child_count(right) && - ts_subtree_extra(left) == ts_subtree_extra(right) && - ts_subtree_external_scanner_state_eq(left, right)))); + if (left.ptr == right.ptr) return true; + if (!left.ptr || !right.ptr) return false; + + // Symbols must match + if (ts_subtree_symbol(left) != ts_subtree_symbol(right)) return false; + + // If both have errors, don't bother keeping both. + if (ts_subtree_error_cost(left) > 0 && ts_subtree_error_cost(right) > 0) return true; + + return ( + ts_subtree_padding(left).bytes == ts_subtree_padding(right).bytes && + ts_subtree_size(left).bytes == ts_subtree_size(right).bytes && + ts_subtree_child_count(left) == ts_subtree_child_count(right) && + ts_subtree_extra(left) == ts_subtree_extra(right) && + ts_subtree_external_scanner_state_eq(left, right) + ); } -static void stack_node_add_link(StackNode *self, StackLink link, SubtreePool *subtree_pool) { +static void stack_node_add_link( + StackNode *self, + StackLink link, + SubtreePool *subtree_pool +) { if (link.node == self) return; for (int i = 0; i < self->link_count; i++) { @@ -193,8 +211,10 @@ static void stack_node_add_link(StackNode *self, StackLink link, SubtreePool *su } // If the previous nodes are mergeable, merge them recursively. - if (existing_link->node->state == link.node->state && - existing_link->node->position.bytes == link.node->position.bytes) { + if ( + existing_link->node->state == link.node->state && + existing_link->node->position.bytes == link.node->position.bytes + ) { for (int j = 0; j < link.node->link_count; j++) { stack_node_add_link(existing_link->node, link.node->links[j], subtree_pool); } @@ -227,7 +247,11 @@ static void stack_node_add_link(StackNode *self, StackLink link, SubtreePool *su if (dynamic_precedence > self->dynamic_precedence) self->dynamic_precedence = dynamic_precedence; } -static void stack_head_delete(StackHead *self, StackNodeArray *pool, SubtreePool *subtree_pool) { +static void stack_head_delete( + StackHead *self, + StackNodeArray *pool, + SubtreePool *subtree_pool +) { if (self->node) { if (self->last_external_token.ptr) { ts_subtree_release(subtree_pool, self->last_external_token); @@ -240,8 +264,11 @@ static void stack_head_delete(StackHead *self, StackNodeArray *pool, SubtreePool } } -static StackVersion ts_stack__add_version(Stack *self, StackVersion original_version, - StackNode *node) { +static StackVersion ts_stack__add_version( + Stack *self, + StackVersion original_version, + StackNode *node +) { StackHead head = { .node = node, .node_count_at_last_error = self->heads.contents[original_version].node_count_at_last_error, @@ -255,8 +282,12 @@ static StackVersion ts_stack__add_version(Stack *self, StackVersion original_ver return (StackVersion)(self->heads.size - 1); } -static void ts_stack__add_slice(Stack *self, StackVersion original_version, - StackNode *node, SubtreeArray *subtrees) { +static void ts_stack__add_slice( + Stack *self, + StackVersion original_version, + StackNode *node, + SubtreeArray *subtrees +) { for (uint32_t i = self->slices.size - 1; i + 1 > 0; i--) { StackVersion version = self->slices.contents[i].version; if (self->heads.contents[version].node == node) { @@ -271,9 +302,13 @@ static void ts_stack__add_slice(Stack *self, StackVersion original_version, array_push(&self->slices, slice); } -inline StackSliceArray stack__iter(Stack *self, StackVersion version, - StackCallback callback, void *payload, - int goal_subtree_count) { +inline StackSliceArray stack__iter( + Stack *self, + StackVersion version, + StackCallback callback, + void *payload, + int goal_subtree_count +) { array_clear(&self->slices); array_clear(&self->iterators); @@ -317,8 +352,9 @@ inline StackSliceArray stack__iter(Stack *self, StackVersion version, } if (should_stop) { - if (!should_pop) + if (!should_pop) { ts_subtree_array_delete(self->subtree_pool, &iterator->subtrees); + } array_erase(&self->iterators, i); i--, size--; continue; @@ -443,30 +479,19 @@ unsigned ts_stack_node_count_since_error(const Stack *self, StackVersion version return head->node->node_count - head->node_count_at_last_error; } -void ts_stack_push(Stack *self, StackVersion version, Subtree subtree, - bool pending, TSStateId state) { +void ts_stack_push( + Stack *self, + StackVersion version, + Subtree subtree, + bool pending, + TSStateId state +) { StackHead *head = array_get(&self->heads, version); StackNode *new_node = stack_node_new(head->node, subtree, pending, state, &self->node_pool); if (!subtree.ptr) head->node_count_at_last_error = new_node->node_count; head->node = new_node; } -inline StackAction iterate_callback(void *payload, const StackIterator *iterator) { - StackIterateSession *session = payload; - session->callback( - session->payload, - iterator->node->state, - iterator->subtree_count - ); - return StackActionNone; -} - -void ts_stack_iterate(Stack *self, StackVersion version, - StackIterateCallback callback, void *payload) { - StackIterateSession session = {payload, callback}; - stack__iter(self, version, iterate_callback, &session, -1); -} - inline StackAction pop_count_callback(void *payload, const StackIterator *iterator) { unsigned *goal_subtree_count = payload; if (iterator->subtree_count == *goal_subtree_count) { @@ -530,7 +555,7 @@ SubtreeArray ts_stack_pop_error(Stack *self, StackVersion version) { break; } } - return (SubtreeArray){.size = 0}; + return (SubtreeArray) {.size = 0}; } inline StackAction pop_all_callback(void *payload, const StackIterator *iterator) { @@ -557,7 +582,7 @@ inline StackAction summarize_stack_callback(void *payload, const StackIterator * if (entry.depth < depth) break; if (entry.depth == depth && entry.state == state) return StackActionNone; } - array_push(session->summary, ((StackSummaryEntry){ + array_push(session->summary, ((StackSummaryEntry) { .position = iterator->node->position, .depth = depth, .state = state, @@ -712,7 +737,7 @@ void ts_stack_clear(Stack *self) { stack_head_delete(&self->heads.contents[i], &self->node_pool, self->subtree_pool); } array_clear(&self->heads); - array_push(&self->heads, ((StackHead){ + array_push(&self->heads, ((StackHead) { .node = self->base_node, .last_external_token = NULL_SUBTREE, .status = StackStatusActive, @@ -760,7 +785,9 @@ bool ts_stack_print_dot_graph(Stack *self, const TSLanguage *language, FILE *f) } fprintf(f, "\"]\n"); - array_push(&self->iterators, ((StackIterator){.node = head->node })); + array_push(&self->iterators, ((StackIterator) { + .node = head->node + })); } bool all_iterators_done = false; diff --git a/lib/src/stack.h b/lib/src/stack.h index ec7a69d2..10d7c24c 100644 --- a/lib/src/stack.h +++ b/lib/src/stack.h @@ -126,8 +126,6 @@ bool ts_stack_print_dot_graph(Stack *, const TSLanguage *, FILE *); typedef void (*StackIterateCallback)(void *, TSStateId, uint32_t); -void ts_stack_iterate(Stack *, StackVersion, StackIterateCallback, void *); - #ifdef __cplusplus } #endif diff --git a/lib/src/subtree.c b/lib/src/subtree.c index d3be47b0..0c2a5ad0 100644 --- a/lib/src/subtree.c +++ b/lib/src/subtree.c @@ -409,14 +409,19 @@ void ts_subtree_summarize_children( self.ptr->padding.bytes + self.ptr->size.bytes + ts_subtree_lookahead_bytes(child); - if (child_lookahead_end_byte > lookahead_end_byte) lookahead_end_byte = child_lookahead_end_byte; + if (child_lookahead_end_byte > lookahead_end_byte) { + lookahead_end_byte = child_lookahead_end_byte; + } if (ts_subtree_symbol(child) != ts_builtin_sym_error_repeat) { self.ptr->error_cost += ts_subtree_error_cost(child); } uint32_t grandchild_count = ts_subtree_child_count(child); - if (self.ptr->symbol == ts_builtin_sym_error || self.ptr->symbol == ts_builtin_sym_error_repeat) { + if ( + self.ptr->symbol == ts_builtin_sym_error || + self.ptr->symbol == ts_builtin_sym_error_repeat + ) { if (!ts_subtree_extra(child) && !(ts_subtree_is_error(child) && grandchild_count == 0)) { if (ts_subtree_visible(child)) { self.ptr->error_cost += ERROR_COST_PER_SKIPPED_TREE; @@ -454,7 +459,10 @@ void ts_subtree_summarize_children( self.ptr->lookahead_bytes = lookahead_end_byte - self.ptr->size.bytes - self.ptr->padding.bytes; - if (self.ptr->symbol == ts_builtin_sym_error || self.ptr->symbol == ts_builtin_sym_error_repeat) { + if ( + self.ptr->symbol == ts_builtin_sym_error || + self.ptr->symbol == ts_builtin_sym_error_repeat + ) { self.ptr->error_cost += ERROR_COST_PER_RECOVERY + ERROR_COST_PER_SKIPPED_CHAR * self.ptr->size.bytes + @@ -603,37 +611,6 @@ void ts_subtree_release(SubtreePool *pool, Subtree self) { } } -bool ts_subtree_eq(Subtree self, Subtree other) { - if (self.data.is_inline || other.data.is_inline) { - return memcmp(&self, &other, sizeof(SubtreeInlineData)) == 0; - } - - if (self.ptr) { - if (!other.ptr) return false; - } else { - return !other.ptr; - } - - if (self.ptr->symbol != other.ptr->symbol) return false; - if (self.ptr->visible != other.ptr->visible) return false; - if (self.ptr->named != other.ptr->named) return false; - if (self.ptr->padding.bytes != other.ptr->padding.bytes) return false; - if (self.ptr->size.bytes != other.ptr->size.bytes) return false; - if (self.ptr->symbol == ts_builtin_sym_error) return self.ptr->lookahead_char == other.ptr->lookahead_char; - if (self.ptr->child_count != other.ptr->child_count) return false; - if (self.ptr->child_count > 0) { - if (self.ptr->visible_child_count != other.ptr->visible_child_count) return false; - if (self.ptr->named_child_count != other.ptr->named_child_count) return false; - - for (uint32_t i = 0; i < self.ptr->child_count; i++) { - if (!ts_subtree_eq(ts_subtree_children(self)[i], ts_subtree_children(other)[i])) { - return false; - } - } - } - return true; -} - int ts_subtree_compare(Subtree left, Subtree right) { if (ts_subtree_symbol(left) < ts_subtree_symbol(right)) return -1; if (ts_subtree_symbol(right) < ts_subtree_symbol(left)) return 1; diff --git a/lib/src/subtree.h b/lib/src/subtree.h index 86982df8..3037de0e 100644 --- a/lib/src/subtree.h +++ b/lib/src/subtree.h @@ -197,7 +197,6 @@ Subtree ts_subtree_new_missing_leaf(SubtreePool *, TSSymbol, Length, const TSLan MutableSubtree ts_subtree_make_mut(SubtreePool *, Subtree); void ts_subtree_retain(Subtree); void ts_subtree_release(SubtreePool *, Subtree); -bool ts_subtree_eq(Subtree, Subtree); int ts_subtree_compare(Subtree, Subtree); void ts_subtree_set_symbol(MutableSubtree *, TSSymbol, const TSLanguage *); void ts_subtree_summarize(MutableSubtree, const Subtree *, uint32_t, const TSLanguage *); diff --git a/lib/src/tree_cursor.c b/lib/src/tree_cursor.c index 6b4829f5..8bc1359c 100644 --- a/lib/src/tree_cursor.c +++ b/lib/src/tree_cursor.c @@ -34,9 +34,11 @@ static inline CursorChildIterator ts_tree_cursor_iterate_children(const TreeCurs }; } -static inline bool ts_tree_cursor_child_iterator_next(CursorChildIterator *self, - TreeCursorEntry *result, - bool *visible) { +static inline bool ts_tree_cursor_child_iterator_next( + CursorChildIterator *self, + TreeCursorEntry *result, + bool *visible +) { if (!self->parent.ptr || self->child_index == self->parent.ptr->child_count) return false; const Subtree *child = &ts_subtree_children(self->parent)[self->child_index]; *result = (TreeCursorEntry) { diff --git a/test/fixtures/test_grammars/external_tokens/scanner.c b/test/fixtures/test_grammars/external_tokens/scanner.c index c187f41b..490100d4 100644 --- a/test/fixtures/test_grammars/external_tokens/scanner.c +++ b/test/fixtures/test_grammars/external_tokens/scanner.c @@ -14,7 +14,7 @@ typedef struct { void *tree_sitter_external_tokens_external_scanner_create() { Scanner *scanner = malloc(sizeof(Scanner)); - *scanner = (Scanner){ + *scanner = (Scanner) { .open_delimiter = 0, .close_delimiter = 0, .depth = 0