minishell/parser/src/stack/stack_manipulate3.c

80 lines
2.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* stack_manipulate3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/31 17:00:07 by maiboyer #+# #+# */
/* Updated: 2024/09/19 18:26:48 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser/inner/stack_inner.h"
#include "parser/inner/stack_inner.h"
t_stack_action pop_error_callback(void *payload,
const t_stack_iterator *iterator)
{
bool *found_error;
if (iterator->subtrees.len > 0)
{
found_error = payload;
if (!*found_error
&& ts_subtree_is_error(iterator->subtrees.buffer[0]))
{
*found_error = true;
return (SActionPop | SActionStop);
}
else
return (SActionStop);
}
else
return (SActionNone);
}
t_vec_subtree ts_stack_pop_error(t_stack *self, t_stack_version version)
{
t_stack_node *n;
bool found_error;
t_stack_slice_array pop;
t_usize i;
n = array_get(&self->heads, version)->node;
i = 0;
while (i < n->link_count)
{
if (n->links[i].subtree && ts_subtree_is_error(n->links[i].subtree))
{
found_error = false;
pop = stack__iter(self, \
(struct s_stack_iter_args){version, pop_error_callback, &found_error, 1});
if (pop.size > 0)
{
ts_stack_renumber_version(\
self, pop.contents[0].version, version);
return (pop.contents[0].subtrees);
}
break ;
}
i++;
}
return ((t_vec_subtree){NULL, 0, 0, NULL});
}
t_stack_action pop_all_callback(void *payload,
const t_stack_iterator *iterator)
{
(void)payload;
if (iterator->node->link_count == 0)
return (SActionPop);
else
return (SActionNone);
}
t_stack_slice_array ts_stack_pop_all(t_stack *self, t_stack_version version)
{
return (stack__iter(self, \
(struct s_stack_iter_args){version, pop_all_callback, NULL, 0}));
}