diff --git a/parser/src/passes.c b/parser/src/passes.c index 03a04a47..ab8445d9 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/06 14:43:56 by rparodi ### ########.fr */ +/* Updated: 2024/10/06 14:53:20 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,7 +56,7 @@ static const struct s_ts_pass_def g_ts_passes[] = {\ {ts_double_lcarret, "double lcarret => dlcarret"}, \ {ts_double_rcarret, "double rcarrer => drcarret"}, \ // there should be an ts_fold_arith here - {ts_split_paren, "split double parenthesis"}, \ +{ts_split_paren, "split double parenthesis"}, \ {ts_fold_redir, "fold redir+argument"}, \ }; diff --git a/parser/src/passes/fold_double_quote.c b/parser/src/passes/fold_double_quote.c new file mode 100644 index 00000000..565a9dbc --- /dev/null +++ b/parser/src/passes/fold_double_quote.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fold_double_quote.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */ +/* Updated: 2024/10/06 15:13:45 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "me/string/string.h" +#include "parser/passes.h" +#include "me/types.h" +#include "me/vec/vec_token.h" +#include "parser/token.h" +#include "stdbool.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) + +bool _type_extansion(enum e_token type) +{ + if (type == TOK_DOLLAR || type == TOK_DQUOTE || type == TOK_SQUOTE) + return (true); + return (false); +} + +t_error ts_fold_double_quote(t_vec_token input, t_vec_token *output) +{ + t_vec_token out; + t_usize i; + t_usize j; + t_token tmp; + + i = 0; + out = vec_token_new(input.len, token_free); + while (i < input.len) + { + if (token_is_noquote(input.buffer[i].type)) + { + j = 0; + tmp = token_new(TOK_NQUOTE); + while (i + j < input.len \ + && token_is_noquote(input.buffer[i + j].type)) + if (type_extansion(input.buffer->type)) + string_push(&tmp.string, input.buffer[i + j++].subtokens.buffer); + vec_token_push(&out, tmp); + i += j; + } + else + vec_token_push(&out, token_clone(&input.buffer[i++])); + } + vec_token_free(input); + return (*output = out, NO_ERROR); +}