diff --git a/parser/src/passes.c b/parser/src/passes.c index 71af7cae..c1cd408d 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/04 17:19:58 by rparodi ### ########.fr */ +/* Updated: 2024/10/04 17:27:36 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,12 +38,12 @@ // bellow is some function to apply all the different passes ! -static const struct s_ts_pass_def g_ts_passes[] = { - {ts_double_string_pass, "double string parser"}, +static const struct s_ts_pass_def g_ts_passes[] = {\ + {ts_double_string_pass, "double string parser"}, \ {ts_fold_expension, "fold expansion"}, }; -t_error ts_apply_passes(t_vec_token ts, t_vec_token *out) +t_error ts_apply_passes(t_vec_token ts, t_vec_token *out) { t_usize i; t_vec_token next; @@ -63,15 +63,15 @@ t_error ts_apply_passes(t_vec_token ts, t_vec_token *out) return (*out = ts, NO_ERROR); } -static const struct s_ts_pass_def g_ts_dq_passes[] = { - {ts_do_fuck_all, "does nothing lol"}, +static const struct s_ts_pass_def g_ts_dq_passes[] = {\ + {ts_do_fuck_all, "does nothing lol"}, \ {ts_fold_expension, "fold expansion"}, }; -t_error ts_dq_apply_passes(t_vec_token ts, t_vec_token *out) +t_error ts_dq_apply_passes(t_vec_token ts, t_vec_token *out) { t_usize i; - t_vec_token next; + t_vec_token next; i = 0; while (i < sizeof(g_ts_dq_passes) / sizeof(g_ts_dq_passes[0])) @@ -79,7 +79,8 @@ t_error ts_dq_apply_passes(t_vec_token ts, t_vec_token *out) if (g_ts_dq_passes[i].fn == NULL) return (vec_token_free(ts), ERROR); if ((g_ts_dq_passes[i].fn)(ts, &next)) - return (me_eprintf("failed on '%s' dq token pass\n", g_ts_dq_passes[i].name), ERROR); + return (me_eprintf(\ + "failed on '%s' dq token pass\n", g_ts_dq_passes[i].name), ERROR); else me_printf("Applied '%s' dq_pass\n", g_ts_dq_passes[i].name); ts = next; diff --git a/parser/src/passes/fold_whitespace.c b/parser/src/passes/fold_whitespace.c new file mode 100644 index 00000000..73a0afa3 --- /dev/null +++ b/parser/src/passes/fold_whitespace.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* fold_whitespace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */ +/* Updated: 2024/10/04 17:41:40 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser/passes.h" +#include "me/types.h" +#include "me/vec/vec_token.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_fold_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); + if (input.buffer[i].type == TOK_WHITESPACE) + vec_token_push(&out, token_clone(&input.buffer[i])); + while (i < input.len) + { + if (input.len >= i + 1) + { + if (input.buffer[i].type == TOK_WHITESPACE) + i++; + } + i++; + } + vec_token_free(input); + return (*output = out, NO_ERROR); +}