]> git.dkaiser.de - 42/minishell.git/commitdiff
Refactor parser
authorDominik Kaiser <dkaiser@student.42heilbronn.de>
Fri, 2 Aug 2024 13:29:00 +0000 (15:29 +0200)
committerGitHub <noreply@github.com>
Fri, 2 Aug 2024 13:29:00 +0000 (15:29 +0200)
* Fix norme for free_node.c

* Uglify parser.c

* Merge parser updates into refactor-parser

* Outsource collect_assigns and collect_redirs

* Make parse_cmd.c norme conform

* Make free_tokens() norme conform

* Make collect_assigns.c norme conform

* Some refactoring of collect_redirs

* Refactor collect_redirs

include/minishell.h
src/collect_assigns.c [new file with mode: 0644]
src/collect_redirs.c [new file with mode: 0644]
src/free_node.c
src/free_token.c
src/parse_cmd.c
src/parser.c

index d980d425fe2d4e0a31b7ddf2b2e7e7cbe002fed2..b21acbc3e60538a254a20eedfc94c60956c740a2 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/22 17:14:49 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/07/22 15:49:34 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/08/02 13:55:48 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 # include <termios.h>
 # include <unistd.h>
 
-int            init(void);
-int            init_signal_handling(void);
+int                            init(void);
+int                            init_signal_handling(void);
 
-void   repl(const char *prompt);
+void                   repl(const char *prompt);
 
-t_list *parse(t_token *tokens);
-t_node *parse_cmd(t_token *tokens);
+t_list                 *parse(t_token *tokens);
+t_node                 *parse_cmd(t_token *tokens);
+t_redirection  *collect_redirs(t_token **tokens);
+t_assign       **collect_assigns(t_token **tokens);
 
-void   print_ast(t_node *ast);
+void                   print_ast(t_node *ast);
 #endif
diff --git a/src/collect_assigns.c b/src/collect_assigns.c
new file mode 100644 (file)
index 0000000..da43503
--- /dev/null
@@ -0,0 +1,84 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   collect_assigns.c                                  :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/08/02 13:54:36 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/08/02 14:37:41 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "minishell.h"
+#include "token.h"
+
+static t_assign        *to_assign(char *str);
+static int             count_tokens(t_token *tokens);
+static int             is_quote(char c);
+
+t_assign       **collect_assigns(t_token **tokens)
+{
+       t_token         *cur;
+       t_assign        **result;
+       int                     i;
+
+       result = malloc(sizeof(t_assign *) * (count_tokens(*tokens) + 1));
+       if (result == NULL)
+               return (free_tokens(*tokens), NULL);
+       cur = *tokens;
+       i = 0;
+       while (cur != NULL && cur->type == STRING_TOKEN
+               && !is_quote(cur->content.string[0]) && ft_strchr(cur->content.string,
+                       '=') != NULL)
+       {
+               result[i++] = to_assign(cur->content.string);
+               if (cur->next != NULL)
+               {
+                       cur = cur->next;
+                       free_token(cur->previous);
+               }
+               else
+                       free_token(cur);
+       }
+       *tokens = cur;
+       result[i] = NULL;
+       return (result);
+}
+
+static t_assign        *to_assign(char *str)
+{
+       t_assign        *result;
+       char            *split_pos;
+
+       split_pos = ft_strchr(str, '=');
+       *split_pos = '\0';
+       result = malloc(sizeof(t_assign));
+       if (result == NULL)
+       {
+               return (NULL);
+       }
+       result->var = str;
+       result->value = split_pos + 1;
+       return (result);
+}
+
+static int     count_tokens(t_token *tokens)
+{
+       int     len;
+
+       len = 0;
+       while (tokens != NULL && tokens->type == STRING_TOKEN
+               && !is_quote(tokens->content.string[0])
+               && ft_strchr(tokens->content.string, '=') != NULL)
+       {
+               len++;
+               tokens = tokens->next;
+       }
+       return (len);
+}
+
+static int     is_quote(char c)
+{
+       return (c == '"' || c == '\'');
+}
diff --git a/src/collect_redirs.c b/src/collect_redirs.c
new file mode 100644 (file)
index 0000000..76b08da
--- /dev/null
@@ -0,0 +1,80 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   collect_redirs.c                                   :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/08/02 13:49:31 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/08/02 15:21:04 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "minishell.h"
+
+static void            collect_redir(t_token **tokens, t_redirection *result,
+                                       t_token *cur);
+static void            set_redir(t_redirection *redir, int type, char *specifier);
+static int             is_output_redir(int i);
+
+t_redirection  *collect_redirs(t_token **tokens)
+{
+       t_redirection   *result;
+       t_token                 *cur;
+
+       cur = *tokens;
+       result = malloc(sizeof(t_redirection) * 2);
+       if (result == NULL)
+               return (free_tokens(*tokens), NULL);
+       set_redir(&result[0], 0, NULL);
+       set_redir(&result[1], 0, NULL);
+       while (cur != NULL && cur->next != NULL)
+       {
+               if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)
+                       collect_redir(tokens, result, cur);
+               else if (cur->type == REDIR_TOKEN)
+               {
+                       dbg("TODO: Add parsing errmsg");
+                       return (free(result), NULL);
+               }
+               else
+                       cur = cur->next;
+       }
+       return (result);
+}
+
+static void    collect_redir(t_token **tokens, t_redirection *result, t_token *cur)
+{
+       set_redir(&result[is_output_redir(cur->content.redir_type)],
+               cur->content.redir_type, cur->next->content.string);
+       cur = cur->next;
+       free_token_and_connect(cur->previous);
+       if (cur->next != NULL)
+       {
+               if (cur->previous == NULL)
+                       *tokens = cur->next;
+               cur = cur->next;
+               free_token_and_connect(cur->previous);
+       }
+       else
+               free_token(cur);
+}
+
+static void    set_redir(t_redirection *redir, int type, char *specifier)
+{
+       redir->type = type;
+       redir->specifier = specifier;
+}
+
+static int     is_output_redir(int i)
+{
+       if (i & (INPUT_FILE | INPUT_LIMITER))
+               return (0);
+       else if (i & (OUTPUT_APPEND | OUTPUT_OVERRIDE))
+               return (1);
+       else
+       {
+               panic(UNREACHABLE);
+               return (-1);
+       }
+}
index f8677d516039a5379a24342b801923158e2eb38d..f387c0aab20aa571d9fdad64b1f280f76927a287 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/27 11:41:46 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/07/22 14:30:14 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/08/02 13:28:47 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -47,9 +47,11 @@ static void  free_cmd_node(t_node *node)
        }
        free(node->content.cmd.args);
        free_assigns(node->content.cmd.assigns);
