From 3d7df851b5b58d0f0a2f9894d3b1e7d07619f517 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Wed, 17 Feb 2016 20:41:29 -0800 Subject: [PATCH] Rename Vector -> Array --- spec/runtime/stack_spec.cc | 60 ++++++++--------- src/runtime/{vector.h => array.h} | 66 +++++++++---------- src/runtime/document.c | 3 +- src/runtime/parser.c | 59 +++++++++-------- src/runtime/parser.h | 6 +- src/runtime/stack.c | 104 +++++++++++++++--------------- src/runtime/stack.h | 7 +- src/runtime/string_input.c | 2 +- 8 files changed, 152 insertions(+), 155 deletions(-) rename src/runtime/{vector.h => array.h} (51%) diff --git a/spec/runtime/stack_spec.cc b/spec/runtime/stack_spec.cc index 2a033c6c..5343654b 100644 --- a/spec/runtime/stack_spec.cc +++ b/spec/runtime/stack_spec.cc @@ -37,7 +37,7 @@ int tree_selection_spy_callback(void *data, TSTree *left, TSTree *right) { return 1; } -void free_pop_results(StackPopResultVector *pop_results) { +void free_pop_results(StackPopResultArray *pop_results) { for (size_t i = 0; i < pop_results->size; i++) { StackPopResult *pop_result = &pop_results->contents[i]; @@ -141,10 +141,10 @@ describe("Stack", [&]() { /* * A0. */ - StackPopResultVector results = ts_stack_pop(stack, 0, 2, false); + StackPopResultArray results = ts_stack_pop(stack, 0, 2, false); AssertThat(results.size, Equals(1)); - StackPopResult result = *vector_get(&results, 0); + StackPopResult result = results.contents[0]; AssertThat(result.tree_count, Equals(2)); AssertThat(result.trees[0], Equals(trees[1])); AssertThat(result.trees[1], Equals(trees[2])); @@ -157,7 +157,7 @@ describe("Stack", [&]() { results = ts_stack_pop(stack, 0, 1, false); AssertThat(results.size, Equals(1)); - result = *(StackPopResult *)vector_get(&results, 0); + result = results.contents[0]; AssertThat(result.tree_count, Equals(1)); AssertThat(result.trees[0], Equals(trees[0])); AssertThat(ts_stack_head(stack, 0), Equals(nullptr)); @@ -168,10 +168,10 @@ describe("Stack", [&]() { it("does not count 'extra' trees toward the count", [&]() { trees[1]->extra = true; - StackPopResultVector results = ts_stack_pop(stack, 0, 2, false); + StackPopResultArray results = ts_stack_pop(stack, 0, 2, false); AssertThat(results.size, Equals(1)); - StackPopResult result = *vector_get(&results, 0); + StackPopResult result = results.contents[0]; AssertThat(result.tree_count, Equals(3)); AssertThat(result.trees[0], Equals(trees[0])); AssertThat(result.trees[1], Equals(trees[1])); @@ -182,10 +182,10 @@ describe("Stack", [&]() { }); it("pops the entire stack when given a negative count", [&]() { - StackPopResultVector results = ts_stack_pop(stack, 0, -1, false); + StackPopResultArray results = ts_stack_pop(stack, 0, -1, false); AssertThat(results.size, Equals(1)); - StackPopResult result = *vector_get(&results, 0); + StackPopResult result = results.contents[0]; AssertThat(result.tree_count, Equals(3)); AssertThat(result.trees[0], Equals(trees[0])); AssertThat(result.trees[1], Equals(trees[1])); @@ -213,14 +213,14 @@ describe("Stack", [&]() { * \. */ ts_stack_push(stack, 0, stateD, trees[3]); - StackPopResultVector pop_results = ts_stack_pop(stack, 1, 1, false); + StackPopResultArray pop_results = ts_stack_pop(stack, 1, 1, false); AssertThat(ts_stack_head_count(stack), Equals(2)); AssertThat(*ts_stack_head(stack, 0), Equals({trees[3], stateD, tree_len * 4})); AssertThat(*ts_stack_head(stack, 1), Equals({trees[1], stateB, tree_len * 2})); AssertThat(pop_results.size, Equals(1)); - StackPopResult *pop_result = (StackPopResult *)vector_get(&pop_results, 0); - AssertThat(pop_result->tree_count, Equals(1)); + StackPopResult pop_result = pop_results.contents[0]; + AssertThat(pop_result.tree_count, Equals(1)); free_pop_results(&pop_results); /* @@ -381,16 +381,16 @@ describe("Stack", [&]() { * A0__B1__C2. * \__E4. */ - StackPopResultVector results = ts_stack_pop(stack, 0, 2, false); + StackPopResultArray results = ts_stack_pop(stack, 0, 2, false); AssertThat(results.size, Equals(2)); - StackPopResult pop1 = *(StackPopResult *)vector_get(&results, 0); + StackPopResult pop1 = results.contents[0]; AssertThat(pop1.head_index, Equals(0)); AssertThat(pop1.tree_count, Equals(2)); AssertThat(pop1.trees[0], Equals(trees[3])); AssertThat(pop1.trees[1], Equals(trees[6])); - StackPopResult pop2 = *(StackPopResult *)vector_get(&results, 1); + StackPopResult pop2 = results.contents[1]; AssertThat(pop2.head_index, Equals(1)); AssertThat(pop2.tree_count, Equals(2)); AssertThat(pop2.trees[0], Equals(trees[5])); @@ -417,7 +417,7 @@ describe("Stack", [&]() { * A0__B1__C2__D3__G6. * \__E4__F5__/ */ - StackPopResultVector results = ts_stack_pop(stack, 0, 1, false); + StackPopResultArray results = ts_stack_pop(stack, 0, 1, false); AssertThat(results.size, Equals(1)); AssertThat(ts_stack_head_count(stack), Equals(1)); @@ -439,17 +439,17 @@ describe("Stack", [&]() { * A0__B1__C2__D3. * \__E4__F5. */ - StackPopResultVector results = ts_stack_pop(stack, 0, 2, false); + StackPopResultArray results = ts_stack_pop(stack, 0, 2, false); AssertThat(ts_stack_head_count(stack), Equals(2)); AssertThat(results.size, Equals(2)); - StackPopResult pop1 = *(StackPopResult *)vector_get(&results, 0); + StackPopResult pop1 = results.contents[0]; AssertThat(pop1.head_index, Equals(0)); AssertThat(pop1.tree_count, Equals(2)); AssertThat(pop1.trees[0], Equals(trees[6])); AssertThat(pop1.trees[1], Equals(trees[7])); - StackPopResult pop2 = *(StackPopResult *)vector_get(&results, 1); + StackPopResult pop2 = results.contents[1]; AssertThat(pop2.head_index, Equals(1)); AssertThat(pop2.tree_count, Equals(2)); AssertThat(pop2.trees[0], Equals(trees[6])); @@ -467,12 +467,12 @@ describe("Stack", [&]() { /* * A0__B1. */ - StackPopResultVector results = ts_stack_pop(stack, 0, 3, false); + StackPopResultArray results = ts_stack_pop(stack, 0, 3, false); AssertThat(ts_stack_head_count(stack), Equals(1)); AssertThat(*ts_stack_head(stack, 0), Equals({trees[1], stateB, tree_len * 2})); AssertThat(results.size, Equals(1)); - StackPopResult pop1 = *(StackPopResult *)vector_get(&results, 0); + StackPopResult pop1 = results.contents[0]; AssertThat(pop1.tree_count, Equals(3)); AssertThat(pop1.head_index, Equals(0)); AssertThat(pop1.trees[0], Equals(trees[2])); @@ -488,12 +488,12 @@ describe("Stack", [&]() { /* * A0__B1. */ - StackPopResultVector results = ts_stack_pop(stack, 0, 3, false); + StackPopResultArray results = ts_stack_pop(stack, 0, 3, false); AssertThat(ts_stack_head_count(stack), Equals(1)); AssertThat(*ts_stack_head(stack, 0), Equals({trees[1], stateB, tree_len * 2})); AssertThat(results.size, Equals(1)); - StackPopResult pop1 = *(StackPopResult *)vector_get(&results, 0); + StackPopResult pop1 = results.contents[0]; AssertThat(pop1.tree_count, Equals(3)); AssertThat(pop1.head_index, Equals(0)); AssertThat(pop1.trees[0], Equals(trees[4])); @@ -540,25 +540,25 @@ describe("Stack", [&]() { * \__E4__F5. * \__G6__H7. */ - StackPopResultVector results = ts_stack_pop(stack, 0, 2, false); + StackPopResultArray results = ts_stack_pop(stack, 0, 2, false); AssertThat(ts_stack_head_count(stack), Equals(3)); AssertThat(results.size, Equals(3)); - StackPopResult pop1 = *(StackPopResult *)vector_get(&results, 0); + StackPopResult pop1 = results.contents[0]; AssertThat(ts_stack_top_tree(stack, 0), Equals(trees[3])); AssertThat(pop1.head_index, Equals(0)); AssertThat(pop1.tree_count, Equals(2)); AssertThat(pop1.trees[0], Equals(trees[8])); AssertThat(pop1.trees[1], Equals(trees[9])); - StackPopResult pop2 = *(StackPopResult *)vector_get(&results, 1); + StackPopResult pop2 = results.contents[1]; AssertThat(ts_stack_top_tree(stack, 1), Equals(trees[5])); AssertThat(pop2.head_index, Equals(1)); AssertThat(pop2.tree_count, Equals(2)); AssertThat(pop2.trees, Equals(pop1.trees)); - StackPopResult pop3 = *(StackPopResult *)vector_get(&results, 2); + StackPopResult pop3 = results.contents[2]; AssertThat(ts_stack_top_tree(stack, 2), Equals(trees[7])); AssertThat(pop3.head_index, Equals(2)); AssertThat(pop3.tree_count, Equals(2)); @@ -575,12 +575,12 @@ describe("Stack", [&]() { * \__E4. * \__G6. */ - StackPopResultVector results = ts_stack_pop(stack, 0, 3, false); + StackPopResultArray results = ts_stack_pop(stack, 0, 3, false); AssertThat(ts_stack_head_count(stack), Equals(3)); AssertThat(results.size, Equals(3)); - StackPopResult pop1 = *(StackPopResult *)vector_get(&results, 0); + StackPopResult pop1 = results.contents[0]; AssertThat(ts_stack_top_tree(stack, 0), Equals(trees[2])); AssertThat(pop1.head_index, Equals(0)); AssertThat(pop1.tree_count, Equals(3)); @@ -588,7 +588,7 @@ describe("Stack", [&]() { AssertThat(pop1.trees[1], Equals(trees[8])); AssertThat(pop1.trees[2], Equals(trees[9])); - StackPopResult pop2 = *(StackPopResult *)vector_get(&results, 1); + StackPopResult pop2 = results.contents[1]; AssertThat(ts_stack_top_tree(stack, 1), Equals(trees[4])); AssertThat(pop2.head_index, Equals(1)); AssertThat(pop2.tree_count, Equals(3)); @@ -596,7 +596,7 @@ describe("Stack", [&]() { AssertThat(pop2.trees[1], Equals(trees[8])); AssertThat(pop2.trees[2], Equals(trees[9])); - StackPopResult pop3 = *(StackPopResult *)vector_get(&results, 2); + StackPopResult pop3 = results.contents[2]; AssertThat(ts_stack_top_tree(stack, 2), Equals(trees[6])); AssertThat(pop3.head_index, Equals(2)); AssertThat(pop3.tree_count, Equals(3)); diff --git a/src/runtime/vector.h b/src/runtime/array.h similarity index 51% rename from src/runtime/vector.h rename to src/runtime/array.h index 1d859165..7434316f 100644 --- a/src/runtime/vector.h +++ b/src/runtime/array.h @@ -1,5 +1,5 @@ -#ifndef RUNTIME_VECTOR_H_ -#define RUNTIME_VECTOR_H_ +#ifndef RUNTIME_ARRAY_H_ +#define RUNTIME_ARRAY_H_ #ifdef __cplusplus extern "C" { @@ -11,65 +11,65 @@ extern "C" { #include #include "runtime/alloc.h" -#define Vector(T) \ +#define Array(T) \ struct { \ T *contents; \ size_t size; \ size_t capacity; \ } -#define vector_init(self) \ +#define array_init(self) \ ((self)->size = 0, (self)->capacity = 0, (self)->contents = NULL) -#define vector_get(self, index) \ +#define array_get(self, index) \ (assert((size_t)index < (self)->size), &(self)->contents[index]) -#define vector_front(self) vector_get(self, 0) +#define array_front(self) array_get(self, 0) -#define vector_back(self) vector_get(self, (self)->size - 1) +#define array_back(self) array_get(self, (self)->size - 1) -#define vector_clear(self) ((self)->size = 0) +#define array_clear(self) ((self)->size = 0) -#define vector_grow(self, new_capacity) \ - vector__grow((VoidVector *)(self), vector__elem_size(self), new_capacity) +#define array_grow(self, new_capacity) \ + array__grow((VoidArray *)(self), array__elem_size(self), new_capacity) -#define vector_erase(self, index) \ - vector__erase((VoidVector *)(self), vector__elem_size(self), index) +#define array_erase(self, index) \ + array__erase((VoidArray *)(self), array__elem_size(self), index) -#define vector_delete(self) vector__delete((VoidVector *)self) +#define array_delete(self) array__delete((VoidArray *)self) -#define vector_push(self, element) \ - (((self)->size < (self)->capacity || \ - vector_grow((self), (self)->capacity * 2)) && \ +#define array_push(self, element) \ + (((self)->size < (self)->capacity || \ + array_grow((self), (self)->capacity * 2)) && \ ((self)->contents[(self)->size++] = (element), true)) -#define vector_pop(self) ((self)->contents[--(self)->size]) +#define array_pop(self) ((self)->contents[--(self)->size]) -#define vector_reverse(self) \ - vector__reverse((VoidVector *)(self), vector__elem_size(self)) +#define array_reverse(self) \ + array__reverse((VoidArray *)(self), array__elem_size(self)) -#define vector_copy(self) \ - { \ - memcpy(ts_calloc((self)->capacity, vector__elem_size(self)), \ - (self)->contents, (self)->size *vector__elem_size(self)), \ - (self)->size, (self)->capacity, \ +#define array_copy(self) \ + { \ + memcpy(ts_calloc((self)->capacity, array__elem_size(self)), \ + (self)->contents, (self)->size *array__elem_size(self)), \ + (self)->size, (self)->capacity, \ } // Private -typedef Vector(void) VoidVector; +typedef Array(void) VoidArray; -#define vector__elem_size(self) sizeof(*(self)->contents) +#define array__elem_size(self) sizeof(*(self)->contents) -static inline void vector__delete(VoidVector *self) { +static inline void array__delete(VoidArray *self) { ts_free(self->contents); self->contents = NULL; self->size = 0; self->capacity = 0; } -static inline void vector__erase(VoidVector *self, size_t element_size, - size_t index) { +static inline void array__erase(VoidArray *self, size_t element_size, + size_t index) { assert(index < self->size); char *contents = (char *)self->contents; memmove(contents + index * element_size, contents + (index + 1) * element_size, @@ -77,8 +77,8 @@ static inline void vector__erase(VoidVector *self, size_t element_size, self->size--; } -static inline bool vector__grow(VoidVector *self, size_t element_size, - size_t new_capacity) { +static inline bool array__grow(VoidArray *self, size_t element_size, + size_t new_capacity) { if (new_capacity == 0) return true; @@ -95,7 +95,7 @@ static inline bool vector__grow(VoidVector *self, size_t element_size, return true; } -static inline void vector__reverse(VoidVector *self, size_t element_size) { +static inline void array__reverse(VoidArray *self, size_t element_size) { char swap[element_size]; char *contents = (char *)self->contents; for (size_t i = 0, limit = self->size / 2; i < limit; i++) { @@ -111,4 +111,4 @@ static inline void vector__reverse(VoidVector *self, size_t element_size) { } #endif -#endif // RUNTIME_VECTOR_H_ +#endif // RUNTIME_ARRAY_H_ diff --git a/src/runtime/document.c b/src/runtime/document.c index cbe2a236..c852b816 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -26,7 +26,8 @@ void ts_document_free(TSDocument *self) { ts_parser_destroy(&self->parser); if (self->tree) ts_tree_release(self->tree); - ts_document_set_input(self, (TSInput){NULL, NULL, NULL, TSInputEncodingUTF8}); + ts_document_set_input(self, + (TSInput){ NULL, NULL, NULL, TSInputEncodingUTF8 }); ts_free(self); } diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 8b4aa18e..e60c7a75 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -7,7 +7,7 @@ #include "runtime/tree.h" #include "runtime/lexer.h" #include "runtime/length.h" -#include "runtime/vector.h" +#include "runtime/array.h" #include "runtime/language.h" #include "runtime/alloc.h" @@ -47,7 +47,7 @@ static ParseActionResult ts_parser__breakdown_top_of_stack(TSParser *self, TSTree *last_child = NULL; do { - StackPopResultVector pop_results = ts_stack_pop(self->stack, head, 1, false); + StackPopResultArray pop_results = ts_stack_pop(self->stack, head, 1, false); if (!pop_results.size) return FailedToUpdateStackHead; assert(pop_results.size > 0); @@ -56,7 +56,7 @@ static ParseActionResult ts_parser__breakdown_top_of_stack(TSParser *self, * Since only one entry (not counting extra trees) is being popped from the * stack, there should only be one possible array of removed trees. */ - StackPopResult *first_result = vector_get(&pop_results, 0); + StackPopResult *first_result = array_get(&pop_results, 0); assert(first_result->tree_count > 0); TSTree **removed_trees = first_result->trees; TSTree *parent = removed_trees[0]; @@ -187,7 +187,7 @@ static bool ts_parser__can_reuse(TSParser *self, int head, TSTree *subtree) { * run the lexer. */ static TSTree *ts_parser__get_next_lookahead(TSParser *self, int head) { - LookaheadState *state = vector_get(&self->lookahead_states, head); + LookaheadState *state = array_get(&self->lookahead_states, head); TSLength position = ts_stack_top_position(self->stack, head); while (state->reusable_subtree) { @@ -238,13 +238,13 @@ static TSTree *ts_parser__get_next_lookahead(TSParser *self, int head) { static int ts_parser__split(TSParser *self, int head) { int result = ts_stack_split(self->stack, head); assert(result == (int)self->lookahead_states.size); - LookaheadState lookahead_state = *vector_get(&self->lookahead_states, head); - vector_push(&self->lookahead_states, lookahead_state); + LookaheadState lookahead_state = *array_get(&self->lookahead_states, head); + array_push(&self->lookahead_states, lookahead_state); return result; } static void ts_parser__remove_head(TSParser *self, int head) { - vector_erase(&self->lookahead_states, head); + array_erase(&self->lookahead_states, head); ts_stack_remove_head(self->stack, head); } @@ -278,7 +278,7 @@ static ParseActionResult ts_parser__shift(TSParser *self, int head, return FailedToUpdateStackHead; case StackPushResultMerged: LOG("merge head:%d", head); - vector_erase(&self->lookahead_states, head); + array_erase(&self->lookahead_states, head); return RemovedStackHead; default: return UpdatedStackHead; @@ -307,10 +307,10 @@ static ParseActionResult ts_parser__reduce(TSParser *self, int head, TSSymbol symbol, int child_count, bool extra, bool fragile, bool count_extra) { - vector_clear(&self->reduce_parents); + array_clear(&self->reduce_parents); const TSSymbolMetadata *all_metadata = self->language->symbol_metadata; TSSymbolMetadata metadata = all_metadata[symbol]; - StackPopResultVector pop_results = + StackPopResultArray pop_results = ts_stack_pop(self->stack, head, child_count, count_extra); if (!pop_results.size) return FailedToUpdateStackHead; @@ -358,7 +358,7 @@ static ParseActionResult ts_parser__reduce(TSParser *self, int head, } } - if (!vector_push(&self->reduce_parents, parent)) + if (!array_push(&self->reduce_parents, parent)) goto error; int new_head = pop_result->head_index - removed_heads; @@ -376,9 +376,8 @@ static ParseActionResult ts_parser__reduce(TSParser *self, int head, * the lookahead state for this head, for the new head. */ LOG("split_during_reduce new_head:%d", new_head); - LookaheadState lookahead_state = - *vector_get(&self->lookahead_states, head); - if (!vector_push(&self->lookahead_states, lookahead_state)) + LookaheadState lookahead_state = *array_get(&self->lookahead_states, head); + if (!array_push(&self->lookahead_states, lookahead_state)) goto error; } @@ -417,7 +416,7 @@ static ParseActionResult ts_parser__reduce(TSParser *self, int head, goto error; case StackPushResultMerged: LOG("merge_during_reduce head:%d", new_head); - vector_erase(&self->lookahead_states, new_head); + array_erase(&self->lookahead_states, new_head); removed_heads++; continue; case StackPushResultContinued: @@ -432,7 +431,7 @@ static ParseActionResult ts_parser__reduce(TSParser *self, int head, case StackPushResultFailed: return FailedToUpdateStackHead; case StackPushResultMerged: - vector_erase(&self->lookahead_states, new_head); + array_erase(&self->lookahead_states, new_head); removed_heads++; break; case StackPushResultContinued: @@ -444,7 +443,7 @@ static ParseActionResult ts_parser__reduce(TSParser *self, int head, } for (size_t i = 0; i < self->reduce_parents.size; i++) { - TSTree **parent = vector_get(&self->reduce_parents, i); + TSTree **parent = array_get(&self->reduce_parents, i); if (fragile || self->is_split || ts_stack_head_count(self->stack) > 1) { (*parent)->fragile_left = true; @@ -564,14 +563,14 @@ static ParseActionResult ts_parser__start(TSParser *self, TSInput input, .reusable_subtree_pos = 0, .is_verifying = false, }; - vector_clear(&self->lookahead_states); - vector_push(&self->lookahead_states, lookahead_state); + array_clear(&self->lookahead_states); + array_push(&self->lookahead_states, lookahead_state); self->finished_tree = NULL; return UpdatedStackHead; } static ParseActionResult ts_parser__accept(TSParser *self, int head) { - StackPopResultVector pop_results = ts_stack_pop(self->stack, head, -1, true); + StackPopResultArray pop_results = ts_stack_pop(self->stack, head, -1, true); if (!pop_results.size) goto error; @@ -622,7 +621,7 @@ static ParseActionResult ts_parser__accept(TSParser *self, int head) { error: if (pop_results.size) { - StackPopResult *pop_result = vector_front(&pop_results); + StackPopResult *pop_result = array_front(&pop_results); for (size_t i = 0; i < pop_result->tree_count; i++) ts_tree_release(pop_result->trees[i]); ts_free(pop_result->trees); @@ -661,7 +660,7 @@ static ParseActionResult ts_parser__consume_lookahead(TSParser *self, int head, } LookaheadState *lookahead_state = - vector_get(&self->lookahead_states, current_head); + array_get(&self->lookahead_states, current_head); // TODO: Remove this by making a separate symbol for errors returned from // the lexer. @@ -745,17 +744,17 @@ bool ts_parser_init(TSParser *self) { ts_lexer_init(&self->lexer); self->finished_tree = NULL; self->stack = NULL; - vector_init(&self->lookahead_states); - vector_init(&self->reduce_parents); + array_init(&self->lookahead_states); + array_init(&self->reduce_parents); self->stack = ts_stack_new(); if (!self->stack) goto error; - if (!vector_grow(&self->lookahead_states, 4)) + if (!array_grow(&self->lookahead_states, 4)) goto error; - if (!vector_grow(&self->reduce_parents, 4)) + if (!array_grow(&self->reduce_parents, 4)) goto error; return true; @@ -766,9 +765,9 @@ error: self->stack = NULL; } if (self->lookahead_states.contents) - vector_delete(&self->lookahead_states); + array_delete(&self->lookahead_states); if (self->reduce_parents.contents) - vector_delete(&self->reduce_parents); + array_delete(&self->reduce_parents); return false; } @@ -776,9 +775,9 @@ void ts_parser_destroy(TSParser *self) { if (self->stack) ts_stack_delete(self->stack); if (self->lookahead_states.contents) - vector_delete(&self->lookahead_states); + array_delete(&self->lookahead_states); if (self->reduce_parents.contents) - vector_delete(&self->reduce_parents); + array_delete(&self->reduce_parents); } TSDebugger ts_parser_debugger(const TSParser *self) { diff --git a/src/runtime/parser.h b/src/runtime/parser.h index 7bb8aeb5..73223e97 100644 --- a/src/runtime/parser.h +++ b/src/runtime/parser.h @@ -6,7 +6,7 @@ extern "C" { #endif #include "runtime/stack.h" -#include "runtime/vector.h" +#include "runtime/array.h" typedef struct LookaheadState LookaheadState; @@ -14,8 +14,8 @@ typedef struct { TSLexer lexer; Stack *stack; const TSLanguage *language; - Vector(LookaheadState) lookahead_states; - Vector(TSTree *) reduce_parents; + Array(LookaheadState) lookahead_states; + Array(TSTree *) reduce_parents; TSTree *finished_tree; bool is_split; } TSParser; diff --git a/src/runtime/stack.c b/src/runtime/stack.c index 33e785bd..2f7cfc64 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -1,7 +1,7 @@ #include "tree_sitter/parser.h" #include "runtime/alloc.h" #include "runtime/tree.h" -#include "runtime/vector.h" +#include "runtime/array.h" #include "runtime/stack.h" #include "runtime/length.h" #include @@ -19,20 +19,20 @@ typedef struct StackNode { short unsigned int ref_count; } StackNode; -typedef Vector(TSTree *) TreeVector; +typedef Array(TSTree *) TreeArray; typedef struct { size_t goal_tree_count; StackNode *node; - TreeVector trees; + TreeArray trees; bool is_shared; } PopPath; struct Stack { - Vector(StackNode *) heads; - StackPopResultVector pop_results; - Vector(PopPath) pop_paths; - Vector(StackNode *) node_pool; + Array(StackNode *) heads; + StackPopResultArray pop_results; + Array(PopPath) pop_paths; + Array(StackNode *) node_pool; void *tree_selection_payload; TreeSelectionFunction tree_selection_function; }; @@ -50,39 +50,39 @@ Stack *ts_stack_new() { if (!self) goto error; - vector_init(&self->heads); - vector_init(&self->pop_results); - vector_init(&self->pop_paths); - vector_init(&self->node_pool); + array_init(&self->heads); + array_init(&self->pop_results); + array_init(&self->pop_paths); + array_init(&self->node_pool); self->tree_selection_payload = NULL; self->tree_selection_function = ts_stack__default_tree_selection; - if (!vector_grow(&self->heads, 4)) + if (!array_grow(&self->heads, 4)) goto error; - if (!vector_grow(&self->pop_results, 4)) + if (!array_grow(&self->pop_results, 4)) goto error; - if (!vector_grow(&self->pop_paths, 4)) + if (!array_grow(&self->pop_paths, 4)) goto error; - if (!vector_grow(&self->node_pool, 20)) + if (!array_grow(&self->node_pool, 20)) goto error; - vector_push(&self->heads, NULL); + array_push(&self->heads, NULL); return self; error: if (self) { if (self->heads.contents) - vector_delete(&self->heads); + array_delete(&self->heads); if (self->pop_results.contents) - vector_delete(&self->pop_results); + array_delete(&self->pop_results); if (self->pop_paths.contents) - vector_delete(&self->pop_paths); + array_delete(&self->pop_paths); if (self->node_pool.contents) - vector_delete(&self->node_pool); + array_delete(&self->node_pool); ts_free(self); } return NULL; @@ -148,7 +148,7 @@ static bool stack_node_release(Stack *self, StackNode *node) { if (self->node_pool.size >= MAX_NODE_POOL_SIZE) ts_free(node); else - vector_push(&self->node_pool, node); + array_push(&self->node_pool, node); return true; } else { @@ -165,7 +165,7 @@ static StackNode *stack_node_new(Stack *self, StackNode *next, TSStateId state, if (!node) return NULL; } else { - node = vector_pop(&self->node_pool); + node = array_pop(&self->node_pool); } ts_tree_retain(tree); @@ -260,7 +260,7 @@ static void ts_stack__add_node_successor(Stack *self, StackNode *node, */ static int ts_stack__add_head(Stack *self, StackNode *node) { - if (vector_push(&self->heads, node)) { + if (array_push(&self->heads, node)) { stack_node_retain(node); return self->heads.size - 1; } else { @@ -277,9 +277,9 @@ static int ts_stack__find_head(Stack *self, StackNode *node) { } void ts_stack_remove_head(Stack *self, int head_index) { - StackNode *node = *vector_get(&self->heads, head_index); + StackNode *node = *array_get(&self->heads, head_index); stack_node_release(self, node); - vector_erase(&self->heads, head_index); + array_erase(&self->heads, head_index); } /* @@ -289,7 +289,7 @@ void ts_stack_remove_head(Stack *self, int head_index) { StackPushResult ts_stack_push(Stack *self, int head_index, TSStateId state, TSTree *tree) { TSLength position = ts_tree_total_size(tree); - StackNode *current_head = *vector_get(&self->heads, head_index); + StackNode *current_head = *array_get(&self->heads, head_index); if (current_head) position = ts_length_add(current_head->entry.position, position); @@ -319,24 +319,22 @@ int ts_stack_split(Stack *self, int head_index) { return ts_stack__add_head(self, head); } -StackPopResultVector ts_stack_pop(Stack *self, int head_index, int child_count, - bool count_extra) { - vector_clear(&self->pop_results); - vector_clear(&self->pop_paths); +StackPopResultArray ts_stack_pop(Stack *self, int head_index, int child_count, + bool count_extra) { + array_clear(&self->pop_results); + array_clear(&self->pop_paths); - StackNode *previous_head = *vector_get(&self->heads, head_index); + StackNode *previous_head = *array_get(&self->heads, head_index); int capacity = (child_count == -1) ? STARTING_TREE_CAPACITY : child_count; PopPath initial_path = { - .goal_tree_count = child_count, - .node = previous_head, - .is_shared = false, + .goal_tree_count = child_count, .node = previous_head, .is_shared = false, }; - vector_init(&initial_path.trees); + array_init(&initial_path.trees); - if (!vector_grow(&initial_path.trees, capacity)) + if (!array_grow(&initial_path.trees, capacity)) goto error; - if (!vector_push(&self->pop_paths, initial_path)) + if (!array_push(&self->pop_paths, initial_path)) goto error; /* @@ -367,22 +365,22 @@ StackPopResultVector ts_stack_pop(Stack *self, int head_index, int child_count, * the additional successors. */ if (path->is_shared) { - path->trees = (TreeVector)vector_copy(&path->trees); + path->trees = (TreeArray)array_copy(&path->trees); for (size_t j = 0; j < path->trees.size; j++) ts_tree_retain(path->trees.contents[j]); path->is_shared = false; } ts_tree_retain(node->entry.tree); - if (!vector_push(&path->trees, node->entry.tree)) + if (!array_push(&path->trees, node->entry.tree)) goto error; path->node = path->node->successors[0]; for (int j = 1; j < node->successor_count; j++) { - if (!vector_push(&self->pop_paths, *path)) + if (!array_push(&self->pop_paths, *path)) goto error; - PopPath *next_path = vector_back(&self->pop_paths); + PopPath *next_path = array_back(&self->pop_paths); next_path->node = node->successors[j]; next_path->is_shared = true; } @@ -393,7 +391,7 @@ StackPopResultVector ts_stack_pop(Stack *self, int head_index, int child_count, PopPath *path = &self->pop_paths.contents[i]; if (!path->is_shared) - vector_reverse(&path->trees); + array_reverse(&path->trees); StackPopResult result = { .trees = path->trees.contents, @@ -426,7 +424,7 @@ StackPopResultVector ts_stack_pop(Stack *self, int head_index, int child_count, } } - if (!vector_push(&self->pop_results, result)) + if (!array_push(&self->pop_results, result)) goto error; } @@ -434,14 +432,14 @@ StackPopResultVector ts_stack_pop(Stack *self, int head_index, int child_count, return self->pop_results; error: - vector_delete(&initial_path.trees); - StackPopResultVector result; - vector_init(&result); + array_delete(&initial_path.trees); + StackPopResultArray result; + array_init(&result); return result; } void ts_stack_shrink(Stack *self, int head_index, int count) { - StackNode *head = *vector_get(&self->heads, head_index); + StackNode *head = *array_get(&self->heads, head_index); StackNode *new_head = head; for (int i = 0; i < count; i++) { if (new_head->successor_count == 0) @@ -456,8 +454,8 @@ void ts_stack_shrink(Stack *self, int head_index, int count) { void ts_stack_clear(Stack *self) { for (size_t i = 0; i < self->heads.size; i++) stack_node_release(self, self->heads.contents[i]); - vector_clear(&self->heads); - vector_push(&self->heads, NULL); + array_clear(&self->heads); + array_push(&self->heads, NULL); } void ts_stack_set_tree_selection_callback(Stack *self, void *payload, @@ -468,15 +466,15 @@ void ts_stack_set_tree_selection_callback(Stack *self, void *payload, void ts_stack_delete(Stack *self) { if (self->pop_paths.contents) - vector_delete(&self->pop_results); + array_delete(&self->pop_results); if (self->pop_paths.contents) - vector_delete(&self->pop_paths); + array_delete(&self->pop_paths); ts_stack_clear(self); if (self->node_pool.contents) { for (size_t i = 0; i < self->node_pool.size; i++) ts_free(self->node_pool.contents[i]); - vector_delete(&self->node_pool); + array_delete(&self->node_pool); } - vector_delete(&self->heads); + array_delete(&self->heads); ts_free(self); } diff --git a/src/runtime/stack.h b/src/runtime/stack.h index 853cff34..874a618c 100644 --- a/src/runtime/stack.h +++ b/src/runtime/stack.h @@ -6,7 +6,7 @@ extern "C" { #endif #include "tree_sitter/parser.h" -#include "runtime/vector.h" +#include "runtime/array.h" typedef struct Stack Stack; @@ -28,7 +28,7 @@ typedef enum { StackPushResultContinued, } StackPushResult; -typedef Vector(StackPopResult) StackPopResultVector; +typedef Array(StackPopResult) StackPopResultArray; typedef int (*TreeSelectionFunction)(void *, TSTree *, TSTree *); @@ -92,8 +92,7 @@ StackPushResult ts_stack_push(Stack *, int head, TSStateId, TSTree *); * which had previously been merged. It returns a struct that indicates the * index of each revealed head and the trees removed from that head. */ -StackPopResultVector ts_stack_pop(Stack *, int head, int count, - bool count_extra); +StackPopResultArray ts_stack_pop(Stack *, int head, int count, bool count_extra); /* * Remove the given number of entries from the given head of the stack. diff --git a/src/runtime/string_input.c b/src/runtime/string_input.c index 6785bbc9..b1b87edc 100644 --- a/src/runtime/string_input.c +++ b/src/runtime/string_input.c @@ -42,5 +42,5 @@ TSInput ts_string_input_make(const char *string) { }; error: - return (TSInput){NULL, NULL, NULL, TSInputEncodingUTF8}; + return (TSInput){ NULL, NULL, NULL, TSInputEncodingUTF8 }; }