Normed parser/src/node.c into multiple files

This commit is contained in:
Maieul BOYER 2024-08-31 17:30:30 +00:00
parent 009be4a4b4
commit 36d9d411ba
10 changed files with 242 additions and 165 deletions

View file

@ -11,6 +11,8 @@ node/node_child_inner \
node/node_constructor \ node/node_constructor \
node/node_fields \ node/node_fields \
node/node_getter_funcs1 \ node/node_getter_funcs1 \
node/node_getter_funcs2 \
node/node_getter_funcs3 \
node/node_iterator \ node/node_iterator \
node/node_relevent \ node/node_relevent \
parser \ parser \

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 17:14:40 by maiboyer #+# #+# */ /* Created: 2024/08/31 17:14:40 by maiboyer #+# #+# */
/* Updated: 2024/08/31 17:15:06 by maiboyer ### ########.fr */ /* Updated: 2024/08/31 17:21:02 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,35 +14,46 @@
t_u32 ts_node_child_count(TSNode self) t_u32 ts_node_child_count(TSNode self)
{ {
Subtree tree = ts_node__subtree(self); Subtree tree;
tree = ts_node__subtree(self);
if (ts_subtree_child_count(tree) > 0) if (ts_subtree_child_count(tree) > 0)
return tree->visible_child_count; return (tree->visible_child_count);
else else
return 0; return (0);
} }
t_u32 ts_node_named_child_count(TSNode self) t_u32 ts_node_named_child_count(TSNode self)
{ {
Subtree tree = ts_node__subtree(self); Subtree tree;
tree = ts_node__subtree(self);
if (ts_subtree_child_count(tree) > 0) if (ts_subtree_child_count(tree) > 0)
return tree->named_child_count; return (tree->named_child_count);
else else
return 0; return (0);
} }
TSFieldId ts_node_field_id_for_child(TSNode self, t_u32 child_index) TSFieldId ts_node_field_id_for_child(TSNode self, t_u32 child_index)
{ {
TSNode result = self; TSNode result;
bool did_descend = true; bool did_descend;
TSFieldId inherited_field_id = 0; TSFieldId inherited_field_id;
TSNode child;
t_u32 index;
NodeChildIterator iterator;
t_u32 grandchild_index;
t_u32 grandchild_count;
TSFieldId field_id;
result = self;
did_descend = true;
inherited_field_id = 0;
while (did_descend) while (did_descend)
{ {
did_descend = false; did_descend = false;
index = 0;
TSNode child; iterator = ts_node_iterate_children(&result);
t_u32 index = 0;
NodeChildIterator iterator = ts_node_iterate_children(&result);
while (ts_node_child_iterator_next(&iterator, &child)) while (ts_node_child_iterator_next(&iterator, &child))
{ {
if (ts_node__is_relevant(child, true)) if (ts_node__is_relevant(child, true))
@ -50,21 +61,23 @@ TSFieldId ts_node_field_id_for_child(TSNode self, t_u32 child_index)
if (index == child_index) if (index == child_index)
{ {
if (ts_node_is_extra(child)) if (ts_node_is_extra(child))
return 0; return (0);
TSFieldId field_id = ts_node__field_id_from_language(result, iterator.structural_child_index - 1); field_id = ts_node__field_id_from_language(result,
iterator.structural_child_index - 1);
if (field_id) if (field_id)
return field_id; return (field_id);
return inherited_field_id; return (inherited_field_id);
} }
index++; index++;
} }
else else
{ {
t_u32 grandchild_index = child_index - index; grandchild_index = child_index - index;
t_u32 grandchild_count = ts_node__relevant_child_count(child, true); grandchild_count = ts_node__relevant_child_count(child, true);
if (grandchild_index < grandchild_count) if (grandchild_index < grandchild_count)
{ {
TSFieldId field_id = ts_node__field_id_from_language(result, iterator.structural_child_index - 1); field_id = ts_node__field_id_from_language(result,
iterator.structural_child_index - 1);
if (field_id) if (field_id)
inherited_field_id = field_id; inherited_field_id = field_id;
did_descend = true; did_descend = true;
@ -76,5 +89,5 @@ TSFieldId ts_node_field_id_for_child(TSNode self, t_u32 child_index)
} }
} }
} }
return 0; return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 17:16:18 by maiboyer #+# #+# */ /* Created: 2024/08/31 17:16:18 by maiboyer #+# #+# */
/* Updated: 2024/08/31 17:16:34 by maiboyer ### ########.fr */ /* Updated: 2024/08/31 17:23:14 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,33 +14,36 @@
TSNode ts_node__child(TSNode self, t_u32 child_index, bool include_anonymous) TSNode ts_node__child(TSNode self, t_u32 child_index, bool include_anonymous)
{ {
TSNode result = self; TSNode result;
bool did_descend = true; bool did_descend;
TSNode child;
t_u32 index;
NodeChildIterator iterator;
t_u32 grandchild_index;
t_u32 grandchild_count;
result = self;
did_descend = true;
while (did_descend) while (did_descend)
{ {
did_descend = false; did_descend = false;
index = 0;
TSNode child; iterator = ts_node_iterate_children(&result);
t_u32 index = 0;
NodeChildIterator iterator = ts_node_iterate_children(&result);
while (ts_node_child_iterator_next(&iterator, &child)) while (ts_node_child_iterator_next(&iterator, &child))
{ {
if (ts_node__is_relevant(child, include_anonymous)) if (ts_node__is_relevant(child, include_anonymous))
{ {
if (index == child_index) if (index == child_index)
{ return (child);
return child;
}
index++; index++;
} }
else else
{ {
t_u32 grandchild_index = child_index - index; grandchild_index = child_index - index;
t_u32 grandchild_count = ts_node__relevant_child_count(child, include_anonymous); grandchild_count = ts_node__relevant_child_count(child,
include_anonymous);
if (grandchild_index < grandchild_count) if (grandchild_index < grandchild_count)
{ {
printf("did_descend\n");
did_descend = true; did_descend = true;
result = child; result = child;
child_index = grandchild_index; child_index = grandchild_index;
@ -50,6 +53,5 @@ TSNode ts_node__child(TSNode self, t_u32 child_index, bool include_anonymous)
} }
} }
} }
return (ts_node__null());
return ts_node__null();
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 17:10:57 by maiboyer #+# #+# */ /* Created: 2024/08/31 17:10:57 by maiboyer #+# #+# */
/* Updated: 2024/08/31 17:11:24 by maiboyer ### ########.fr */ /* Updated: 2024/08/31 17:24:15 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,14 +14,16 @@
#include "parser/length.h" #include "parser/length.h"
#include "parser/subtree.h" #include "parser/subtree.h"
TSNode ts_node_new(const TSTree *tree, const Subtree *subtree, Length position, TSSymbol alias) TSNode ts_node_new(const TSTree *tree, const Subtree *subtree, \
Length position, TSSymbol alias)
{ {
return (TSNode){ return ((TSNode){
position.bytes, position.extent.row, position.extent.column, alias, subtree, tree, position.bytes, position.extent.row, position.extent.column, \
}; alias, subtree, tree,
});
} }
TSNode ts_node__null(void) TSNode ts_node__null(void)
{ {
return ts_node_new(NULL, NULL, length_zero(), 0); return (ts_node_new(NULL, NULL, length_zero(), 0));
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 17:15:23 by maiboyer #+# #+# */ /* Created: 2024/08/31 17:15:23 by maiboyer #+# #+# */
/* Updated: 2024/08/31 17:15:55 by maiboyer ### ########.fr */ /* Updated: 2024/08/31 17:25:26 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,26 +15,38 @@
#include "parser/language.h" #include "parser/language.h"
#include "parser/tree.h" #include "parser/tree.h"
t_const_str ts_node__field_name_from_language(TSNode self, t_u32 structural_child_index) t_const_str ts_node__field_name_from_language(TSNode self,
t_u32 structural_child_index)
{ {
const TSFieldMapEntry *field_map, *field_map_end; const TSFieldMapEntry *field_map;
ts_language_field_map(self.tree->language, ts_node__subtree(self)->production_id, &field_map, &field_map_end); const TSFieldMapEntry *field_map_end;
for (; field_map != field_map_end; field_map++)
ts_language_field_map(self.tree->language,
ts_node__subtree(self)->production_id, &field_map, &field_map_end);
while (field_map != field_map_end)
{ {
if (!field_map->inherited && field_map->child_index == structural_child_index) if (!field_map->inherited
return self.tree->language->field_names[field_map->field_id]; && field_map->child_index == structural_child_index)
return (self.tree->language->field_names[field_map->field_id]);
field_map++;
} }
return NULL; return (NULL);
} }
TSFieldId ts_node__field_id_from_language(TSNode self, t_u32 structural_child_index) TSFieldId ts_node__field_id_from_language(TSNode self,
t_u32 structural_child_index)
{ {
const TSFieldMapEntry *field_map, *field_map_end; const TSFieldMapEntry *field_map;
ts_language_field_map(self.tree->language, ts_node__subtree(self)->production_id, &field_map, &field_map_end); const TSFieldMapEntry *field_map_end;
for (; field_map != field_map_end; field_map++)
ts_language_field_map(self.tree->language,
ts_node__subtree(self)->production_id, &field_map, &field_map_end);
while (field_map != field_map_end)
{ {
if (!field_map->inherited && field_map->child_index == structural_child_index) if (!field_map->inherited
return field_map->field_id; && field_map->child_index == structural_child_index)
return (field_map->field_id);
field_map++;
} }
return 0; return (0);
} }

View file

@ -6,88 +6,35 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 17:04:21 by maiboyer #+# #+# */ /* Created: 2024/08/31 17:04:21 by maiboyer #+# #+# */
/* Updated: 2024/08/31 17:07:17 by maiboyer ### ########.fr */ /* Updated: 2024/08/31 17:29:45 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "parser/_inner/node.h" #include "parser/_inner/node.h"
#include "parser/tree.h"
#include "parser/language.h" #include "parser/language.h"
#include "parser/tree.h"
t_u32 ts_node_start_byte(TSNode self)
{
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.start_row, self.start_col};
}
t_u32 ts_node__alias(const TSNode *self)
{
return self->alias;
}
Subtree ts_node__subtree(TSNode self)
{
return *(const Subtree *)self.id;
}
t_u32 ts_node_end_byte(TSNode self)
{
return ts_node_start_byte(self) + ts_subtree_size(ts_node__subtree(self)).bytes;
}
TSSymbol ts_node_symbol(TSNode self)
{
TSSymbol symbol = ts_node__alias(&self);
if (!symbol)
symbol = ts_subtree_symbol(ts_node__subtree(self));
return ts_language_public_symbol(self.tree->language, symbol);
}
t_const_str ts_node_type(TSNode self)
{
TSSymbol symbol = ts_node__alias(&self);
if (!symbol)
symbol = ts_subtree_symbol(ts_node__subtree(self));
return ts_language_symbol_name(self.tree->language, symbol);
}
TSSymbol ts_node_grammar_symbol(TSNode self)
{
return ts_subtree_symbol(ts_node__subtree(self));
}
t_const_str ts_node_grammar_type(TSNode self)
{
TSSymbol symbol = ts_subtree_symbol(ts_node__subtree(self));
return ts_language_symbol_name(self.tree->language, symbol);
}
bool ts_node_is_extra(TSNode self) bool ts_node_is_extra(TSNode self)
{ {
return ts_subtree_extra(ts_node__subtree(self)); return (ts_subtree_extra(ts_node__subtree(self)));
} }
bool ts_node_is_named(TSNode self) bool ts_node_is_named(TSNode self)
{ {
TSSymbol alias = ts_node__alias(&self); TSSymbol alias;
return alias ? ts_language_symbol_metadata(self.tree->language, alias).named : ts_subtree_named(ts_node__subtree(self));
alias = ts_node__alias(&self);
if (alias)
return (ts_language_symbol_metadata(self.tree->language, alias).named);
return (ts_subtree_named(ts_node__subtree(self)));
} }
TSNode ts_node_child(TSNode self, t_u32 child_index) TSNode ts_node_child(TSNode self, t_u32 child_index)
{ {
return ts_node__child(self, child_index, true); return (ts_node__child(self, child_index, true));
} }
TSNode ts_node_named_child(TSNode self, t_u32 child_index) TSNode ts_node_named_child(TSNode self, t_u32 child_index)
{ {
return ts_node__child(self, child_index, false); return (ts_node__child(self, child_index, false));
} }

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* node_getter_funcs2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 17:29:00 by maiboyer #+# #+# */
/* Updated: 2024/08/31 17:29:25 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser/_inner/node.h"
#include "parser/language.h"
#include "parser/tree.h"
t_u32 ts_node_start_byte(TSNode self)
{
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.start_row, self.start_col});
}
t_u32 ts_node__alias(const TSNode *self)
{
return (self->alias);
}
Subtree ts_node__subtree(TSNode self)
{
return (*(const Subtree *)self.id);
}

View file

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* node_getter_funcs3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 17:29:34 by maiboyer #+# #+# */
/* Updated: 2024/08/31 17:29:48 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser/_inner/node.h"
#include "parser/language.h"
#include "parser/tree.h"
t_u32 ts_node_end_byte(TSNode self)
{
return (ts_node_start_byte(self)
+ ts_subtree_size(ts_node__subtree(self)).bytes);
}
TSSymbol ts_node_symbol(TSNode self)
{
TSSymbol symbol;
symbol = ts_node__alias(&self);
if (!symbol)
symbol = ts_subtree_symbol(ts_node__subtree(self));
return (ts_language_public_symbol(self.tree->language, symbol));
}
t_const_str ts_node_type(TSNode self)
{
TSSymbol symbol;
symbol = ts_node__alias(&self);
if (!symbol)
symbol = ts_subtree_symbol(ts_node__subtree(self));
return (ts_language_symbol_name(self.tree->language, symbol));
}
TSSymbol ts_node_grammar_symbol(TSNode self)
{
return (ts_subtree_symbol(ts_node__subtree(self)));
}
t_const_str ts_node_grammar_type(TSNode self)
{
TSSymbol symbol;
symbol = ts_subtree_symbol(ts_node__subtree(self));
return (ts_language_symbol_name(self.tree->language, symbol));
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 17:14:00 by maiboyer #+# #+# */ /* Created: 2024/08/31 17:14:00 by maiboyer #+# #+# */
/* Updated: 2024/08/31 17:14:14 by maiboyer ### ########.fr */ /* Updated: 2024/08/31 17:28:20 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,47 +16,49 @@
NodeChildIterator ts_node_iterate_children(const TSNode *node) NodeChildIterator ts_node_iterate_children(const TSNode *node)
{ {
Subtree subtree = ts_node__subtree(*node); Subtree subtree;
const TSSymbol *alias_sequence;
subtree = ts_node__subtree(*node);
if (ts_subtree_child_count(subtree) == 0) if (ts_subtree_child_count(subtree) == 0)
{ return ((NodeChildIterator){NULL, node->tree, length_zero(), 0, 0,
return (NodeChildIterator){NULL, node->tree, length_zero(), 0, 0, NULL}; NULL});
} alias_sequence = ts_language_alias_sequence(node->tree->language,
const TSSymbol *alias_sequence = ts_language_alias_sequence(node->tree->language, subtree->production_id); subtree->production_id);
return (NodeChildIterator){ return ((NodeChildIterator){
.tree = node->tree, .tree = node->tree,
.parent = subtree, .parent = subtree,
.position = {ts_node_start_byte(*node), ts_node_start_point(*node)}, .position = {ts_node_start_byte(*node), ts_node_start_point(*node)},
.child_index = 0, .child_index = 0,
.structural_child_index = 0, .structural_child_index = 0,
.alias_sequence = alias_sequence, .alias_sequence = alias_sequence,
}; });
} }
bool ts_node_child_iterator_done(NodeChildIterator *self) bool ts_node_child_iterator_done(NodeChildIterator *self)
{ {
return self->child_index == self->parent->child_count; return (self->child_index == self->parent->child_count);
} }
bool ts_node_child_iterator_next(NodeChildIterator *self, TSNode *result) bool ts_node_child_iterator_next(NodeChildIterator *self, TSNode *result)
{ {
const Subtree *child;
TSSymbol alias_symbol;
if (!self->parent || ts_node_child_iterator_done(self)) if (!self->parent || ts_node_child_iterator_done(self))
return false; return (false);
const Subtree *child = &ts_subtree_children(self->parent)[self->child_index]; alias_symbol = 0;
TSSymbol alias_symbol = 0; child = &ts_subtree_children(self->parent)[self->child_index];
if (!ts_subtree_extra(*child)) if (!ts_subtree_extra(*child))
{ {
if (self->alias_sequence) if (self->alias_sequence)
{
alias_symbol = self->alias_sequence[self->structural_child_index]; alias_symbol = self->alias_sequence[self->structural_child_index];
}
self->structural_child_index++; self->structural_child_index++;
} }
if (self->child_index > 0) if (self->child_index > 0)
{
self->position = length_add(self->position, ts_subtree_padding(*child)); self->position = length_add(self->position, ts_subtree_padding(*child));
}
*result = ts_node_new(self->tree, child, self->position, alias_symbol); *result = ts_node_new(self->tree, child, self->position, alias_symbol);
self->position = length_add(self->position, ts_subtree_size(*child)); self->position = length_add(self->position, ts_subtree_size(*child));
self->child_index++; self->child_index++;
return true; return (true);
} }

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 17:16:53 by maiboyer #+# #+# */ /* Created: 2024/08/31 17:16:53 by maiboyer #+# #+# */
/* Updated: 2024/08/31 17:17:09 by maiboyer ### ########.fr */ /* Updated: 2024/08/31 17:26:57 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,25 +18,28 @@ bool ts_node__is_relevant(TSNode self, bool include_anonymous)
{ {
TSSymbol alias; TSSymbol alias;
Subtree tree; Subtree tree;
tree = ts_node__subtree(self); tree = ts_node__subtree(self);
if (include_anonymous) if (include_anonymous)
return ts_subtree_visible(tree) || ts_node__alias(&self); return (ts_subtree_visible(tree) || ts_node__alias(&self));
alias = ts_node__alias(&self); alias = ts_node__alias(&self);
if (alias) if (alias)
return ts_language_symbol_metadata(self.tree->language, alias).named; return (ts_language_symbol_metadata(self.tree->language, alias).named);
else else
return ts_subtree_visible(tree) && ts_subtree_named(tree); return (ts_subtree_visible(tree) && ts_subtree_named(tree));
} }
t_u32 ts_node__relevant_child_count(TSNode self, bool include_anonymous) t_u32 ts_node__relevant_child_count(TSNode self, bool include_anonymous)
{ {
Subtree tree = ts_node__subtree(self); Subtree tree;
tree = ts_node__subtree(self);
if (ts_subtree_child_count(tree) > 0) if (ts_subtree_child_count(tree) > 0)
{ {
if (include_anonymous) if (include_anonymous)
return tree->visible_child_count; return (tree->visible_child_count);
else else
return tree->named_child_count; return (tree->named_child_count);
} }
return 0; return (0);
} }