-       if (node->content.cmd.redirs[0].type != 0 && node->content.cmd.redirs[0].specifier != NULL)
+       if (node->content.cmd.redirs[0].type != 0
+               && node->content.cmd.redirs[0].specifier != NULL)
                free(node->content.cmd.redirs[0].specifier);
-       if (node->content.cmd.redirs[1].type != 0 && node->content.cmd.redirs[0].specifier != NULL)
+       if (node->content.cmd.redirs[1].type != 0
+               && node->content.cmd.redirs[0].specifier != NULL)
                free(node->content.cmd.redirs[1].specifier);
 }
 
index 3c8ce1a187acdbbca1d4bd8294b001f5d07ec65b..9b035ac1c545e13cd3ea0e4fab8736a94a04980a 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/27 14:38:57 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/08/02 14:14:30 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/08/02 14:23:56 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -30,7 +30,7 @@ void  free_token_and_connect(t_token *token)
        free(token);
 }
 
-void free_tokens(t_token *tokens)
+void   free_tokens(t_token *tokens)
 {
        while (tokens->next != NULL)
        {
index 8ed9785142c2d46008a27123da467641d7282bcd..068a4c1a6ca4381f77640e9cc52e33f50b8f14c8 100644 (file)
@@ -6,18 +6,13 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/08 15:06:25 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/08/02 13:22:18 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/08/02 14:22:32 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "minishell.h"
 
-static t_redirection   *collect_redirs(t_token **tokens);
-static t_assign                        **collect_assigns(t_token **tokens);
-static char                            **collect_args(t_token **tokens);
-static t_assign                        *to_assign(char *str);
-static void                            set_redir(t_redirection *redir, int type,
-                                                       char *specifier);
+static char    **collect_args(t_token **tokens);
 
 t_node *parse_cmd(t_token *tokens)
 {
@@ -31,120 +26,6 @@ t_node      *parse_cmd(t_token *tokens)
        return (new_cmd_node(args, assigns, redirs));
 }
 
-static t_redirection   *collect_redirs(t_token **tokens)
-{
-       t_redirection   *result;
-       t_token                 *cur;
-       int                             idx;
-
-       cur = *tokens;
-       result = malloc(sizeof(t_redirection) * 2);
-       if (result == NULL)
-       {
-               // free all tokens
-               return (NULL);
-       }
-       set_redir(&result[0], 0, NULL);
-       set_redir(&result[1], 0, NULL);
-       while (cur != NULL && cur->next != NULL)
-       {
-               if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)
-               {
-                       if (cur->content.redir_type & (INPUT_FILE | INPUT_LIMITER))
-                               idx = 0;
-                       else if (cur->content.redir_type & (OUTPUT_APPEND | OUTPUT_OVERRIDE))
-                               idx = 1;
-                       set_redir(&result[idx], cur->content.redir_type,
-                               cur->next->content.string);
-                       cur = cur->next;
-                       free_token_and_connect(cur->previous);
-                       if (cur->next != NULL)
-                       {
-                               if (cur->previous == NULL)
-                                       *tokens = cur->next;
-                               cur = cur->next;
-                               free_token_and_connect(cur->previous);
-                       }
-                       else
-                       {
-                               free_token(cur);
-                       }
-               }
-               else if (cur->type == REDIR_TOKEN)
-               {
-                       // err Parsing error
-                       free(result);
-                       return (NULL);
-               }
-               else
-               {
-                       cur = cur->next;
-               }
-       }
-       return (result);
-}
-
-static t_assign        **collect_assigns(t_token **tokens)
-{
-       t_token         *cur;
-       t_assign        **result;
-       int                     i;
-
-       cur = *tokens;
-       i = 0;
-       while (cur != NULL && cur->type == STRING_TOKEN
-               && cur->content.string[0] != '"' && cur->content.string[0] != '\''
-               && ft_strchr(cur->content.string, '=') != NULL)
-       {
-               i++;
-               cur = cur->next;
-       }
-       result = malloc(sizeof(t_assign *) * (i + 1));
-       if (!result)
-       {
-               // free all tokens
-               return (NULL);
-       }
-       cur = *tokens;
-       i = 0;
-       while (cur != NULL && cur->type == STRING_TOKEN
-               && cur->content.string[0] != '"' && cur->content.string[0] != '\''
-               && ft_strchr(cur->content.string, '=') != NULL)
-       {
-               result[i] = to_assign(cur->content.string);
-               i++;
-               if (cur->next != NULL)
-               {
-                       cur = cur->next;
-                       free_token(cur->previous);
-               }
-               else
-               {
-                       free_token(cur);
-               }
-       }
-       *tokens = cur;
-       result[i] = NULL;
-       return (result);
-}
-
-static t_assign        *to_assign(char *str)
-{
-       t_assign        *result;
-       char            *split_pos;
-
-       split_pos = ft_strchr(str, '=');
-       *split_pos = '\0';
-       result = malloc(sizeof(t_assign));
-       if (result == NULL)
-       {
-               return (NULL);
-       }
-       result->var = str;
-       result->value = split_pos + 1;
-       return (result);
-}
-
 static char    **collect_args(t_token **tokens)
 {
        t_token *cur;
@@ -159,26 +40,17 @@ static char        **collect_args(t_token **tokens)
                cur = cur->next;
        }
        result = malloc(sizeof(char *) * (i + 1));
-       if (!result)
-       {
-               // free all tokens;
-               return (NULL);
-       }
+       if (result == NULL)
+               return (free_tokens(*tokens), NULL);
        cur = *tokens;
        i = 0;
        while (cur != NULL && cur->type == STRING_TOKEN)
        {
                result[i] = cur->content.string;
-               // free token
                i++;
                cur = cur->next;
+               free_token(cur->previous);
        }
        result[i] = NULL;
        return (result);
 }
