From 7cb3582b1ae907c581cba22ca972806c2af9168b Mon Sep 17 00:00:00 2001 From: maix0 Date: Sat, 5 Oct 2024 18:06:51 +0200 Subject: [PATCH] update: fold into a TOK_REDIR token --- parser/Filelist.parser.mk | 1 + parser/include/parser/passes.h | 3 +- parser/include/parser/token.h | 3 +- parser/src/passes.c | 3 +- parser/src/passes/fold_no_quote.c | 2 +- parser/src/passes/fold_redir.c | 56 +++++++++++++++++++++++++++++++ 6 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 parser/src/passes/fold_redir.c diff --git a/parser/Filelist.parser.mk b/parser/Filelist.parser.mk index c7b14639..361539a3 100644 --- a/parser/Filelist.parser.mk +++ b/parser/Filelist.parser.mk @@ -6,6 +6,7 @@ passes/fold_double_carret \ passes/fold_double_pipe \ passes/fold_expansion \ passes/fold_no_quote \ +passes/fold_redir \ passes/fold_whitespace \ passes/template_file \ token_lifetime \ diff --git a/parser/include/parser/passes.h b/parser/include/parser/passes.h index ec85bcf9..cd166ae9 100644 --- a/parser/include/parser/passes.h +++ b/parser/include/parser/passes.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/02 18:43:41 by maiboyer #+# #+# */ -/* Updated: 2024/10/05 13:10:56 by maiboyer ### ########.fr */ +/* Updated: 2024/10/05 18:03:54 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,6 +43,7 @@ t_error ts_double_string_pass(t_vec_token input, t_vec_token *output); t_error ts_fold_no_quote(t_vec_token input, t_vec_token *output); t_error ts_fold_whitespace(t_vec_token input, t_vec_token *output); t_error ts_do_fuck_all(t_vec_token input, t_vec_token *output); +t_error ts_fold_redir(t_vec_token input, t_vec_token *output); t_error ts_fold_expension(t_vec_token input, t_vec_token *output); diff --git a/parser/include/parser/token.h b/parser/include/parser/token.h index b41024dc..d6a4aa73 100644 --- a/parser/include/parser/token.h +++ b/parser/include/parser/token.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/26 17:59:23 by maiboyer #+# #+# */ -/* Updated: 2024/10/05 13:11:34 by maiboyer ### ########.fr */ +/* Updated: 2024/10/05 18:02:03 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,6 +39,7 @@ enum e_token TOK_WHITESPACE, // whitespace outside of quoted strings TOK_NALPHANUM, // a non alphanumeric character, used in the expansion folding, then folded back into NQUOTE TOK_WORD, // a meta token, which contains subtokens + TOK_REDIR, // a meta token, which contains being an [D](L|R)CARRET and the arg being a WORD }; typedef struct s_token diff --git a/parser/src/passes.c b/parser/src/passes.c index 7b2f41b5..adea93f2 100644 --- a/parser/src/passes.c +++ b/parser/src/passes.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/02 18:41:16 by maiboyer #+# #+# */ -/* Updated: 2024/10/05 13:12:08 by maiboyer ### ########.fr */ +/* Updated: 2024/10/05 18:03:39 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,6 +42,7 @@ static const struct s_ts_pass_def g_ts_passes[] = {\ {ts_double_pipe, "double pipe => or"}, {ts_double_lcarret, "double lcarret => dlcarret"}, {ts_double_rcarret, "double rcarrer => drcarret"}, + {ts_fold_redir, "fold redir+argument"}, }; t_error ts_apply_passes(t_vec_token ts, t_vec_token *out) diff --git a/parser/src/passes/fold_no_quote.c b/parser/src/passes/fold_no_quote.c index bc6456d4..3ce3d698 100644 --- a/parser/src/passes/fold_no_quote.c +++ b/parser/src/passes/fold_no_quote.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */ -/* Updated: 2024/10/05 13:19:50 by maiboyer ### ########.fr */ +/* Updated: 2024/10/05 18:05:49 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/parser/src/passes/fold_redir.c b/parser/src/passes/fold_redir.c new file mode 100644 index 00000000..705a2a81 --- /dev/null +++ b/parser/src/passes/fold_redir.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fold_redir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */ +/* Updated: 2024/10/05 18:02:25 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser/passes.h" +#include "me/types.h" +#include "me/vec/vec_token.h" +#include "parser/token.h" + +bool _is_token_carret(enum e_token ttype) +{ + return (ttype == TOK_LCARRET || ttype == TOK_DLCARRET || ttype == TOK_RCARRET || ttype == TOK_DRCARRET); +} + +/// This is a sample pass +/// +/// There is a few rules the rest of the tokenizer machinery assumes +/// theses function follows: +/// - the input vec WILL be freed when the function return, even in +/// case of error +/// - the output vector isn't populated if the function returns an error, +/// thus it shouldn't be freed in case of error +/// - the output tokens may not be direct copy of the input tokens, +/// but need to be cloned (different allocations for stuff) +/// This function will take any `` +t_error ts_fold_redir(t_vec_token input, t_vec_token *output) +{ + t_vec_token out; + t_usize i; + t_token tmp; + + i = 0; + out = vec_token_new(input.len, token_free); + while (i < input.len) + { + if (vec_token_get(&input, i + 1) != NULL && _is_token_carret(vec_token_get(&input, i)->type) && vec_token_get(&input, i + 1)->type == TOK_WORD) + { + tmp = token_new_meta(TOK_REDIR); + vec_token_push(&tmp.subtokens, token_clone(vec_token_get(&input, i++))); + vec_token_push(&tmp.subtokens, token_clone(vec_token_get(&input, i++))); + vec_token_push(&out, tmp); + } + else + vec_token_push(&out, token_clone(&input.buffer[i++])); + } + vec_token_free(input); + return (*output = out, NO_ERROR); +}