tokenization but simple and double quotes works hehe
This commit is contained in:
@@ -103,23 +103,36 @@ void debug_linked_list(t_node *head, char *msg)
|
||||
token = ft_strdup(" UNSET");
|
||||
else
|
||||
token = ft_strdup(" ???");
|
||||
|
||||
|
||||
// set vals for pressision token
|
||||
if (current->pressision == AND)
|
||||
if (current->pressision == COMMAND)
|
||||
pres = ft_strdup("COMMAND");
|
||||
else if (current->pressision == UNDEFINED)
|
||||
pres = ft_strdup("UNDEF ");
|
||||
else if (current->pressision == AND)
|
||||
pres = ft_strdup("AND ");
|
||||
if (current->pressision == OR)
|
||||
else if (current->pressision == OR)
|
||||
pres = ft_strdup("OR ");
|
||||
if (current->pressision == PIPE)
|
||||
else if (current->pressision == PIPE)
|
||||
pres = ft_strdup("PIPE ");
|
||||
if (current->pressision == SUBSH_S)
|
||||
else if (current->pressision == SUBSH_S)
|
||||
pres = ft_strdup("SUBSH_S");
|
||||
if (current->pressision == SUBSH_E)
|
||||
else if (current->pressision == SUBSH_E)
|
||||
pres = ft_strdup("SUBSH_E");
|
||||
else if (current->pressision == RED_L)
|
||||
pres = ft_strdup("RED_L ");
|
||||
else if (current->pressision == RED_R)
|
||||
pres = ft_strdup("RED_R ");
|
||||
else if (current->pressision == HEREDOC)
|
||||
pres = ft_strdup("HEREDOC");
|
||||
else if (current->pressision == D_RED_R)
|
||||
pres = ft_strdup("D_RED_R");
|
||||
else
|
||||
pres = ft_strdup("??? ");
|
||||
|
||||
printf("| Node - TOKEN: %s.%s -> val: |%s|\n", token, pres, current->val);
|
||||
free(token);
|
||||
free(pres);
|
||||
current = current->next;
|
||||
}
|
||||
printf("----------------------------------------------------------\n\n");
|
||||
|
||||
56
tests/tokenizer/token_and_pres.c
Normal file
56
tests/tokenizer/token_and_pres.c
Normal file
@@ -0,0 +1,56 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* token_and_pres.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/21 08:54:32 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/21 08:54:32 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "./tokenizer.h"
|
||||
|
||||
static t_pres get_operator(char *s)
|
||||
{
|
||||
if (s[0] == '&' && s[1] && s[1] == '&')
|
||||
return (AND);
|
||||
else if (s[0] == '|' && s[1] && s[1] == '|')
|
||||
return (OR);
|
||||
else if (s[0] == '|' && !s[1])
|
||||
return (PIPE);
|
||||
else if (s[0] == '(' && !s[1])
|
||||
return (SUBSH_S);
|
||||
else if (s[0] == ')' && !s[1])
|
||||
return (SUBSH_E);
|
||||
else if (s[0] == '<' && s[1] && s[1] == '<')
|
||||
return (HEREDOC);
|
||||
else if (s[0] == '>' && s[1] && s[1] == '>')
|
||||
return (D_RED_R);
|
||||
else if (s[0] == '<' && !s[1])
|
||||
return (RED_L);
|
||||
else if (s[0] == '>' && !s[1])
|
||||
return (RED_R);
|
||||
return (UNDEFINED);
|
||||
}
|
||||
|
||||
t_token get_token(char *str)
|
||||
{
|
||||
t_token token;
|
||||
|
||||
if (!ft_strncmp(str, "&", 1) || !ft_strncmp(str, "|", 1)
|
||||
|| !ft_strncmp(str, "(", 1) || !ft_strncmp(str, ")", 1)
|
||||
|| !ft_strncmp(str, "<", 1) || !ft_strncmp(str, ">", 1))
|
||||
token = OPERATOR;
|
||||
else
|
||||
token = WORD;
|
||||
return (token);
|
||||
}
|
||||
|
||||
t_pres get_pressision(char *s, t_token token)
|
||||
{
|
||||
if (token == OPERATOR)
|
||||
return (get_operator(s));
|
||||
return (COMMAND);
|
||||
}
|
||||
@@ -18,7 +18,7 @@ static t_node *tokenize_base(char *str)
|
||||
t_node *head;
|
||||
char **tab;
|
||||
|
||||
tab = ft_split(str, " \t\n");
|
||||
tab = ft_split_keep(str, " \t\n");
|
||||
if (!tab)
|
||||
return (NULL);
|
||||
head = create_node(tab[0], 0);
|
||||
@@ -43,6 +43,7 @@ static void set_token(t_node *head)
|
||||
while (it != NULL)
|
||||
{
|
||||
it->token = get_token(it->val);
|
||||
it->pressision = get_pressision(it->val, it->token);
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
@@ -76,16 +77,17 @@ static int unstick_nodes(t_node *head)
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int stick_quote_node(t_node *head)
|
||||
static int stick_quote_node(t_node *head, char q)
|
||||
{
|
||||
t_node *it;
|
||||
|
||||
it = head;
|
||||
while (it != NULL)
|
||||
{
|
||||
if (it->val[0] == '"')
|
||||
if (it->val[0] == q && !ft_strchr(&it->val[1], q)
|
||||
&& find_quote_node(it->next, q))
|
||||
{
|
||||
while (it->next->val[0] != '"')
|
||||
while (it->next->val[0] != q)
|
||||
if (!merge_with_next_node(it))
|
||||
return (0);
|
||||
if (!merge_with_next_node(it))
|
||||
@@ -103,9 +105,16 @@ t_node *tokenize(char *str)
|
||||
head = tokenize_base(str);
|
||||
if (!head)
|
||||
return (NULL);
|
||||
debug_linked_list(head, "Base_cut");
|
||||
if (!trim_nodes(head))
|
||||
return (NULL);
|
||||
debug_linked_list(head, "Trimed");
|
||||
if (!unstick_nodes(head))
|
||||
return (NULL);
|
||||
stick_quote_node(head);
|
||||
debug_linked_list(head, "Nodes Unsticked");
|
||||
stick_quote_node(head, 39);
|
||||
stick_quote_node(head, '"');
|
||||
debug_linked_list(head, "Quote Sticked");
|
||||
set_token(head);
|
||||
return (head);
|
||||
}
|
||||
|
||||
@@ -24,11 +24,17 @@ typedef enum e_token
|
||||
|
||||
typedef enum e_pres
|
||||
{
|
||||
UNDEFINED,
|
||||
COMMAND,
|
||||
AND,
|
||||
OR,
|
||||
PIPE,
|
||||
SUBSH_S,
|
||||
SUBSH_E
|
||||
SUBSH_E,
|
||||
RED_L,
|
||||
RED_R,
|
||||
HEREDOC,
|
||||
D_RED_R
|
||||
} t_pres;
|
||||
|
||||
typedef struct s_node
|
||||
@@ -45,11 +51,13 @@ int add_node_back(t_node *head, char *val, t_token token);
|
||||
int merge_with_next_node(t_node *node);
|
||||
void free_linked_list(t_node *stack);
|
||||
t_token get_token(char *str);
|
||||
t_pres get_pressision(char *str, t_token token);
|
||||
int create_node_after(t_node *elem, char *val);
|
||||
char *copy_meta_xor(char *val, int *copied, int rev);
|
||||
int is_meta(char c);
|
||||
int is_sticked(char *val);
|
||||
|
||||
int trim_nodes(t_node *head);
|
||||
void debug_linked_list(t_node *head, char *msg);
|
||||
int find_quote_node(t_node *head, char q);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,22 +1,9 @@
|
||||
#include "tokenizer.h"
|
||||
|
||||
t_token get_token(char *str)
|
||||
{
|
||||
t_token token;
|
||||
|
||||
if (!strncmp(str, "&", 1) || !strncmp(str, "|", 1) || !strncmp(str, "(", 1)
|
||||
|| !strncmp(str, ")", 1) || !strncmp(str, "<", 1) || !strncmp(str, ">",
|
||||
1))
|
||||
token = OPERATOR;
|
||||
else
|
||||
token = WORD;
|
||||
return (token);
|
||||
}
|
||||
|
||||
int is_meta(char c)
|
||||
{
|
||||
if (c == '&' || c == '|' || c == '<' || c == '>' || c == '(' || c == ')'
|
||||
|| c == '"')
|
||||
|| c == '"' || c == 39)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
@@ -42,3 +29,63 @@ int is_sticked(char *val)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_str_count(char *s, char c)
|
||||
{
|
||||
int i;
|
||||
int count;
|
||||
|
||||
i = -1;
|
||||
count = 0;
|
||||
while (s[++i])
|
||||
if (s[i] == c)
|
||||
count++;
|
||||
return (count);
|
||||
}
|
||||
|
||||
|
||||
int trim_nodes(t_node *head)
|
||||
{
|
||||
t_node *it;
|
||||
char *tmp;
|
||||
char in_quote;
|
||||
|
||||
it = head;
|
||||
in_quote = 0;
|
||||
while (it != NULL)
|
||||
{
|
||||
if (ft_str_count(it->val, 39) == 1
|
||||
|| ft_str_count(it->val, '"') == 1)
|
||||
{
|
||||
if (!in_quote)
|
||||
in_quote = it->val[0];
|
||||
else if (it->val[0] == in_quote)
|
||||
in_quote = 0;
|
||||
}
|
||||
if (!in_quote)
|
||||
{
|
||||
tmp = ft_strtrim(it->val, " \t\n");
|
||||
if (!tmp)
|
||||
return (0);
|
||||
free(it->val);
|
||||
it->val = tmp;
|
||||
}
|
||||
it = it->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int find_quote_node(t_node *head, char q)
|
||||
{
|
||||
t_node *it;
|
||||
|
||||
it = head;
|
||||
while (it != NULL)
|
||||
{
|
||||
if (it->val[0] == q)
|
||||
return (1);
|
||||
it = it->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,7 @@ char *copy_meta_xor(char *val, int *copied, int rev)
|
||||
|
||||
i = 0;
|
||||
while (is_meta(val[i]) ^ rev)
|
||||
{
|
||||
if (!rev && val[i] != val[0])
|
||||
break ;
|
||||
i++;
|
||||
}
|
||||
*copied = i;
|
||||
out = malloc(i + 1);
|
||||
j = -1;
|
||||
|
||||
Reference in New Issue
Block a user