diff --git a/parser/Filelist.parser.mk b/parser/Filelist.parser.mk index e7399632..aa97e534 100644 --- a/parser/Filelist.parser.mk +++ b/parser/Filelist.parser.mk @@ -12,6 +12,7 @@ passes/fold_redir \ passes/fold_whitespace \ passes/fold_word \ passes/paren_to_nquote \ +passes/remove_whitespace \ passes/split_double_paren \ passes/template_file \ token_lifetime \ diff --git a/parser/include/parser/passes.h b/parser/include/parser/passes.h index fdced4d4..40437119 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/07 16:46:03 by maiboyer ### ########.fr */ +/* Updated: 2024/10/08 15:19:03 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,5 +51,6 @@ t_error ts_split_paren(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_expension(t_vec_token input, t_vec_token *output); t_error ts_fold_redir(t_vec_token input, t_vec_token *output); +t_error ts_remove_whitespace(t_vec_token input, t_vec_token *output); #endif /* PASSES_H */ diff --git a/parser/src/passes.c b/parser/src/passes.c index c2279cb9..e0ca6e16 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/08 14:52:02 by maiboyer ### ########.fr */ +/* Updated: 2024/10/08 15:19:01 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,6 +53,7 @@ static const struct s_ts_pass_def g_ts_passes[] = {\ // there should be an ts_fold_arith here {ts_split_paren, "split double parenthesis"}, \ {ts_fold_redir, "fold redir+argument"}, \ + {ts_remove_whitespace, "rm extra whitespace"}, \ {ts_fold_cmd, "fold into cmd"}, \ }; diff --git a/parser/src/passes/remove_whitespace.c b/parser/src/passes/remove_whitespace.c new file mode 100644 index 00000000..f0b8abcf --- /dev/null +++ b/parser/src/passes/remove_whitespace.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* remove_whitespace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */ +/* Updated: 2024/10/08 15:18:58 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/types.h" +#include "me/vec/vec_token.h" +#include "parser/passes.h" +#include "parser/token.h" + +/// 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) + +t_error ts_remove_whitespace(t_vec_token input, t_vec_token *output) +{ + t_vec_token out; + t_usize i; + + i = 0; + out = vec_token_new(input.len, token_free); + while (i < input.len) + { + if (vec_token_get(&input, i)->type != TOK_WHITESPACE) + vec_token_push(&out, token_clone(vec_token_get(&input, i))); + i++; + } + vec_token_free(input); + return (*output = out, NO_ERROR); +}