diff --git a/42sh/Makefile b/42sh/Makefile index 8f8e4c79..67a83bca 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -216,6 +216,8 @@ lexer/get_reserved_words.c\ lexer/get_state_global.c\ lexer/get_state_redir.c\ lexer/insert_newline.c\ +lexer/isrw_delim.c\ +lexer/keep_last_type.c\ lexer/lexer_backslash.c\ lexer/lexer_bquote.c\ lexer/lexer_curly_braces.c\ diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index 8e78d178..243e2063 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/20 15:01:01 by wescande ### ########.fr */ +/* Updated: 2017/03/20 16:24:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,9 +63,6 @@ struct s_rvwords int type; }; -/* extern t_rvwords g_rvwords[]; */ -/* extern int (*g_lexer[])(t_list **alst, t_lexer *lexer); */ - int ft_post_tokenize(t_list **alst, char **str); t_token *token_init(); @@ -92,6 +89,8 @@ t_lexstate get_state_global(t_lexer *lexer); t_lexstate get_state_redir(t_lexer *lexer); int get_lexer_stack(t_lexer lexer); int get_reserved_words(t_list *temp); +int isrw_delim(t_type type); +int keep_last_type(t_type *type, t_list *token); int insert_newline(t_list **alst); int do_lexer_routine(t_list **token, char *stream); diff --git a/42sh/src/lexer/do_lexer_routine.c b/42sh/src/lexer/do_lexer_routine.c index 6c996280..4958965d 100644 --- a/42sh/src/lexer/do_lexer_routine.c +++ b/42sh/src/lexer/do_lexer_routine.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/19 14:24:38 by wescande #+# #+# */ -/* Updated: 2017/03/19 14:31:39 by wescande ### ########.fr */ +/* Updated: 2017/03/20 16:42:26 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,15 +14,20 @@ int do_lexer_routine(t_list **token, char *stream) { - t_list *ltoken; - t_data *data; + t_list *ltoken; + t_data *data; + static t_type last; data = data_singleton(); - if (data->lexer.state == HEREDOC || data->parser.state == UNDEFINED) - { + if (data->lexer.state == HEREDOC) ft_strappend(&data->lexer.str, (char[]){'\n', 0}); + if (data->parser.state == UNDEFINED && !isrw_delim(last)) + ft_strappend(&data->lexer.str, (char[]){';', 0}); + else if (data->parser.state == UNDEFINED && last != 0) + ft_strappend(&data->lexer.str, (char[]){' ', 0}); + if (data->lexer.state == HEREDOC || (data->parser.state == UNDEFINED + && last != 0)) data->lexer.pos++; - } ft_strappend(&data->lexer.str, stream); if (get_lexer_stack(data->lexer) == BACKSLASH) pop(&data->lexer.stack); @@ -31,6 +36,9 @@ int do_lexer_routine(t_list **token, char *stream) exit(1); if (get_lexer_stack(data->lexer) > 2) return (1); + if (get_reserved_words(*token)) + return (1); + keep_last_type(&last, *token); data->lexer.state = DEFAULT; return (0); } diff --git a/42sh/src/lexer/get_reserved_words.c b/42sh/src/lexer/get_reserved_words.c index 42702849..39b4d333 100644 --- a/42sh/src/lexer/get_reserved_words.c +++ b/42sh/src/lexer/get_reserved_words.c @@ -6,13 +6,13 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 00:07:05 by ariard #+# #+# */ -/* Updated: 2017/03/17 20:19:59 by jhalford ### ########.fr */ +/* Updated: 2017/03/20 16:01:29 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_rvwords g_rvwords[] = +static t_rvwords g_rvwords[] = { {"while", TK_WHILE}, {"done", TK_DONE}, @@ -37,8 +37,9 @@ static int recognization_rvwords(t_token *pv_tk, t_token *at_tk) || pv_tk->type == TK_DO || pv_tk->type == TK_IF || pv_tk->type == TK_FI || pv_tk->type == TK_THEN || pv_tk->type == TK_ELIF || pv_tk->type == TK_ELSE - || pv_tk->type == TK_DSEMI) || (pv_tk->type == TK_PAREN_CLOSE - && at_tk->type == TK_PAREN_OPEN)); + || pv_tk->type == TK_DSEMI || pv_tk->type == TK_PAREN_OPEN + || pv_tk->type == TK_LBRACE || pv_tk->type == TK_UNTIL) + || (pv_tk->type == TK_PAREN_CLOSE && at_tk->type == TK_PAREN_OPEN)); } static int match_words(t_token *token) diff --git a/42sh/src/lexer/isrw_delim.c b/42sh/src/lexer/isrw_delim.c new file mode 100644 index 00000000..ed45feba --- /dev/null +++ b/42sh/src/lexer/isrw_delim.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* istoken.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/20 15:54:23 by ariard #+# #+# */ +/* Updated: 2017/03/20 16:38:43 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int isrw_delim(t_type type) +{ + return (type == TK_NEWLINE || type == TK_AMP + || type == TK_SEMI || type == TK_PIPE + || type == TK_WHILE || type == TK_UNTIL + || type == TK_DONE || type == TK_RBRACE + || type == TK_DO || type == TK_IF + || type == TK_FI || type == TK_THEN + || type == TK_ELIF || type == TK_ELSE + || type == TK_DSEMI || type == TK_PAREN_CLOSE + || type == TK_PAREN_OPEN || type == TK_LBRACE + || type == 0); +} diff --git a/42sh/src/lexer/keep_last_type.c b/42sh/src/lexer/keep_last_type.c new file mode 100644 index 00000000..f92829a7 --- /dev/null +++ b/42sh/src/lexer/keep_last_type.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* keep_last_type.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/20 16:15:34 by ariard #+# #+# */ +/* Updated: 2017/03/20 16:25:06 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int keep_last_type(t_type *last, t_list *token) +{ + t_list *tmp; + + if (!(tmp = ft_lstlast(token))) + return ((*last = 0)); + return (*last = ((t_token *)tmp->content)->type); +} diff --git a/42sh/src/parser/do_parser_routine.c b/42sh/src/parser/do_parser_routine.c index 26dfdd34..cf0160d2 100644 --- a/42sh/src/parser/do_parser_routine.c +++ b/42sh/src/parser/do_parser_routine.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/19 14:24:14 by wescande #+# #+# */ -/* Updated: 2017/03/19 14:24:22 by wescande ### ########.fr */ +/* Updated: 2017/03/20 15:47:03 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,6 @@ int do_parser_routine(t_list **token, t_btree **ast) t_data *data; data = data_singleton(); - if (get_reserved_words(*token)) - return (1); if (insert_newline(token)) return (1); if (data->parser.state == SUCCESS && stack_init(&data->parser))