normed: parser/src/passes/

This commit is contained in:
maix0 2024-10-12 15:53:05 +02:00
parent 3d11b63428
commit 1ba7fc5f32
6 changed files with 29 additions and 157 deletions

View file

@ -4,7 +4,6 @@ passes/double_quote_parsing \
passes/fold_cmd \
passes/fold_double_amp \
passes/fold_double_carret \
passes/fold_double_paren \
passes/fold_double_pipe \
passes/fold_expansion \
passes/fold_no_quote \
@ -14,7 +13,6 @@ passes/fold_word \
passes/paren_to_nquote \
passes/remove_whitespace \
passes/split_double_paren \
passes/template_file \
passes/verify_invalid_tokens \
token_lifetime \
token_name \

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
/* Updated: 2024/10/08 14:44:55 by maiboyer ### ########.fr */
/* Updated: 2024/10/12 15:51:43 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,7 +19,27 @@
static bool _is_cmd_node(enum e_token ttype)
{
return (ttype == TOK_WHITESPACE || ttype == TOK_WORD || ttype == TOK_REDIR);
};
}
static void _handle_cmd_node(\
t_vec_token *input, t_usize *i, t_vec_token *out)
{
t_usize j;
t_token tmp;
j = 0;
tmp = token_new_meta(TOK_CMD);
while (*i + j < input->len \
&& _is_cmd_node(input->buffer[*i + j].type))
{
if (input->buffer[*i + j].type != TOK_WHITESPACE)
vec_token_push(&tmp.subtokens, \
token_clone(&input->buffer[*i + j]));
j++;
}
vec_token_push(out, tmp);
*i += j;
}
/// This is a sample pass
///
@ -36,32 +56,16 @@ t_error ts_fold_cmd(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 (_is_cmd_node(input.buffer[i].type))
{
j = 0;
tmp = token_new_meta(TOK_CMD);
while (i + j < input.len \
&& _is_cmd_node(input.buffer[i + j].type))
{
if (input.buffer[i + j].type != TOK_WHITESPACE)
vec_token_push(&tmp.subtokens, token_clone(&input.buffer[i + j]));
j++;
}
vec_token_push(&out, tmp);
i += j;
}
_handle_cmd_node(&input, &i, &out);
else
{
vec_token_push(&out, token_clone(&input.buffer[i++]));
}
}
vec_token_free(input);
return (*output = out, NO_ERROR);
}

View file

@ -1,87 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fold_double_paren.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
/* Updated: 2024/10/06 13:40:36 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/string/string.h"
#include "parser/passes.h"
#include "me/types.h"
#include "me/vec/vec_token.h"
#include "parser/token.h"
static void _fold_parens_helper(t_vec_token *v, enum e_token ty, t_const_str s)
{
t_token tmp;
tmp = token_new(ty);
string_push(&tmp.string, s);
vec_token_push(v, tmp);
}
/// 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_double_lparen(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 (i + 1 >= input.len)
vec_token_push(&out, token_clone(&input.buffer[i]));
else if (input.buffer[i].type == TOK_LPAREN
&& input.buffer[i + 1].type == TOK_LPAREN)
{
_fold_parens_helper(&out, TOK_DLPAREN, "((");
i++;
}
else
vec_token_push(&out, token_clone(&input.buffer[i]));
i++;
}
vec_token_free(input);
return (*output = out, NO_ERROR);
}
t_error ts_double_rparen(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 (i + 1 >= input.len)
vec_token_push(&out, token_clone(&input.buffer[i]));
else if (input.buffer[i].type == TOK_RPAREN
&& input.buffer[i + 1].type == TOK_RPAREN)
{
_fold_parens_helper(&out, TOK_DRPAREN, "))");
i++;
}
else
vec_token_push(&out, token_clone(&input.buffer[i]));
i++;
}
vec_token_free(input);
return (*output = out, NO_ERROR);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
/* Updated: 2024/10/07 16:48:37 by maiboyer ### ########.fr */
/* Updated: 2024/10/12 15:52:16 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
/* Updated: 2024/10/08 15:18:58 by maiboyer ### ########.fr */
/* Updated: 2024/10/12 15:52:35 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -1,43 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* template_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
/* Updated: 2024/10/04 17:46:43 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_do_fuck_all(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)
{
vec_token_push(&out, token_clone(&input.buffer[i]));
i++;
}
vec_token_free(input);
return (*output = out, NO_ERROR);
}