parser V[chepa combien]
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/01/15 08:23:41 by nalebrun #+# #+# */
|
/* Created: 2025/01/15 08:23:41 by nalebrun #+# #+# */
|
||||||
/* Updated: 2025/01/15 08:23:41 by nalebrun ### ########.fr */
|
/* Updated: 2025/01/20 13:15:47 by nalebrun ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -16,6 +16,8 @@ void truncate_comment(char *str)
|
|||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
if (!str)
|
||||||
|
return ;
|
||||||
i = 0;
|
i = 0;
|
||||||
while (str[i])
|
while (str[i])
|
||||||
{
|
{
|
||||||
@@ -28,18 +30,18 @@ void truncate_comment(char *str)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
(void)ac;
|
|
||||||
|
|
||||||
t_node *lst;
|
t_node *lst;
|
||||||
|
char *expanded;
|
||||||
|
|
||||||
|
(void)ac;
|
||||||
truncate_comment(av[1]);
|
truncate_comment(av[1]);
|
||||||
lst = tokenize(av[1]);
|
expanded = expander(av[1]);
|
||||||
|
lst = tokenize(expanded);
|
||||||
if (!lst)
|
if (!lst)
|
||||||
return (1);
|
return (free(expanded), 1);
|
||||||
// debug_linked_list(lst);
|
ft_free(expanded);
|
||||||
|
debug_linked_list(lst, "Tokenized");
|
||||||
free_linked_list(lst);
|
free_linked_list(lst);
|
||||||
// free la list wesh
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* tokenizer_utils.c :+: :+: :+: */
|
/* linked_list.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/01/15 13:38:49 by nalebrun #+# #+# */
|
/* Created: 2025/01/15 13:38:49 by nalebrun #+# #+# */
|
||||||
/* Updated: 2025/01/15 13:38:49 by nalebrun ### ########.fr */
|
/* Updated: 2025/01/20 13:15:03 by nalebrun ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -64,14 +64,34 @@ int create_node_after(t_node *elem, char *val)
|
|||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int merge_with_next_node(t_node *node)
|
||||||
|
{
|
||||||
|
char *tmp_val;
|
||||||
|
t_node *tmp_next;
|
||||||
|
|
||||||
|
tmp_val = ft_strjoin(node->val, node->next->val);
|
||||||
|
if (!tmp_val)
|
||||||
|
return (0);
|
||||||
|
ft_free(&node->val);
|
||||||
|
node->val = tmp_val;
|
||||||
|
ft_free(&node->next->val);
|
||||||
|
tmp_next = node->next->next;
|
||||||
|
free(node->next);
|
||||||
|
node->next = tmp_next;
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
// have to be deleted after
|
// have to be deleted after
|
||||||
void debug_linked_list(t_node *head, char *msg)
|
void debug_linked_list(t_node *head, char *msg)
|
||||||
{
|
{
|
||||||
t_node *current = head;
|
t_node *current;
|
||||||
char *token;
|
char *token;
|
||||||
|
|
||||||
printf("----------------------------------------{%s} \n", msg);
|
current = head;
|
||||||
while (current != NULL) {
|
printf("----------------------------------------------------------{%s} \n",
|
||||||
|
msg);
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
if (current->token == OPERATOR)
|
if (current->token == OPERATOR)
|
||||||
token = ft_strdup("OPERATOR");
|
token = ft_strdup("OPERATOR");
|
||||||
else if (current->token == WORD)
|
else if (current->token == WORD)
|
||||||
@@ -84,5 +104,5 @@ void debug_linked_list(t_node *head, char *msg)
|
|||||||
free(token);
|
free(token);
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
printf("----------------------------------------\n\n");
|
printf("----------------------------------------------------------\n\n");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/01/15 13:27:57 by nalebrun #+# #+# */
|
/* Created: 2025/01/15 13:27:57 by nalebrun #+# #+# */
|
||||||
/* Updated: 2025/01/15 13:27:57 by nalebrun ### ########.fr */
|
/* Updated: 2025/01/20 13:15:25 by nalebrun ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -50,17 +50,46 @@ static void set_token(t_node *head)
|
|||||||
static int unstick_nodes(t_node *head)
|
static int unstick_nodes(t_node *head)
|
||||||
{
|
{
|
||||||
t_node *it;
|
t_node *it;
|
||||||
|
char *first_str;
|
||||||
|
char *second_str;
|
||||||
|
int copied;
|
||||||
|
|
||||||
it = head;
|
it = head;
|
||||||
while (it != NULL)
|
while (it != NULL)
|
||||||
{
|
{
|
||||||
if (is_sticked(it->val)) // undefined fct for the moment
|
if (is_sticked(it->val))
|
||||||
{
|
{
|
||||||
// if meta -> first part = jusqua plus meta
|
if (is_meta(it->val[0]))
|
||||||
// else -> first part = jusqua meta
|
first_str = copy_meta_xor(it->val, &copied, 0);
|
||||||
// secnd part = rest
|
else
|
||||||
// it->val = first part
|
first_str = copy_meta_xor(it->val, &copied, 1);
|
||||||
// create a new node after [create_node_after()] with the second part of the string
|
second_str = ft_substr(it->val, copied, ft_strlen(it->val)
|
||||||
|
- copied);
|
||||||
|
ft_free(&it->val);
|
||||||
|
it->val = ft_strdup(first_str);
|
||||||
|
create_node_after(it, second_str);
|
||||||
|
ft_free(&first_str);
|
||||||
|
ft_free(&second_str);
|
||||||
|
}
|
||||||
|
it = it->next;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int stick_quote_node(t_node *head)
|
||||||
|
{
|
||||||
|
t_node *it;
|
||||||
|
|
||||||
|
it = head;
|
||||||
|
while (it != NULL)
|
||||||
|
{
|
||||||
|
if (it->val[0] == '"')
|
||||||
|
{
|
||||||
|
while (it->next->val[0] != '"')
|
||||||
|
if (!merge_with_next_node(it))
|
||||||
|
return (0);
|
||||||
|
if (!merge_with_next_node(it))
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
it = it->next;
|
it = it->next;
|
||||||
}
|
}
|
||||||
@@ -74,13 +103,9 @@ t_node *tokenize(char *str)
|
|||||||
head = tokenize_base(str);
|
head = tokenize_base(str);
|
||||||
if (!head)
|
if (!head)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
debug_linked_list(head, "base tokenized");
|
|
||||||
if (!unstick_nodes(head))
|
if (!unstick_nodes(head))
|
||||||
return (NULL);
|
return (NULL);
|
||||||
debug_linked_list(head, "nodes unsticked");
|
stick_quote_node(head);
|
||||||
// stick_quote_node(head);
|
|
||||||
// debug_linked_list(head);
|
|
||||||
set_token(head);
|
set_token(head);
|
||||||
debug_linked_list(head, "token set");
|
|
||||||
return (head);
|
return (head);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* parser.h :+: :+: :+: */
|
/* tokenizer.h :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/01/15 13:30:12 by nalebrun #+# #+# */
|
/* Created: 2025/01/15 13:30:12 by nalebrun #+# #+# */
|
||||||
/* Updated: 2025/01/15 13:30:12 by nalebrun ### ########.fr */
|
/* Updated: 2025/01/20 13:15:34 by nalebrun ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -32,9 +32,14 @@ typedef struct s_node
|
|||||||
t_node *tokenize(char *str);
|
t_node *tokenize(char *str);
|
||||||
t_node *create_node(char *val, t_token token);
|
t_node *create_node(char *val, t_token token);
|
||||||
int add_node_back(t_node *head, char *val, t_token token);
|
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);
|
void free_linked_list(t_node *stack);
|
||||||
t_token get_token(char *str);
|
t_token get_token(char *str);
|
||||||
int create_node_after(t_node *elem, char *val);
|
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);
|
||||||
|
|
||||||
void debug_linked_list(t_node *head, char *msg);
|
void debug_linked_list(t_node *head, char *msg);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -4,11 +4,41 @@ t_token get_token(char *str)
|
|||||||
{
|
{
|
||||||
t_token token;
|
t_token token;
|
||||||
|
|
||||||
if (!strncmp(str, "&", 1) || !strncmp(str, "|", 1)
|
if (!strncmp(str, "&", 1) || !strncmp(str, "|", 1) || !strncmp(str, "(", 1)
|
||||||
|| !strncmp(str, "(", 1) || !strncmp(str, ")", 1)
|
|| !strncmp(str, ")", 1) || !strncmp(str, "<", 1) || !strncmp(str, ">",
|
||||||
|| !strncmp(str, "<", 1) || !strncmp(str, ">", 1))
|
1))
|
||||||
token = OPERATOR;
|
token = OPERATOR;
|
||||||
else
|
else
|
||||||
token = WORD;
|
token = WORD;
|
||||||
return (token);
|
return (token);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int is_meta(char c)
|
||||||
|
{
|
||||||
|
if (c == '&' || c == '|' || c == '<' || c == '>' || c == '(' || c == ')'
|
||||||
|
|| c == '"')
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_sticked(char *val)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int meta;
|
||||||
|
int unmeta;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
meta = 0;
|
||||||
|
unmeta = 0;
|
||||||
|
while (val[i])
|
||||||
|
{
|
||||||
|
if (is_meta(val[i]))
|
||||||
|
meta = 1;
|
||||||
|
if (!is_meta(val[i]))
|
||||||
|
unmeta = 1;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
if (meta && unmeta)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|||||||
23
tests/tokenizer/unstick_node_utils.c
Normal file
23
tests/tokenizer/unstick_node_utils.c
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#include "tokenizer.h"
|
||||||
|
|
||||||
|
char *copy_meta_xor(char *val, int *copied, int rev)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
char *out;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (is_meta(val[i]) ^ rev)
|
||||||
|
{
|
||||||
|
if (!rev && val[i] != val[0])
|
||||||
|
break ;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
*copied = i;
|
||||||
|
out = malloc(i + 1);
|
||||||
|
j = -1;
|
||||||
|
while (++j < i)
|
||||||
|
out[j] = val[j];
|
||||||
|
out[i] = 0;
|
||||||
|
return (out);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user