feat: adding the whitespace treatment (fold_whitespace)

This commit is contained in:
Raphael 2024-10-04 17:43:15 +02:00
parent 912d095886
commit 762c7df1a9
2 changed files with 58 additions and 9 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 18:41:16 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 ! // bellow is some function to apply all the different passes !
static const struct s_ts_pass_def g_ts_passes[] = { static const struct s_ts_pass_def g_ts_passes[] = {\
{ts_double_string_pass, "double string parser"}, {ts_double_string_pass, "double string parser"}, \
{ts_fold_expension, "fold expansion"}, {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_usize i;
t_vec_token next; 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); return (*out = ts, NO_ERROR);
} }
static const struct s_ts_pass_def g_ts_dq_passes[] = { static const struct s_ts_pass_def g_ts_dq_passes[] = {\
{ts_do_fuck_all, "does nothing lol"}, {ts_do_fuck_all, "does nothing lol"}, \
{ts_fold_expension, "fold expansion"}, {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_usize i;
t_vec_token next; t_vec_token next;
i = 0; i = 0;
while (i < sizeof(g_ts_dq_passes) / sizeof(g_ts_dq_passes[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) if (g_ts_dq_passes[i].fn == NULL)
return (vec_token_free(ts), ERROR); return (vec_token_free(ts), ERROR);
if ((g_ts_dq_passes[i].fn)(ts, &next)) 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 else
me_printf("Applied '%s' dq_pass\n", g_ts_dq_passes[i].name); me_printf("Applied '%s' dq_pass\n", g_ts_dq_passes[i].name);
ts = next; ts = next;

View file

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fold_whitespace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}