Renamed some structs

This commit is contained in:
Maieul BOYER 2024-08-31 15:36:44 +00:00
parent e91a509d4c
commit deba39ea8a
4 changed files with 265 additions and 254 deletions

View file

@ -6,39 +6,38 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 12:03:07 by maiboyer #+# #+# */ /* Created: 2024/08/31 12:03:07 by maiboyer #+# #+# */
/* Updated: 2024/08/31 12:03:08 by maiboyer ### ########.fr */ /* Updated: 2024/08/31 15:27:32 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef PARSE_STACK_H #ifndef PARSE_STACK_H
#define PARSE_STACK_H #define PARSE_STACK_H
#include "me/types.h"
#include "parser/array.h" #include "parser/array.h"
#include "parser/subtree.h" #include "parser/subtree.h"
#include "me/types.h"
typedef struct Stack Stack; typedef struct s_stack Stack;
typedef t_u32 StackVersion; typedef t_u32 t_stack_version;
typedef struct s_stack_slice t_stack_slice;
typedef struct s_stack_summary_entry t_stack_summary_entry;
typedef Array(t_stack_slice) t_stack_slice_array;
typedef Array(t_stack_summary_entry) t_stack_summary;
#define STACK_VERSION_NONE ((StackVersion)-1) #define STACK_VERSION_NONE ((t_stack_version) - 1)
typedef struct StackSlice struct s_stack_slice
{ {
SubtreeArray subtrees; SubtreeArray subtrees;
StackVersion version; t_stack_version version;
} StackSlice; };
typedef Array(StackSlice) StackSliceArray;
typedef struct StackSummaryEntry struct s_stack_summary_entry
{ {
Length position; Length position;
t_u32 depth; t_u32 depth;
TSStateId state; TSStateId state;
} StackSummaryEntry; };
typedef Array(StackSummaryEntry) StackSummary;
typedef void (*StackIterateCallback)(void *, TSStateId, t_u32);
// Create a stack. // Create a stack.
Stack *ts_stack_new(/*SubtreePool **/); Stack *ts_stack_new(/*SubtreePool **/);
@ -51,86 +50,86 @@ t_u32 ts_stack_version_count(const Stack *);
// Get the state at the top of the given version of the stack. If the stack is // Get the state at the top of the given version of the stack. If the stack is
// empty, this returns the initial state, 0. // empty, this returns the initial state, 0.
TSStateId ts_stack_state(const Stack *, StackVersion); TSStateId ts_stack_state(const Stack *, t_stack_version);
// Get the last external token associated with a given version of the stack. // Get the last external token associated with a given version of the stack.
Subtree ts_stack_last_external_token(const Stack *, StackVersion); Subtree ts_stack_last_external_token(const Stack *, t_stack_version);
// Set the last external token associated with a given version of the stack. // Set the last external token associated with a given version of the stack.
void ts_stack_set_last_external_token(Stack *, StackVersion, Subtree); void ts_stack_set_last_external_token(Stack *, t_stack_version, Subtree);
// Get the position of the given version of the stack within the document. // Get the position of the given version of the stack within the document.
Length ts_stack_position(const Stack *, StackVersion); Length ts_stack_position(const Stack *, t_stack_version);
// Push a tree and state onto the given version of the stack. // Push a tree and state onto the given version of the stack.
// //
// This transfers ownership of the tree to the Stack. Callers that // This transfers ownership of the tree to the Stack. Callers that
// need to retain ownership of the tree for their own purposes should // need to retain ownership of the tree for their own purposes should
// first retain the tree. // first retain the tree.
void ts_stack_push(Stack *, StackVersion, Subtree, bool, TSStateId); void ts_stack_push(Stack *, t_stack_version, Subtree, bool, TSStateId);
// Pop the given number of entries from the given version of the stack. This // Pop the given number of entries from the given version of the stack. This
// operation can increase the number of stack versions by revealing multiple // operation can increase the number of stack versions by revealing multiple
// versions which had previously been merged. It returns an array that // versions which had previously been merged. It returns an array that
// specifies the index of each revealed version and the trees that were // specifies the index of each revealed version and the trees that were
// removed from that version. // removed from that version.
StackSliceArray ts_stack_pop_count(Stack *, StackVersion, t_u32 count); t_stack_slice_array ts_stack_pop_count(Stack *, t_stack_version, t_u32 count);
// Remove an error at the top of the given version of the stack. // Remove an error at the top of the given version of the stack.
SubtreeArray ts_stack_pop_error(Stack *, StackVersion); SubtreeArray ts_stack_pop_error(Stack *, t_stack_version);
// Remove any pending trees from the top of the given version of the stack. // Remove any pending trees from the top of the given version of the stack.
StackSliceArray ts_stack_pop_pending(Stack *, StackVersion); t_stack_slice_array ts_stack_pop_pending(Stack *, t_stack_version);
// Remove any all trees from the given version of the stack. // Remove any all trees from the given version of the stack.
StackSliceArray ts_stack_pop_all(Stack *, StackVersion); t_stack_slice_array ts_stack_pop_all(Stack *, t_stack_version);
// Get the maximum number of tree nodes reachable from this version of the stack // Get the maximum number of tree nodes reachable from this version of the stack
// since the last error was detected. // since the last error was detected.
t_u32 ts_stack_node_count_since_error(const Stack *, StackVersion); t_u32 ts_stack_node_count_since_error(const Stack *, t_stack_version);
int ts_stack_dynamic_precedence(Stack *, StackVersion); int ts_stack_dynamic_precedence(Stack *, t_stack_version);
bool ts_stack_has_advanced_since_error(const Stack *, StackVersion); bool ts_stack_has_advanced_since_error(const Stack *, t_stack_version);
// Compute a summary of all the parse states near the top of the given // Compute a summary of all the parse states near the top of the given
// version of the stack and store the summary for later retrieval. // version of the stack and store the summary for later retrieval.
void ts_stack_record_summary(Stack *, StackVersion, t_u32 max_depth); void ts_stack_record_summary(Stack *, t_stack_version, t_u32 max_depth);
// Retrieve a summary of all the parse states near the top of the // Retrieve a summary of all the parse states near the top of the
// given version of the stack. // given version of the stack.
StackSummary *ts_stack_get_summary(Stack *, StackVersion); t_stack_summary *ts_stack_get_summary(Stack *, t_stack_version);
// Get the total cost of all errors on the given version of the stack. // Get the total cost of all errors on the given version of the stack.
t_u32 ts_stack_error_cost(const Stack *, StackVersion version); t_u32 ts_stack_error_cost(const Stack *, t_stack_version version);
// Merge the given two stack versions if possible, returning true // Merge the given two stack versions if possible, returning true
// if they were successfully merged and false otherwise. // if they were successfully merged and false otherwise.
bool ts_stack_merge(Stack *, StackVersion, StackVersion); bool ts_stack_merge(Stack *, t_stack_version, t_stack_version);
// Determine whether the given two stack versions can be merged. // Determine whether the given two stack versions can be merged.
bool ts_stack_can_merge(Stack *, StackVersion, StackVersion); bool ts_stack_can_merge(Stack *, t_stack_version, t_stack_version);
Subtree ts_stack_resume(Stack *, StackVersion); Subtree ts_stack_resume(Stack *, t_stack_version);
void ts_stack_pause(Stack *, StackVersion, Subtree); void ts_stack_pause(Stack *, t_stack_version, Subtree);
void ts_stack_halt(Stack *, StackVersion); void ts_stack_halt(Stack *, t_stack_version);
bool ts_stack_is_active(const Stack *, StackVersion); bool ts_stack_is_active(const Stack *, t_stack_version);
bool ts_stack_is_paused(const Stack *, StackVersion); bool ts_stack_is_paused(const Stack *, t_stack_version);
bool ts_stack_is_halted(const Stack *, StackVersion); bool ts_stack_is_halted(const Stack *, t_stack_version);
void ts_stack_renumber_version(Stack *, StackVersion, StackVersion); void ts_stack_renumber_version(Stack *, t_stack_version, t_stack_version);
void ts_stack_swap_versions(Stack *, StackVersion, StackVersion); void ts_stack_swap_versions(Stack *, t_stack_version, t_stack_version);
StackVersion ts_stack_copy_version(Stack *, StackVersion); t_stack_version ts_stack_copy_version(Stack *, t_stack_version);
// Remove the given version from the stack. // Remove the given version from the stack.
void ts_stack_remove_version(Stack *, StackVersion); void ts_stack_remove_version(Stack *, t_stack_version);
void ts_stack_clear(Stack *); void ts_stack_clear(Stack *);

