Updated to remove more usless stuff

This commit is contained in:
Maix0 2024-08-22 16:41:48 +02:00
parent 5b7eb9784b
commit db0903c3a8
31 changed files with 480 additions and 1058 deletions

View file

@ -1,9 +1,9 @@
#include "me/types.h"
#include "parser/api.h"
#include "parser/language.h"
#include "parser/point.h"
#include "parser/subtree.h"
#include "parser/tree.h"
#include "parser/point.h"
#include "me/types.h"
typedef struct NodeChildIterator
{
@ -20,9 +20,7 @@ typedef struct NodeChildIterator
TSNode ts_node_new(const TSTree *tree, const Subtree *subtree, Length position, TSSymbol alias)
{
return (TSNode){
{position.bytes, position.extent.row, position.extent.column, alias},
subtree,
tree,
position.bytes, position.extent.row, position.extent.column, alias, subtree, tree,
};
}
@ -35,17 +33,22 @@ static inline TSNode ts_node__null(void)
t_u32 ts_node_start_byte(TSNode self)
{
return self.context[0];
return self.start_byte;
}
const TSLanguage *ts_node_language(TSNode self)
{
return self.tree->language;
}
TSPoint ts_node_start_point(TSNode self)
{
return (TSPoint){self.context[1], self.context[2]};
return (TSPoint){self.start_row, self.start_col};
}
static inline t_u32 ts_node__alias(const TSNode *self)
{
return self->context[3];
return self->alias;
}
static inline Subtree ts_node__subtree(TSNode self)
@ -62,7 +65,7 @@ static inline NodeChildIterator ts_node_iterate_children(const TSNode *node)
{
return (NodeChildIterator){NULL_SUBTREE, node->tree, length_zero(), 0, 0, NULL};
}
const TSSymbol *alias_sequence = ts_language_alias_sequence(node->tree->language, subtree.ptr->production_id);
const TSSymbol *alias_sequence = ts_language_alias_sequence(node->tree->language, subtree->production_id);
return (NodeChildIterator){
.tree = node->tree,
.parent = subtree,
@ -75,12 +78,12 @@ static inline NodeChildIterator ts_node_iterate_children(const TSNode *node)
static inline bool ts_node_child_iterator_done(NodeChildIterator *self)
{
return self->child_index == self->parent.ptr->child_count;
return self->child_index == self->parent->child_count;
}
static inline bool ts_node_child_iterator_next(NodeChildIterator *self, TSNode *result)
{
if (!self->parent.ptr || ts_node_child_iterator_done(self))
if (!self->parent || ts_node_child_iterator_done(self))
return false;
const Subtree *child = &ts_subtree_children(self->parent)[self->child_index];
TSSymbol alias_symbol = 0;
@ -132,11 +135,11 @@ static inline t_u32 ts_node__relevant_child_count(TSNode self, bool include_anon
{
if (include_anonymous)
{
return tree.ptr->visible_child_count;
return tree->visible_child_count;
}
else
{
return tree.ptr->named_child_count;
return tree->named_child_count;
}
}
else
@ -173,6 +176,7 @@ static inline TSNode ts_node__child(TSNode self, t_u32 child_index, bool include
t_u32 grandchild_count = ts_node__relevant_child_count(child, include_anonymous);
if (grandchild_index < grandchild_count)
{
printf("did_descend\n");
did_descend = true;
result = child;
child_index = grandchild_index;
@ -186,271 +190,6 @@ static inline TSNode ts_node__child(TSNode self, t_u32 child_index, bool include
return ts_node__null();
}
/* static inline TSNode ts_node__prev_sibling(TSNode self, bool include_anonymous) */
/* { */
/* Subtree self_subtree = ts_node__subtree(self); */
/* bool self_is_empty = ts_subtree_total_bytes(self_subtree) == 0; */
/* t_u32 target_end_byte = ts_node_end_byte(self); */
/**/
/* TSNode node = ts_node_parent(self); */
/* TSNode earlier_node = ts_node__null(); */
/* bool earlier_node_is_relevant = false; */
/**/
/* while (!ts_node_is_null(node)) */
/* { */
/* TSNode earlier_child = ts_node__null(); */
/* bool earlier_child_is_relevant = false; */
/* bool found_child_containing_target = false; */
/**/
/* TSNode child; */
/* NodeChildIterator iterator = ts_node_iterate_children(&node); */
/* while (ts_node_child_iterator_next(&iterator, &child)) */
/* { */
/* if (child.id == self.id) */
/* break; */
/* if (iterator.position.bytes > target_end_byte) */
/* { */
/* found_child_containing_target = true; */
/* break; */
/* } */
/**/
/* if (iterator.position.bytes == target_end_byte && */
/* (!self_is_empty || ts_subtree_has_trailing_empty_descendant(ts_node__subtree(child), self_subtree))) */
/* { */
/* found_child_containing_target = true; */
/* break; */
/* } */
/**/
/* if (ts_node__is_relevant(child, include_anonymous)) */
/* { */
/* earlier_child = child; */
/* earlier_child_is_relevant = true; */
/* } */
/* else if (ts_node__relevant_child_count(child, include_anonymous) > 0) */
/* { */
/* earlier_child = child; */
/* earlier_child_is_relevant = false; */
/* } */
/* } */
/**/
/* if (found_child_containing_target) */
/* { */
/* if (!ts_node_is_null(earlier_child)) */
/* { */
/* earlier_node = earlier_child; */
/* earlier_node_is_relevant = earlier_child_is_relevant; */
/* } */
/* node = child; */
/* } */
/* else if (earlier_child_is_relevant) */
/* { */
/* return earlier_child; */
/* } */
/* else if (!ts_node_is_null(earlier_child)) */
/* { */
/* node = earlier_child; */
/* } */
/* else if (earlier_node_is_relevant) */
/* { */
/* return earlier_node; */
/* } */
/* else */
/* { */
/* node = earlier_node; */
/* earlier_node = ts_node__null(); */
/* earlier_node_is_relevant = false; */
/* } */
/* } */
/**/
/* return ts_node__null(); */
/* } */
/**/
/* static inline TSNode ts_node__next_sibling(TSNode self, bool include_anonymous) */
/* { */
/* t_u32 target_end_byte = ts_node_end_byte(self); */
/**/
/* TSNode node = ts_node_parent(self); */
/* TSNode later_node = ts_node__null(); */
/* bool later_node_is_relevant = false; */
/**/
/* while (!ts_node_is_null(node)) */
/* { */
/* TSNode later_child = ts_node__null(); */
/* bool later_child_is_relevant = false; */
/* TSNode child_containing_target = ts_node__null(); */
/**/
/* TSNode child; */
/* NodeChildIterator iterator = ts_node_iterate_children(&node); */
/* while (ts_node_child_iterator_next(&iterator, &child)) */
/* { */
/* if (iterator.position.bytes < target_end_byte) */
/* continue; */
/* if (ts_node_start_byte(child) <= ts_node_start_byte(self)) */
/* { */
/* if (ts_node__subtree(child).ptr != ts_node__subtree(self).ptr) */
/* { */
/* child_containing_target = child; */
/* } */
/* } */
/* else if (ts_node__is_relevant(child, include_anonymous)) */
/* { */
/* later_child = child; */
/* later_child_is_relevant = true; */
/* break; */
/* } */
/* else if (ts_node__relevant_child_count(child, include_anonymous) > 0) */
/* { */
/* later_child = child; */
/* later_child_is_relevant = false; */
/* break; */
/* } */
/* } */
/**/
/* if (!ts_node_is_null(child_containing_target)) */
/* { */
/* if (!ts_node_is_null(later_child)) */
/* { */
/* later_node = later_child; */
/* later_node_is_relevant = later_child_is_relevant; */
/* } */
/* node = child_containing_target; */
/* } */
/* else if (later_child_is_relevant) */
/* { */
/* return later_child; */
/* } */
/* else if (!ts_node_is_null(later_child)) */
/* { */
/* node = later_child; */
/* } */
/* else if (later_node_is_relevant) */
/* { */
/* return later_node; */
/* } */
/* else */
/* { */
/* node = later_node; */
/* } */
/* } */
/**/
/* return ts_node__null(); */
/* } */
/**/
/* static inline TSNode ts_node__first_child_for_byte(TSNode self, t_u32 goal, bool include_anonymous) */
/* { */
/* TSNode node = self; */
/* bool did_descend = true; */
/**/
/* while (did_descend) */
/* { */
/* did_descend = false; */
/**/
/* TSNode child; */
/* NodeChildIterator iterator = ts_node_iterate_children(&node); */
/* while (ts_node_child_iterator_next(&iterator, &child)) */
/* { */
/* if (ts_node_end_byte(child) > goal) */
/* { */
/* if (ts_node__is_relevant(child, include_anonymous)) */
/* { */
/* return child; */
/* } */
/* else if (ts_node_child_count(child) > 0) */
/* { */
/* did_descend = true; */
/* node = child; */
/* break; */
/* } */
/* } */
/* } */
/* } */
/**/
/* return ts_node__null(); */
/* } */
/**/
/* static inline TSNode ts_node__descendant_for_byte_range(TSNode self, t_u32 range_start, t_u32 range_end, bool include_anonymous) */
/* { */
/* TSNode node = self; */
/* TSNode last_visible_node = self; */
/**/
/* bool did_descend = true; */
/* while (did_descend) */
/* { */
/* did_descend = false; */
/**/
/* TSNode child; */
/* NodeChildIterator iterator = ts_node_iterate_children(&node); */
/* while (ts_node_child_iterator_next(&iterator, &child)) */
/* { */
/* t_u32 node_end = iterator.position.bytes; */
/**/
/* // The end of this node must extend far enough forward to touch */
/* // the end of the range and exceed the start of the range. */
/* if (node_end < range_end) */
/* continue; */
/* if (node_end <= range_start) */
/* continue; */
/**/
/* // The start of this node must extend far enough backward to */
/* // touch the start of the range. */
/* if (range_start < ts_node_start_byte(child)) */
/* break; */
/**/
/* node = child; */
/* if (ts_node__is_relevant(node, include_anonymous)) */
/* { */
/* last_visible_node = node; */
/* } */
/* did_descend = true; */
/* break; */
/* } */
/* } */
/**/
/* return last_visible_node; */
/* } */
/**/
/* static inline TSNode ts_node__descendant_for_point_range(TSNode self, TSPoint range_start, TSPoint range_end, bool include_anonymous) */
/* { */
/* TSNode node = self; */
/* TSNode last_visible_node = self; */
/**/
/* bool did_descend = true; */
/* while (did_descend) */
/* { */
/* did_descend = false; */
/**/
/* TSNode child; */
/* NodeChildIterator iterator = ts_node_iterate_children(&node); */
/* while (ts_node_child_iterator_next(&iterator, &child)) */
/* { */
/* TSPoint node_end = iterator.position.extent; */
/**/
/* // The end of this node must extend far enough forward to touch */
/* // the end of the range and exceed the start of the range. */
/* if (point_lt(node_end, range_end)) */
/* continue; */
/* if (point_lte(node_end, range_start)) */
/* continue; */
/**/
/* // The start of this node must extend far enough backward to */
/* // touch the start of the range. */
/* if (point_lt(range_start, ts_node_start_point(child))) */
/* break; */
/**/
/* node = child; */
/* if (ts_node__is_relevant(node, include_anonymous)) */
/* { */
/* last_visible_node = node; */
/* } */
/* did_descend = true; */
/* break; */
/* } */
/* } */
/**/
/* return last_visible_node; */
/* } */
// TSNode - public
t_u32 ts_node_end_byte(TSNode self)
@ -458,11 +197,6 @@ t_u32 ts_node_end_byte(TSNode self)
return ts_node_start_byte(self) + ts_subtree_size(ts_node__subtree(self)).bytes;
}
TSPoint ts_node_end_point(TSNode self)
{
return point_add(ts_node_start_point(self), ts_subtree_size(ts_node__subtree(self)).extent);
}
TSSymbol ts_node_symbol(TSNode self)
{
TSSymbol symbol = ts_node__alias(&self);
@ -479,11 +213,6 @@ t_const_str ts_node_type(TSNode self)
return ts_language_symbol_name(self.tree->language, symbol);
}
const TSLanguage *ts_node_language(TSNode self)
{
return self.tree->language;
}
TSSymbol ts_node_grammar_symbol(TSNode self)
{
return ts_subtree_symbol(ts_node__subtree(self));
@ -495,23 +224,6 @@ t_const_str ts_node_grammar_type(TSNode self)
return ts_language_symbol_name(self.tree->language, symbol);
}
char *ts_node_string(TSNode self)
{
TSSymbol alias_symbol = ts_node__alias(&self);
return ts_subtree_string(ts_node__subtree(self), alias_symbol, ts_language_symbol_metadata(self.tree->language, alias_symbol).visible,
self.tree->language, false);
}
bool ts_node_eq(TSNode self, TSNode other)
{
return self.tree == other.tree && self.id == other.id;
}
bool ts_node_is_null(TSNode self)
{
return self.id == 0;
}
bool ts_node_is_extra(TSNode self)
{
return ts_subtree_extra(ts_node__subtree(self));
@ -523,86 +235,6 @@ bool ts_node_is_named(TSNode self)
return alias ? ts_language_symbol_metadata(self.tree->language, alias).named : ts_subtree_named(ts_node__subtree(self));
}
bool ts_node_is_missing(TSNode self)
{
return ts_subtree_missing(ts_node__subtree(self));
}
bool ts_node_has_changes(TSNode self)
{
return ts_subtree_has_changes(ts_node__subtree(self));
}
bool ts_node_has_error(TSNode self)
{
return ts_subtree_error_cost(ts_node__subtree(self)) > 0;
}
bool ts_node_is_error(TSNode self)
{
TSSymbol symbol = ts_node_symbol(self);
return symbol == ts_builtin_sym_error;
}
t_u32 ts_node_descendant_count(TSNode self)
{
return ts_subtree_visible_descendant_count(ts_node__subtree(self)) + 1;
}
TSStateId ts_node_parse_state(TSNode self)
{
return ts_subtree_parse_state(ts_node__subtree(self));
}
TSStateId ts_node_next_parse_state(TSNode self)
{
const TSLanguage *language = self.tree->language;
t_u16 state = ts_node_parse_state(self);
if (state == TS_TREE_STATE_NONE)
{
return TS_TREE_STATE_NONE;
}
t_u16 symbol = ts_node_grammar_symbol(self);
return ts_language_next_state(language, state, symbol);
}
TSNode ts_node_parent(TSNode self)
{
TSNode node = ts_tree_root_node(self.tree);
if (node.id == self.id)
return ts_node__null();
while (true)
{
TSNode next_node = ts_node_child_containing_descendant(node, self);
if (ts_node_is_null(next_node))
break;
node = next_node;
}
return node;
}
TSNode ts_node_child_containing_descendant(TSNode self, TSNode subnode)
{
t_u32 start_byte = ts_node_start_byte(subnode);
t_u32 end_byte = ts_node_end_byte(subnode);
do
{
NodeChildIterator iter = ts_node_iterate_children(&self);
do
{
if (!ts_node_child_iterator_next(&iter, &self) || ts_node_start_byte(self) > start_byte || self.id == subnode.id)
{
return ts_node__null();
}
} while (iter.position.bytes < end_byte || ts_node_child_count(self) == 0);
} while (!ts_node__is_relevant(self, true));
return self;
}
TSNode ts_node_child(TSNode self, t_u32 child_index)
{
return ts_node__child(self, child_index, true);
@ -613,96 +245,10 @@ TSNode ts_node_named_child(TSNode self, t_u32 child_index)
return ts_node__child(self, child_index, false);
}
TSNode ts_node_child_by_field_id(TSNode self, TSFieldId field_id)
{
recur:
if (!field_id || ts_node_child_count(self) == 0)
return ts_node__null();
const TSFieldMapEntry *field_map, *field_map_end;
ts_language_field_map(self.tree->language, ts_node__subtree(self).ptr->production_id, &field_map, &field_map_end);
if (field_map == field_map_end)
return ts_node__null();
// The field mappings are sorted by their field id. Scan all
// the mappings to find the ones for the given field id.
while (field_map->field_id < field_id)
{
field_map++;
if (field_map == field_map_end)
return ts_node__null();
}
while (field_map_end[-1].field_id > field_id)
{
field_map_end--;
if (field_map == field_map_end)
return ts_node__null();
}
TSNode child;
NodeChildIterator iterator = ts_node_iterate_children(&self);
while (ts_node_child_iterator_next(&iterator, &child))
{
if (!ts_subtree_extra(ts_node__subtree(child)))
{
t_u32 index = iterator.structural_child_index - 1;
if (index < field_map->child_index)
continue;
// Hidden nodes' fields are "inherited" by their visible parent.
if (field_map->inherited)
{
// If this is the *last* possible child node for this field,
// then perform a tail call to avoid recursion.
if (field_map + 1 == field_map_end)
{
self = child;
goto recur;
}
// Otherwise, descend into this child, but if it doesn't contain
// the field, continue searching subsequent children.
else
{
TSNode result = ts_node_child_by_field_id(child, field_id);
if (result.id)
return result;
field_map++;
if (field_map == field_map_end)
return ts_node__null();
}
}
else if (ts_node__is_relevant(child, true))
{
return child;
}
// If the field refers to a hidden node with visible children,
// return the first visible child.
else if (ts_node_child_count(child) > 0)
{
return ts_node_child(child, 0);
}
// Otherwise, continue searching subsequent children.
else
{
field_map++;
if (field_map == field_map_end)
return ts_node__null();
}
}
}
return ts_node__null();
}
static inline t_const_str ts_node__field_name_from_language(TSNode self, t_u32 structural_child_index)
{
const TSFieldMapEntry *field_map, *field_map_end;
ts_language_field_map(self.tree->language, ts_node__subtree(self).ptr->production_id, &field_map, &field_map_end);
ts_language_field_map(self.tree->language, ts_node__subtree(self)->production_id, &field_map, &field_map_end);
for (; field_map != field_map_end; field_map++)
{
if (!field_map->inherited && field_map->child_index == structural_child_index)
@ -713,11 +259,45 @@ static inline t_const_str ts_node__field_name_from_language(TSNode self, t_u32 s
return NULL;
}
t_const_str ts_node_field_name_for_child(TSNode self, t_u32 child_index)
static inline TSFieldId ts_node__field_id_from_language(TSNode self, t_u32 structural_child_index)
{
TSNode result = self;
bool did_descend = true;
t_const_str inherited_field_name = NULL;
const TSFieldMapEntry *field_map, *field_map_end;
ts_language_field_map(self.tree->language, ts_node__subtree(self)->production_id, &field_map, &field_map_end);
for (; field_map != field_map_end; field_map++)
{
if (!field_map->inherited && field_map->child_index == structural_child_index)
return field_map->field_id;
}
return 0;
}
t_u32 ts_node_child_count(TSNode self)
{
Subtree tree = ts_node__subtree(self);
if (ts_subtree_child_count(tree) > 0)
{
return tree->visible_child_count;
}
else
{
return 0;
}
}
t_u32 ts_node_named_child_count(TSNode self)
{
Subtree tree = ts_node__subtree(self);
if (ts_subtree_child_count(tree) > 0)
return tree->named_child_count;
else
return 0;
}
TSFieldId ts_node_field_id_for_child(TSNode self, t_u32 child_index)
{
TSNode result = self;
bool did_descend = true;
TSFieldId inherited_field_id = 0;
while (did_descend)
{
@ -733,13 +313,11 @@ t_const_str ts_node_field_name_for_child(TSNode self, t_u32 child_index)
if (index == child_index)
{
if (ts_node_is_extra(child))
{
return NULL;
}
t_const_str field_name = ts_node__field_name_from_language(result, iterator.structural_child_index - 1);
if (field_name)
return field_name;
return inherited_field_name;
return 0;
TSFieldId field_id = ts_node__field_id_from_language(result, iterator.structural_child_index - 1);
if (field_id)
return field_id;
return inherited_field_id;
}
index++;
}
@ -749,10 +327,9 @@ t_const_str ts_node_field_name_for_child(TSNode self, t_u32 child_index)
t_u32 grandchild_count = ts_node__relevant_child_count(child, true);
if (grandchild_index < grandchild_count)
{
t_const_str field_name = ts_node__field_name_from_language(result, iterator.structural_child_index - 1);
if (field_name)
inherited_field_name = field_name;
TSFieldId field_id = ts_node__field_id_from_language(result, iterator.structural_child_index - 1);
if (field_id)
inherited_field_id = field_id;
did_descend = true;
result = child;
child_index = grandchild_index;
@ -762,46 +339,5 @@ t_const_str ts_node_field_name_for_child(TSNode self, t_u32 child_index)
}
}
}
return NULL;
}
TSNode ts_node_child_by_field_name(TSNode self, t_const_str name, t_u32 name_length)
{
TSFieldId field_id = ts_language_field_id_for_name(self.tree->language, name, name_length);
return ts_node_child_by_field_id(self, field_id);
}
t_u32 ts_node_child_count(TSNode self)
{
Subtree tree = ts_node__subtree(self);
if (ts_subtree_child_count(tree) > 0)
{
return tree.ptr->visible_child_count;
}
else
{
return 0;
}
}
t_u32 ts_node_named_child_count(TSNode self)
{
Subtree tree = ts_node__subtree(self);
if (ts_subtree_child_count(tree) > 0)
{
return tree.ptr->named_child_count;
}
else
{
return 0;
}
}
TSSymbol ts_node_field_id_for_child(TSNode self, t_u32 child_index)
{
t_const_str name = ts_node_field_name_for_child(self, child_index);
if (name == NULL)
return (0);
return (ts_language_field_id_for_name(ts_node_language(self), name, strlen(name)));
return 0;
}