From 579a26f5593039ffbbd1a81e45ecf0ef8797cb5d Mon Sep 17 00:00:00 2001 From: nass1pro Date: Tue, 9 Jun 2020 19:48:34 +0200 Subject: add lexer add single quote --- src/.DS_Store | Bin 0 -> 6148 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/.DS_Store (limited to 'src') diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000..68abd32 Binary files /dev/null and b/src/.DS_Store differ -- cgit From d971bd8d16608f316396aba7a579d0b1f1af5aeb Mon Sep 17 00:00:00 2001 From: nass1pro Date: Fri, 12 Jun 2020 13:52:58 +0200 Subject: Added e_token enum --- src/lexer/.DS_Store | Bin 0 -> 6148 bytes src/lexer/lexer.c | 178 ++++++++++++++++++++++++++++++++++++++++++++++++ src/lexer/lexer_utils.c | 89 ++++++++++++++++++++++++ 3 files changed, 267 insertions(+) create mode 100644 src/lexer/.DS_Store create mode 100644 src/lexer/lexer.c create mode 100644 src/lexer/lexer_utils.c (limited to 'src') diff --git a/src/lexer/.DS_Store b/src/lexer/.DS_Store new file mode 100644 index 0000000..fcaf8ef Binary files /dev/null and b/src/lexer/.DS_Store differ diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c new file mode 100644 index 0000000..994ea55 --- /dev/null +++ b/src/lexer/lexer.c @@ -0,0 +1,178 @@ + +#include "lexer.h" + +int len_is_not_sep(char *input) +{ + int i; + + i = -1; + while(input[++i]) + { + if (lexer_sep(input[i])) + { + if (input[i + 1] == ' ') + while(input[++i] == ' ') + ; + return(i); + } + if (input[i] == '\'' || input[i] == '"') + return(i); + if (input[i] == ' ') + { + while(input[++i] == ' ') + ; + return (i); + } + } + return(i); +} + +int check_input(char *input) +{ + int i; + + i = 0; + if (lexer_sep(input[i])) + { + while(input[i] == input[i + 1]) + i++; + i += lexe_space(&input[i + 1]); + return (i + 1); + } + if (input[i] == 39 || input[i] == '"') + return(lexer_verif_entre_cote(input, i)); + if (input[i] == ' ') + { + while(input[++i] == ' ') + ; + return (i); + } + return(len_is_not_sep(&input[i])); +} + + +int check_input_out(char *input) +{ + int i; + int j; + + i = 0; + while(input[i] != '\0') + { + j = 0; + j += len_is_not_sep(&input[i]); + if (j != 0) + return(j); + i += j; + j = check_input(&input[i]); + return(j); + } + return(0); +} + +t_token *lexer_lst_token_str(char *input, int i, int j) +{ + t_token *lst_token; + + if (!(lst_token = malloc(sizeof(t_token) * 1))) + return (NULL); + lst_token->token = 0; + lst_token->value = NULL; + if (!(lst_token->value = malloc(sizeof(char) * j + 1))) + return(0); + if (!(ft_strlcpy(lst_token->value, &input[i], j + 1))) + { + free(lst_token); + return(0); + } + //printf("%s-\n", lst_token->value); + return (lst_token); +} + +enum e_token_tag token_verif_stick(t_token *lst_token) +{ + int i; + + i = ft_strlen(lst_token->value); + + if (lst_token->value[i - 1] == ' ') + return(lst_token->token); + return(lst_token->token | LTAG_STICK); +} + +enum e_token_tag token_str_or_cote(t_token *lst_token) +{ + int i; + + i = 0; + while(lst_token->value[i] != '\0') + { + if(lst_token->value[i] == '\'') + { + lst_token->token = LTAG_STR_SINGLE; + return(token_verif_stick(lst_token)); + } + if(lst_token->value[i] == '"') + { + lst_token->token = LTAG_STR_DOUBLE; + return(token_verif_stick(lst_token)); + } + else + { + lst_token->token = LTAG_STR; + return(token_verif_stick(lst_token)); + } + i++; + } + return(0); +} + +t_token *push_token_enum_and_trim(t_token *lst_token) +{ + enum e_token_tag tk; + + tk = ret_token(lst_token->value, 0); + if (tk == 0) + { + lst_token->token = token_str_or_cote(lst_token); + } + printf("%s-, %d\n",lst_token->value, (int)lst_token->token); + return (lst_token); +} + +static t_ftlst *create_token_list(char *input, t_ftlst **lst) +{ + t_token *lst_token; + t_ftlst *new; + int i; + int j; + + i = 0; + while (i < (int)ft_strlen(input)) + { + 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); + new = ft_lstnew((void *) lst_token); + ft_lstpush_back(lst, new); + i += j; + } + return (*lst); +} + +t_ftlst *lexer(char *input) +{ + t_ftlst **lst; + int i; + + if (!input) + return (0); + lst = malloc(sizeof(t_ftlst *) * 1); + if (!lst) + return(0); + *lst = create_token_list(input, lst); + i = ft_lstsize(*lst); + free(lst); + return (0); +} diff --git a/src/lexer/lexer_utils.c b/src/lexer/lexer_utils.c new file mode 100644 index 0000000..72d8288 --- /dev/null +++ b/src/lexer/lexer_utils.c @@ -0,0 +1,89 @@ + +#include "lexer.h" + + + +enum e_token_tag ret_token_sep_redir_append(char *input, int i) +{ + if (input[i + 1] == '>') + return(LTAG_REDIR_APPEND); + return (LTAG_REDIR_OUT); + +} + +enum e_token_tag ret_token(char *input, int i) +{ + if (input[i] == ';') + return(LTAG_AND); + if (input[i] == '&') + return(LTAG_END); + if (input[i] == '|' && input[i + 1] == '|') + return(LTAG_OR); + if(input[i] == '|') + return(LTAG_PIPE); + if (input[i] == '>') + return(ret_token_sep_redir_append(input,i)); + if (input[i] == '<') + return(LTAG_REDIR_IN); + if (input[i] == '(') + return(LTAG_PARENT_OPEN); + if (input[i] == ')') + return(LTAG_PARENT_CLOSE); + return(0); + +} + + +int lexer_sep(char input) +{ + char *sep; + int i; + + i = 0; + sep = ";&|><()"; + while(sep[i] != '\0') + { + if(sep[i] == input) + return(1); + i++; + } + return (0); +} + +int lexe_space(char *input) +{ + int i; + + i=0; + while(input[i] == ' ') + i++; + return(i); +} + +static int lex_verif_simple_cote(char *input, int i) +{ + i++; + while(input[i] != '\0') + { + ++i; + if(input[i] == '\'') + break; + } + if (input[i + 1] == ' ') + while(input[i + 1] == ' ') + i++; + return(i + 1); +} + +int lexer_verif_entre_cote(char *input, int i) +{ + if(input[i] == '\'') + return(lex_verif_simple_cote(input, i)); + i++; + while(input[i] != '"' && (input[i] != '\0')) + ++i; + if (input[i + 1] == ' ') + while(input[i + 1] == ' ') + i++; + return(i + 1); +} -- cgit From 72ec3b700e8e3142eb9e79098a357f01a6085589 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 13 Jun 2020 10:34:30 +0200 Subject: Fixing multiple definition of lexer --- src/glob.c | 1 + src/lexer/lexer.c | 6 +++--- src/lexer/main.c | 29 +++++++++++++++++++++++++++++ src/parse/lexer.c | 16 ---------------- src/parse/parse.c | 16 ---------------- 5 files changed, 33 insertions(+), 35 deletions(-) create mode 100644 src/lexer/main.c delete mode 100644 src/parse/lexer.c delete mode 100644 src/parse/parse.c (limited to 'src') diff --git a/src/glob.c b/src/glob.c index 2d544da..d1d1bdf 100644 --- a/src/glob.c +++ b/src/glob.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "ms_glob.h" +#include /* ** \brief Match vector start size diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 994ea55..185ff83 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -163,8 +163,8 @@ static t_ftlst *create_token_list(char *input, t_ftlst **lst) t_ftlst *lexer(char *input) { - t_ftlst **lst; - int i; + t_ftlst **lst; + /* int i; */ if (!input) return (0); @@ -172,7 +172,7 @@ t_ftlst *lexer(char *input) if (!lst) return(0); *lst = create_token_list(input, lst); - i = ft_lstsize(*lst); + /* i = ft_lstsize(*lst); */ free(lst); return (0); } diff --git a/src/lexer/main.c b/src/lexer/main.c new file mode 100644 index 0000000..753969d --- /dev/null +++ b/src/lexer/main.c @@ -0,0 +1,29 @@ + +#include "lexer.h" + +/*int main(int argc, char **argv) +{ + char *input; + + if (!(input = malloc(sizeof(char) * ft_strlen(argv[1]) + 1))) + return(0); + ft_strlcpy(input, argv[1], ft_strlen(argv[1]) + 1); + + lexer(input); + free(input); + exit(0); + return (0); +}*/ +/* +#include +int main(int argc, char **argv, char **envp) +{ + printf("ARGV:\n"); + for (int i = 0; i < argc; i++) + printf("[%d] %s\n", i, argv[i]); + printf("\nENV:\n"); + for (int i = 0; envp[i] != NULL && i < 10; i++) + printf("[%d] %s\n", i, envp[i]); + return 0; +} +*/ diff --git a/src/parse/lexer.c b/src/parse/lexer.c deleted file mode 100644 index 74a3bd4..0000000 --- a/src/parse/lexer.c +++ /dev/null @@ -1,16 +0,0 @@ -/* -** \file lexer.c -** \brief Lexer -*/ - -#include "minishell.h" - -char **lexer(char *input) -{ - char **out_lex; - - (void)out_lex; - if (!input) - return (NULL); - return (NULL); -} diff --git a/src/parse/parse.c b/src/parse/parse.c deleted file mode 100644 index ed1aa28..0000000 --- a/src/parse/parse.c +++ /dev/null @@ -1,16 +0,0 @@ -/* -** \file parse.c -** \brief Parser -*/ - -#include "parse.h" - -t_ast *parse(char *input) -{ -/* char **out_lex; */ -/* if (!(out_lex = lexer(input))) */ -/* return (NULL); */ - - (void)input; - return (NULL); -} -- cgit