From 64d5500c8d5c0efaa7e3f4e621d20490400d4025 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Thu, 16 Mar 2017 22:30:02 +0100 Subject: [PATCH] main good --- 42sh/Makefile | 3 + 42sh/includes/lexer.h | 4 +- 42sh/includes/minishell.h | 29 +++--- 42sh/includes/parser.h | 6 +- 42sh/includes/types.h | 6 +- 42sh/libft | 2 +- 42sh/src/builtin/builtin_math.c | 2 +- 42sh/src/lexer/get_state_global.c | 2 +- 42sh/src/lexer/get_state_redir.c | 2 +- 42sh/src/lexer/lexer_default.c | 4 +- 42sh/src/lexer/lexer_destroy.c | 22 +++++ 42sh/src/lexer/lexer_init.c | 2 +- 42sh/src/lexer/lexer_lex.c | 2 +- 42sh/src/lexer/lexer_sep.c | 4 +- 42sh/src/lexer/token_print.c | 3 +- 42sh/src/line-editing/readline.c | 2 +- 42sh/src/main/data_init.c | 7 +- 42sh/src/main/instruction_free.c | 4 +- 42sh/src/main/main.c | 152 ++++++++++++++++-------------- 42sh/src/parser/add_number.c | 2 +- 42sh/src/parser/add_redir.c | 6 +- 42sh/src/parser/error_syntax.c | 13 +-- 42sh/src/parser/eval_sym.c | 4 +- 42sh/src/parser/ft_parse.c | 7 +- 42sh/src/parser/heredoc_parser.c | 6 +- 42sh/src/parser/parser_destroy.c | 22 +++++ 42sh/src/parser/parser_init.c | 7 +- 42sh/src/parser/produce_sym.c | 8 +- 42sh/src/parser/stack_init.c | 23 +++++ 29 files changed, 221 insertions(+), 135 deletions(-) create mode 100644 42sh/src/lexer/lexer_destroy.c create mode 100644 42sh/src/parser/parser_destroy.c create mode 100644 42sh/src/parser/stack_init.c diff --git a/42sh/Makefile b/42sh/Makefile index f32921cf..a584d1a7 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -220,6 +220,7 @@ lexer/lexer_bquote.c\ lexer/lexer_curly_braces.c\ lexer/lexer_default.c\ lexer/lexer_delim.c\ +lexer/lexer_destroy.c\ lexer/lexer_dquote.c\ lexer/lexer_end.c\ lexer/lexer_great.c\ @@ -287,11 +288,13 @@ parser/error_syntax.c\ parser/eval_sym.c\ parser/ft_parse.c\ parser/heredoc_parser.c\ +parser/parser_destroy.c\ parser/parser_init.c\ parser/pop_stack.c\ parser/produce_sym.c\ parser/push_stack.c\ parser/read_stack.c\ +parser/stack_init.c\ parser/tree_wrapper.c SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 618939db..285a5329 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/03/08 23:20:20 by ariard ### ########.fr */ +/* Updated: 2017/03/16 20:37:26 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -101,7 +101,7 @@ int get_reserved_words(t_list **alst); int insert_newline(t_list **alst); void lexer_init(t_lexer *lexer); - +void lexer_destroy(t_lexer *lexer); int lexer_lex(t_list **alst, t_lexer *lexer); int lexer_default(t_list **alst, t_lexer *lexer); int lexer_newline(t_list **alst, t_lexer *lexer); diff --git a/42sh/includes/minishell.h b/42sh/includes/minishell.h index 011445df..262ed34e 100644 --- a/42sh/includes/minishell.h +++ b/42sh/includes/minishell.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ -/* Updated: 2017/03/16 15:41:33 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 22:18:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,18 +34,21 @@ struct s_data { - t_flag opts; - int fd; - char **env; - char **local_var; - int argc; - char **argv; - t_line line; - t_comp *comp; - t_exec exec; - t_jobc jobc; - t_list *heredoc_queue; - t_list *lst_func; + t_flag opts; + int fd; + char **env; + int argc; + char **argv; + t_line line; + t_list *token; + t_btree *ast; + t_lexer lexer; + t_parser parser; + t_comp *comp; + t_exec exec; + t_jobc jobc; + char **local_var; + t_list *lst_func; }; int shell_init(int ac, char **av); diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index bb370204..df6149d7 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -30,6 +30,7 @@ struct s_parser t_parstate state; t_list *stack; t_sym *new_sym; + t_list *heredoc_queue; }; struct s_aggrematch @@ -64,6 +65,9 @@ struct s_errormatch /* extern t_stackmatch g_stackmatch[]; */ void parser_init(t_parser *parser); +void parser_destroy(t_parser *parser); +int stack_init(t_parser *parser); + int ft_parse(t_btree **ast, t_list **token, t_parser *parser); int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst); @@ -75,7 +79,7 @@ int pop_stack(t_list **stack, t_sym erase_sym); int pop_heredoc(t_list **lst); -int error_syntax(t_list **token, t_parser *parser, t_btree **ast); +int error_syntax(t_list **token); int error_eof(void); int ft_read_stack(t_sym *stack); diff --git a/42sh/includes/types.h b/42sh/includes/types.h index 9098de75..aad1f77e 100644 --- a/42sh/includes/types.h +++ b/42sh/includes/types.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ -/* Updated: 2017/03/14 22:53:17 by ariard ### ########.fr */ +/* Updated: 2017/03/16 18:17:16 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,8 @@ typedef struct s_line t_line; typedef struct s_comp t_comp; typedef struct s_exec t_exec; typedef struct s_jobc t_jobc; +typedef struct s_lexer t_lexer; +typedef struct s_parser t_parser; typedef enum e_mode t_mode; typedef struct s_data_template t_btexport; @@ -43,7 +45,6 @@ typedef struct s_data_template t_btexport; ** Lexer types */ -typedef struct s_lexer t_lexer; typedef enum e_lexstate t_lexstate; typedef struct s_token t_token; typedef struct s_rvwords t_rvwords; @@ -77,7 +78,6 @@ typedef struct s_redir t_redir; typedef struct s_cmd t_cmd; typedef union u_astdata t_astdata; typedef union u_word t_word; -typedef struct s_parser t_parser; typedef int t_sym; typedef enum e_parstate t_parstate; typedef struct s_aggrematch t_aggrematch; diff --git a/42sh/libft b/42sh/libft index 04b8d5cd..89d3f6e6 160000 --- a/42sh/libft +++ b/42sh/libft @@ -1 +1 @@ -Subproject commit 04b8d5cd090e86b0f9d8160b01e49d43666deddf +Subproject commit 89d3f6e67e707e42fca42cc89e3f39e5f840908a diff --git a/42sh/src/builtin/builtin_math.c b/42sh/src/builtin/builtin_math.c index c37771c6..a2ec3343 100644 --- a/42sh/src/builtin/builtin_math.c +++ b/42sh/src/builtin/builtin_math.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 10:58:49 by ariard #+# #+# */ -/* Updated: 2017/03/16 14:24:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 17:44:15 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/get_state_global.c b/42sh/src/lexer/get_state_global.c index 305e03a9..aca394e5 100644 --- a/42sh/src/lexer/get_state_global.c +++ b/42sh/src/lexer/get_state_global.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:39:06 by jhalford #+# #+# */ -/* Updated: 2017/03/13 19:03:43 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 19:00:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/get_state_redir.c b/42sh/src/lexer/get_state_redir.c index 5ac62b6c..997dde9a 100644 --- a/42sh/src/lexer/get_state_redir.c +++ b/42sh/src/lexer/get_state_redir.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:37:28 by jhalford #+# #+# */ -/* Updated: 2017/03/10 13:12:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 19:00:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_default.c b/42sh/src/lexer/lexer_default.c index a2802c5c..2ab8a22d 100644 --- a/42sh/src/lexer/lexer_default.c +++ b/42sh/src/lexer/lexer_default.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 18:36:21 by jhalford #+# #+# */ -/* Updated: 2017/03/05 16:20:18 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 19:05:25 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ int lexer_default(t_list **alst, t_lexer *lexer) t_token *token; char c; - c = lexer->str[lexer->pos]; + c = lexer->str[lexer->pos]; if ((lexer->state = get_state_global(lexer))) return (lexer_lex(alst, lexer)); if ((lexer->state = get_state_redir(lexer))) diff --git a/42sh/src/lexer/lexer_destroy.c b/42sh/src/lexer/lexer_destroy.c new file mode 100644 index 00000000..73596cd0 --- /dev/null +++ b/42sh/src/lexer/lexer_destroy.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* lexer_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/16 20:25:08 by ariard #+# #+# */ +/* Updated: 2017/03/16 20:43:06 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void lexer_destroy(t_lexer *lexer) +{ + ft_strdel(&lexer->str); + lexer->pos = 0; + lexer->state = 0; + lexer->stack = NULL; + lexer->heredoc_stack = NULL; +} diff --git a/42sh/src/lexer/lexer_init.c b/42sh/src/lexer/lexer_init.c index a7000895..261bf644 100644 --- a/42sh/src/lexer/lexer_init.c +++ b/42sh/src/lexer/lexer_init.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/21 16:14:08 by ariard #+# #+# */ -/* Updated: 2017/03/04 16:32:15 by ariard ### ########.fr */ +/* Updated: 2017/03/16 17:57:47 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_lex.c b/42sh/src/lexer/lexer_lex.c index 39ab2483..ff86f0e6 100644 --- a/42sh/src/lexer/lexer_lex.c +++ b/42sh/src/lexer/lexer_lex.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:08:51 by jhalford #+# #+# */ -/* Updated: 2017/03/16 14:45:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 19:04:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_sep.c b/42sh/src/lexer/lexer_sep.c index 76c6b7e8..b3f4fcf9 100644 --- a/42sh/src/lexer/lexer_sep.c +++ b/42sh/src/lexer/lexer_sep.c @@ -6,11 +6,11 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 16:29:57 by jhalford #+# #+# */ -/* Updated: 2017/03/09 18:48:00 by ariard ### ########.fr */ +/* Updated: 2017/03/16 18:21:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ -#include "lexer.h" +#include "minishell.h" int lexer_sep(t_list **alst, t_lexer *lexer) { diff --git a/42sh/src/lexer/token_print.c b/42sh/src/lexer/token_print.c index c17e1605..a5269ccb 100644 --- a/42sh/src/lexer/token_print.c +++ b/42sh/src/lexer/token_print.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */ -/* Updated: 2017/03/08 12:14:32 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 19:10:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ void token_print(t_list *lst) while (lst) { + DG("in token print"); if (lst->content) token = lst->content; if (token->type) diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index 68aaef15..eb80b9e8 100644 --- a/42sh/src/line-editing/readline.c +++ b/42sh/src/line-editing/readline.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/03/16 15:22:11 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 18:40:39 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 11e9f92e..0a66550a 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2017/03/15 16:23:18 by wescande ### ########.fr */ +/* Updated: 2017/03/16 22:15:29 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,10 @@ int data_init(int ac, char **av) data->opts = 0; exec_reset(); data->lst_func = NULL; - data->heredoc_queue = NULL; + data->token = NULL; + data->ast = NULL; + lexer_init(&data->lexer); + parser_init(&data->parser); if ((term_name = ft_getenv(data->env, "TERM")) == NULL) return (-1); if (tgetent(NULL, term_name) != 1) diff --git a/42sh/src/main/instruction_free.c b/42sh/src/main/instruction_free.c index 4d4103e9..4b644120 100644 --- a/42sh/src/main/instruction_free.c +++ b/42sh/src/main/instruction_free.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/15 15:54:59 by gwojda #+# #+# */ -/* Updated: 2017/03/16 14:13:33 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 18:27:57 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ int instruction_free(t_list **token, t_btree **ast, t_lexer *lexer, t_parser *pa token = NULL; ft_lstdel(&parser->stack, &ft_lst_cfree); btree_del(ast, &ft_lst_cfree); - ft_lstdel(&data_singleton()->heredoc_queue, &redir_free); + ft_lstdel(&data_singleton()->parser.heredoc_queue, &redir_free); ft_lstdel(&data_singleton()->exec.op_stack, &ft_lst_cfree); ft_strdel(&lexer->str); free(parser->new_sym); diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 64f83380..b9b9537c 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,113 +6,123 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/16 15:45:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 22:26:56 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static int do_lexer_routine(t_list **token, t_lexer *lexer, t_parser *parser, char *str) -{ - t_list *ltoken; - - if (lexer->state == HEREDOC || parser->state == UNDEFINED) - { - ft_strappend(&lexer->str, (char[]){'\n', 0}); - lexer->pos++; - } - ft_strappend(&lexer->str, str); - if (get_lexer_stack(*lexer) == BACKSLASH) - pop(&lexer->stack); - ltoken = ft_lstlast(*token); - if (lexer_lex(token ? <oken : token, lexer) < 1) - exit(1); - if (get_lexer_stack(*lexer) > 2) - return (1); - lexer->state = DEFAULT; - return (0); -} - -static int do_parser_routine(t_list **token, t_btree **ast, t_lexer *lexer, t_parser *parser) -{ - if (get_reserved_words(token)) - return (1); - token_print(*token); - if (insert_newline(token)) - return (1); - ft_parse(ast, token, parser); - if ((lexer->state = data_singleton()->heredoc_queue ? HEREDOC : DEFAULT)) - return (0); - if (parser->state == ERROR) - { - error_syntax(token, parser, ast); - return (1); - } - else if (parser->state == SUCCESS) - return (1); - return (0); -} - -static int do_readline_routine(t_lexer *lexer, t_parser *parser, char **str) +static int do_readline_routine(char **stream) { int ret; int has_prompt; + t_data *data; - has_prompt = !(get_lexer_stack(*lexer) || parser->state == UNDEFINED || lexer->state == HEREDOC); - ret = readline(has_prompt, str); + data = data_singleton(); + has_prompt = !(get_lexer_stack(data->lexer) + || data->parser.state == UNDEFINED || data->lexer.state == HEREDOC); + ret = readline(has_prompt, stream); if (ret == -1) exit(1); - if (ret == 1 && parser->state == UNDEFINED) + if (ret == 1 && data->parser.state == UNDEFINED) error_eof(); return (ret); } -static int handle_instruction(t_lexer *lexer, t_parser *parser) +static int do_lexer_routine(char *stream) { - t_list *token; - t_btree *ast; - int ret; - char *str; + t_list *ltoken; + t_data *data; - token = NULL; - ast = NULL; - str = NULL; - while (1) + data = data_singleton(); + if (data->lexer.state == HEREDOC || data->parser.state == UNDEFINED) { - if ((ret = do_readline_routine(lexer, parser, &str)) > 0) - return (ret); - if (do_lexer_routine(&token, lexer, parser, str) > 0) - continue ; - if (do_parser_routine(&token, &ast, lexer, parser) > 0) - break ; + ft_strappend(&data->lexer.str, (char[]){'\n', 0}); + data->lexer.pos++; } - /* btree_print(STDBUG, ast, &ft_putast); */ - if (parser->state == SUCCESS && ft_exec(&ast) < 0) + ft_strappend(&data->lexer.str, stream); + if (get_lexer_stack(data->lexer) == BACKSLASH) + pop(&data->lexer.stack); + ltoken = ft_lstlast(data->token); + if (lexer_lex(data->token ? <oken : &data->token, &data->lexer) < 0) exit(1); - if (SH_IS_INTERACTIVE(data_singleton()->opts) && *lexer->str) - ft_add_str_in_history(lexer->str); + if (get_lexer_stack(data->lexer) > 2) + return (1); + data->lexer.state = DEFAULT; return (0); } +static int do_parser_routine(void) +{ + t_data *data; + + data = data_singleton(); + if (get_reserved_words(&data->token)) + return (1); + if (insert_newline(&data->token)) + return (2); + if (data->parser.state == SUCCESS && stack_init(&data->parser)) + exit(1); + if (ft_parse(&data->ast, &data->token, &data->parser)) + exit(1); + if ((data->lexer.state = data->parser.heredoc_queue ? HEREDOC : DEFAULT)) + return (0); + if (data->parser.state == ERROR) + { + error_syntax(&data->token); + data->parser.state = SUCCESS; + return (1); + } + else if (data->parser.state == SUCCESS) + return (1); + return (0); +} + +static int handle_instruction() +{ + int ret; + char *stream; + t_data *data; + + stream = NULL; + data = data_singleton(); + while (1) + { + if ((ret = do_readline_routine(&stream)) > 0) + return (ret); + if (do_lexer_routine(stream) > 0) + continue ; + if (do_parser_routine() > 0) + break ; + } + /* btree_print(STDBUG, ast, &ft_putast); */ + if (data->parser.state == SUCCESS && ft_exec(&data->ast) < 0) + exit(1); + DG("stream [%s]", data->lexer.str); + if (SH_IS_INTERACTIVE(data->opts) && data->lexer.str) + ft_add_str_in_history(data->lexer.str); + return (0); +} int main(int ac, char **av) { int ret; - t_lexer lexer; - t_parser parser; + t_data *data; g_argv = av; setlocale(LC_ALL, ""); DG("{inv}{bol}{gre}start of shell{eoc}"); if (shell_init(ac, av)) return (1); - lexer_init(&lexer); - parser_init(&parser); DG("JOBC is %s", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF"); + data = data_singleton(); while (1) { - ret = handle_instruction(&lexer, &parser); - /* instruction_free(&token, &ast, &lexer, &parser); */ + ret = handle_instruction(); + lexer_destroy(&data->lexer); + parser_destroy(&data->parser); + ft_lstdel(&data->token, &ft_lst_cfree); + btree_del(&data->ast, &ft_lst_cfree); if (ret == 1) break ; } diff --git a/42sh/src/parser/add_number.c b/42sh/src/parser/add_number.c index d044c123..3ee2de29 100644 --- a/42sh/src/parser/add_number.c +++ b/42sh/src/parser/add_number.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/05 17:28:31 by ariard #+# #+# */ -/* Updated: 2017/03/13 16:58:33 by ariard ### ########.fr */ +/* Updated: 2017/03/16 17:56:16 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_redir.c b/42sh/src/parser/add_redir.c index b4f9611a..57ef23ea 100644 --- a/42sh/src/parser/add_redir.c +++ b/42sh/src/parser/add_redir.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 16:39:05 by ariard #+# #+# */ -/* Updated: 2017/03/15 02:09:54 by ariard ### ########.fr */ +/* Updated: 2017/03/16 18:34:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,7 +87,7 @@ static int add_redir_type_number(t_btree **ast, t_list **lst) t_list **queue; t_list *redir_lst; - queue = &data_singleton()->heredoc_queue; + queue = &data_singleton()->parser.heredoc_queue; token = (*lst)->content; node = (*ast)->item; redir_lst = ft_lstlast(node->data.cmd.redir); @@ -116,7 +116,7 @@ int add_redir_type(t_btree **ast, t_list **lst) t_list *redir_lst; t_list **queue; - queue = &data_singleton()->heredoc_queue; + queue = &data_singleton()->parser.heredoc_queue; if (!*ast) gen_node(ast); token = (*lst)->content; diff --git a/42sh/src/parser/error_syntax.c b/42sh/src/parser/error_syntax.c index d1d79d72..aef8478a 100644 --- a/42sh/src/parser/error_syntax.c +++ b/42sh/src/parser/error_syntax.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:15:35 by ariard #+# #+# */ -/* Updated: 2017/03/16 15:34:39 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 17:49:23 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,14 +51,11 @@ static t_errormatch g_errormatch[] = {0, NULL}, }; -int error_syntax(t_list **lst, t_parser *parser, - t_btree **ast) +int error_syntax(t_list **lst) { t_token *token; int i; - (void)parser; - (void)ast; if (!*lst) return (0); token = (*lst)->content; @@ -67,18 +64,18 @@ int error_syntax(t_list **lst, t_parser *parser, { if (g_errormatch[i].token == token->type) { - ft_dprintf(2, "syntax error near unexpected token « %s »\n", + ft_dprintf(2, "{red}syntax error near unexpected token « %s »{eoc}\n", g_errormatch[i].error); return (0); } i++; } - ft_dprintf(2, "syntax error near unexpected token « %s »\n", token->data); + ft_dprintf(2, "{red}syntax error near unexpected token « %s »\n{eoc}", token->data); return (0); } int error_eof(void) { - ft_putstr_fd("syntax error near unexpected EOF", 2); + ft_dprintf(2, "{red}syntax error near unexpected EOF{eoc}"); return (1); } diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index ffaf9334..0965e10a 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 16:11:21 by ariard #+# #+# */ -/* Updated: 2017/03/14 22:01:19 by ariard ### ########.fr */ +/* Updated: 2017/03/16 21:24:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -1243,7 +1243,7 @@ int eval_sym(t_list **stack, t_sym new_sym) if (!*stack) return (1); head = (*stack)->content; -// DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); + DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); i = 0; while (g_stackmatch[i].top) { diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index d65aa8b8..f0e30113 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 16:17:38 by ariard #+# #+# */ -/* Updated: 2017/03/16 14:32:34 by jhalford ### ########.fr */ +/* Updated: 2017/03/16 22:11:41 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,13 +35,12 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) { t_sym *head; - (void)ast; if (pop_heredoc(token)) return (0); while (*token) { produce_sym(&parser->stack, parser->new_sym, token); -// DG("new sym %s", read_state(*parser->new_sym)); + DG("new sym %s", read_state(*parser->new_sym)); if (eval_sym(&parser->stack, *parser->new_sym)) return ((parser->state = ERROR)); else @@ -62,7 +61,5 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) else ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free); } - if (parser->state == SUCCESS) - DG("sucessful parsing"); return (0); } diff --git a/42sh/src/parser/heredoc_parser.c b/42sh/src/parser/heredoc_parser.c index 78a74e01..02776e5e 100644 --- a/42sh/src/parser/heredoc_parser.c +++ b/42sh/src/parser/heredoc_parser.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 16:21:05 by ariard #+# #+# */ -/* Updated: 2017/03/14 15:02:34 by gwojda ### ########.fr */ +/* Updated: 2017/03/16 17:51:54 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,9 +20,9 @@ int pop_heredoc(t_list **lst) char *tmp; token = (*lst)->content; - if (token->type == HEREDOCDATA && data_singleton()->heredoc_queue != NULL) + if (token->type == HEREDOCDATA && data_singleton()->parser.heredoc_queue != NULL) { - head = &data_singleton()->heredoc_queue; + head = &data_singleton()->parser.heredoc_queue; redir = *(t_redir**)(*head)->content; if (head && token) { diff --git a/42sh/src/parser/parser_destroy.c b/42sh/src/parser/parser_destroy.c new file mode 100644 index 00000000..f24f7087 --- /dev/null +++ b/42sh/src/parser/parser_destroy.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/16 19:30:17 by ariard #+# #+# */ +/* Updated: 2017/03/16 22:13:50 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void parser_destroy(t_parser *parser) +{ + parser->state = SUCCESS; + ft_lstdel(&parser->stack, &ft_lst_cfree); + ft_lstdel(&data_singleton()->parser.heredoc_queue, &ft_lst_cfree); + free(parser->new_sym); + parser->new_sym = NULL; +} diff --git a/42sh/src/parser/parser_init.c b/42sh/src/parser/parser_init.c index 8b8ca5c1..8f86e68c 100644 --- a/42sh/src/parser/parser_init.c +++ b/42sh/src/parser/parser_init.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/21 16:14:04 by ariard #+# #+# */ -/* Updated: 2017/03/11 16:20:31 by ariard ### ########.fr */ +/* Updated: 2017/03/16 22:13:39 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,8 +15,7 @@ void parser_init(t_parser *parser) { parser->state = SUCCESS; - parser->new_sym = ft_memalloc(sizeof(t_sym)); + parser->new_sym = NULL; parser->stack = NULL; - push_stack(&parser->stack, TERMINUS); - push_stack(&parser->stack, LINEBREAK); + parser->heredoc_queue = NULL; } diff --git a/42sh/src/parser/produce_sym.c b/42sh/src/parser/produce_sym.c index bc71df07..b73514d0 100644 --- a/42sh/src/parser/produce_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:58:34 by ariard #+# #+# */ -/* Updated: 2017/03/14 22:03:42 by ariard ### ########.fr */ +/* Updated: 2017/03/16 21:31:55 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -127,12 +127,14 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst) t_sym *head; int i; + if (!*stack) + DG("stack absente"); if (!*stack || !*lst) return (1); token = (*lst)->content; head = (*stack)->content; -// DG("produce stack : %s && token : %s", read_state(*head), -// read_state(token->type)); + DG("produce stack : %s && token : %s", read_state(*head), + read_state(token->type)); i = 0; *new_sym = 0; while (g_prodmatch[i].new_sym) diff --git a/42sh/src/parser/stack_init.c b/42sh/src/parser/stack_init.c new file mode 100644 index 00000000..6abc9386 --- /dev/null +++ b/42sh/src/parser/stack_init.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* stack_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/16 20:31:32 by ariard #+# #+# */ +/* Updated: 2017/03/16 22:11:59 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int stack_init(t_parser *parser) +{ + ft_lstdel(&parser->stack, NULL); + push_stack(&parser->stack, TERMINUS); + push_stack(&parser->stack, LINEBREAK); + if (!parser->new_sym && !(parser->new_sym = ft_memalloc(sizeof(t_sym)))) + return (-1); + return (0); +}