-
-static void    set_redir(t_redirection *redir, int type, char *specifier)
-{
-       redir->type = type;
-       redir->specifier = specifier;
-}
index 66c67855d11af4ef28d4993d0e0ec5a9c17d86cd..f9484c9fa37c14ca314bfd303d21a0df197400f9 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/29 15:53:29 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/07/09 11:38:12 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/08/02 13:48:09 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -14,8 +14,8 @@
 
 static t_token *find_token_by_type(t_token *tokens, int type);
 t_token                        *split_at_first(t_token **tokens, int type);
-
 static t_node  *parse_statement(t_token *tokens);
+static void            free_node_wrapper(void *node);
 
 t_list *parse(t_token *tokens)
 {
@@ -38,10 +38,7 @@ t_list       *parse(t_token *tokens)
                        current = current->next;
                }
                if (current == NULL)
-               {
-                       // Free: ft_lstclear(&result, free_node);
-                       return (NULL);
-               }
+                       return (ft_lstclear(&result, free_node_wrapper), NULL);
                current_tokens = split_at_first(&tokens, NEWLINE_TOKEN);
        }
        return (result);
@@ -91,3 +88,8 @@ static t_token        *find_token_by_type(t_token *tokens, int type)
        }
        return (NULL);
 }
+
+static void    free_node_wrapper(void *node)
+{
+       free_node((t_node *)node);
+}