diff --git a/42sh/eewfew b/42sh/eewfew new file mode 100644 index 00000000..e69de29b diff --git a/42sh/includes/.parser.h.swn b/42sh/includes/.parser.h.swn index ccd9ee3b..da1db700 100644 Binary files a/42sh/includes/.parser.h.swn and b/42sh/includes/.parser.h.swn differ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index b0613516..c453cc6f 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/02/18 20:06:02 by ariard ### ########.fr */ +/* Updated: 2017/02/19 16:29:14 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -163,6 +163,16 @@ typedef struct s_stackmatch t_stackmatch; extern t_stackmatch g_stackmatch[]; +struct s_errormatch +{ + t_type token; + char *error; +}; + +typedef struct s_errormatch t_errormatch; + +extern t_errormatch g_errormatch[]; + int ft_parse2(t_btree **ast, t_list **token); int ft_parse(t_btree **ast, t_list **token); diff --git a/42sh/sample/error.sh b/42sh/sample/error.sh new file mode 100644 index 00000000..0cf7ff3a --- /dev/null +++ b/42sh/sample/error.sh @@ -0,0 +1 @@ +ls ; pwd | | diff --git a/42sh/sample/while.sh b/42sh/sample/while.sh index c6a6867c..26568e98 100644 --- a/42sh/sample/while.sh +++ b/42sh/sample/while.sh @@ -4,5 +4,8 @@ do do pwd done - pwd ; ls + while ls + do + pwd + done done diff --git a/42sh/src/main/shell_script.c b/42sh/src/main/shell_script.c index 5b907c72..32f9899d 100644 --- a/42sh/src/main/shell_script.c +++ b/42sh/src/main/shell_script.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/22 23:06:34 by ariard #+# #+# */ -/* Updated: 2017/02/17 23:27:06 by ariard ### ########.fr */ +/* Updated: 2017/02/19 16:02:17 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/.eval_sym.c.swn b/42sh/src/parser/.eval_sym.c.swn new file mode 100644 index 00000000..291aa4f1 Binary files /dev/null and b/42sh/src/parser/.eval_sym.c.swn differ diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index dbd3ddd9..fd7bfaf4 100644 --- a/42sh/src/parser/add_cmd.c +++ b/42sh/src/parser/add_cmd.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 20:49:15 by ariard #+# #+# */ -/* Updated: 2017/02/18 18:56:15 by ariard ### ########.fr */ +/* Updated: 2017/02/18 20:12:23 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_loop.c b/42sh/src/parser/add_loop.c index dbfc7533..0067faa9 100644 --- a/42sh/src/parser/add_loop.c +++ b/42sh/src/parser/add_loop.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 22:17:14 by ariard #+# #+# */ -/* Updated: 2017/02/18 20:10:08 by ariard ### ########.fr */ +/* Updated: 2017/02/18 20:12:26 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_sep.c b/42sh/src/parser/add_sep.c index 3d7e8923..fb7dcb1e 100644 --- a/42sh/src/parser/add_sep.c +++ b/42sh/src/parser/add_sep.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 19:12:07 by ariard #+# #+# */ -/* Updated: 2017/02/18 20:06:47 by ariard ### ########.fr */ +/* Updated: 2017/02/19 16:48:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ int add_sep(t_btree **ast, t_list **lst) DG("add sep"); if (isloop(ast) == 1) return (add_loop_sep(ast, lst)); + if (!*ast) + gen_node(ast); node = (*ast)->item; token = (*lst)->content; // if (node->type != TK_DO) diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 6f6933cc..d1020a8c 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 17:39:18 by ariard #+# #+# */ -/* Updated: 2017/02/18 16:58:05 by ariard ### ########.fr */ +/* Updated: 2017/02/19 16:48:12 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -179,7 +179,6 @@ t_aggrematch g_aggrematch[] = {COMPLETE_COMMAND, NEWLINE_LIST, COMPLETE_COMMANDS, COMPLETE_COMMANDS}, {COMPLETE_COMMAND, LINEBREAK, COMPLETE_COMMANDS, 0}, {COMPLETE_COMMANDS, LINEBREAK, PROGRAM, LINEBREAK}, -// voir decoupe separateur au lexer {0, 0, 0, 0}, }; @@ -188,8 +187,8 @@ int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state) int i; i = 0; -// DG("aggregate head %s && sym %s", -// read_state(**stack), read_state(*new_sym)); + DG("aggregate head %s && sym %s", + read_state(**stack), read_state(*new_sym)); while (g_aggrematch[i].top) { if (*new_sym == g_aggrematch[i].top @@ -201,7 +200,7 @@ int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state) if (g_aggrematch[i].erase_sym) { pop_stack(stack, g_aggrematch[i].erase_sym); -// DG("stack after pop: %s", read_state(**stack)); + DG("stack after pop: %s", read_state(**stack)); } if (eval_sym(**stack, *new_sym)) return ((*state = ERROR)); diff --git a/42sh/src/parser/build_tree.c b/42sh/src/parser/build_tree.c index 7fc287f0..a5d73447 100644 --- a/42sh/src/parser/build_tree.c +++ b/42sh/src/parser/build_tree.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 18:32:59 by ariard #+# #+# */ -/* Updated: 2017/02/18 18:43:50 by ariard ### ########.fr */ +/* Updated: 2017/02/19 16:35:25 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/error_syntax.c b/42sh/src/parser/error_syntax.c index 9bacf149..83003a69 100644 --- a/42sh/src/parser/error_syntax.c +++ b/42sh/src/parser/error_syntax.c @@ -6,28 +6,67 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:15:35 by ariard #+# #+# */ -/* Updated: 2017/02/13 22:09:03 by ariard ### ########.fr */ +/* Updated: 2017/02/19 16:35:56 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" -int error_syntax(t_list **lst) +t_errormatch g_errormatch[] = +{ + {TK_NEWLINE, "newline"}, + {TK_SEMI, ";"}, + {TK_PIPE, "|"}, + {TK_AMP, "&"}, + {TK_LESS, "<"}, + {TK_GREAT, ">"}, + {TK_DGREAT, ">>"}, + {TK_LESSAND, "<&"}, + {TK_GREATAND, ">&"}, + {TK_LESSGREAT, "<>"}, + {TK_CLOBBER, ">|"}, + {TK_DLESS, "<<"}, + {TK_DLESSDASH, "<<-"}, + {TK_AND_IF, "&&"}, + {TK_OR_IF, "||"}, + {TK_DSEMI, ";;"}, + {TK_IF, "if"}, + {TK_THEN, "then"}, + {TK_ELSE, "else"}, + {TK_ELIF, "elif"}, + {TK_FI, "fi"}, + {TK_DO, "do"}, + {TK_DONE, "done"}, + {TK_CASE, "case"}, + {TK_ESAC, "esac"}, + {TK_WHILE, "while"}, + {TK_UNTIL, "until"}, + {TK_FOR, "for"}, + {TK_LBRACE, "{"}, + {TK_RBRACE, "}"}, + {TK_BANG, "!"}, + {TK_IN, "in"}, + {0, NULL}, +}; + +int error_syntax(t_list **lst) { t_token *token; + int i; token = (*lst)->content; - if (token->type == TK_SEMI) - ft_putstr_fd("syntax error near unexepected token ';'", 2); - else if (token->type == TK_WORD) - ft_putstr_fd("syntax error near unexepected token 'word'", 2); - else if (token->type == TK_GREAT) - ft_putstr_fd("syntax error near unexepected token '>'", 2); - else if (token->type == TK_NEWLINE) - ft_putstr_fd("syntax error near unexepected token 'newline'", 2); - else if (token->type == TK_LESS) - ft_putstr_fd("syntax error near unexepected token '<'", 2); - else - ft_putstr_fd("grammar error, notify ariard", 2); + i = 0; + while (g_errormatch[i].token) + { + if (g_errormatch[i].token == token->type) + { + ft_putstr_fd("syntax error near unexpetcted token `", 2); + ft_putstr_fd(g_errormatch[i].error, 2); + ft_putstr_fd("'", 2); + return (1); + } + i++; + } + ft_putstr_fd("grammar error, notify ariard", 2); return (1); } diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index e56fce59..6f059bfa 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 16:26:30 by ariard #+# #+# */ -/* Updated: 2017/02/18 18:50:18 by ariard ### ########.fr */ +/* Updated: 2017/02/19 16:53:14 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -196,6 +196,7 @@ t_stackmatch g_stackmatch[] = {TK_WHILE, NEWLINE_LIST}, {TK_WHILE, PIPE_SEMI_SEQUENCE}, {TK_WHILE, TK_DO}, + {TK_WHILE, COMPOUND_LIST}, {TK_UNTIL, LINEBREAK}, {TK_UNTIL, TK_BANG}, {TK_UNTIL, SEPARATOR_OP}, @@ -265,6 +266,7 @@ t_stackmatch g_stackmatch[] = {LINEBREAK, CMD_SUPERIOR}, {LINEBREAK, PIPE_SEMI_SEQUENCE}, {LINEBREAK, COMPOUND_LIST}, + {LINEBREAK, PROGRAM}, {NEWLINE_LIST, TK_DO}, {NEWLINE_LIST, CMD_NAME}, {NEWLINE_LIST, NEWLINE_LIST}, @@ -390,6 +392,7 @@ t_stackmatch g_stackmatch[] = {WHILE_CLAUSE, NEWLINE_LIST}, {WHILE_CLAUSE, PIPE_SEMI_SEQUENCE}, {WHILE_CLAUSE, TK_DO}, + {WHILE_CLAUSE, COMPOUND_LIST}, {ELSE_PART, COMPOUND_LIST}, {IF_CLAUSE, LINEBREAK}, {IF_CLAUSE, TK_BANG}, @@ -439,6 +442,7 @@ t_stackmatch g_stackmatch[] = {COMPOUND_COMMAND, PIPE_SEMI_SEQUENCE}, {COMPOUND_COMMAND, FUNC}, {COMPOUND_COMMAND, TK_DO}, + {COMPOUND_COMMAND, COMPOUND_LIST}, {COMMAND, TK_WHILE}, {COMMAND, LINEBREAK}, {COMMAND, TK_DO}, diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index bd0a89c9..2410bf7e 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 14:30:22 by ariard #+# #+# */ -/* Updated: 2017/02/18 18:06:45 by ariard ### ########.fr */ +/* Updated: 2017/02/19 16:54:31 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,7 +58,7 @@ int ft_parse2(t_btree **ast, t_list **token) ft_putstr("success"); build_tree(ast, token); btree_print(STDBUG, *ast, &ft_putast); - if (end_instruction(*stack) && !(*token)->next) + if ((end_instruction(*stack) && !(*token)->next) || *stack == PROGRAM) insert_linebreak(token); else ft_lst_delif(token, (*token)->content, &ft_addrcmp, &token_free); diff --git a/42sh/src/parser/tree_wrapper.c b/42sh/src/parser/tree_wrapper.c index fb69e072..505f2050 100644 --- a/42sh/src/parser/tree_wrapper.c +++ b/42sh/src/parser/tree_wrapper.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 18:57:44 by ariard #+# #+# */ -/* Updated: 2017/02/18 20:06:49 by ariard ### ########.fr */ +/* Updated: 2017/02/19 16:34:17 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,7 +25,6 @@ int gen_node(t_btree **ast) if (!*ast) { - DG("node create"); *ast = btree_create_node(&item, sizeof(item)); ((t_astnode *)(*ast)->item)->data.token = NULL; ((t_astnode *)(*ast)->item)->data.redir.word.word = NULL; diff --git a/42sh/wefwe b/42sh/wefwe new file mode 100644 index 00000000..ce09b016 --- /dev/null +++ b/42sh/wefwe @@ -0,0 +1 @@ +/Users/ariard/Projects/42sh diff --git a/42sh/wfewef b/42sh/wfewef new file mode 100644 index 00000000..e69de29b