update: added pass to catch invalid token (respective of bonus too !)
This commit is contained in:
parent
4d56158633
commit
83311a8c15
4 changed files with 67 additions and 11 deletions
|
|
@ -15,6 +15,7 @@ passes/paren_to_nquote \
|
||||||
passes/remove_whitespace \
|
passes/remove_whitespace \
|
||||||
passes/split_double_paren \
|
passes/split_double_paren \
|
||||||
passes/template_file \
|
passes/template_file \
|
||||||
|
passes/verify_invalid_tokens \
|
||||||
token_lifetime \
|
token_lifetime \
|
||||||
token_name \
|
token_name \
|
||||||
token_utils \
|
token_utils \
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/02 18:43:41 by maiboyer #+# #+# */
|
/* Created: 2024/10/02 18:43:41 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/10/08 15:19:03 by maiboyer ### ########.fr */
|
/* Updated: 2024/10/10 15:27:32 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -35,6 +35,8 @@ t_error ts_dq_apply_passes(t_vec_token ts, t_vec_token *out);
|
||||||
// list passes function here
|
// list passes function here
|
||||||
|
|
||||||
// this is a example one, does absolutly nothing lol
|
// this is a example one, does absolutly nothing lol
|
||||||
|
t_error ts_do_fuck_all(t_vec_token input, t_vec_token *output);
|
||||||
|
|
||||||
t_error ts_double_amp(t_vec_token input, t_vec_token *output);
|
t_error ts_double_amp(t_vec_token input, t_vec_token *output);
|
||||||
t_error ts_double_lcarret(t_vec_token input, t_vec_token *output);
|
t_error ts_double_lcarret(t_vec_token input, t_vec_token *output);
|
||||||
t_error ts_double_lparen(t_vec_token input, t_vec_token *output);
|
t_error ts_double_lparen(t_vec_token input, t_vec_token *output);
|
||||||
|
|
@ -48,7 +50,7 @@ t_error ts_fold_no_quote(t_vec_token input, t_vec_token *output);
|
||||||
t_error ts_fold_whitespace(t_vec_token input, t_vec_token *output);
|
t_error ts_fold_whitespace(t_vec_token input, t_vec_token *output);
|
||||||
t_error ts_paren_to_noquote(t_vec_token input, t_vec_token *output);
|
t_error ts_paren_to_noquote(t_vec_token input, t_vec_token *output);
|
||||||
t_error ts_split_paren(t_vec_token input, t_vec_token *output);
|
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_verify_tokens(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_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_fold_redir(t_vec_token input, t_vec_token *output);
|
||||||
t_error ts_remove_whitespace(t_vec_token input, t_vec_token *output);
|
t_error ts_remove_whitespace(t_vec_token input, t_vec_token *output);
|
||||||
|
|
|
||||||
|
|
@ -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/08 15:19:01 by maiboyer ### ########.fr */
|
/* Updated: 2024/10/10 15:27:12 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -45,16 +45,13 @@ static const struct s_ts_pass_def g_ts_passes[] = {\
|
||||||
{ts_fold_whitespace, "fold whitespace"}, \
|
{ts_fold_whitespace, "fold whitespace"}, \
|
||||||
{ts_double_amp, "double amp => and"}, \
|
{ts_double_amp, "double amp => and"}, \
|
||||||
{ts_double_pipe, "double pipe => or"}, \
|
{ts_double_pipe, "double pipe => or"}, \
|
||||||
{ts_double_lparen, "double lparen => dlparen"}, \
|
|
||||||
{ts_double_rparen, "double rparen => drparen"}, \
|
|
||||||
{ts_double_lcarret, "double lcarret => dlcarret"}, \
|
{ts_double_lcarret, "double lcarret => dlcarret"}, \
|
||||||
{ts_double_rcarret, "double rcarrer => drcarret"}, \
|
{ts_double_rcarret, "double rcarrer => drcarret"}, \
|
||||||
{ts_fold_into_word, "fold into words"}, \
|
{ts_fold_into_word, "fold into words"}, \
|
||||||
// there should be an ts_fold_arith here
|
|
||||||
{ts_split_paren, "split double parenthesis"}, \
|
|
||||||
{ts_fold_redir, "fold redir+argument"}, \
|
{ts_fold_redir, "fold redir+argument"}, \
|
||||||
{ts_remove_whitespace, "rm extra whitespace"}, \
|
{ts_remove_whitespace, "rm extra whitespace"}, \
|
||||||
{ts_fold_cmd, "fold into cmd"}, \
|
{ts_fold_cmd, "fold into cmd"}, \
|
||||||
|
{ts_verify_tokens, "verify only tokens"}, \
|
||||||
};
|
};
|
||||||
|
|
||||||
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)
|
||||||
|
|
@ -80,11 +77,7 @@ t_error ts_apply_passes(t_vec_token ts, t_vec_token *out)
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct s_ts_pass_def g_ts_dq_passes[] = {\
|
static const struct s_ts_pass_def g_ts_dq_passes[] = {\
|
||||||
{ts_double_lparen, "double lparen => dlparen"}, \
|
|
||||||
{ts_double_rparen, "double rparen => drparen"}, \
|
|
||||||
{ts_fold_expension, "fold expansion"}, \
|
{ts_fold_expension, "fold expansion"}, \
|
||||||
// there should be an ts_fold_arith here
|
|
||||||
{ts_split_paren, "split double parenthesis"}, \
|
|
||||||
{ts_paren_to_noquote, "parenthesis to noquote"}, \
|
{ts_paren_to_noquote, "parenthesis to noquote"}, \
|
||||||
{ts_fold_no_quote, "fold no quote"},
|
{ts_fold_no_quote, "fold no quote"},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
60
parser/src/passes/verify_invalid_tokens.c
Normal file
60
parser/src/passes/verify_invalid_tokens.c
Normal file
|
|
@ -0,0 +1,60 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* verify_invalid_tokens.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/10/10 15:28:49 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "me/printf/printf.h"
|
||||||
|
#include "me/types.h"
|
||||||
|
#include "me/vec/vec_token.h"
|
||||||
|
#include "parser/passes.h"
|
||||||
|
#include "parser/token.h"
|
||||||
|
|
||||||
|
bool _is_invalid(enum e_token ty)
|
||||||
|
{
|
||||||
|
return (ty == TOK_NONE || ty == TOK_SEMICOLON || ty == TOK_AMP \
|
||||||
|
|| (!BONUS && \
|
||||||
|
(ty == TOK_OR || ty == TOK_AND || ty == TOK_LPAREN || ty == TOK_RPAREN)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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_verify_tokens(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 (_is_invalid(input.buffer[i].type))
|
||||||
|
{
|
||||||
|
me_eprintf("Invalid token found, this could be due to the fact " \
|
||||||
|
"that the project wasn't compiled with bonuses, or you failed " \
|
||||||
|
"to type your command.\ntok->type = %02X\n", input.buffer[i].type);
|
||||||
|
vec_token_free(input);
|
||||||
|
vec_token_free(out);
|
||||||
|
return (ERROR);
|
||||||
|
}
|
||||||
|
vec_token_push(&out, token_clone(&input.buffer[i]));
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
vec_token_free(input);
|
||||||
|
return (*output = out, NO_ERROR);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue