parsing: do not use a builder struct

since only one field was being used, just pass that field around
This commit is contained in:
Khaïs COLIN 2025-04-14 14:52:56 +02:00
parent 7425195350
commit 53693e171e
2 changed files with 62 additions and 74 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */ /* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */
/* Updated: 2025/04/14 12:57:46 by khais ### ########.fr */ /* Updated: 2025/04/14 14:52:24 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -115,21 +115,21 @@ static t_redirect *redir_from_words(t_worddesc *operator,
return (redir); return (redir);
} }
t_redirect *minishell_redir_parse(t_minishell *app, t_cmd_builder *builder) t_redirect *minishell_redir_parse(t_minishell *app, t_wordlist *tokens)
{ {
t_redirect *redir_list; t_redirect *redir_list;
t_worddesc *redir_operator; t_worddesc *redir_operator;
t_worddesc *redir_specifier; t_worddesc *redir_specifier;
t_redirect *new_redir; t_redirect *new_redir;
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); ft_printf("%s() %s\n", __FUNCTION__, tokens->word->word);
redir_list = NULL; redir_list = NULL;
while (is_redir(builder->tokens->word)) while (is_redir(tokens->word))
{ {
if (builder->tokens->next == NULL) if (tokens->next == NULL)
return (redirect_destroy(redir_list), NULL); return (redirect_destroy(redir_list), NULL);
redir_operator = wordlist_pop(&builder->tokens); redir_operator = wordlist_pop(&tokens);
redir_specifier = wordlist_pop(&builder->tokens); redir_specifier = wordlist_pop(&tokens);
new_redir = redir_from_words(redir_operator, redir_specifier, app); new_redir = redir_from_words(redir_operator, redir_specifier, app);
t_redirect_add_back(&redir_list, new_redir); t_redirect_add_back(&redir_list, new_redir);
} }
@ -139,12 +139,12 @@ t_redirect *minishell_redir_parse(t_minishell *app, t_cmd_builder *builder)
/* /*
** TODO NORM ** TODO NORM
*/ */
t_cmd *minishell_simple_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_simple_parse(t_minishell *app, t_wordlist *tokens)
{ {
t_cmd *simple; t_cmd *simple;
t_redirect *redir; t_redirect *redir;
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); ft_printf("%s() %s\n", __FUNCTION__, tokens->word->word);
simple = ft_calloc(1, sizeof(t_cmd)); simple = ft_calloc(1, sizeof(t_cmd));
if (!simple) if (!simple)
{ {
@ -158,35 +158,35 @@ t_cmd *minishell_simple_parse(t_minishell *app, t_cmd_builder *builder)
ft_errno(FT_ENOMEM); ft_errno(FT_ENOMEM);
return (free(simple), NULL); return (free(simple), NULL);
} }
redir = minishell_redir_parse(app, builder); redir = minishell_redir_parse(app, tokens);
t_redirect_add_back(&simple->value.simple->redirections, redir); t_redirect_add_back(&simple->value.simple->redirections, redir);
if (ft_errno_get() != FT_ESUCCESS) if (ft_errno_get() != FT_ESUCCESS)
return (t_cmd_destroy(simple), NULL); return (t_cmd_destroy(simple), NULL);
while (builder->tokens && builder->tokens->word->token_type == WORD_TOKEN) while (tokens && tokens->word->token_type == WORD_TOKEN)
{ {
simple->value.simple->words = wordlist_push(simple->value.simple->words, simple->value.simple->words = wordlist_push(simple->value.simple->words,
wordlist_pop(&builder->tokens)); wordlist_pop(&tokens));
if (!simple->value.simple->words) if (!simple->value.simple->words)
return (ft_errno(FT_EERRNO), t_cmd_destroy(simple), NULL); return (ft_errno(FT_EERRNO), t_cmd_destroy(simple), NULL);
redir = minishell_redir_parse(app, builder); redir = minishell_redir_parse(app, tokens);
t_redirect_add_back(&simple->value.simple->redirections, redir); t_redirect_add_back(&simple->value.simple->redirections, redir);
} }
if (!simple->value.simple->words) if (!simple->value.simple->words)
return (parse_error(app, builder->tokens->word), NULL); return (parse_error(app, tokens->word), NULL);
return (simple); return (simple);
} }
t_cmd *minishell_opt_pipeline_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_opt_pipeline_parse(t_minishell *app, t_wordlist *tokens)
{ {
t_cmd *opt; t_cmd *opt;
t_worddesc *token; t_worddesc *token;
token = builder->tokens->word; token = tokens->word;
if (token->token_type == PIPE_TOKEN) if (token->token_type == PIPE_TOKEN)
{ {
token = wordlist_pop(&builder->tokens); token = wordlist_pop(&tokens);
worddesc_destroy(token); worddesc_destroy(token);
opt = minishell_group_or_smp_parse(app, builder); opt = minishell_group_or_smp_parse(app, tokens);
if (!opt) if (!opt)
ft_errno(FT_EERRNO); ft_errno(FT_EERRNO);
return (opt); return (opt);
@ -197,58 +197,58 @@ t_cmd *minishell_opt_pipeline_parse(t_minishell *app, t_cmd_builder *builder)
/* /*
** TODO NORM -> function is too long ** TODO NORM -> function is too long
*/ */
t_cmd *minishell_group_or_smp_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_group_or_smp_parse(t_minishell *app, t_wordlist *tokens)
{ {
t_cmd *subtree; t_cmd *subtree;
t_cmd *group; t_cmd *group;
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); ft_printf("%s() %s\n", __FUNCTION__, tokens->word->word);
if (builder->tokens->word->token_type == OPEN_PARENTH_TOKEN) if (tokens->word->token_type == OPEN_PARENTH_TOKEN)
{ {
worddesc_destroy(wordlist_pop(&builder->tokens)); worddesc_destroy(wordlist_pop(&tokens));
subtree = minishell_cmds_parse(app, builder); subtree = minishell_cmds_parse(app, tokens);
if (!subtree if (!subtree
|| builder->tokens->word->token_type != CLOSE_PARENTH_TOKEN) || tokens->word->token_type != CLOSE_PARENTH_TOKEN)
{ {
ft_errno(FT_EERRNO); ft_errno(FT_EERRNO);
if (builder->tokens->word->token_type != CLOSE_PARENTH_TOKEN) if (tokens->word->token_type != CLOSE_PARENTH_TOKEN)
parse_error(app, builder->tokens->word); parse_error(app, tokens->word);
return (t_cmd_destroy(subtree), NULL); return (t_cmd_destroy(subtree), NULL);
} }
worddesc_destroy(wordlist_pop(&builder->tokens)); worddesc_destroy(wordlist_pop(&tokens));
group = ft_calloc(1, sizeof(t_cmd)); group = ft_calloc(1, sizeof(t_cmd));
if (!group) if (!group)
return (ft_errno(FT_ENOMEM), t_cmd_destroy(subtree), NULL); return (ft_errno(FT_ENOMEM), t_cmd_destroy(subtree), NULL);
group->value.group->cmd = subtree; group->value.group->cmd = subtree;
group->value.group->redirects = minishell_redir_parse(app, builder); group->value.group->redirects = minishell_redir_parse(app, tokens);
if (group->value.group->redirects == NULL if (group->value.group->redirects == NULL
&& ft_errno_get() != FT_ESUCCESS) && ft_errno_get() != FT_ESUCCESS)
return (t_cmd_destroy(group), NULL); return (t_cmd_destroy(group), NULL);
return (group); return (group);
} }
subtree = minishell_simple_parse(app, builder); subtree = minishell_simple_parse(app, tokens);
if (!subtree) if (!subtree)
ft_errno(FT_EERRNO); ft_errno(FT_EERRNO);
return (subtree); return (subtree);
} }
t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_cmd_builder *builder, t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_wordlist *tokens,
t_connector *connec) t_connector *connec)
{ {
t_worddesc *token; t_worddesc *token;
t_cmd *opt; t_cmd *opt;
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); ft_printf("%s() %s\n", __FUNCTION__, tokens->word->word);
token = builder->tokens->word; token = tokens->word;
if (token->token_type == OR_TOKEN || token->token_type == AND_TOKEN) if (token->token_type == OR_TOKEN || token->token_type == AND_TOKEN)
{ {
if (token->token_type == OR_TOKEN) if (token->token_type == OR_TOKEN)
*connec = FT_OR; *connec = FT_OR;
else else
*connec = FT_AND; *connec = FT_AND;
token = wordlist_pop(&builder->tokens); token = wordlist_pop(&tokens);
worddesc_destroy(token); worddesc_destroy(token);
opt = minishell_pipeline_parse(app, builder); opt = minishell_pipeline_parse(app, tokens);
if (!opt) if (!opt)
ft_errno(FT_EERRNO); ft_errno(FT_EERRNO);
return (opt); return (opt);
@ -283,17 +283,17 @@ static t_cmd *connec_reorient_subtree(t_cmd **list, t_cmd **subtree,
return (*subtree); return (*subtree);
} }
t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_pipeline_parse(t_minishell *app, t_wordlist *tokens)
{ {
t_cmd *subtree; t_cmd *subtree;
t_cmd *opt; t_cmd *opt;
t_cmd *pipeline; t_cmd *pipeline;
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); ft_printf("%s() %s\n", __FUNCTION__, tokens->word->word);
subtree = minishell_group_or_smp_parse(app, builder); subtree = minishell_group_or_smp_parse(app, tokens);
if (!subtree) if (!subtree)
return (NULL); return (NULL);
opt = minishell_opt_pipeline_parse(app, builder); opt = minishell_opt_pipeline_parse(app, tokens);
if (!opt && ft_errno_get() != FT_ESUCCESS) if (!opt && ft_errno_get() != FT_ESUCCESS)
return (NULL); return (NULL);
if (!opt) if (!opt)
@ -305,7 +305,7 @@ t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder)
app->last_return_value = 1; app->last_return_value = 1;
return (ft_perror("minishell_pipeline_parse"), NULL); return (ft_perror("minishell_pipeline_parse"), NULL);
} }
opt = minishell_opt_pipeline_parse(app, builder); opt = minishell_opt_pipeline_parse(app, tokens);
} }
if (ft_errno_get() != FT_ESUCCESS) if (ft_errno_get() != FT_ESUCCESS)
return (t_cmd_destroy(subtree), NULL); return (t_cmd_destroy(subtree), NULL);
@ -316,18 +316,18 @@ t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder)
** Parse list of commands or pipeline. ** Parse list of commands or pipeline.
*/ */
// TODO remove debugs // TODO remove debugs
t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_cmds_parse(t_minishell *app, t_wordlist *tokens)
{ {
t_cmd *subtree; t_cmd *subtree;
t_cmd *opt; t_cmd *opt;
t_cmd *list; t_cmd *list;
t_connector connec; t_connector connec;
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word); ft_printf("%s() %s\n", __FUNCTION__, tokens->word->word);
subtree = minishell_pipeline_parse(app, builder); subtree = minishell_pipeline_parse(app, tokens);
if (!subtree) if (!subtree)
return (NULL); return (NULL);
opt = minishell_opt_cmds_parse(app, builder, &connec); opt = minishell_opt_cmds_parse(app, tokens, &connec);
if (!opt && ft_errno_get() != FT_ESUCCESS) if (!opt && ft_errno_get() != FT_ESUCCESS)
return (t_cmd_destroy(subtree), NULL); return (t_cmd_destroy(subtree), NULL);
if (!opt) if (!opt)
@ -339,7 +339,7 @@ t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder)
app->last_return_value = 1; app->last_return_value = 1;
return (ft_perror("minishell_cmds_parse"), NULL); return (ft_perror("minishell_cmds_parse"), NULL);
} }
opt = minishell_opt_cmds_parse(app, builder, &connec); opt = minishell_opt_cmds_parse(app, tokens, &connec);
} }
if (ft_errno_get() != FT_ESUCCESS) if (ft_errno_get() != FT_ESUCCESS)
return (t_cmd_destroy(subtree), NULL); return (t_cmd_destroy(subtree), NULL);
@ -351,27 +351,27 @@ t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder)
*/ */
t_cmd *minishell_parse(t_minishell *app, char *command_line) t_cmd *minishell_parse(t_minishell *app, char *command_line)
{ {
t_cmd_builder builder; t_cmd *root_cmd;
t_cmd *root_cmd; t_wordlist *tokens;
ft_errno(FT_ESUCCESS); ft_errno(FT_ESUCCESS);
app->last_return_value = 0; app->last_return_value = 0;
if (!command_line) if (!command_line)
return (NULL); return (NULL);
builder.tokens = minishell_wordsplit(command_line); tokens = minishell_wordsplit(command_line);
if (!builder.tokens) if (!tokens)
return (NULL); return (NULL);
root_cmd = minishell_cmds_parse(app, &builder); root_cmd = minishell_cmds_parse(app, tokens);
if (!root_cmd) if (!root_cmd)
{ {
if (ft_errno_get() != FT_ESUCCESS && !app->last_return_value) if (ft_errno_get() != FT_ESUCCESS && !app->last_return_value)
app->last_return_value = 1; app->last_return_value = 1;
return (wordlist_destroy(builder.tokens), NULL); return (wordlist_destroy(tokens), NULL);
} }
if (builder.tokens) if (tokens)
{ {
parse_error(app, builder.tokens->word); parse_error(app, tokens->word);
wordlist_destroy(builder.tokens); wordlist_destroy(tokens);
t_cmd_destroy(root_cmd); t_cmd_destroy(root_cmd);
return (NULL); return (NULL);
} }

View file

@ -5,8 +5,8 @@
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 12:53/43 by khais #+# #+# */ /* Created: 2025/04/14 14:46/44 by khais #+# #+# */
/* Updated: 2025/04/14 12:53:43 by khais ### ########.fr */ /* Updated: 2025/04/14 14:46:44 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,31 +17,19 @@
# include "../ft_errno.h" # include "../ft_errno.h"
# include <stdbool.h> # include <stdbool.h>
typedef struct s_cmd_builder
{
t_wordlist *tokens;
t_wordlist *current_wordlist;
t_redirect *current_redirection_list;
t_worddesc *current_word;
/*
** last_here_doc is meant to store only one entry.
*/
t_redirectee *last_here_doc;
} t_cmd_builder;
void parse_error(t_minishell *app, t_worddesc *token); void parse_error(t_minishell *app, t_worddesc *token);
t_redirect *t_redirect_add_back(t_redirect **init, t_redirect *back); t_redirect *t_redirect_add_back(t_redirect **init, t_redirect *back);
t_cmd *minishell_parse(t_minishell *app, char *command_line); t_cmd *minishell_parse(t_minishell *app, char *command_line);
t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder); t_cmd *minishell_cmds_parse(t_minishell *app, t_wordlist *tokens);
t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder); t_cmd *minishell_pipeline_parse(t_minishell *app, t_wordlist *tokens);
t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_cmd_builder *builder, t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_wordlist *tokens,
t_connector *connec); t_connector *connec);
t_cmd *minishell_group_or_smp_parse(t_minishell *app, t_cmd *minishell_group_or_smp_parse(t_minishell *app,
t_cmd_builder *builder); t_wordlist *tokens);
t_cmd *minishell_opt_pipeline_parse(t_minishell *app, t_cmd *minishell_opt_pipeline_parse(t_minishell *app,
t_cmd_builder *builder); t_wordlist *tokens);
t_cmd *minishell_simple_parse(t_minishell *app, t_cmd_builder *builder); t_cmd *minishell_simple_parse(t_minishell *app, t_wordlist *tokens);
t_redirect *minishell_redir_parse(t_minishell *app, t_cmd_builder *builder); t_redirect *minishell_redir_parse(t_minishell *app, t_wordlist *tokens);
#endif #endif