update: normed more stuff
This commit is contained in:
parent
74937f457f
commit
d42f9ae49f
8 changed files with 349 additions and 230 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/02 20:01:50 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/09/06 17:00:26 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/09/14 14:30:56 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -76,4 +76,42 @@ struct s_subtree_data
|
|||
// The fundamental building block of a syntax tree.
|
||||
typedef t_subtree_data *t_subtree;
|
||||
|
||||
typedef struct s_subtree_new_leaf_args t_st_newleaf_args;
|
||||
typedef struct s_subtree_new_error_args t_st_newerr_args;
|
||||
|
||||
struct s_subtree_new_leaf_args
|
||||
{
|
||||
TSSymbol symbol;
|
||||
Length padding;
|
||||
Length size;
|
||||
t_u32 lookahead_bytes;
|
||||
TSStateId parse_state;
|
||||
bool has_external_tokens;
|
||||
bool depends_on_column;
|
||||
bool is_keyword;
|
||||
const TSLanguage *language;
|
||||
};
|
||||
|
||||
struct s_subtree_new_error_args
|
||||
{
|
||||
t_i32 lookahead_char;
|
||||
Length padding;
|
||||
Length size;
|
||||
t_u32 bytes_scanned;
|
||||
TSStateId parse_state;
|
||||
const TSLanguage *language;
|
||||
};
|
||||
|
||||
struct s_summarize_state
|
||||
{
|
||||
t_u32 structural_index;
|
||||
const TSSymbol *alias_sequence;
|
||||
t_u32 lookahead_end_byte;
|
||||
t_subtree *children;
|
||||
t_subtree child;
|
||||
t_u32 child_lookahead_end_byte;
|
||||
t_u32 grandchild_count;
|
||||
t_usize i;
|
||||
};
|
||||
|
||||
#endif /* SUBTREE_INNER_H */
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/08/31 12:03:06 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/09/02 20:25:33 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/09/14 14:14:46 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -42,8 +42,8 @@ void ts_subtree_array_delete(t_vec_subtree *);
|
|||
void ts_subtree_array_remove_trailing_extras(t_vec_subtree *, t_vec_subtree *);
|
||||
void ts_subtree_array_reverse(t_vec_subtree *);
|
||||
|
||||
t_subtree ts_subtree_new_leaf(TSSymbol, Length, Length, t_u32, TSStateId, bool, bool, bool, const TSLanguage *);
|
||||
t_subtree ts_subtree_new_error(t_i32, Length, Length, t_u32, TSStateId, const TSLanguage *);
|
||||
t_subtree ts_subtree_new_leaf(t_st_newleaf_args args);
|
||||
t_subtree ts_subtree_new_error(t_st_newerr_args args);
|
||||
t_subtree ts_subtree_new_node(TSSymbol, t_vec_subtree *, t_u32, const TSLanguage *);
|
||||
t_subtree ts_subtree_new_error_node(t_vec_subtree *, bool, const TSLanguage *);
|
||||
t_subtree ts_subtree_new_missing_leaf(TSSymbol, Length, t_u32, const TSLanguage *);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/13 13:54:24 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/09/13 13:54:46 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/09/14 14:32:56 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -122,8 +122,8 @@ t_subtree ts_parser__lex(TSParser *self, t_stack_version version,
|
|||
padding = length_sub(error_start_position, start_position);
|
||||
size = length_sub(error_end_position, error_start_position);
|
||||
lookahead_bytes = lookahead_end_byte - error_end_position.bytes;
|
||||
result = ts_subtree_new_error(first_error_character, padding, size,
|
||||
lookahead_bytes, parse_state, self->language);
|
||||
result = ts_subtree_new_error((t_st_newerr_args){first_error_character, padding, size,
|
||||
lookahead_bytes, parse_state, self->language});
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -151,9 +151,9 @@ t_subtree ts_parser__lex(TSParser *self, t_stack_version version,
|
|||
symbol = self->lexer.data.result_symbol;
|
||||
}
|
||||
}
|
||||
result = ts_subtree_new_leaf(symbol, padding, size, lookahead_bytes,
|
||||
result = ts_subtree_new_leaf((t_st_newleaf_args){symbol, padding, size, lookahead_bytes,
|
||||
parse_state, found_external_token, called_get_column,
|
||||
is_keyword, self->language);
|
||||
is_keyword, self->language});
|
||||
if (found_external_token)
|
||||
{
|
||||
ts_external_scanner_state_init(&result->external_scanner_state,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/14 12:53:20 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/09/14 12:53:42 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/09/14 14:02:24 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -14,94 +14,110 @@
|
|||
#include "parser/inner/ptypes.h"
|
||||
#include "parser/subtree.h"
|
||||
|
||||
bool _subtree_compress_inner(\
|
||||
t_subtree *tree, t_symbol symbol, t_vec_subtree *stack)
|
||||
{
|
||||
t_subtree child[2];
|
||||
|
||||
if ((*tree)->ref_count > 1 || (*tree)->child_count < 2)
|
||||
return (true);
|
||||
child[0] = (ts_subtree_children(*tree)[0]);
|
||||
if (child[0]->child_count < 2 || child[0]->ref_count > 1
|
||||
|| child[0]->symbol != symbol)
|
||||
return (true);
|
||||
child[1] = (ts_subtree_children(child[0])[0]);
|
||||
if (child[1]->child_count < 2 || child[1]->ref_count > 1
|
||||
|| child[1]->symbol != symbol)
|
||||
return (true);
|
||||
ts_subtree_children(*tree)[0] = child[1];
|
||||
ts_subtree_children(child[0])[0] = ts_subtree_children(child[1])[\
|
||||
child[1]->child_count - 1];
|
||||
ts_subtree_children(child[1])[child[1]->child_count - 1] = (child[0]);
|
||||
vec_subtree_push(stack, *tree);
|
||||
*tree = child[1];
|
||||
return (false);
|
||||
}
|
||||
|
||||
void ts_subtree__compress(t_subtree self, t_u32 count,
|
||||
const t_language *language, t_vec_subtree *stack)
|
||||
{
|
||||
t_u32 initial_stack_size;
|
||||
t_symbol symbol;
|
||||
t_subtree child[2];
|
||||
t_subtree tree;
|
||||
TSSymbol symbol;
|
||||
t_u32 i;
|
||||
t_subtree child;
|
||||
t_subtree grandchild;
|
||||
t_u32 initial_stack_size;
|
||||
|
||||
initial_stack_size = stack->len;
|
||||
tree = self;
|
||||
symbol = tree->symbol;
|
||||
i = 0;
|
||||
while (i < count)
|
||||
{
|
||||
if (tree->ref_count > 1 || tree->child_count < 2)
|
||||
while (i++ < count)
|
||||
if (_subtree_compress_inner(&tree, symbol, stack))
|
||||
break ;
|
||||
child = (ts_subtree_children(tree)[0]);
|
||||
if (child->child_count < 2 || child->ref_count > 1
|
||||
|| child->symbol != symbol)
|
||||
break ;
|
||||
grandchild = (ts_subtree_children(child)[0]);
|
||||
if (grandchild->child_count < 2 || grandchild->ref_count > 1
|
||||
|| grandchild->symbol != symbol)
|
||||
break ;
|
||||
ts_subtree_children(tree)[0] = (grandchild);
|
||||
ts_subtree_children(child)[0] = ts_subtree_children(grandchild)[\
|
||||
grandchild->child_count - 1];
|
||||
ts_subtree_children(grandchild)[grandchild->child_count - 1] = (child);
|
||||
vec_subtree_push(stack, tree);
|
||||
tree = grandchild;
|
||||
i++;
|
||||
}
|
||||
while (stack->len > initial_stack_size)
|
||||
{
|
||||
vec_subtree_pop(stack, &tree);
|
||||
child = (ts_subtree_children(tree)[0]);
|
||||
grandchild = (ts_subtree_children(child)[child->child_count - 1]);
|
||||
ts_subtree_summarize_children(grandchild, language);
|
||||
ts_subtree_summarize_children(child, language);
|
||||
child[0] = (ts_subtree_children(tree)[0]);
|
||||
child[1] = (ts_subtree_children(child[0])[child[0]->child_count - 1]);
|
||||
ts_subtree_summarize_children(child[1], language);
|
||||
ts_subtree_summarize_children(child[0], language);
|
||||
ts_subtree_summarize_children(tree, language);
|
||||
}
|
||||
}
|
||||
|
||||
void ts_subtree_balance(t_subtree self, const t_language *language)
|
||||
void _subtree_balance_repush(t_vec_subtree *tree_stack, t_subtree tree)
|
||||
{
|
||||
t_u32 i;
|
||||
t_subtree child;
|
||||
|
||||
i = 0;
|
||||
while (i < tree->child_count)
|
||||
{
|
||||
child = ts_subtree_children(tree)[i];
|
||||
if (ts_subtree_child_count(child) > 0 && child->ref_count == 1)
|
||||
vec_subtree_push(tree_stack, child);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void _subtree_balance_inner(\
|
||||
const t_language *language, t_vec_subtree *tree_stack)
|
||||
{
|
||||
t_i64 repeat_delta;
|
||||
t_subtree child1;
|
||||
t_subtree child2;
|
||||
t_subtree child;
|
||||
t_subtree child[2];
|
||||
t_subtree tree;
|
||||
t_u32 i;
|
||||
t_u32 n;
|
||||
t_vec_subtree tree_stack;
|
||||
|
||||
tree_stack = vec_subtree_new(16, NULL);
|
||||
if (ts_subtree_child_count(self) > 0 && self->ref_count == 1)
|
||||
vec_subtree_push(&tree_stack, (self));
|
||||
while (tree_stack.len > 0)
|
||||
if (vec_subtree_pop(tree_stack, &tree) && tree->repeat_depth > 0)
|
||||
{
|
||||
vec_subtree_pop(&tree_stack, &tree);
|
||||
if (tree->repeat_depth > 0)
|
||||
{
|
||||
child1 = ts_subtree_children(tree)[0];
|
||||
child2 = ts_subtree_children(tree)[tree->child_count - 1];
|
||||
repeat_delta = (t_i64)ts_subtree_repeat_depth(child1)
|
||||
- (t_i64)ts_subtree_repeat_depth(child2);
|
||||
child[0] = ts_subtree_children(tree)[0];
|
||||
child[1] = ts_subtree_children(tree)[tree->child_count - 1];
|
||||
repeat_delta = (t_i64)ts_subtree_repeat_depth(child[0])
|
||||
- (t_i64)ts_subtree_repeat_depth(child[1]);
|
||||
if (repeat_delta > 0)
|
||||
{
|
||||
n = (t_u32)repeat_delta;
|
||||
i = n / 2;
|
||||
while (i > 0)
|
||||
{
|
||||
ts_subtree__compress(tree, i, language, &tree_stack);
|
||||
ts_subtree__compress(tree, i, language, tree_stack);
|
||||
n -= i;
|
||||
i /= 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
i = 0;
|
||||
while (i < tree->child_count)
|
||||
{
|
||||
child = ts_subtree_children(tree)[i];
|
||||
if (ts_subtree_child_count(child) > 0 && child->ref_count == 1)
|
||||
vec_subtree_push(&tree_stack, (child));
|
||||
i++;
|
||||
}
|
||||
}
|
||||
_subtree_balance_repush(tree_stack, tree);
|
||||
}
|
||||
|
||||
void ts_subtree_balance(t_subtree self, const t_language *language)
|
||||
{
|
||||
t_vec_subtree tree_stack;
|
||||
|
||||
tree_stack = vec_subtree_new(16, NULL);
|
||||
if (ts_subtree_child_count(self) > 0 && self->ref_count == 1)
|
||||
vec_subtree_push(&tree_stack, self);
|
||||
while (tree_stack.len > 0)
|
||||
_subtree_balance_inner(language, &tree_stack);
|
||||
vec_subtree_free(tree_stack);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,39 +1,33 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* subtree_funcs1.c :+: :+: :+: */
|
||||
/* subtree_funcs.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/02 20:36:10 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/09/02 20:39:57 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/09/14 14:06:16 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/vec/vec_subtree.h"
|
||||
#include "parser/subtree.h"
|
||||
|
||||
void ts_subtree_release(t_subtree self)
|
||||
void _subtree_release_inner(t_vec_subtree *to_free)
|
||||
{
|
||||
t_vec_subtree to_free;
|
||||
t_subtree tree;
|
||||
t_subtree *children;
|
||||
t_usize i;
|
||||
t_subtree *children;
|
||||
t_subtree tree;
|
||||
|
||||
to_free = vec_subtree_new(16, NULL);
|
||||
if (--self->ref_count == 0)
|
||||
vec_subtree_push(&to_free, self);
|
||||
while (to_free.len > 0)
|
||||
{
|
||||
vec_subtree_pop(&to_free, &tree);
|
||||
vec_subtree_pop(to_free, &tree);
|
||||
if (tree->child_count > 0)
|
||||
{
|
||||
i = 0;
|
||||
children = ts_subtree_children(tree);
|
||||
i = 0;
|
||||
while (i < tree->child_count)
|
||||
{
|
||||
if (--(children[i])->ref_count == 0)
|
||||
vec_subtree_push(&to_free, children[i]);
|
||||
vec_subtree_push(to_free, children[i]);
|
||||
i++;
|
||||
}
|
||||
mem_free(children);
|
||||
|
|
@ -44,7 +38,17 @@ void ts_subtree_release(t_subtree self)
|
|||
ts_external_scanner_state_delete(&tree->external_scanner_state);
|
||||
mem_free(tree);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ts_subtree_release(t_subtree self)
|
||||
{
|
||||
t_vec_subtree to_free;
|
||||
|
||||
to_free = vec_subtree_new(16, NULL);
|
||||
if (--self->ref_count == 0)
|
||||
vec_subtree_push(&to_free, self);
|
||||
while (to_free.len > 0)
|
||||
_subtree_release_inner(&to_free);
|
||||
vec_subtree_free(to_free);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/02 21:33:35 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/09/03 14:15:44 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/09/14 14:14:56 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -16,7 +16,7 @@
|
|||
// Create a new parent node with the given children.
|
||||
//
|
||||
// This takes ownership of the children array.
|
||||
t_subtree ts_subtree_new_node(TSSymbol symbol, t_vec_subtree *children,
|
||||
t_subtree ts_subtree_new_node(TSSymbol symbol, t_vec_subtree *children, \
|
||||
t_u32 production_id, const TSLanguage *language)
|
||||
{
|
||||
TSSymbolMetadata metadata;
|
||||
|
|
@ -62,46 +62,46 @@ t_subtree ts_subtree_new_missing_leaf(TSSymbol symbol, Length padding,
|
|||
{
|
||||
t_subtree result;
|
||||
|
||||
result = ts_subtree_new_leaf(symbol, padding, length_zero(),
|
||||
lookahead_bytes, 0, false, false, false, language);
|
||||
result = ts_subtree_new_leaf((t_st_newleaf_args){\
|
||||
symbol, padding, length_zero(), lookahead_bytes, 0, \
|
||||
false, false, false, language});
|
||||
result->is_missing = true;
|
||||
return (result);
|
||||
}
|
||||
|
||||
t_subtree ts_subtree_new_leaf(TSSymbol symbol, Length padding, Length size,
|
||||
t_u32 lookahead_bytes, TSStateId parse_state, bool has_external_tokens,
|
||||
bool depends_on_column, bool is_keyword, const TSLanguage *language)
|
||||
t_subtree ts_subtree_new_leaf(t_st_newleaf_args args)
|
||||
{
|
||||
TSSymbolMetadata metadata;
|
||||
bool extra;
|
||||
t_subtree_data *data;
|
||||
|
||||
extra = symbol == ts_builtin_sym_end;
|
||||
metadata = ts_language_symbol_metadata(language, symbol);
|
||||
extra = args.symbol == ts_builtin_sym_end;
|
||||
metadata = ts_language_symbol_metadata(args.language, args.symbol);
|
||||
data = mem_alloc(sizeof(*data));
|
||||
*data = (t_subtree_data){.ref_count = 1, .padding = padding, .size = size,
|
||||
.lookahead_bytes = lookahead_bytes, .error_cost = 0, .child_count = 0,
|
||||
.symbol = symbol, .parse_state = parse_state,
|
||||
*data = (t_subtree_data){.ref_count = 1, .padding = args.padding,
|
||||
.size = args.size, .lookahead_bytes = args.lookahead_bytes,
|
||||
.error_cost = 0, .child_count = 0,
|
||||
.symbol = args.symbol, .parse_state = args.parse_state,
|
||||
.visible = metadata.visible, .named = metadata.named, .extra = extra,
|
||||
.fragile_left = false, .fragile_right = false, .has_changes = false,
|
||||
.has_external_tokens = has_external_tokens,
|
||||
.has_external_tokens = args.has_external_tokens,
|
||||
.has_external_scanner_state_change = false,
|
||||
.depends_on_column = depends_on_column, .is_missing = false,
|
||||
.is_keyword = is_keyword, {{.first_leaf = {.symbol = 0,
|
||||
.depends_on_column = args.depends_on_column, .is_missing = false,
|
||||
.is_keyword = args.is_keyword, {{.first_leaf = {.symbol = 0,
|
||||
.parse_state = 0}}}};
|
||||
return ((t_subtree)data);
|
||||
}
|
||||
|
||||
t_subtree ts_subtree_new_error(t_i32 lookahead_char, Length padding,
|
||||
Length size, t_u32 bytes_scanned, TSStateId parse_state,
|
||||
const TSLanguage *language)
|
||||
t_subtree ts_subtree_new_error(t_st_newerr_args args)
|
||||
{
|
||||
t_subtree result;
|
||||
|
||||
result = ts_subtree_new_leaf(ts_builtin_sym_error, padding, size,
|
||||
bytes_scanned, parse_state, false, false, false, language);
|
||||
result = ts_subtree_new_leaf(\
|
||||
(t_st_newleaf_args){ts_builtin_sym_error, args.padding, \
|
||||
args.size, args.bytes_scanned, \
|
||||
args.parse_state, false, false, false, args.language});
|
||||
result->fragile_left = true;
|
||||
result->fragile_right = true;
|
||||
result->lookahead_char = lookahead_char;
|
||||
result->lookahead_char = args.lookahead_char;
|
||||
return (result);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,119 +6,27 @@
|
|||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/02 21:35:24 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/09/02 21:44:43 by maiboyer ### ########.fr */
|
||||
/* Updated: 2024/09/14 14:31:14 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parser/language.h"
|
||||
#include "parser/subtree.h"
|
||||
|
||||
// Assign all of the node's properties that depend on its children.
|
||||
void ts_subtree_summarize_children(t_subtree self,
|
||||
const TSLanguage *language)
|
||||
void _summarize_loop_inner1(t_subtree self, \
|
||||
const TSLanguage *language, struct s_summarize_state *s);
|
||||
void _summarize_loop_inner2(t_subtree self, \
|
||||
const TSLanguage *language, struct s_summarize_state *s);
|
||||
void _summarize_loop_inner3(t_subtree self, \
|
||||
const TSLanguage *language, struct s_summarize_state *s);
|
||||
void _summarize_loop_inner4(t_subtree self, \
|
||||
const TSLanguage *language, struct s_summarize_state *s);
|
||||
|
||||
void _sumarize_end(t_subtree self, t_subtree *children)
|
||||
{
|
||||
t_u32 structural_index;
|
||||
const TSSymbol *alias_sequence;
|
||||
t_u32 lookahead_end_byte;
|
||||
t_subtree *children;
|
||||
t_subtree child;
|
||||
t_u32 child_lookahead_end_byte;
|
||||
t_u32 grandchild_count;
|
||||
t_subtree first_child;
|
||||
t_subtree last_child;
|
||||
t_usize i;
|
||||
|
||||
alias_sequence = ts_language_alias_sequence(language, self->production_id);
|
||||
self->named_child_count = 0;
|
||||
self->visible_child_count = 0;
|
||||
self->error_cost = 0;
|
||||
self->repeat_depth = 0;
|
||||
self->visible_descendant_count = 0;
|
||||
self->has_external_tokens = false;
|
||||
self->depends_on_column = false;
|
||||
self->has_external_scanner_state_change = false;
|
||||
self->dynamic_precedence = 0;
|
||||
structural_index = 0;
|
||||
lookahead_end_byte = 0;
|
||||
children = ts_subtree_children(self);
|
||||
i = 0;
|
||||
while (i < self->child_count)
|
||||
{
|
||||
child = children[i];
|
||||
if (self->size.extent.row == 0 && ts_subtree_depends_on_column(child))
|
||||
self->depends_on_column = true;
|
||||
if (ts_subtree_has_external_scanner_state_change(child))
|
||||
self->has_external_scanner_state_change = true;
|
||||
if (i == 0)
|
||||
{
|
||||
self->padding = ts_subtree_padding(child);
|
||||
self->size = ts_subtree_size(child);
|
||||
}
|
||||
else
|
||||
self->size = length_add(self->size, ts_subtree_total_size(child));
|
||||
child_lookahead_end_byte = self->padding.bytes + self->size.bytes
|
||||
+ ts_subtree_lookahead_bytes(child);
|
||||
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->error_cost += ts_subtree_error_cost(child);
|
||||
grandchild_count = ts_subtree_child_count(child);
|
||||
if (self->symbol == ts_builtin_sym_error
|
||||
|| self->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->error_cost += ERROR_COST_PER_SKIPPED_TREE;
|
||||
else if (grandchild_count > 0)
|
||||
self->error_cost += ERROR_COST_PER_SKIPPED_TREE
|
||||
* child->visible_child_count;
|
||||
}
|
||||
}
|
||||
self->dynamic_precedence += ts_subtree_dynamic_precedence(child);
|
||||
self->visible_descendant_count
|
||||
+= ts_subtree_visible_descendant_count(child);
|
||||
if (alias_sequence && alias_sequence[structural_index] != 0
|
||||
&& !ts_subtree_extra(child))
|
||||
{
|
||||
self->visible_descendant_count++;
|
||||
self->visible_child_count++;
|
||||
if (ts_language_symbol_metadata(language,
|
||||
alias_sequence[structural_index]).named)
|
||||
self->named_child_count++;
|
||||
}
|
||||
else if (ts_subtree_visible(child))
|
||||
{
|
||||
self->visible_descendant_count++;
|
||||
self->visible_child_count++;
|
||||
if (ts_subtree_named(child))
|
||||
self->named_child_count++;
|
||||
}
|
||||
else if (grandchild_count > 0)
|
||||
{
|
||||
self->visible_child_count += child->visible_child_count;
|
||||
self->named_child_count += child->named_child_count;
|
||||
}
|
||||
if (ts_subtree_has_external_tokens(child))
|
||||
self->has_external_tokens = true;
|
||||
if (ts_subtree_is_error(child))
|
||||
{
|
||||
self->fragile_left = true;
|
||||
self->fragile_right = true;
|
||||
self->parse_state = TS_TREE_STATE_NONE;
|
||||
}
|
||||
if (!ts_subtree_extra(child))
|
||||
structural_index++;
|
||||
i++;
|
||||
}
|
||||
self->lookahead_bytes = lookahead_end_byte - self->size.bytes
|
||||
- self->padding.bytes;
|
||||
if (self->symbol == ts_builtin_sym_error
|
||||
|| self->symbol == ts_builtin_sym_error_repeat)
|
||||
self->error_cost += ERROR_COST_PER_RECOVERY
|
||||
+ ERROR_COST_PER_SKIPPED_CHAR * self->size.bytes
|
||||
+ ERROR_COST_PER_SKIPPED_LINE * self->size.extent.row;
|
||||
if (self->child_count > 0)
|
||||
{
|
||||
first_child = children[0];
|
||||
|
|
@ -140,3 +48,51 @@ void ts_subtree_summarize_children(t_subtree self,
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct s_summarize_state _init_sumnarize_state(t_subtree self,
|
||||
const TSLanguage *language)
|
||||
{
|
||||
struct s_summarize_state s;
|
||||
|
||||
s = (typeof(s)){};
|
||||
self->named_child_count = 0;
|
||||
self->visible_child_count = 0;
|
||||
self->error_cost = 0;
|
||||
self->repeat_depth = 0;
|
||||
self->visible_descendant_count = 0;
|
||||
self->has_external_tokens = false;
|
||||
self->depends_on_column = false;
|
||||
self->has_external_scanner_state_change = false;
|
||||
self->dynamic_precedence = 0;
|
||||
s.alias_sequence = \
|
||||
ts_language_alias_sequence(language, self->production_id);
|
||||
s.structural_index = 0;
|
||||
s.lookahead_end_byte = 0;
|
||||
s.children = ts_subtree_children(self);
|
||||
s.i = 0;
|
||||
return (s);
|
||||
}
|
||||
|
||||
// Assign all of the node's properties that depend on its children.
|
||||
void ts_subtree_summarize_children(t_subtree self,
|
||||
const TSLanguage *language)
|
||||
{
|
||||
struct s_summarize_state s;
|
||||
|
||||
s = _init_sumnarize_state(self, language);
|
||||
while (s.i < self->child_count)
|
||||
{
|
||||
_summarize_loop_inner1(self, language, &s);
|
||||
_summarize_loop_inner2(self, language, &s);
|
||||
_summarize_loop_inner3(self, language, &s);
|
||||
_summarize_loop_inner4(self, language, &s);
|
||||
}
|
||||
self->lookahead_bytes = s.lookahead_end_byte - self->size.bytes
|
||||
- self->padding.bytes;
|
||||
if (self->symbol == ts_builtin_sym_error
|
||||
|| self->symbol == ts_builtin_sym_error_repeat)
|
||||
self->error_cost += ERROR_COST_PER_RECOVERY
|
||||
+ ERROR_COST_PER_SKIPPED_CHAR * self->size.bytes
|
||||
+ ERROR_COST_PER_SKIPPED_LINE * self->size.extent.row;
|
||||
_sumarize_end(self, s.children);
|
||||
}
|
||||
|
|
|
|||
105
parser/src/subtree/subtree_summarize_loop.c
Normal file
105
parser/src/subtree/subtree_summarize_loop.c
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* subtree_summarize_loop.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/14 14:30:15 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/09/14 14:33:33 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "parser/language.h"
|
||||
#include "parser/subtree.h"
|
||||
|
||||
void _summarize_loop_inner1(t_subtree self,
|
||||
const TSLanguage *language, struct s_summarize_state *s)
|
||||
{
|
||||
(void)(language);
|
||||
s->child = s->children[s->i];
|
||||
if (self->size.extent.row == 0 && ts_subtree_depends_on_column(s->child))
|
||||
self->depends_on_column = true;
|
||||
if (ts_subtree_has_external_scanner_state_change(s->child))
|
||||
self->has_external_scanner_state_change = true;
|
||||
if (s->i == 0)
|
||||
{
|
||||
self->padding = ts_subtree_padding(s->child);
|
||||
self->size = ts_subtree_size(s->child);
|
||||
}
|
||||
else
|
||||
self->size = length_add(self->size, ts_subtree_total_size(s->child));
|
||||
s->child_lookahead_end_byte = self->padding.bytes + self->size.bytes
|
||||
+ ts_subtree_lookahead_bytes(s->child);
|
||||
if (s->child_lookahead_end_byte > s->lookahead_end_byte)
|
||||
s->lookahead_end_byte = s->child_lookahead_end_byte;
|
||||
if (ts_subtree_symbol(s->child) != ts_builtin_sym_error_repeat)
|
||||
self->error_cost += ts_subtree_error_cost(s->child);
|
||||
s->grandchild_count = ts_subtree_child_count(s->child);
|
||||
}
|
||||
|
||||
void _summarize_loop_inner2(t_subtree self,
|
||||
const TSLanguage *language, struct s_summarize_state *s)
|
||||
{
|
||||
(void)(language);
|
||||
if (self->symbol == ts_builtin_sym_error
|
||||
|| self->symbol == ts_builtin_sym_error_repeat)
|
||||
{
|
||||
if (!ts_subtree_extra(s->child) && !(ts_subtree_is_error(s->child)
|
||||
&& s->grandchild_count == 0))
|
||||
{
|
||||
if (ts_subtree_visible(s->child))
|
||||
self->error_cost += ERROR_COST_PER_SKIPPED_TREE;
|
||||
else if (s->grandchild_count > 0)
|
||||
self->error_cost += ERROR_COST_PER_SKIPPED_TREE
|
||||
* s->child->visible_child_count;
|
||||
}
|
||||
}
|
||||
self->dynamic_precedence += ts_subtree_dynamic_precedence(s->child);
|
||||
self->visible_descendant_count
|
||||
+= ts_subtree_visible_descendant_count(s->child);
|
||||
}
|
||||
|
||||
void _summarize_loop_inner3(t_subtree self,
|
||||
const TSLanguage *language, struct s_summarize_state *s)
|
||||
{
|
||||
(void)(language);
|
||||
if (s->alias_sequence && s->alias_sequence[s->structural_index] != 0
|
||||
&& !ts_subtree_extra(s->child))
|
||||
{
|
||||
self->visible_descendant_count++;
|
||||
self->visible_child_count++;
|
||||
if (ts_language_symbol_metadata(language,
|
||||
s->alias_sequence[s->structural_index]).named)
|
||||
self->named_child_count++;
|
||||
}
|
||||
else if (ts_subtree_visible(s->child))
|
||||
{
|
||||
self->visible_descendant_count++;
|
||||
self->visible_child_count++;
|
||||
if (ts_subtree_named(s->child))
|
||||
self->named_child_count++;
|
||||
}
|
||||
else if (s->grandchild_count > 0)
|
||||
{
|
||||
self->visible_child_count += s->child->visible_child_count;
|
||||
self->named_child_count += s->child->named_child_count;
|
||||
}
|
||||
}
|
||||
|
||||
void _summarize_loop_inner4(t_subtree self,
|
||||
const TSLanguage *language, struct s_summarize_state *s)
|
||||
{
|
||||
(void)(language);
|
||||
if (ts_subtree_has_external_tokens(s->child))
|
||||
self->has_external_tokens = true;
|
||||
if (ts_subtree_is_error(s->child))
|
||||
{
|
||||
self->fragile_left = true;
|
||||
self->fragile_right = true;
|
||||
self->parse_state = TS_TREE_STATE_NONE;
|
||||
}
|
||||
if (!ts_subtree_extra(s->child))
|
||||
s->structural_index++;
|
||||
s->i++;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue