From 6e97753fa7307cfc0a2ea426edaac6c683e916f7 Mon Sep 17 00:00:00 2001 From: nass1pro Date: Fri, 17 Jul 2020 13:10:18 +0200 Subject: Added parser syntax error --- src/lexer/lexer.c | 13 ++++++-- src/main.c | 11 +++++-- src/parse/parse.c | 91 +++++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 96 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index a6a2755..7c6d527 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/07/16 08:18:25 by nahaddac #+# #+# */ -/* Updated: 2020/07/17 13:08:08 by charles ### ########.fr */ +/* Updated: 2020/07/18 08:58:42 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,7 +24,6 @@ int len_is_not_sep(char *input) i +=2; if (input[i] == '\\') i += len_is_not_sep(&input[i]); - } if (lexer_sep(input[i])) return(i); @@ -50,9 +49,17 @@ int check_input(char *input) if (input[i] == '\\' && lexer_sep(input[i + 1])) i += 2; if (input[i] == '(' || input[i] == ')') - return (i + 1); + { + i +=1; + if(input[i] == ' ') + while(input[i++] != ' ') + ; + return (i); + } if (lexer_sep(input[i])) { + if (input[i] == ';') + return (i += lexe_space(&input[i + 1]) + 1); while(input[i] == input[i + 1] && op < 2) { i++; diff --git a/src/main.c b/src/main.c index 9687de4..332bfee 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: cacharle +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 11:45:44 by cacharle #+# #+# */ -/* Updated: 2020/07/17 13:39:05 by charles ### ########.fr */ +/* Updated: 2020/07/18 08:58:52 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -80,7 +80,7 @@ int main(int argc, char **argv, char **envp) if (parser_out == NULL || parser_out->syntax_error) return (1); - /* ast_print(0, parser_out->ast); */ + //ast_print(0, parser_out->ast); /* printf("===cmd_argv===\n"); */ /* ft_lstiter(parser_out->ast->cmd_argv, token_debug); */ @@ -108,8 +108,13 @@ int main(int argc, char **argv, char **envp) return (1); t_ret *parser_out = parse(lex_out); - if (parser_out == NULL || parser_out->syntax_error) + if (parser_out == NULL) return (1); + if (parser_out->syntax_error) + { + print_prompt(); + continue; + } int fds[2] = {MS_NO_FD, MS_NO_FD}; int eval_out = eval(fds, env, path, parser_out->ast); diff --git a/src/parse/parse.c b/src/parse/parse.c index 69cba8d..6922530 100644 --- a/src/parse/parse.c +++ b/src/parse/parse.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */ -/* Updated: 2020/07/17 10:36:39 by nahaddac ### ########.fr */ +/* Updated: 2020/07/17 16:12:53 by nahaddac ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,9 +40,11 @@ t_ret *ret_wrap_ast(t_ast *ast, t_ftlst *rest) return ret; } -t_ftlst *parse_redir(t_ftlst *input, t_ftlst **redirs) +t_ret *parse_redir(t_ftlst *input, t_ftlst **redirs) { enum e_token_tag tag; + t_ret *tmp; + char *error; push_token(redirs, input->data); input = input->next; @@ -57,16 +59,35 @@ t_ftlst *parse_redir(t_ftlst *input, t_ftlst **redirs) tag = ((t_token *)input->data)->tag; } if (!(tag & TAG_IS_STR)) - return (NULL); + { + error = ft_strjoin3(g_basename,": syntax error unexpected token `", ((t_token *)input->data)->content); + error = ft_strjoin(error, "\'\n"); + ft_putstr_fd(error, 2); + tmp = ret_wrap_ast(NULL, NULL); + tmp->syntax_error = true; + return tmp; + } push_token(redirs, input->data); - return (input); + return (ret_wrap_ast(NULL, input)); } t_ret *parse_cmd(t_ftlst *input) { enum e_token_tag tag; t_ast *ast; + char *error; + t_ret *tmp; + tag = ((t_token *)input->data)->tag; + if (tag & TAG_IS_SEP) + { + error = ft_strjoin3(g_basename,": syntax error unexpected token `", ((t_token *)input->data)->content); + error = ft_strjoin(error, "\'\n"); + ft_putstr_fd(error, 2); + tmp = ret_wrap_ast(NULL, NULL); + tmp->syntax_error = true; + return tmp; + } ast = ast_new(AST_CMD); while (input != NULL) { @@ -75,7 +96,10 @@ t_ret *parse_cmd(t_ftlst *input) push_token(&ast->cmd_argv, input->data); else if (tag & TAG_IS_REDIR) { - input = parse_redir(input, &ast->redirs); + tmp = parse_redir(input, &ast->redirs); + if (tmp->syntax_error || tmp == NULL) + return tmp; + input = tmp->rest; } else break; @@ -96,18 +120,39 @@ t_ret *parse_op(t_ftlst *input) t_ret *left_ret; t_ret *right_ret; enum e_token_tag tag; + t_ret *tmp; + char *error; left_ret = parse_expr(input); + if (left_ret == NULL || left_ret->syntax_error) + return left_ret; input = left_ret->rest; if (input == NULL || ((t_token*)input->data)->tag & TAG_PARENT_CLOSE) return ret_wrap_ast(left_ret->ast, input); tag = ((t_token*)input->data)->tag; - /* les sep */ + if (!(tag & TAG_IS_SEP)) + { + error = ft_strjoin3(g_basename,": syntax error near unexpected token `", ((t_token *)input->data)->content); + error = ft_strjoin(error, "\'\n"); + ft_putstr_fd(error, 2); + tmp = ret_wrap_ast(NULL, NULL); + tmp->syntax_error = true; + return tmp; + } input = input->next; - + if(input == NULL) + { + error = ft_strjoin(g_basename,": syntax error expected token "); + error = ft_strjoin(error, "\n"); + ft_putstr_fd(error, 2); + tmp = ret_wrap_ast(NULL, NULL); + tmp->syntax_error = true; + return tmp; + } right_ret = parse_op(input); - + if (right_ret == NULL || right_ret->syntax_error) + return right_ret; ast = ast_new(AST_OP); ast->op.left = left_ret->ast; ast->op.right = right_ret->ast; @@ -120,17 +165,37 @@ t_ret *parse_expr(t_ftlst *input) t_ret *tmp; enum e_token_tag tag; t_ast *new_ast; + char *error; tag = ((t_token*)input->data)->tag; if (tag & TAG_PARENT_OPEN) { - tmp = parse_op(input->next); + if (!(tmp = parse_op(input->next)) || tmp->syntax_error) + return tmp; input = tmp->rest; - tag = ((t_token*)input->data)->tag; - if (!(tag & TAG_PARENT_CLOSE)) - return (NULL); + if (input == NULL) + { + error = ft_strjoin(g_basename,": syntax error expected token"); + ft_putstr_fd(error, 2); + tmp->syntax_error = true; + return (tmp); + } + tag = ((t_token*)input->data)->tag; + if (!(tag & TAG_PARENT_CLOSE) ) + { + error = ft_strjoin(g_basename,": syntax error expected token `)''"); + ft_putstr_fd(error, 2); + tmp->syntax_error = TRUE; + return (tmp); + } + // if (tag & TAG_IS_SEP) + // { + // error = ft_strjoin(g_basename,": syntax error expected token `)''"); + // ft_putstr_fd(error, 2); + // tmp->syntax_error = TRUE; + // return (tmp); + // } input = input->next; - new_ast = ast_new(AST_PARENT); new_ast->parent_ast = tmp->ast; tmp->ast = new_ast; -- cgit