token_type: assign token_type to worddesc during wordsplit

Also added some tests for that part.
This commit is contained in:
Khaïs COLIN 2025-04-09 13:25:58 +02:00
parent 4d6a64bf6a
commit 3ec90f7770
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
6 changed files with 254 additions and 11 deletions

View file

@ -82,6 +82,7 @@ srcs = \
src/parser/wordlist/wordlist_idx.c \
src/parser/wordlist/wordlist_quicksort.c \
src/parser/wordlist/wordlist_utils.c \
src/parser/wordsplit/get_token_type.c \
src/parser/wordsplit/rule_utils.c \
src/parser/wordsplit/tokenizing_1_5.c \
src/parser/wordsplit/tokenizing_6_10.c \

View file

@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_token_type.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/09 15:52/43 by khais #+# #+# */
/* Updated: 2025/04/09 15:52:43 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "get_token_type.h"
t_token_type get_token_type(t_token_build *builder)
{
t_token_type ret;
ret = WORD_TOKEN;
if (builder->currently_in_operator)
{
if (ft_strcmp("(", builder->cur_token->buffer) == 0)
ret = OPEN_PARENTH_TOKEN;
if (ft_strcmp(")", builder->cur_token->buffer) == 0)
ret = CLOSE_PARENTH_TOKEN;
if (ft_strcmp("|", builder->cur_token->buffer) == 0)
ret = PIPE_TOKEN;
if (ft_strcmp("||", builder->cur_token->buffer) == 0)
ret = OR_TOKEN;
if (ft_strcmp("&&", builder->cur_token->buffer) == 0)
ret = AND_TOKEN;
if (ft_strcmp(">", builder->cur_token->buffer) == 0)
ret = OUT_TRUNC_REDIR_TOKEN;
if (ft_strcmp(">>", builder->cur_token->buffer) == 0)
ret = OUT_APPEND_REDIR_TOKEN;
if (ft_strcmp("<", builder->cur_token->buffer) == 0)
ret = IN_REDIR_TOKEN;
if (ft_strcmp("<<", builder->cur_token->buffer) == 0)
ret = HERE_DOC_REDIR_TOKEN;
}
return (ret);
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_token_type.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/09 13:34:15 by khais #+# #+# */
/* Updated: 2025/04/09 13:34:55 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GET_TOKEN_TYPE_H
# define GET_TOKEN_TYPE_H
# include "../worddesc/worddesc.h"
# include "wordsplit.h"
t_token_type get_token_type(t_token_build *builder);
#endif // GET_TOKEN_TYPE_H

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/20 14:00:45 by khais #+# #+# */
/* Updated: 2025/03/06 15:18:41 by khais ### ########.fr */
/* Updated: 2025/04/09 13:26:33 by khais ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,20 +6,21 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/20 14:02:29 by khais #+# #+# */
/* Updated: 2025/04/08 16:29:49 by khais ### ########.fr */
/* Updated: 2025/04/09 13:34:02 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "wordsplit.h"
#include <stdlib.h>
#include "get_token_type.h"
void delimit(t_token_build *builder)
{
t_token_type type;
type = WORD_TOKEN;
if (builder->cur_token == NULL)
return ;
type = get_token_type(builder);
builder->wordlist = wordlist_push(builder->wordlist,
worddesc_create(builder->cur_token->buffer,
builder->cur_flags, builder->cur_marker->buffer, type));

177
test.sh
View file

@ -607,4 +607,181 @@ expecting <<EOF
╰─ redirections = (empty redir list)
EOF
when_run <<EOF "token type attribution is correct"
.debug
.exec
()|||&&>>><<<
EOF
expecting <<EOF
[dbg: 1]
[exec: 0]
╰─ t_simple_cmd
├─ line = 0
├─ t_wordlist
│ ├─ t_worddesc
│ │ ├─ word = [(]
│ │ ├─ marker = [ ]
│ │ ├─ flags = 0
│ │ ╰─ t_token_type = OPEN_PARENTH_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = [)]
│ │ ├─ marker = [ ]
│ │ ├─ flags = 0
│ │ ╰─ t_token_type = CLOSE_PARENTH_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = [||]
│ │ ├─ marker = [ ]
│ │ ├─ flags = 0
│ │ ╰─ t_token_type = OR_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = [|]
│ │ ├─ marker = [ ]
│ │ ├─ flags = 0
│ │ ╰─ t_token_type = PIPE_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = [&&]
│ │ ├─ marker = [ ]
│ │ ├─ flags = 0
│ │ ╰─ t_token_type = AND_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = [>>]
│ │ ├─ marker = [ ]
│ │ ├─ flags = 0
│ │ ╰─ t_token_type = OUT_APPEND_REDIR_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = [>]
│ │ ├─ marker = [ ]
│ │ ├─ flags = 0
│ │ ╰─ t_token_type = OUT_TRUNC_REDIR_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = [<<]
│ │ ├─ marker = [ ]
│ │ ├─ flags = 0
│ │ ╰─ t_token_type = HERE_DOC_REDIR_TOKEN
│ ╰─ t_worddesc
│ ├─ word = [<]
│ ├─ marker = [ ]
│ ├─ flags = 0
│ ╰─ t_token_type = IN_REDIR_TOKEN
╰─ redirections = (empty redir list)
EOF
when_run <<EOF "token type attribution is correct when tokens are double-quoted"
.debug
.exec
"(" ")" "|" "||" "&&" ">" ">>" "<" "<<"
EOF
expecting <<EOF
[dbg: 1]
[exec: 0]
╰─ t_simple_cmd
├─ line = 0
├─ t_wordlist
│ ├─ t_worddesc
│ │ ├─ word = ["("]
│ │ ├─ marker = [ " ]
│ │ ├─ flags = 34
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = [")"]
│ │ ├─ marker = [ " ]
│ │ ├─ flags = 34
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = ["|"]
│ │ ├─ marker = [ " ]
│ │ ├─ flags = 34
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = ["||"]
│ │ ├─ marker = [ "" ]
│ │ ├─ flags = 34
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = ["&&"]
│ │ ├─ marker = [ "" ]
│ │ ├─ flags = 34
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = [">"]
│ │ ├─ marker = [ " ]
│ │ ├─ flags = 34
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = [">>"]
│ │ ├─ marker = [ "" ]
│ │ ├─ flags = 34
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = ["<"]
│ │ ├─ marker = [ " ]
│ │ ├─ flags = 34
│ │ ╰─ t_token_type = WORD_TOKEN
│ ╰─ t_worddesc
│ ├─ word = ["<<"]
│ ├─ marker = [ "" ]
│ ├─ flags = 34
│ ╰─ t_token_type = WORD_TOKEN
╰─ redirections = (empty redir list)
EOF
when_run <<EOF "token type attribution is correct when tokens are single-quoted"
.debug
.exec
'(' ')' '|' '||' '&&' '>' '>>' '<' '<<'
EOF
expecting <<EOF
[dbg: 1]
[exec: 0]
╰─ t_simple_cmd
├─ line = 0
├─ t_wordlist
│ ├─ t_worddesc
│ │ ├─ word = ['(']
│ │ ├─ marker = [ ' ]
│ │ ├─ flags = 2
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = [')']
│ │ ├─ marker = [ ' ]
│ │ ├─ flags = 2
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = ['|']
│ │ ├─ marker = [ ' ]
│ │ ├─ flags = 2
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = ['||']
│ │ ├─ marker = [ '' ]
│ │ ├─ flags = 2
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = ['&&']
│ │ ├─ marker = [ '' ]
│ │ ├─ flags = 2
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = ['>']
│ │ ├─ marker = [ ' ]
│ │ ├─ flags = 2
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = ['>>']
│ │ ├─ marker = [ '' ]
│ │ ├─ flags = 2
│ │ ╰─ t_token_type = WORD_TOKEN
│ ├─ t_worddesc
│ │ ├─ word = ['<']
│ │ ├─ marker = [ ' ]
│ │ ├─ flags = 2
│ │ ╰─ t_token_type = WORD_TOKEN
│ ╰─ t_worddesc
│ ├─ word = ['<<']
│ ├─ marker = [ '' ]
│ ├─ flags = 2
│ ╰─ t_token_type = WORD_TOKEN
╰─ redirections = (empty redir list)
EOF
finalize