diff options
| author | nass1pro <nass1pro@gmail.com> | 2020-06-19 13:28:41 +0200 |
|---|---|---|
| committer | nass1pro <nass1pro@gmail.com> | 2020-06-19 13:28:41 +0200 |
| commit | c8091831c4ce1c4cf8703b18de22441aff191f44 (patch) | |
| tree | b30e542c9345135892f971d8e7566fd4cc8c64a4 /src/parse | |
| parent | 677ebe4c32fc7b7bb99f3abb48f7f6f1a428d102 (diff) | |
| download | minishell-c8091831c4ce1c4cf8703b18de22441aff191f44.tar.gz minishell-c8091831c4ce1c4cf8703b18de22441aff191f44.tar.bz2 minishell-c8091831c4ce1c4cf8703b18de22441aff191f44.zip | |
Update parser_error
Diffstat (limited to 'src/parse')
| -rwxr-xr-x | src/parse/parse.c | 69 | ||||
| -rw-r--r-- | src/parse/parse_error.c | 106 |
2 files changed, 156 insertions, 19 deletions
diff --git a/src/parse/parse.c b/src/parse/parse.c index 198ac65..cc4d56d 100755 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -6,7 +6,7 @@ /* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */ -/* Updated: 2020/06/18 12:48:20 by nahaddac ### ########.fr */ +/* Updated: 2020/06/19 13:28:12 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -104,27 +104,58 @@ t_ret *parse_op(t_ftlst *input) return ret_wrap_ast(ast, right_ret->rest); } -t_ret *parse_expr(t_ftlst *input) +t_ret *parse_expr(t_ftlst *input) { - t_ret *tmp; - enum e_token_tag tag; - - tag = ((t_token*)input->data)->tag; - if (tag == TAG_PARENT_OPEN) - { - tmp = parse_op(input->next); - input = tmp->rest; - tag = ((t_token*)input->data)->tag; - if (tag != TAG_PARENT_CLOSE) - return (NULL); - input = input->next; - tmp->rest = input; - return tmp; - } - return parse_cmd(input); + t_ret *tmp; + enum e_token_tag tag; + tag = ((t_token*)input->data)->tag; + if (tag == TAG_PARENT_OPEN) + { + tmp = parse_op(input->next); + input = tmp->rest; + tag = ((t_token*)input->data)->tag; + if (tag != TAG_PARENT_CLOSE) + return (NULL); + input = input->next; + t_ast *new_ast = ast_new(AST_PARENT); + new_ast = tmp->ast; + tmp->ast = new_ast; + if (tag & TAG_IS_REDIR) + { + while(input != NULL) + { + push_token(&tmp->ast->redirs, input->data); + if (tag & TAG_IS_STR && tag & TAG_STICK) + input = input->next; + else if (tag & TAG_IS_REDIR) + input = input->next; + else + break; + tag = ((t_token *)input->data)->tag; + } + } + tmp->rest = input; + return tmp; + } + return parse_cmd(input); } t_ret *parse(t_ftlst *input) { - return parse_op(input); + t_ret *ret; + t_ftlst *in_f; + + in_f = input; + if (input == NULL) + return NULL; + if (!(ret = malloc(sizeof(t_ret) * 1))) + return (NULL); + ret->ast = NULL; + ret->rest = NULL; + if((ret->unexpected = error_syntax_simple(input)) != NULL) + printf("%s\n", ret->unexpected->content); + ret = parse_op(in_f); + ast_destroy(ret->ast); + ft_lstdestroy(&ret->rest, (void (*)(void*))token_destroy); + return (NULL); } diff --git a/src/parse/parse_error.c b/src/parse/parse_error.c new file mode 100644 index 0000000..b721c28 --- /dev/null +++ b/src/parse/parse_error.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/06/18 15:09:48 by nahaddac #+# #+# */ +/* Updated: 2020/06/19 12:46:56 by nahaddac ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +t_token *error_syntax_parent(t_ftlst *in) +{ + int op; + int cl; + t_token *tk; + + op = 0; + cl = 0; + while(in != NULL) + { + tk = in->data; + if (tk->tag & TAG_PARENT_OPEN) + op++; + if(tk->tag & TAG_PARENT_CLOSE) + cl++; + if (cl && op == 0) + { + tk->content = ft_strjoin3( + "minishell: syntax error near unexpected token `", + tk->content, "'"); + return tk; + } + in = in->next; + } + return NULL; +} + +int out_error_first(t_token *tk) +{ + int i; + + i = 0; + if(tk->tag & TAG_IS_SEP) + return(1); + if (tk->tag & TAG_IS_REDIR) + { + while(tk->content[i]) + i++; + if (tk->tag & TAG_REDIR_APPEND && i <= 2) + return (0); + else + return(1); + } + return(0); +} + +t_token *error_syntax_simple(t_ftlst *in) +{ + t_token *tk; + size_t i; + t_ftlst *first; + + tk = in->data; + first = in; + if(tk->tag & TAG_IS_SEP || (tk->tag & TAG_IS_REDIR)) + { + if (out_error_first(tk)) + { + i = ft_strlen(tk->content); + if (i >= 2) + tk->content[2] = '\0'; + tk->content = + ft_strjoin3("minishell: syntax error near unexpected token `", + tk->content, "'"); + return(tk); + } + } + while(in != NULL) + { + i = 0; + tk = in->data; + if(tk->tag & TAG_IS_SEP || (tk->tag & TAG_IS_REDIR)) + { + if (((t_token *)in->next->data)->tag & + ((t_token*)in->next->data)->tag & TAG_IS_SEP || + (((t_token*)in->next->data)->tag & TAG_IS_REDIR)) + { + tk = in->next->data; + i = ft_strlen(tk->content); + if (i >= 3) + tk->content[3] = '\0'; + tk->content = + ft_strjoin3("minishell: syntax error near unexpected token `", + tk->content, "'"); + return(tk); + } + } + in = in->next; + } + return error_syntax_parent(first); + +} |