View file

@ -84,14 +84,14 @@ static const t_u8 *ts_string_input_read(void *_self, t_u32 byte, TSPoint point,
} }
// Parser - Private // Parser - Private
static bool ts_parser__breakdown_top_of_stack(TSParser *self, StackVersion version) static bool ts_parser__breakdown_top_of_stack(TSParser *self, t_stack_version version)
{ {
bool did_break_down = false; bool did_break_down = false;
bool pending = false; bool pending = false;
do do
{ {
StackSliceArray pop = ts_stack_pop_pending(self->stack, version); t_stack_slice_array pop = ts_stack_pop_pending(self->stack, version);
if (!pop.size) if (!pop.size)
break; break;
@ -99,7 +99,7 @@ static bool ts_parser__breakdown_top_of_stack(TSParser *self, StackVersion versi
pending = false; pending = false;
for (t_u32 i = 0; i < pop.size; i++) for (t_u32 i = 0; i < pop.size; i++)
{ {
StackSlice slice = pop.contents[i]; t_stack_slice slice = pop.contents[i];
TSStateId state = ts_stack_state(self->stack, slice.version); TSStateId state = ts_stack_state(self->stack, slice.version);
Subtree parent = *array_front(&slice.subtrees); Subtree parent = *array_front(&slice.subtrees);
@ -193,7 +193,7 @@ static t_error_comparison ts_parser__compare_versions(TSParser *self, t_error_st
return ECNone; return ECNone;
} }
static t_error_status ts_parser__version_status(TSParser *self, StackVersion version) static t_error_status ts_parser__version_status(TSParser *self, t_stack_version version)
{ {
t_u32 cost = ts_stack_error_cost(self->stack, version); t_u32 cost = ts_stack_error_cost(self->stack, version);
bool is_paused = ts_stack_is_paused(self->stack, version); bool is_paused = ts_stack_is_paused(self->stack, version);
@ -205,7 +205,7 @@ static t_error_status ts_parser__version_status(TSParser *self, StackVersion ver
.is_in_error = is_paused || ts_stack_state(self->stack, version) == ERROR_STATE}; .is_in_error = is_paused || ts_stack_state(self->stack, version) == ERROR_STATE};
} }
static bool ts_parser__better_version_exists(TSParser *self, StackVersion version, bool is_in_error, t_u32 cost) static bool ts_parser__better_version_exists(TSParser *self, t_stack_version version, bool is_in_error, t_u32 cost)
{ {
if (self->finished_tree && ts_subtree_error_cost(self->finished_tree) <= cost) if (self->finished_tree && ts_subtree_error_cost(self->finished_tree) <= cost)
{ {
@ -220,7 +220,7 @@ static bool ts_parser__better_version_exists(TSParser *self, StackVersion versio
.node_count = ts_stack_node_count_since_error(self->stack, version), .node_count = ts_stack_node_count_since_error(self->stack, version),
}; };
for (StackVersion i = 0, n = ts_stack_version_count(self->stack); i < n; i++) for (t_stack_version i = 0, n = ts_stack_version_count(self->stack); i < n; i++)
{ {
if (i == version || !ts_stack_is_active(self->stack, i) || ts_stack_position(self->stack, i).bytes < position.bytes) if (i == version || !ts_stack_is_active(self->stack, i) || ts_stack_position(self->stack, i).bytes < position.bytes)
continue; continue;
@ -302,7 +302,7 @@ static bool ts_parser__external_scanner_scan(TSParser *self, TSStateId external_
return self->language->external_scanner.scan(self->external_scanner_payload, &self->lexer.data, valid_external_tokens); return self->language->external_scanner.scan(self->external_scanner_payload, &self->lexer.data, valid_external_tokens);
} }
static Subtree ts_parser__lex(TSParser *self, StackVersion version, TSStateId parse_state) static Subtree ts_parser__lex(TSParser *self, t_stack_version version, TSStateId parse_state)
{ {
TSLexMode lex_mode = self->language->lex_modes[parse_state]; TSLexMode lex_mode = self->language->lex_modes[parse_state];
if (lex_mode.lex_state == (t_u16)-1) if (lex_mode.lex_state == (t_u16)-1)
@ -518,7 +518,7 @@ static bool ts_parser__select_children(TSParser *self, Subtree left, const Subtr
return ts_parser__select_tree(self, left, ts_subtree_from_mut(scratch_tree)); return ts_parser__select_tree(self, left, ts_subtree_from_mut(scratch_tree));
} }
static void ts_parser__shift(TSParser *self, StackVersion version, TSStateId state, Subtree lookahead, bool extra) static void ts_parser__shift(TSParser *self, t_stack_version version, TSStateId state, Subtree lookahead, bool extra)
{ {
bool is_leaf = ts_subtree_child_count(lookahead) == 0; bool is_leaf = ts_subtree_child_count(lookahead) == 0;
Subtree subtree_to_push = lookahead; Subtree subtree_to_push = lookahead;
@ -536,7 +536,7 @@ static void ts_parser__shift(TSParser *self, StackVersion version, TSStateId sta
} }
} }
static StackVersion ts_parser__reduce(TSParser *self, StackVersion version, TSSymbol symbol, t_u32 count, int dynamic_precedence, static t_stack_version ts_parser__reduce(TSParser *self, t_stack_version version, TSSymbol symbol, t_u32 count, int dynamic_precedence,
t_u16 production_id, bool is_fragile, bool end_of_non_terminal_extra) t_u16 production_id, bool is_fragile, bool end_of_non_terminal_extra)
{ {
t_u32 initial_version_count = ts_stack_version_count(self->stack); t_u32 initial_version_count = ts_stack_version_count(self->stack);
@ -546,12 +546,12 @@ static StackVersion ts_parser__reduce(TSParser *self, StackVersion version, TSSy
// path back through the stack. For each path, create a new parent node to // path back through the stack. For each path, create a new parent node to
// contain the popped children, and push it onto the stack in place of the // contain the popped children, and push it onto the stack in place of the
// children. // children.
StackSliceArray pop = ts_stack_pop_count(self->stack, version, count); t_stack_slice_array pop = ts_stack_pop_count(self->stack, version, count);
t_u32 removed_version_count = 0; t_u32 removed_version_count = 0;
for (t_u32 i = 0; i < pop.size; i++) for (t_u32 i = 0; i < pop.size; i++)
{ {
StackSlice slice = pop.contents[i]; t_stack_slice slice = pop.contents[i];
StackVersion slice_version = slice.version - removed_version_count; t_stack_version slice_version = slice.version - removed_version_count;
// This is where new versions are added to the parse stack. The versions // This is where new versions are added to the parse stack. The versions
// will all be sorted and truncated at the end of the outer parsing loop. // will all be sorted and truncated at the end of the outer parsing loop.
@ -564,7 +564,7 @@ static StackVersion ts_parser__reduce(TSParser *self, StackVersion version, TSSy
removed_version_count++; removed_version_count++;
while (i + 1 < pop.size) while (i + 1 < pop.size)
{ {
StackSlice next_slice = pop.contents[i + 1]; t_stack_slice next_slice = pop.contents[i + 1];
if (next_slice.version != slice.version) if (next_slice.version != slice.version)
break; break;
ts_subtree_array_delete(/*&self->tree_pool,*/ &next_slice.subtrees); ts_subtree_array_delete(/*&self->tree_pool,*/ &next_slice.subtrees);
@ -587,7 +587,7 @@ static StackVersion ts_parser__reduce(TSParser *self, StackVersion version, TSSy
// delete the rest of the tree arrays. // delete the rest of the tree arrays.
while (i + 1 < pop.size) while (i + 1 < pop.size)
{ {
StackSlice next_slice = pop.contents[i + 1]; t_stack_slice next_slice = pop.contents[i + 1];
if (next_slice.version != slice.version) if (next_slice.version != slice.version)
break; break;
i++; i++;
@ -635,7 +635,7 @@ static StackVersion ts_parser__reduce(TSParser *self, StackVersion version, TSSy
ts_stack_push(self->stack, slice_version, self->trailing_extras.contents[j], false, next_state); ts_stack_push(self->stack, slice_version, self->trailing_extras.contents[j], false, next_state);
} }
for (StackVersion j = 0; j < slice_version; j++) for (t_stack_version j = 0; j < slice_version; j++)
{ {
if (j == version) if (j == version)
continue; continue;
@ -651,12 +651,12 @@ static StackVersion ts_parser__reduce(TSParser *self, StackVersion version, TSSy
return ts_stack_version_count(self->stack) > initial_version_count ? initial_version_count : STACK_VERSION_NONE; return ts_stack_version_count(self->stack) > initial_version_count ? initial_version_count : STACK_VERSION_NONE;
} }
static void ts_parser__accept(TSParser *self, StackVersion version, Subtree lookahead) static void ts_parser__accept(TSParser *self, t_stack_version version, Subtree lookahead)
{ {
assert(ts_subtree_is_eof(lookahead)); assert(ts_subtree_is_eof(lookahead));
ts_stack_push(self->stack, version, lookahead, false, 1); ts_stack_push(self->stack, version, lookahead, false, 1);
StackSliceArray pop = ts_stack_pop_all(self->stack, version); t_stack_slice_array pop = ts_stack_pop_all(self->stack, version);
for (t_u32 i = 0; i < pop.size; i++) for (t_u32 i = 0; i < pop.size; i++)
{ {
SubtreeArray trees = pop.contents[i].subtrees; SubtreeArray trees = pop.contents[i].subtrees;
@ -705,12 +705,12 @@ static void ts_parser__accept(TSParser *self, StackVersion version, Subtree look
ts_stack_halt(self->stack, version); ts_stack_halt(self->stack, version);
} }
static bool ts_parser__do_all_potential_reductions(TSParser *self, StackVersion starting_version, TSSymbol lookahead_symbol) static bool ts_parser__do_all_potential_reductions(TSParser *self, t_stack_version starting_version, TSSymbol lookahead_symbol)
{ {
t_u32 initial_version_count = ts_stack_version_count(self->stack); t_u32 initial_version_count = ts_stack_version_count(self->stack);
bool can_shift_lookahead_symbol = false; bool can_shift_lookahead_symbol = false;
StackVersion version = starting_version; t_stack_version version = starting_version;
for (t_u32 i = 0; true; i++) for (t_u32 i = 0; true; i++)
{ {
t_u32 version_count = ts_stack_version_count(self->stack); t_u32 version_count = ts_stack_version_count(self->stack);
@ -718,7 +718,7 @@ static bool ts_parser__do_all_potential_reductions(TSParser *self, StackVersion
break; break;
bool merged = false; bool merged = false;
for (StackVersion j = initial_version_count; j < version; j++) for (t_stack_version j = initial_version_count; j < version; j++)
{ {
if (ts_stack_merge(self->stack, j, version)) if (ts_stack_merge(self->stack, j, version))
{ {
@ -774,7 +774,7 @@ static bool ts_parser__do_all_potential_reductions(TSParser *self, StackVersion
} }
} }
StackVersion reduction_version = STACK_VERSION_NONE; t_stack_version reduction_version = STACK_VERSION_NONE;
for (t_u32 j = 0; j < self->reduce_actions.size; j++) for (t_u32 j = 0; j < self->reduce_actions.size; j++)
{ {
ReduceAction action = self->reduce_actions.contents[j]; ReduceAction action = self->reduce_actions.contents[j];
@ -810,14 +810,14 @@ static bool ts_parser__do_all_potential_reductions(TSParser *self, StackVersion
return can_shift_lookahead_symbol; return can_shift_lookahead_symbol;
} }
static bool ts_parser__recover_to_state(TSParser *self, StackVersion version, t_u32 depth, TSStateId goal_state) static bool ts_parser__recover_to_state(TSParser *self, t_stack_version version, t_u32 depth, TSStateId goal_state)
{ {
StackSliceArray pop = ts_stack_pop_count(self->stack, version, depth); t_stack_slice_array pop = ts_stack_pop_count(self->stack, version, depth);
StackVersion previous_version = STACK_VERSION_NONE; t_stack_version previous_version = STACK_VERSION_NONE;
for (t_u32 i = 0; i < pop.size; i++) for (t_u32 i = 0; i < pop.size; i++)
{ {
StackSlice slice = pop.contents[i]; t_stack_slice slice = pop.contents[i];
if (slice.version == previous_version) if (slice.version == previous_version)
{ {
@ -875,12 +875,12 @@ static bool ts_parser__recover_to_state(TSParser *self, StackVersion version, t_
return previous_version != STACK_VERSION_NONE; return previous_version != STACK_VERSION_NONE;
} }
static void ts_parser__recover(TSParser *self, StackVersion version, Subtree lookahead) static void ts_parser__recover(TSParser *self, t_stack_version version, Subtree lookahead)
{ {
bool did_recover = false; bool did_recover = false;
t_u32 previous_version_count = ts_stack_version_count(self->stack); t_u32 previous_version_count = ts_stack_version_count(self->stack);
Length position = ts_stack_position(self->stack, version); Length position = ts_stack_position(self->stack, version);
StackSummary *summary = ts_stack_get_summary(self->stack, version); t_stack_summary *summary = ts_stack_get_summary(self->stack, version);
t_u32 node_count_since_error = ts_stack_node_count_since_error(self->stack, version); t_u32 node_count_since_error = ts_stack_node_count_since_error(self->stack, version);
t_u32 current_error_cost = ts_stack_error_cost(self->stack, version); t_u32 current_error_cost = ts_stack_error_cost(self->stack, version);
@ -900,7 +900,7 @@ static void ts_parser__recover(TSParser *self, StackVersion version, Subtree loo
{ {
for (t_u32 i = 0; i < summary->size; i++) for (t_u32 i = 0; i < summary->size; i++)
{ {
StackSummaryEntry entry = summary->contents[i]; t_stack_summary_entry entry = summary->contents[i];
if (entry.state == ERROR_STATE) if (entry.state == ERROR_STATE)
continue; continue;
@ -1015,7 +1015,7 @@ static void ts_parser__recover(TSParser *self, StackVersion version, Subtree loo
// ERROR. // ERROR.
if (node_count_since_error > 0) if (node_count_since_error > 0)
{ {
StackSliceArray pop = ts_stack_pop_count(self->stack, version, 1); t_stack_slice_array pop = ts_stack_pop_count(self->stack, version, 1);
// TODO: Figure out how to make this condition occur. // TODO: Figure out how to make this condition occur.
// See https://github.com/atom/atom/issues/18450#issuecomment-439579778 // See https://github.com/atom/atom/issues/18450#issuecomment-439579778
@ -1046,7 +1046,7 @@ static void ts_parser__recover(TSParser *self, StackVersion version, Subtree loo
} }
} }
static void ts_parser__handle_error(TSParser *self, StackVersion version, Subtree lookahead) static void ts_parser__handle_error(TSParser *self, t_stack_version version, Subtree lookahead)
{ {
t_u32 previous_version_count = ts_stack_version_count(self->stack); t_u32 previous_version_count = ts_stack_version_count(self->stack);
@ -1060,7 +1060,7 @@ static void ts_parser__handle_error(TSParser *self, StackVersion version, Subtre
// Push a discontinuity onto the stack. Merge all of the stack versions that // Push a discontinuity onto the stack. Merge all of the stack versions that
// were created in the previous step. // were created in the previous step.
bool did_insert_missing_token = false; bool did_insert_missing_token = false;
for (StackVersion v = version; v < version_count;) for (t_stack_version v = version; v < version_count;)
{ {
if (!did_insert_missing_token) if (!did_insert_missing_token)
{ {
@ -1083,7 +1083,7 @@ static void ts_parser__handle_error(TSParser *self, StackVersion version, Subtre
Length padding = length_sub(self->lexer.token_end_position, position); Length padding = length_sub(self->lexer.token_end_position, position);
t_u32 lookahead_bytes = ts_subtree_total_bytes(lookahead) + ts_subtree_lookahead_bytes(lookahead); t_u32 lookahead_bytes = ts_subtree_total_bytes(lookahead) + ts_subtree_lookahead_bytes(lookahead);
StackVersion version_with_missing_tree = ts_stack_copy_version(self->stack, v); t_stack_version version_with_missing_tree = ts_stack_copy_version(self->stack, v);
Subtree missing_tree = Subtree missing_tree =
ts_subtree_new_missing_leaf(/*&self->tree_pool,*/ missing_symbol, padding, lookahead_bytes, self->language); ts_subtree_new_missing_leaf(/*&self->tree_pool,*/ missing_symbol, padding, lookahead_bytes, self->language);
ts_stack_push(self->stack, version_with_missing_tree, missing_tree, false, state_after_missing_symbol); ts_stack_push(self->stack, version_with_missing_tree, missing_tree, false, state_after_missing_symbol);
@ -1118,7 +1118,7 @@ static void ts_parser__handle_error(TSParser *self, StackVersion version, Subtre
ts_parser__recover(self, version, lookahead); ts_parser__recover(self, version, lookahead);
} }
static bool ts_parser__advance(TSParser *self, StackVersion version, bool allow_node_reuse) static bool ts_parser__advance(TSParser *self, t_stack_version version, bool allow_node_reuse)
{ {
(void)(allow_node_reuse); (void)(allow_node_reuse);
TSStateId state = ts_stack_state(self->stack, version); TSStateId state = ts_stack_state(self->stack, version);
@ -1155,7 +1155,7 @@ static bool ts_parser__advance(TSParser *self, StackVersion version, bool allow_
// an ambiguous state. REDUCE actions always create a new stack // an ambiguous state. REDUCE actions always create a new stack
// version, whereas SHIFT actions update the existing stack version // version, whereas SHIFT actions update the existing stack version
// and terminate this loop. // and terminate this loop.
StackVersion last_reduction_version = STACK_VERSION_NONE; t_stack_version last_reduction_version = STACK_VERSION_NONE;
for (t_u32 i = 0; i < table_entry.action_count; i++) for (t_u32 i = 0; i < table_entry.action_count; i++)
{ {
TSParseAction action = table_entry.actions[i]; TSParseAction action = table_entry.actions[i];
@ -1187,7 +1187,7 @@ static bool ts_parser__advance(TSParser *self, StackVersion version, bool allow_
case TSParseActionTypeReduce: { case TSParseActionTypeReduce: {
bool is_fragile = table_entry.action_count > 1; bool is_fragile = table_entry.action_count > 1;
bool end_of_non_terminal_extra = lookahead == NULL; bool end_of_non_terminal_extra = lookahead == NULL;
StackVersion reduction_version = t_stack_version reduction_version =
ts_parser__reduce(self, version, action.reduce.symbol, action.reduce.child_count, action.reduce.dynamic_precedence, ts_parser__reduce(self, version, action.reduce.symbol, action.reduce.child_count, action.reduce.dynamic_precedence,
action.reduce.production_id, is_fragile, end_of_non_terminal_extra); action.reduce.production_id, is_fragile, end_of_non_terminal_extra);
if (reduction_version != STACK_VERSION_NONE) if (reduction_version != STACK_VERSION_NONE)
@ -1290,7 +1290,7 @@ static bool ts_parser__advance(TSParser *self, StackVersion version, bool allow_
static t_u32 ts_parser__condense_stack(TSParser *self) static t_u32 ts_parser__condense_stack(TSParser *self)
{ {
t_u32 min_error_cost = UINT_MAX; t_u32 min_error_cost = UINT_MAX;
for (StackVersion i = 0; i < ts_stack_version_count(self->stack); i++) for (t_stack_version i = 0; i < ts_stack_version_count(self->stack); i++)
{ {
// Prune any versions that have been marked for removal. // Prune any versions that have been marked for removal.
if (ts_stack_is_halted(self->stack, i)) if (ts_stack_is_halted(self->stack, i))
@ -1311,7 +1311,7 @@ static t_u32 ts_parser__condense_stack(TSParser *self)
// Examine each pair of stack versions, removing any versions that // Examine each pair of stack versions, removing any versions that
// are clearly worse than another version. Ensure that the versions // are clearly worse than another version. Ensure that the versions
// are ordered from most promising to least promising. // are ordered from most promising to least promising.
for (StackVersion j = 0; j < i; j++) for (t_stack_version j = 0; j < i; j++)
{ {
t_error_status status_j = ts_parser__version_status(self, j); t_error_status status_j = ts_parser__version_status(self, j);
@ -1366,7 +1366,7 @@ static t_u32 ts_parser__condense_stack(TSParser *self)
if (ts_stack_version_count(self->stack) > 0) if (ts_stack_version_count(self->stack) > 0)
{ {
bool has_unpaused_version = false; bool has_unpaused_version = false;
for (StackVersion i = 0, n = ts_stack_version_count(self->stack); i < n; i++) for (t_stack_version i = 0, n = ts_stack_version_count(self->stack); i < n; i++)
{ {
if (ts_stack_is_paused(self->stack, i)) if (ts_stack_is_paused(self->stack, i))
{ {
@ -1485,7 +1485,7 @@ TSTree *ts_parser_parse(TSParser *self, TSInput input)
t_u32 version_count = 0; t_u32 version_count = 0;
do do
{ {
for (StackVersion version = 0; version_count = ts_stack_version_count(self->stack), version < version_count; version++) for (t_stack_version version = 0; version_count = ts_stack_version_count(self->stack), version < version_count; version++)
{ {
bool allow_node_reuse = version_count == 1; bool allow_node_reuse = version_count == 1;
while (ts_stack_is_active(self->stack, version)) while (ts_stack_is_active(self->stack, version))

View file

@ -1,3 +1,14 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 15:22:58 by maiboyer #+# #+# */
/* Updated: 2024/08/31 15:35:03 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser/stack.h" #include "parser/stack.h"
#include "me/mem/mem.h" #include "me/mem/mem.h"
@ -13,20 +24,28 @@
#define MAX_NODE_POOL_SIZE 0 #define MAX_NODE_POOL_SIZE 0
#define MAX_ITERATOR_COUNT 0 #define MAX_ITERATOR_COUNT 0
typedef struct StackNode StackNode; typedef enum e_stack_status t_stack_status;
typedef struct s_stack_head t_stack_head;
typedef struct s_stack_iterator t_stack_iterator;
typedef struct s_stack_link t_stack_link;
typedef struct s_stack_node t_stack_node;
typedef struct s_summarize_stack_session t_summarize_stack_session;
typedef t_u32 t_stack_action;
typedef struct StackLink typedef t_stack_action (*t_stack_callback)(void *, const t_stack_iterator *);
struct s_stack_link
{ {
StackNode *node; t_stack_node *node;
Subtree subtree; Subtree subtree;
bool is_pending; bool is_pending;
} StackLink; };
struct StackNode struct s_stack_node
{ {
TSStateId state; TSStateId state;
Length position; Length position;
StackLink links[MAX_LINK_COUNT]; t_stack_link links[MAX_LINK_COUNT];
t_u16 link_count; t_u16 link_count;
t_u32 ref_count; t_u32 ref_count;
t_u32 error_cost; t_u32 error_cost;
@ -34,55 +53,53 @@ struct StackNode
int dynamic_precedence; int dynamic_precedence;
}; };
typedef struct StackIterator StackIterator; struct s_stack_iterator
struct StackIterator
{ {
StackNode *node; t_stack_node *node;
SubtreeArray subtrees; SubtreeArray subtrees;
t_u32 subtree_count; t_u32 subtree_count;
bool is_pending; bool is_pending;
}; };
typedef Array(StackNode *) StackNodeArray; enum e_stack_status
typedef enum StackStatus StackStatus;
enum StackStatus
{ {
StackStatusActive, SStatusActive,
StackStatusPaused, SStatusPaused,
StackStatusHalted, SStatusHalted,
}; };
typedef struct StackHead StackHead; struct s_stack_head
struct StackHead
{ {
StackNode *node; t_stack_node *node;
StackSummary *summary; t_stack_summary *summary;
t_u32 node_count_at_last_error; t_u32 node_count_at_last_error;
Subtree last_external_token; Subtree last_external_token;
Subtree lookahead_when_paused; Subtree lookahead_when_paused;
StackStatus status; t_stack_status status;
}; };
struct Stack struct s_stack
{ {
Array(StackHead) heads; Array(t_stack_head) heads;
StackSliceArray slices; t_stack_slice_array slices;
Array(StackIterator) iterators; Array(t_stack_iterator) iterators;
StackNode *base_node; t_stack_node *base_node;
}; };
typedef t_u32 StackAction; enum e_stack_action
enum StackAction
{ {
StackActionNone, SActionNone,
StackActionStop = 1, SActionStop = 1,
StackActionPop = 2, SActionPop = 2,
}; };
typedef StackAction (*StackCallback)(void *, const StackIterator *); struct s_summarize_stack_session
{
t_stack_summary *summary;
t_u32 max_depth;
};
static void stack_node_retain(StackNode *self) static void stack_node_retain(t_stack_node *self)
{ {
if (!self) if (!self)
return; return;
@ -91,7 +108,7 @@ static void stack_node_retain(StackNode *self)
assert(self->ref_count != 0); assert(self->ref_count != 0);
} }
static void stack_node_release(StackNode *self) static void stack_node_release(t_stack_node *self)
{ {
recur: recur:
assert(self->ref_count != 0); assert(self->ref_count != 0);
@ -99,17 +116,17 @@ recur:
if (self->ref_count > 0) if (self->ref_count > 0)
return; return;
StackNode *first_predecessor = NULL; t_stack_node *first_predecessor = NULL;
if (self->link_count > 0) if (self->link_count > 0)
{ {
for (t_u32 i = self->link_count - 1; i > 0; i--) for (t_u32 i = self->link_count - 1; i > 0; i--)
{ {
StackLink link = self->links[i]; t_stack_link link = self->links[i];
if (link.subtree) if (link.subtree)
ts_subtree_release(link.subtree); ts_subtree_release(link.subtree);
stack_node_release(link.node); stack_node_release(link.node);
} }
StackLink link = self->links[0]; t_stack_link link = self->links[0];
if (link.subtree) if (link.subtree)
ts_subtree_release(link.subtree); ts_subtree_release(link.subtree);
first_predecessor = self->links[0].node; first_predecessor = self->links[0].node;
@ -141,15 +158,15 @@ static t_u32 stack__subtree_node_count(Subtree subtree)
return count; return count;
} }
static StackNode *stack_node_new(StackNode *previous_node, Subtree subtree, bool is_pending, TSStateId state) static t_stack_node *stack_node_new(t_stack_node *previous_node, Subtree subtree, bool is_pending, TSStateId state)
{ {
StackNode *node = mem_alloc(sizeof(StackNode)); t_stack_node *node = mem_alloc(sizeof(t_stack_node));
*node = (StackNode){.ref_count = 1, .link_count = 0, .state = state}; *node = (t_stack_node){.ref_count = 1, .link_count = 0, .state = state};
if (previous_node != NULL) if (previous_node != NULL)
{ {
node->link_count = 1; node->link_count = 1;
node->links[0] = (StackLink){ node->links[0] = (t_stack_link){
.node = previous_node, .node = previous_node,
.subtree = subtree, .subtree = subtree,
.is_pending = is_pending, .is_pending = is_pending,
@ -197,14 +214,14 @@ static bool stack__subtree_is_equivalent(Subtree left, Subtree right)
ts_subtree_extra(left) == ts_subtree_extra(right) && ts_subtree_external_scanner_state_eq(left, 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) static void stack_node_add_link(t_stack_node *self, t_stack_link link)
{ {
if (link.node == self) if (link.node == self)
return; return;
for (int i = 0; i < self->link_count; i++) for (int i = 0; i < self->link_count; i++)
{ {
StackLink *existing_link = &self->links[i]; t_stack_link *existing_link = &self->links[i];
if (stack__subtree_is_equivalent(existing_link->subtree, link.subtree)) if (stack__subtree_is_equivalent(existing_link->subtree, link.subtree))
{ {
// In general, we preserve ambiguities until they are removed from the stack // In general, we preserve ambiguities until they are removed from the stack
@ -266,7 +283,7 @@ static void stack_node_add_link(StackNode *self, StackLink link)
self->dynamic_precedence = dynamic_precedence; self->dynamic_precedence = dynamic_precedence;
} }
static void stack_head_delete(StackHead *self) static void stack_head_delete(t_stack_head *self)
{ {
if (self->node) if (self->node)
{ {
@ -287,47 +304,48 @@ static void stack_head_delete(StackHead *self)
} }
} }
static StackVersion ts_stack__add_version(Stack *self, StackVersion original_version, StackNode *node) static t_stack_version ts_stack__add_version(Stack *self, t_stack_version original_version, t_stack_node *node)
{ {
StackHead head = { t_stack_head head = {
.node = node, .node = node,
.node_count_at_last_error = self->heads.contents[original_version].node_count_at_last_error, .node_count_at_last_error = self->heads.contents[original_version].node_count_at_last_error,
.last_external_token = self->heads.contents[original_version].last_external_token, .last_external_token = self->heads.contents[original_version].last_external_token,
.status = StackStatusActive, .status = SStatusActive,
.lookahead_when_paused = NULL, .lookahead_when_paused = NULL,
}; };
array_push(&self->heads, head); array_push(&self->heads, head);
stack_node_retain(node); stack_node_retain(node);
if (head.last_external_token) if (head.last_external_token)
ts_subtree_retain(head.last_external_token); ts_subtree_retain(head.last_external_token);
return (StackVersion)(self->heads.size - 1); return (t_stack_version)(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, t_stack_version original_version, t_stack_node *node, SubtreeArray *subtrees)
{ {
for (t_u32 i = self->slices.size - 1; i + 1 > 0; i--) for (t_u32 i = self->slices.size - 1; i + 1 > 0; i--)
{ {
StackVersion version = self->slices.contents[i].version; t_stack_version version = self->slices.contents[i].version;
if (self->heads.contents[version].node == node) if (self->heads.contents[version].node == node)
{ {
StackSlice slice = {*subtrees, version}; t_stack_slice slice = {*subtrees, version};
array_insert(&self->slices, i + 1, slice); array_insert(&self->slices, i + 1, slice);
return; return;
} }
} }
StackVersion version = ts_stack__add_version(self, original_version, node); t_stack_version version = ts_stack__add_version(self, original_version, node);
StackSlice slice = {*subtrees, version}; t_stack_slice slice = {*subtrees, version};
array_push(&self->slices, slice); array_push(&self->slices, slice);
} }
static StackSliceArray stack__iter(Stack *self, StackVersion version, StackCallback callback, void *payload, int goal_subtree_count) static t_stack_slice_array stack__iter(Stack *self, t_stack_version version, t_stack_callback callback, void *payload,
int goal_subtree_count)
{ {
array_clear(&self->slices); array_clear(&self->slices);
array_clear(&self->iterators); array_clear(&self->iterators);
StackHead *head = array_get(&self->heads, version); t_stack_head *head = array_get(&self->heads, version);
StackIterator new_iterator = { t_stack_iterator new_iterator = {
.node = head->node, .node = head->node,
.subtrees = array_new(), .subtrees = array_new(),
.subtree_count = 0, .subtree_count = 0,
@ -347,12 +365,12 @@ static StackSliceArray stack__iter(Stack *self, StackVersion version, StackCallb
{ {
for (t_u32 i = 0, size = self->iterators.size; i < size; i++) for (t_u32 i = 0, size = self->iterators.size; i < size; i++)
{ {
StackIterator *iterator = &self->iterators.contents[i]; t_stack_iterator *iterator = &self->iterators.contents[i];
StackNode *node = iterator->node; t_stack_node *node = iterator->node;
StackAction action = callback(payload, iterator); t_stack_action action = callback(payload, iterator);
bool should_pop = action & StackActionPop; bool should_pop = action & SActionPop;
bool should_stop = action & StackActionStop || node->link_count == 0; bool should_stop = action & SActionStop || node->link_count == 0;
if (should_pop) if (should_pop)
{ {
@ -378,8 +396,8 @@ static StackSliceArray stack__iter(Stack *self, StackVersion version, StackCallb
for (t_u32 j = 1; j <= node->link_count; j++) for (t_u32 j = 1; j <= node->link_count; j++)
{ {
StackIterator *next_iterator; t_stack_iterator *next_iterator;
StackLink link; t_stack_link link;
if (j == node->link_count) if (j == node->link_count)
{ {
link = node->links[0]; link = node->links[0];
@ -390,7 +408,7 @@ static StackSliceArray stack__iter(Stack *self, StackVersion version, StackCallb
if (self->iterators.size >= MAX_ITERATOR_COUNT) if (self->iterators.size >= MAX_ITERATOR_COUNT)
continue; continue;
link = node->links[j]; link = node->links[j];
StackIterator current_iterator = self->iterators.contents[i]; t_stack_iterator current_iterator = self->iterators.contents[i];
array_push(&self->iterators, current_iterator); array_push(&self->iterators, current_iterator);
next_iterator = array_back(&self->iterators); next_iterator = array_back(&self->iterators);
ts_subtree_array_copy(next_iterator->subtrees, &next_iterator->subtrees); ts_subtree_array_copy(next_iterator->subtrees, &next_iterator->subtrees);
@ -442,7 +460,7 @@ Stack *ts_stack_new(void)
self->base_node = stack_node_new(NULL, NULL, false, 1); self->base_node = stack_node_new(NULL, NULL, false, 1);
ts_stack_clear(self); ts_stack_clear(self);
return self; return (self);
} }
void ts_stack_delete(Stack *self) void ts_stack_delete(Stack *self)
@ -466,24 +484,24 @@ t_u32 ts_stack_version_count(const Stack *self)
return self->heads.size; return self->heads.size;
} }
TSStateId ts_stack_state(const Stack *self, StackVersion version) TSStateId ts_stack_state(const Stack *self, t_stack_version version)
{ {
return array_get(&self->heads, version)->node->state; return array_get(&self->heads, version)->node->state;
} }
Length ts_stack_position(const Stack *self, StackVersion version) Length ts_stack_position(const Stack *self, t_stack_version version)
{ {
return array_get(&self->heads, version)->node->position; return array_get(&self->heads, version)->node->position;
} }
Subtree ts_stack_last_external_token(const Stack *self, StackVersion version) Subtree ts_stack_last_external_token(const Stack *self, t_stack_version version)
{ {
return array_get(&self->heads, version)->last_external_token; return array_get(&self->heads, version)->last_external_token;
} }
void ts_stack_set_last_external_token(Stack *self, StackVersion version, Subtree token) void ts_stack_set_last_external_token(Stack *self, t_stack_version version, Subtree token)
{ {
StackHead *head = array_get(&self->heads, version); t_stack_head *head = array_get(&self->heads, version);
if (token) if (token)
ts_subtree_retain(token); ts_subtree_retain(token);
if (head->last_external_token) if (head->last_external_token)
@ -491,20 +509,20 @@ void ts_stack_set_last_external_token(Stack *self, StackVersion version, Subtree
head->last_external_token = token; head->last_external_token = token;
} }
t_u32 ts_stack_error_cost(const Stack *self, StackVersion version) t_u32 ts_stack_error_cost(const Stack *self, t_stack_version version)
{ {
StackHead *head = array_get(&self->heads, version); t_stack_head *head = array_get(&self->heads, version);
t_u32 result = head->node->error_cost; t_u32 result = head->node->error_cost;
if (head->status == StackStatusPaused || (head->node->state == ERROR_STATE && !head->node->links[0].subtree)) if (head->status == SStatusPaused || (head->node->state == ERROR_STATE && !head->node->links[0].subtree))
{ {
result += ERROR_COST_PER_RECOVERY; result += ERROR_COST_PER_RECOVERY;
} }
return result; return result;
} }
t_u32 ts_stack_node_count_since_error(const Stack *self, StackVersion version) t_u32 ts_stack_node_count_since_error(const Stack *self, t_stack_version version)
{ {
StackHead *head = array_get(&self->heads, version); t_stack_head *head = array_get(&self->heads, version);
if (head->node->node_count < head->node_count_at_last_error) if (head->node->node_count < head->node_count_at_last_error)
{ {
head->node_count_at_last_error = head->node->node_count; head->node_count_at_last_error = head->node->node_count;
@ -512,56 +530,56 @@ t_u32 ts_stack_node_count_since_error(const Stack *self, StackVersion version)
return head->node->node_count - head->node_count_at_last_error; 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, t_stack_version version, Subtree subtree, bool pending, TSStateId state)
{ {
StackHead *head = array_get(&self->heads, version); t_stack_head *head = array_get(&self->heads, version);
StackNode *new_node = stack_node_new(head->node, subtree, pending, state); t_stack_node *new_node = stack_node_new(head->node, subtree, pending, state);
if (!subtree) if (!subtree)
head->node_count_at_last_error = new_node->node_count; head->node_count_at_last_error = new_node->node_count;
head->node = new_node; head->node = new_node;
} }
StackAction pop_count_callback(void *payload, const StackIterator *iterator) t_stack_action pop_count_callback(void *payload, const t_stack_iterator *iterator)
{ {
t_u32 *goal_subtree_count = payload; t_u32 *goal_subtree_count = payload;
if (iterator->subtree_count == *goal_subtree_count) if (iterator->subtree_count == *goal_subtree_count)
{ {
return StackActionPop | StackActionStop; return SActionPop | SActionStop;
} }
else else
{ {
return StackActionNone; return SActionNone;
} }
} }
StackSliceArray ts_stack_pop_count(Stack *self, StackVersion version, t_u32 count) t_stack_slice_array ts_stack_pop_count(Stack *self, t_stack_version version, t_u32 count)
{ {
return stack__iter(self, version, pop_count_callback, &count, (int)count); return stack__iter(self, version, pop_count_callback, &count, (int)count);
} }
StackAction pop_pending_callback(void *payload, const StackIterator *iterator) t_stack_action pop_pending_callback(void *payload, const t_stack_iterator *iterator)
{ {
(void)payload; (void)payload;
if (iterator->subtree_count >= 1) if (iterator->subtree_count >= 1)
{ {
if (iterator->is_pending) if (iterator->is_pending)
{ {
return StackActionPop | StackActionStop; return SActionPop | SActionStop;
} }
else else
{ {
return StackActionStop; return SActionStop;
} }
} }
else else
{ {
return StackActionNone; return SActionNone;
} }
} }
StackSliceArray ts_stack_pop_pending(Stack *self, StackVersion version) t_stack_slice_array ts_stack_pop_pending(Stack *self, t_stack_version version)
{ {
StackSliceArray pop = stack__iter(self, version, pop_pending_callback, NULL, 0); t_stack_slice_array pop = stack__iter(self, version, pop_pending_callback, NULL, 0);
if (pop.size > 0) if (pop.size > 0)
{ {
ts_stack_renumber_version(self, pop.contents[0].version, version); ts_stack_renumber_version(self, pop.contents[0].version, version);
@ -570,7 +588,7 @@ StackSliceArray ts_stack_pop_pending(Stack *self, StackVersion version)
return pop; return pop;
} }
StackAction pop_error_callback(void *payload, const StackIterator *iterator) t_stack_action pop_error_callback(void *payload, const t_stack_iterator *iterator)
{ {
if (iterator->subtrees.size > 0) if (iterator->subtrees.size > 0)
{ {
@ -578,28 +596,28 @@ StackAction pop_error_callback(void *payload, const StackIterator *iterator)
if (!*found_error && ts_subtree_is_error(iterator->subtrees.contents[0])) if (!*found_error && ts_subtree_is_error(iterator->subtrees.contents[0]))
{ {
*found_error = true; *found_error = true;
return StackActionPop | StackActionStop; return SActionPop | SActionStop;
} }
else else
{ {
return StackActionStop; return SActionStop;
} }
} }
else else
{ {
return StackActionNone; return SActionNone;
} }
} }
SubtreeArray ts_stack_pop_error(Stack *self, StackVersion version) SubtreeArray ts_stack_pop_error(Stack *self, t_stack_version version)
{ {
StackNode *node = array_get(&self->heads, version)->node; t_stack_node *node = array_get(&self->heads, version)->node;
for (t_u32 i = 0; i < node->link_count; i++) for (t_u32 i = 0; i < node->link_count; i++)
{ {
if (node->links[i].subtree && ts_subtree_is_error(node->links[i].subtree)) if (node->links[i].subtree && ts_subtree_is_error(node->links[i].subtree))
{ {
bool found_error = false; bool found_error = false;
StackSliceArray pop = stack__iter(self, version, pop_error_callback, &found_error, 1); t_stack_slice_array pop = stack__iter(self, version, pop_error_callback, &found_error, 1);
if (pop.size > 0) if (pop.size > 0)
{ {
assert(pop.size == 1); assert(pop.size == 1);
@ -612,52 +630,46 @@ SubtreeArray ts_stack_pop_error(Stack *self, StackVersion version)
return (SubtreeArray){.size = 0}; return (SubtreeArray){.size = 0};
} }
StackAction pop_all_callback(void *payload, const StackIterator *iterator) t_stack_action pop_all_callback(void *payload, const t_stack_iterator *iterator)
{ {
(void)payload; (void)payload;
return iterator->node->link_count == 0 ? StackActionPop : StackActionNone; return iterator->node->link_count == 0 ? SActionPop : SActionNone;
} }
StackSliceArray ts_stack_pop_all(Stack *self, StackVersion version) t_stack_slice_array ts_stack_pop_all(Stack *self, t_stack_version version)
{ {
return stack__iter(self, version, pop_all_callback, NULL, 0); return stack__iter(self, version, pop_all_callback, NULL, 0);
} }
typedef struct SummarizeStackSession t_stack_action summarize_stack_callback(void *payload, const t_stack_iterator *iterator)
{ {
StackSummary *summary; t_summarize_stack_session *session = payload;
t_u32 max_depth;
} SummarizeStackSession;
StackAction summarize_stack_callback(void *payload, const StackIterator *iterator)
{
SummarizeStackSession *session = payload;
TSStateId state = iterator->node->state; TSStateId state = iterator->node->state;
t_u32 depth = iterator->subtree_count; t_u32 depth = iterator->subtree_count;
if (depth > session->max_depth) if (depth > session->max_depth)
return StackActionStop; return SActionStop;
for (t_u32 i = session->summary->size - 1; i + 1 > 0; i--) for (t_u32 i = session->summary->size - 1; i + 1 > 0; i--)
{ {
StackSummaryEntry entry = session->summary->contents[i]; t_stack_summary_entry entry = session->summary->contents[i];
if (entry.depth < depth) if (entry.depth < depth)
break; break;
if (entry.depth == depth && entry.state == state) if (entry.depth == depth && entry.state == state)
return StackActionNone; return SActionNone;
} }
array_push(session->summary, ((StackSummaryEntry){ array_push(session->summary, ((t_stack_summary_entry){
.position = iterator->node->position, .position = iterator->node->position,
.depth = depth, .depth = depth,
.state = state, .state = state,
})); }));
return StackActionNone; return SActionNone;
} }
void ts_stack_record_summary(Stack *self, StackVersion version, t_u32 max_depth) void ts_stack_record_summary(Stack *self, t_stack_version version, t_u32 max_depth)
{ {
SummarizeStackSession session = {.summary = mem_alloc(sizeof(StackSummary)), .max_depth = max_depth}; t_summarize_stack_session session = {.summary = mem_alloc(sizeof(t_stack_summary)), .max_depth = max_depth};
array_init(session.summary); array_init(session.summary);
stack__iter(self, version, summarize_stack_callback, &session, -1); stack__iter(self, version, summarize_stack_callback, &session, -1);
StackHead *head = &self->heads.contents[version]; t_stack_head *head = &self->heads.contents[version];
if (head->summary) if (head->summary)
{ {
array_delete(head->summary); array_delete(head->summary);
@ -666,20 +678,20 @@ void ts_stack_record_summary(Stack *self, StackVersion version, t_u32 max_depth)
head->summary = session.summary; head->summary = session.summary;
} }
StackSummary *ts_stack_get_summary(Stack *self, StackVersion version) t_stack_summary *ts_stack_get_summary(Stack *self, t_stack_version version)
{ {
return array_get(&self->heads, version)->summary; return array_get(&self->heads, version)->summary;
} }
int ts_stack_dynamic_precedence(Stack *self, StackVersion version) int ts_stack_dynamic_precedence(Stack *self, t_stack_version version)
{ {
return array_get(&self->heads, version)->node->dynamic_precedence; return array_get(&self->heads, version)->node->dynamic_precedence;
} }
bool ts_stack_has_advanced_since_error(const Stack *self, StackVersion version) bool ts_stack_has_advanced_since_error(const Stack *self, t_stack_version version)
{ {
const StackHead *head = array_get(&self->heads, version); const t_stack_head *head = array_get(&self->heads, version);
const StackNode *node = head->node; const t_stack_node *node = head->node;
if (node->error_cost == 0) if (node->error_cost == 0)
return true; return true;
while (node) while (node)
@ -705,20 +717,20 @@ bool ts_stack_has_advanced_since_error(const Stack *self, StackVersion version)
return false; return false;
} }
void ts_stack_remove_version(Stack *self, StackVersion version) void ts_stack_remove_version(Stack *self, t_stack_version version)
{ {
stack_head_delete(array_get(&self->heads, version)); stack_head_delete(array_get(&self->heads, version));
array_erase(&self->heads, version); array_erase(&self->heads, version);
} }
void ts_stack_renumber_version(Stack *self, StackVersion v1, StackVersion v2) void ts_stack_renumber_version(Stack *self, t_stack_version v1, t_stack_version v2)
{ {
if (v1 == v2) if (v1 == v2)
return; return;
assert(v2 < v1); assert(v2 < v1);
assert((t_u32)v1 < self->heads.size); assert((t_u32)v1 < self->heads.size);
StackHead *source_head = &self->heads.contents[v1]; t_stack_head *source_head = &self->heads.contents[v1];
StackHead *target_head = &self->heads.contents[v2]; t_stack_head *target_head = &self->heads.contents[v2];
if (target_head->summary && !source_head->summary) if (target_head->summary && !source_head->summary)
{ {
source_head->summary = target_head->summary; source_head->summary = target_head->summary;
@ -729,18 +741,18 @@ void ts_stack_renumber_version(Stack *self, StackVersion v1, StackVersion v2)
array_erase(&self->heads, v1); array_erase(&self->heads, v1);
} }
void ts_stack_swap_versions(Stack *self, StackVersion v1, StackVersion v2) void ts_stack_swap_versions(Stack *self, t_stack_version v1, t_stack_version v2)
{ {
StackHead temporary_head = self->heads.contents[v1]; t_stack_head temporary_head = self->heads.contents[v1];
self->heads.contents[v1] = self->heads.contents[v2]; self->heads.contents[v1] = self->heads.contents[v2];
self->heads.contents[v2] = temporary_head; self->heads.contents[v2] = temporary_head;
} }
StackVersion ts_stack_copy_version(Stack *self, StackVersion version) t_stack_version ts_stack_copy_version(Stack *self, t_stack_version version)
{ {
assert(version < self->heads.size); assert(version < self->heads.size);
array_push(&self->heads, self->heads.contents[version]); array_push(&self->heads, self->heads.contents[version]);
StackHead *head = array_back(&self->heads); t_stack_head *head = array_back(&self->heads);
stack_node_retain(head->node); stack_node_retain(head->node);
if (head->last_external_token) if (head->last_external_token)
ts_subtree_retain(head->last_external_token); ts_subtree_retain(head->last_external_token);
@ -748,12 +760,12 @@ StackVersion ts_stack_copy_version(Stack *self, StackVersion version)
return self->heads.size - 1; return self->heads.size - 1;
} }
bool ts_stack_merge(Stack *self, StackVersion version1, StackVersion version2) bool ts_stack_merge(Stack *self, t_stack_version version1, t_stack_version version2)
{ {
if (!ts_stack_can_merge(self, version1, version2)) if (!ts_stack_can_merge(self, version1, version2))
return false; return false;
StackHead *head1 = &self->heads.contents[version1]; t_stack_head *head1 = &self->heads.contents[version1];
StackHead *head2 = &self->heads.contents[version2]; t_stack_head *head2 = &self->heads.contents[version2];
for (t_u32 i = 0; i < head2->node->link_count; i++) for (t_u32 i = 0; i < head2->node->link_count; i++)
{ {
stack_node_add_link(head1->node, head2->node->links[i]); stack_node_add_link(head1->node, head2->node->links[i]);
@ -766,49 +778,49 @@ bool ts_stack_merge(Stack *self, StackVersion version1, StackVersion version2)
return true; return true;
} }
bool ts_stack_can_merge(Stack *self, StackVersion version1, StackVersion version2) bool ts_stack_can_merge(Stack *self, t_stack_version version1, t_stack_version version2)
{ {
StackHead *head1 = &self->heads.contents[version1]; t_stack_head *head1 = &self->heads.contents[version1];
StackHead *head2 = &self->heads.contents[version2]; t_stack_head *head2 = &self->heads.contents[version2];
return head1->status == StackStatusActive && head2->status == StackStatusActive && head1->node->state == head2->node->state && return head1->status == SStatusActive && head2->status == SStatusActive && head1->node->state == head2->node->state &&
head1->node->position.bytes == head2->node->position.bytes && head1->node->error_cost == head2->node->error_cost && head1->node->position.bytes == head2->node->position.bytes && head1->node->error_cost == head2->node->error_cost &&
ts_subtree_external_scanner_state_eq(head1->last_external_token, head2->last_external_token); ts_subtree_external_scanner_state_eq(head1->last_external_token, head2->last_external_token);
} }
void ts_stack_halt(Stack *self, StackVersion version) void ts_stack_halt(Stack *self, t_stack_version version)
{ {
array_get(&self->heads, version)->status = StackStatusHalted; array_get(&self->heads, version)->status = SStatusHalted;
} }
void ts_stack_pause(Stack *self, StackVersion version, Subtree lookahead) void ts_stack_pause(Stack *self, t_stack_version version, Subtree lookahead)
{ {
StackHead *head = array_get(&self->heads, version); t_stack_head *head = array_get(&self->heads, version);
head->status = StackStatusPaused; head->status = SStatusPaused;
head->lookahead_when_paused = lookahead; head->lookahead_when_paused = lookahead;
head->node_count_at_last_error = head->node->node_count; head->node_count_at_last_error = head->node->node_count;
} }
bool ts_stack_is_active(const Stack *self, StackVersion version) bool ts_stack_is_active(const Stack *self, t_stack_version version)
{ {
return array_get(&self->heads, version)->status == StackStatusActive; return array_get(&self->heads, version)->status == SStatusActive;
} }
bool ts_stack_is_halted(const Stack *self, StackVersion version) bool ts_stack_is_halted(const Stack *self, t_stack_version version)
{ {
return array_get(&self->heads, version)->status == StackStatusHalted; return array_get(&self->heads, version)->status == SStatusHalted;
} }
bool ts_stack_is_paused(const Stack *self, StackVersion version) bool ts_stack_is_paused(const Stack *self, t_stack_version version)
{ {
return array_get(&self->heads, version)->status == StackStatusPaused; return array_get(&self->heads, version)->status == SStatusPaused;
} }
Subtree ts_stack_resume(Stack *self, StackVersion version) Subtree ts_stack_resume(Stack *self, t_stack_version version)
{ {
StackHead *head = array_get(&self->heads, version); t_stack_head *head = array_get(&self->heads, version);
assert(head->status == StackStatusPaused); assert(head->status == SStatusPaused);
Subtree result = head->lookahead_when_paused; Subtree result = head->lookahead_when_paused;
head->status = StackStatusActive; head->status = SStatusActive;
head->lookahead_when_paused = NULL; head->lookahead_when_paused = NULL;
return result; return result;
} }
@ -821,9 +833,9 @@ void ts_stack_clear(Stack *self)
stack_head_delete(&self->heads.contents[i]); stack_head_delete(&self->heads.contents[i]);
} }
array_clear(&self->heads); array_clear(&self->heads);
array_push(&self->heads, ((StackHead){ array_push(&self->heads, ((t_stack_head){
.node = self->base_node, .node = self->base_node,
.status = StackStatusActive, .status = SStatusActive,
.last_external_token = NULL, .last_external_token = NULL,
.lookahead_when_paused = NULL, .lookahead_when_paused = NULL,
})); }));

View file

@ -48,7 +48,7 @@ for line in dump_binary.stdout.split('\n'):
if (len (words) >= 8 and words[3] == 'FUNC'): if (len (words) >= 8 and words[3] == 'FUNC'):
symbols_binary.add(words[7]) symbols_binary.add(words[7])
diff = list(symbols_binary - symbols_archive) diff = list(symbols_archive - symbols_binary)
diff.sort() diff.sort()
for sym in diff: for sym in diff:
print(f"{sym}") print(f"{sym}")