aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornass1pro <nass1pro@gmail.com>2020-06-15 19:24:25 +0200
committernass1pro <nass1pro@gmail.com>2020-06-15 19:24:25 +0200
commit7b383ea28c818441ae5a75ed573dc03e992cd89f (patch)
treef8824d2b4fa6016f15ff11e3483c94ee1cfcb1e3
parent6bb9eafa5fe8de5194801ab5ff3bd898a853c9fb (diff)
downloadminishell-7b383ea28c818441ae5a75ed573dc03e992cd89f.tar.gz
minishell-7b383ea28c818441ae5a75ed573dc03e992cd89f.tar.bz2
minishell-7b383ea28c818441ae5a75ed573dc03e992cd89f.zip
Update lexer
-rw-r--r--include/lexer.h1
-rw-r--r--src/lexer/lexer.c22
-rw-r--r--src/lexer/trim.c55
-rw-r--r--src/main.c4
4 files changed, 66 insertions, 16 deletions
diff --git a/include/lexer.h b/include/lexer.h
index effc94c..b0e7f65 100644
--- a/include/lexer.h
+++ b/include/lexer.h
@@ -44,6 +44,7 @@ t_token *token_new(enum e_token_tag tag, char *content);
void token_destroy(t_token *token);
void token_destroy_lst(t_ftlst *tokens);
void token_destroy_lst2(t_ftlst *tokens1, t_ftlst *tokens2);
+t_token *push_token_enum(t_token *lst_token);
t_ftlst *lexe_trim_out(t_ftlst *lst);
diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c
index 3df47f6..49be432 100644
--- a/src/lexer/lexer.c
+++ b/src/lexer/lexer.c
@@ -84,7 +84,6 @@ t_token *lexer_lst_token_str(char *input, int i, int j)
free(lst_token);
return(0);
}
- //printf("%s-\n", lst_token->content);
return (lst_token);
}
@@ -93,7 +92,6 @@ enum e_token_tag token_verif_stick(t_token *lst_token)
int i;
i = ft_strlen(lst_token->content);
-
if (i > 0)
if (lst_token->content[i - 1] == ' ')
return(lst_token->tag);
@@ -109,7 +107,7 @@ enum e_token_tag token_str_or_cote(t_token *lst_token)
{
if(lst_token->content[i] == '\'')
{
- return (lst_token->tag = TAG_STR_SINGLE);
+ lst_token->tag = TAG_STR_SINGLE;
return(token_verif_stick(lst_token));
}
if(lst_token->content[i] == '"')
@@ -127,7 +125,7 @@ enum e_token_tag token_str_or_cote(t_token *lst_token)
return(0);
}
-t_token *push_token_enum_and_trim(t_token *lst_token)
+t_token *push_token_enum(t_token *lst_token)
{
enum e_token_tag tk;
@@ -137,7 +135,6 @@ t_token *push_token_enum_and_trim(t_token *lst_token)
lst_token->tag = token_str_or_cote(lst_token);
else
lst_token->tag = tk;
- //printf("%s-, %d\n",lst_token->content, lst_token->tag);
return (lst_token);
}
@@ -154,7 +151,7 @@ static t_ftlst *create_token_list(char *input, t_ftlst **lst)
j = 0;
j += check_input(&input[i]);
lst_token = lexer_lst_token_str(input,i,j);
- lst_token = push_token_enum_and_trim(lst_token);
+ lst_token = push_token_enum(lst_token);
new = ft_lstnew((void *) lst_token);
ft_lstpush_back(lst, new);
i += j;
@@ -164,16 +161,15 @@ static t_ftlst *create_token_list(char *input, t_ftlst **lst)
t_ftlst *lexer(char *input)
{
- t_ftlst **lst;
- /* int i; */
+ t_ftlst *lst;
if (!input)
return (0);
- lst = malloc(sizeof(t_ftlst *) * 1);
+ lst = malloc(sizeof(t_ftlst ) * 1);
if (!lst)
return(0);
- *lst = NULL;
- *lst = create_token_list(input, lst);
- /* i = ft_lstsize(*lst); */
- return (lexe_trim_out(lst));
+ lst = NULL;
+ lst = create_token_list(input, &lst);
+ lst = lexe_trim_out(lst);
+ return (lst);
}
diff --git a/src/lexer/trim.c b/src/lexer/trim.c
index 9fbb6cc..0f2cde1 100644
--- a/src/lexer/trim.c
+++ b/src/lexer/trim.c
@@ -1,7 +1,60 @@
#include "lexer.h"
+char *del_space(char *str)
+{
+ int i;
+ char *s;
+
+ i = 0;
+ while(str[++i] != '\0')
+ {
+ if(str[i] == '\\' && str[i + 1] == ' ')
+ i += 2;
+ if(str[i] == ' ')
+ break;
+ }
+ s = ft_strsubf(str, 0, i);
+ return(s);
+}
+
+char *del_quote(char *str)
+{
+ int i;
+ char *s;
+
+ i = 1;
+ while(str[++i] != '\0')
+ if (str[i] == '\'' || str[i] == '"')
+ break;
+ s = ft_strsubf(str, 1, i - 1);
+ return (s);
+}
+
t_ftlst *lexe_trim_out(t_ftlst *lst)
{
- return (lst);
+ t_ftlst *first;
+ t_token *tk;
+
+ first = lst;
+ while(lst != NULL)
+ {
+ tk = lst->data;
+ if (tk->tag >= TAG_STR_DOUBLE || tk->tag >= TAG_STR_SINGLE)
+ {
+ tk->content = del_quote(tk->content);
+ if(lst->next == NULL)
+ if (tk->tag & TAG_STICK)
+ tk->tag -= TAG_STICK;
+ }
+ else
+ {
+ tk->content = del_space(tk->content);
+ if(lst->next == NULL)
+ if (tk->tag & TAG_STICK)
+ tk->tag -= TAG_STICK;
+ }
+ lst = lst->next;
+ }
+ return (first);
}
diff --git a/src/main.c b/src/main.c
index 1d30270..26db59f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -26,7 +26,7 @@ void token_debug(void *v)
t_token *t;
t= v;
- //printf("[%4d] (%s)\n", t->tag, t->content);
+ printf("[%4d] (%s)\n", t->tag, t->content);
}
int main(int argc, char **argv, char **envp)
@@ -44,7 +44,7 @@ int main(int argc, char **argv, char **envp)
ft_lstiter(lex_out, token_debug);
- t_ret *parser_out = parse(lex_out);
+ //t_ret *parser_out = parse(lex_out);
// printf("%s\n", ((t_token *)parser_out->ast->cmd_argv->data)->content);
// printf("%s\n", ((t_token *)parser_out->ast->redirs->data)->content);