From 3bb997212b3a0d140a34932f9deff52793673d49 Mon Sep 17 00:00:00 2001 From: Charles Cabergs Date: Wed, 16 Sep 2020 16:40:22 +0200 Subject: Added g_state to store global variables, Refactoring tok_assign_str --- src/lexer/lexer.c | 23 +----------- src/lexer/trim.c | 110 +++++++++++++++++++++++++++--------------------------- src/lexer/utils.c | 87 ++++++++++++++++++------------------------ 3 files changed, 93 insertions(+), 127 deletions(-) (limited to 'src/lexer') diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index a1d7469..26355fe 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/09/14 16:33:58 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:37:49 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,7 +53,7 @@ int tok_len(char *input) } if (input[i] == '(' || input[i] == ')') return (i + 1); - if (lexer_sep(input[i])) // fucked on & alone + if (lexer_sep(input[i])) { if (input[i] == input[i + 1]) i++; @@ -114,22 +114,3 @@ int lexer(char *input, t_tok_lst **out) return status; } - -/* int check_input_out(char *input) */ -/* { */ -/* int i; */ -/* int j; */ -/* */ -/* i = 0; */ -/* while(input[i] != '\0') */ -/* { */ -/* j = 0; */ -/* j += len_until_sep(&input[i]); */ -/* if (j != 0) */ -/* return(j); */ -/* i += j; */ -/* j = check_input(&input[i]); */ -/* return(j); */ -/* } */ -/* return(0); */ -/* } */ diff --git a/src/lexer/trim.c b/src/lexer/trim.c index 0bdacac..34e162a 100644 --- a/src/lexer/trim.c +++ b/src/lexer/trim.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/07/16 08:18:36 by nahaddac #+# #+# */ -/* Updated: 2020/09/15 18:31:56 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:39:37 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,83 +16,83 @@ void del_space(char *str) { - int i; + int i; - i = ft_strlen(str); - if (ft_isblank(str[i - 1])) - { - i -= 1; - while (ft_isblank(str[i])) - { - if (str[i - 1] == '\\') - break ; - i--; - } + i = ft_strlen(str); + if (ft_isblank(str[i - 1])) + { + i -= 1; + while (ft_isblank(str[i])) + { + if (str[i - 1] == '\\') + break ; + i--; + } str[i + 1] = '\0'; - } + } } int del_quote(char *str) { - size_t i; - size_t quote_counter; + size_t i; + size_t quote_counter; - i = 0; - quote_counter = 1; - if (str[0] == '\'') + i = 0; + quote_counter = 1; + if (str[0] == '\'') { - while (str[i++] != '\0') - { - if (str[i] == '\'') - { - quote_counter++; - break ; - } - } + while (str[i++] != '\0') + { + if (str[i] == '\'') + { + quote_counter++; + break ; + } + } } - else if (str[0] == '"') + else if (str[0] == '"') { - while (str[i++] != '\0') - { - if (str[i] == '\\') - i += 2; - if (str[i] == '"') - { - quote_counter++; - break ; - } - } + while (str[i++] != '\0') + { + if (str[i] == '\\') + i += 2; + if (str[i] == '"') + { + quote_counter++; + break ; + } + } } - if (quote_counter % 2 == 1) + if (quote_counter % 2 == 1) { errorf("unexpected EOF while looking for matching `%c'\n", str[0]); - return (1); + return (1); } str[i] = '\0'; ft_memmove(str, str + 1, ft_strlen(str + 1) + 1); return (0); } -int lexer_trim(t_tok_lst *tokens) +int lexer_trim(t_tok_lst *tokens) { - int status; + int status; - while (tokens != NULL) - { - if (tokens->tag & (TAG_STR_DOUBLE | TAG_STR_SINGLE)) - { - if ((status = del_quote(tokens->content)) != 0) + while (tokens != NULL) + { + if (tokens->tag & (TAG_STR_DOUBLE | TAG_STR_SINGLE)) + { + if ((status = del_quote(tokens->content)) != 0) return (status); if (tokens->next == NULL) tokens->tag &= ~TAG_STICK; - } - else - { - del_space(tokens->content); - if (tokens->next == NULL) + } + else + { + del_space(tokens->content); + if (tokens->next == NULL) tokens->tag &= ~TAG_STICK; - } - tokens = tokens->next; - } - return (0); + } + tokens = tokens->next; + } + return (0); } diff --git a/src/lexer/utils.c b/src/lexer/utils.c index b4846d6..66b2af7 100644 --- a/src/lexer/utils.c +++ b/src/lexer/utils.c @@ -6,7 +6,7 @@ /* By: nahaddac +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/07/16 08:18:15 by nahaddac #+# #+# */ -/* Updated: 2020/09/14 15:16:35 by charles ### ########.fr */ +/* Updated: 2020/09/16 16:37:20 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,25 +15,25 @@ // return token tag corresponding to string id enum e_tok tok_assign_tag(char *content) { - if (content[0] == ';') - return (TAG_END); - if (ft_strncmp(content, "&&", 2) == 0) - return (TAG_AND); - if (ft_strncmp(content, "||", 2) == 0) - return (TAG_OR); - if(content[0] == '|') - return (TAG_PIPE); - if (content[0] == '>') - return (TAG_REDIR_OUT); - if (content[0] == '<') - return (TAG_REDIR_IN); - if (ft_strncmp(content, ">>", 2) == 0) - return (TAG_REDIR_APPEND); - if (content[0] == '(') - return (TAG_PARENT_OPEN); - if (content[0] == ')') - return (TAG_PARENT_CLOSE); - return (0); + if (content[0] == ';') + return (TAG_END); + if (ft_strncmp(content, "&&", 2) == 0) + return (TAG_AND); + if (ft_strncmp(content, "||", 2) == 0) + return (TAG_OR); + if(content[0] == '|') + return (TAG_PIPE); + if (content[0] == '>') + return (TAG_REDIR_OUT); + if (content[0] == '<') + return (TAG_REDIR_IN); + if (ft_strncmp(content, ">>", 2) == 0) + return (TAG_REDIR_APPEND); + if (content[0] == '(') + return (TAG_PARENT_OPEN); + if (content[0] == ')') + return (TAG_PARENT_CLOSE); + return (0); } enum e_tok tok_assign_stick(t_tok_lst *tok) @@ -49,35 +49,20 @@ enum e_tok tok_assign_stick(t_tok_lst *tok) enum e_tok tok_assign_str(t_tok_lst *tok) { - int i; + char *found; - // could use strchr to search ' or " - i = 0; - while (tok->content[i] != '\0') - { - if (tok->content[i] == '\'') - { - tok->tag = TAG_STR_SINGLE; - return (tok_assign_stick(tok)); - } - if (tok->content[i] == '"') - { - tok->tag = TAG_STR_DOUBLE; - return (tok_assign_stick(tok)); - } - else - { - tok->tag = TAG_STR; - return (tok_assign_stick(tok)); - } - i++; - } - return (0); + found = ft_strpbrk(tok->content, "'\""); + if (found == NULL) + tok->tag = TAG_STR; + else if (*found == '\'') + tok->tag = TAG_STR_SINGLE; + else if (*found == '"') + tok->tag = TAG_STR_DOUBLE; + return (tok_assign_stick(tok)); } // check is char is separator -// & alone could be considered a separator int lexer_sep(char c) { return (ft_strchr(";&|><()", c) != NULL); @@ -94,14 +79,14 @@ int quote_len(char *input, int i) char quote_type; quote_type = input[i]; - i++; - while (input[i] != quote_type && input[i] != '\0') - { - if (quote_type == '"' && input[i] == '\\') + i++; + while (input[i] != quote_type && input[i] != '\0') + { + if (quote_type == '"' && input[i] == '\\') i++; - i++; - } + i++; + } while (ft_isblank(input[i + 1])) i++; - return (i + 1); + return (i + 1); } -- cgit