update: added debug print of ts after every pass

This commit is contained in:
maix0 2024-10-05 18:58:42 +02:00
parent 7cb3582b1a
commit 71d9a201b5
9 changed files with 172 additions and 39 deletions

View file

@ -3,6 +3,7 @@ passes \
passes/double_quote_parsing \
passes/fold_double_amp \
passes/fold_double_carret \
passes/fold_double_paren \
passes/fold_double_pipe \
passes/fold_expansion \
passes/fold_no_quote \
@ -12,6 +13,7 @@ passes/template_file \
token_lifetime \
tokenizer \
tokenizer_utils \
ts_print \
GEN_FILES = \
\

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 18:43:41 by maiboyer #+# #+# */
/* Updated: 2024/10/05 18:03:54 by maiboyer ### ########.fr */
/* Updated: 2024/10/05 18:42:17 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -37,14 +37,16 @@ t_error ts_dq_apply_passes(t_vec_token ts, t_vec_token *out);
// this is a example one, does absolutly nothing lol
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_lparen(t_vec_token input, t_vec_token *output);
t_error ts_double_pipe(t_vec_token input, t_vec_token *output);
t_error ts_double_rcarret(t_vec_token input, t_vec_token *output);
t_error ts_double_rparen(t_vec_token input, t_vec_token *output);
t_error ts_double_string_pass(t_vec_token input, t_vec_token *output);
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_do_fuck_all(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_expension(t_vec_token input, t_vec_token *output);
t_error ts_fold_redir(t_vec_token input, t_vec_token *output);
#endif /* PASSES_H */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/26 17:59:23 by maiboyer #+# #+# */
/* Updated: 2024/10/05 18:02:03 by maiboyer ### ########.fr */
/* Updated: 2024/10/05 18:54:13 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,28 +18,31 @@
enum e_token
{
TOK_NONE, // NO TOKEN TYPE == INVALID / INEXISTANT TOKEN
TOK_AEXP, // a meta token, artih expansion
TOK_AMP, // ampersand == &
TOK_AND, // and == &&
TOK_CARRET, // any carret == < > << >>
TOK_DLCARRET, // double left carret == <<
TOK_DLPAREN, // double left parenthesis '(('
TOK_DOLLAR, // dollar == $
TOK_DQUOTE, // double quote string
TOK_DRCARRET, // double right carret == >>
TOK_DRPAREN, // double right parenthesis '))'
TOK_EXPENSION, // an expension == $<no_quote_word>; the $ is not in .string
TOK_LCARRET, // left carret == <
TOK_LPAREN, // left parenthesis == (
TOK_NALPHANUM, // a non alphanumeric character, used in the expansion folding, then folded back into NQUOTE
TOK_NONE, // NO TOKEN TYPE == INVALID / INEXISTANT TOKEN
TOK_NQUOTE, // no quote string
TOK_OR, // or == ||
TOK_PIPE, // pipe == |
TOK_RCARRET, // right carret == >
TOK_REDIR, // a meta token, which contains <OPERATOR> being an [D](L|R)CARRET and the arg being a WORD
TOK_RPAREN, // right parenthesis == )
TOK_SEMICOLON, // semicolor == ;
TOK_SQUOTE, // single quote string
TOK_WHITESPACE, // whitespace outside of quoted strings
TOK_NALPHANUM, // a non alphanumeric character, used in the expansion folding, then folded back into NQUOTE
TOK_WORD, // a meta token, which contains subtokens
TOK_REDIR, // a meta token, which contains <OPERATOR> being an [D](L|R)CARRET and the arg being a WORD
};
typedef struct s_token
@ -61,6 +64,7 @@ void token_free(t_token tok);
bool token_is_meta(t_token tok);
bool token_is_noquote(enum e_token tok);
void ts_print(t_vec_token *ts);
/* PARSING */
t_error tokenize(t_const_str s, t_vec_token *out);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 18:41:16 by maiboyer #+# #+# */
/* Updated: 2024/10/05 18:03:39 by maiboyer ### ########.fr */
/* Updated: 2024/10/05 18:57:59 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -40,8 +40,11 @@ static const struct s_ts_pass_def g_ts_passes[] = {\
{ts_fold_whitespace, "fold whitespace"},
{ts_double_amp, "double amp => and"},
{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_rcarret, "double rcarrer => drcarret"},
// there should be an ts_fold_arith here
{ts_fold_redir, "fold redir+argument"},
};
@ -60,13 +63,17 @@ t_error ts_apply_passes(t_vec_token ts, t_vec_token *out)
else
me_printf("Applied '%s' pass\n", g_ts_passes[i].name);
ts = next;
ts_print(&ts);
i++;
}
return (*out = ts, NO_ERROR);
}
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"},
// there should be an ts_fold_arith here
{ts_fold_no_quote, "fold no quote"},
};
@ -86,6 +93,7 @@ t_error ts_dq_apply_passes(t_vec_token ts, t_vec_token *out)
else
me_printf("Applied '%s' dq_pass\n", g_ts_dq_passes[i].name);
ts = next;
ts_print(&ts);
i++;
}
return (*out = ts, 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/05 13:06:17 by maiboyer ### ########.fr */
/* Updated: 2024/10/05 18:56:12 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -50,8 +50,12 @@ t_error _parse_dquote_inner(t_token dquote, t_vec_token *append)
}
string_push_char(&ctok.string, c);
}
else if ('$')
else if (c == '$')
push_token_and_create_new(&out.subtokens, &ctok, TOK_DOLLAR, "$");
else if (c == '(')
push_token_and_create_new(&out.subtokens, &ctok, TOK_LPAREN, "(");
else if (c == ')')
push_token_and_create_new(&out.subtokens, &ctok, TOK_RPAREN, ")");
else if (!(me_isalnum(c) || c == '_'))
push_token_and_create_new_chr(&out.subtokens, &ctok, TOK_NALPHANUM, c);
else

View file

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fold_double_paren.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
/* Updated: 2024/10/05 18:43:52 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"
/// 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;
t_token tmp;
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)
{
tmp = token_new(TOK_DLPAREN);
string_push(&tmp.string, "((");
vec_token_push(&out, tmp);
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;
t_token tmp;
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)
{
tmp = token_new(TOK_DRPAREN);
string_push(&tmp.string, "))");
vec_token_push(&out, tmp);
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/09/28 14:37:13 by maiboyer #+# #+# */
/* Updated: 2024/10/05 13:13:16 by maiboyer ### ########.fr */
/* Updated: 2024/10/05 18:48:09 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -68,7 +68,13 @@ bool token_is_noquote(enum e_token ttype)
{
return (ttype == TOK_NQUOTE \
|| ttype == TOK_DOLLAR \
|| ttype == TOK_NALPHANUM);
|| ttype == TOK_NALPHANUM \
// false
//|| ttype == TOK_LPAREN \n
//|| ttype == TOK_RPAREN \n
//|| ttype == TOK_DLPAREN \n
//|| ttype == TOK_DRPAREN
);
}
// TO REMOVE

48
parser/src/ts_print.c Normal file
View file

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ts_print.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/05 18:51:50 by maiboyer #+# #+# */
/* Updated: 2024/10/05 18:53:18 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/types.h"
#include "me/vec/vec_token.h"
#include "parser/token.h"
#include <stdio.h>
#include "app/colors.h"
t_str token_name(t_token *out);
static void _print_ts_inner(t_usize i, t_token *token, void *vdepth)
{
t_usize depth;
t_string sdepth;
depth = 0;
if (vdepth != NULL)
depth = *(t_usize *)vdepth;
sdepth = string_new(16);
i = 0;
while (i++ < depth)
string_push_char(&sdepth, '\t');
if (token->subtokens.buffer != NULL)
{
depth++;
printf("%s[" COL_GREEN "%10s"RESET"]\n", sdepth.buf ,token_name(token));
vec_token_iter(&token->subtokens, _print_ts_inner, &depth);
}
else
printf("%s[" COL_GREEN "%10s"RESET"] '"COL_YELLOW"%s"RESET"'\n",\
sdepth.buf ,token_name(token), token->string.buf);
string_free(sdepth);
}
void ts_print(t_vec_token *ts)
{
vec_token_iter(ts, _print_ts_inner, NULL);
}