cmdgroup parsing refactor: use builder pattern

This will make it easier to add the next logic stuff to it
This commit is contained in:
Khaïs COLIN 2025-03-20 12:17:54 +01:00
parent 73a64b2ec3
commit 185a069044
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
4 changed files with 116 additions and 7 deletions

View file

@ -30,6 +30,7 @@ srcs = \
src/executing/here_doc/strip_newline.c \ src/executing/here_doc/strip_newline.c \
src/ft_errno.c \ src/ft_errno.c \
src/get_command.c \ src/get_command.c \
src/parser/cmdgroup/cmdgroup_builder.c \
src/parser/cmdgroup/cmdgroup.c \ src/parser/cmdgroup/cmdgroup.c \
src/parser/cmdlist/cmdlist.c \ src/parser/cmdlist/cmdlist.c \
src/parser/cmdlist/cmdlist_builder.c \ src/parser/cmdlist/cmdlist_builder.c \

View file

@ -6,28 +6,37 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/11 15:18:02 by khais #+# #+# */ /* Created: 2025/03/11 15:18:02 by khais #+# #+# */
/* Updated: 2025/03/19 12:10:19 by khais ### ########.fr */ /* Updated: 2025/03/20 12:27:57 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cmdgroup.h" #include "cmdgroup.h"
#include "cmdgroup_builder.h"
#include <stdlib.h> #include <stdlib.h>
#include "libft.h" #include "libft.h"
#include "../../treedrawing.h" #include "../../treedrawing.h"
#include "../../postprocess/redirections/redirection_list.h" #include "../../postprocess/redirections/redirection_list.h"
#include "libft/libft.h" #include "libft/libft.h"
t_cmdgroup *cmdgroup_from_wordlist(t_wordlist *words) t_cmdgroup *cmdgroup_from_wordlist(t_wordlist *wordlist)
{ {
t_cmdgroup *cmd; t_cmdgroup_builder builder;
t_wordlist *words;
words = wordlist_copy(wordlist);
if (words == NULL) if (words == NULL)
return (NULL); return (NULL);
cmd = ft_calloc(1, sizeof(t_cmdgroup)); if (setup_cmdgroup_builder(&builder, &words) == NULL)
if (cmd == NULL) return (wordlist_destroy(words), NULL);
while (builder.error == false && builder.current_word != NULL)
{
cmdgroup_builder_next_word(&builder, &words);
}
if (builder.current_wordlist != NULL)
cmdgroup_builder_delimit(&builder, &words);
if (builder.error == true)
return (NULL); return (NULL);
cmd->item = cmdlist_from_wordlist(words); return (builder.cmdgroup);
return (cmd);
} }
void cmdgroup_destroy(t_cmdgroup *cmd) void cmdgroup_destroy(t_cmdgroup *cmd)

View file

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmdgroup_builder.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 12:32:14 by khais #+# #+# */
/* Updated: 2025/03/20 12:49:37 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "cmdgroup_builder.h"
#include "libft.h"
/*
** setup a cmdlist_builder by allocating additionall needed memory and
** performing other init steps.
*/
t_cmdgroup_builder *setup_cmdgroup_builder(t_cmdgroup_builder *builder,
t_wordlist **words)
{
ft_bzero(builder, sizeof (*builder));
builder->cmdgroup = ft_calloc(1, sizeof(*builder->cmdgroup));
if (builder->cmdgroup == NULL)
return (NULL);
builder->current_wordlist = NULL;
builder->current_word = wordlist_pop(words);
return (builder);
}
/*
** Advance the state to the next word
*/
void cmdgroup_builder_next_word(t_cmdgroup_builder *builder,
t_wordlist **words)
{
builder->current_wordlist = wordlist_push(builder->current_wordlist,
builder->current_word);
builder->current_word = wordlist_pop(words);
}
/*
** Delimit the cmdlist, setting up the t_cmdlist item.
*/
void cmdgroup_builder_delimit(t_cmdgroup_builder *builder,
t_wordlist **words)
{
builder->cmdgroup->item = cmdlist_from_wordlist(builder->current_wordlist);
wordlist_destroy(builder->current_wordlist);
builder->current_wordlist = NULL;
(void)words;
}

View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmdgroup_builder.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 12:27:20 by khais #+# #+# */
/* Updated: 2025/03/20 12:50:09 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CMDGROUP_BUILDER_H
# define CMDGROUP_BUILDER_H
# include "../wordlist/wordlist.h"
# include "cmdgroup.h"
typedef struct s_cmdgroup_builder
{
/*
** set to true if in an error state
*/
bool error;
/*
** current word that is being examined
*/
t_worddesc *current_word;
/*
** wordlist to be passed to lower stages of parsing
*/
t_wordlist *current_wordlist;
/*
** cmdgroup that is being constructed
*/
t_cmdgroup *cmdgroup;
} t_cmdgroup_builder;
t_cmdgroup_builder *setup_cmdgroup_builder(t_cmdgroup_builder *builder,
t_wordlist **words);
void cmdgroup_builder_next_word(t_cmdgroup_builder *builder,
t_wordlist **words);
void cmdgroup_builder_delimit(t_cmdgroup_builder *builder,
t_wordlist **words);
#endif // CMDGROUP_BUILDER_H