Created: test main for the parser
This commit is contained in:
parent
c28d673db7
commit
3bde20ba23
10 changed files with 236 additions and 37 deletions
87
shcat_c/sources/node/node.c
Normal file
87
shcat_c/sources/node/node.c
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* node.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/28 18:36:40 by maiboyer #+# #+# */
|
||||
/* Updated: 2024/04/28 18:53:21 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "app/node.h"
|
||||
#include "me/mem/mem_alloc.h"
|
||||
#include "me/mem/mem_alloc_array.h"
|
||||
#include "me/string/str_l_copy.h"
|
||||
#include "parser/api.h"
|
||||
|
||||
t_node build_node(TSNode curr, t_const_str input);
|
||||
|
||||
t_node *build_childs(TSNode parent, t_const_str input, t_usize count)
|
||||
{
|
||||
t_node *ret;
|
||||
t_usize idx;
|
||||
TSNode child;
|
||||
|
||||
ret = mem_alloc_array(sizeof(*ret), count);
|
||||
if (ret == NULL)
|
||||
return (NULL);
|
||||
idx = 0;
|
||||
while (idx < count)
|
||||
{
|
||||
child = ts_node_named_child(parent, idx);
|
||||
ret[idx] = build_node(child, input);
|
||||
idx++;
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
t_node build_node(TSNode curr, t_const_str input)
|
||||
{
|
||||
t_node out;
|
||||
|
||||
out.kind = ts_node_symbol(curr);
|
||||
out.kind_str = ts_node_type(curr);
|
||||
out.start = ts_node_start_byte(curr);
|
||||
out.end = ts_node_end_byte(curr);
|
||||
out.input = input;
|
||||
out.single_str = NULL;
|
||||
out.childs_count = ts_node_named_child_count(curr);
|
||||
if (out.childs_count == 0)
|
||||
out.childs = NULL;
|
||||
else
|
||||
out.childs = build_childs(curr, input, out.childs_count);
|
||||
return (out);
|
||||
}
|
||||
|
||||
t_str node_getstr(t_node *node)
|
||||
{
|
||||
t_usize start;
|
||||
t_usize end;
|
||||
t_str ret;
|
||||
|
||||
if (node->single_str == NULL)
|
||||
{
|
||||
start = node->start;
|
||||
end = node->end;
|
||||
if (start > end)
|
||||
return (NULL);
|
||||
ret = mem_alloc(end - start + 1);
|
||||
str_l_copy(ret, node->input + start, end - start + 1);
|
||||
node->single_str = ret;
|
||||
}
|
||||
return (node->single_str);
|
||||
}
|
||||
|
||||
void free_node(t_node t)
|
||||
{
|
||||
t_usize idx;
|
||||
|
||||
idx = 0;
|
||||
while (idx < t.childs_count)
|
||||
free_node(t.childs[idx++]);
|
||||
free(t.childs);
|
||||
if (t.single_str != NULL)
|
||||
free(t.single_str);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue