update: made some small fixes
This commit is contained in:
parent
11898cba9b
commit
4f1a08cbe6
8 changed files with 47 additions and 17 deletions
|
|
@ -1,4 +1,6 @@
|
||||||
SRC_FILES = \
|
SRC_FILES = \
|
||||||
|
passes \
|
||||||
|
passes/template_file \
|
||||||
token_lifetime \
|
token_lifetime \
|
||||||
tokenizer \
|
tokenizer \
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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/02 19:13:19 by maiboyer ### ########.fr */
|
/* Updated: 2024/10/03 21:32:32 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -25,6 +25,7 @@ struct s_ts_pass_def
|
||||||
t_const_str name;
|
t_const_str name;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
t_error ts_apply_passes(t_vec_token ts, t_vec_token *out);
|
||||||
|
|
||||||
// list passes function here
|
// list passes function here
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/09/26 17:59:23 by maiboyer #+# #+# */
|
/* Created: 2024/09/26 17:59:23 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/10/02 19:12:26 by maiboyer ### ########.fr */
|
/* Updated: 2024/10/03 21:33:14 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -58,6 +58,6 @@ bool token_is_meta(t_token tok);
|
||||||
t_token token_clone(t_token *tok);
|
t_token token_clone(t_token *tok);
|
||||||
|
|
||||||
/* PARSING */
|
/* PARSING */
|
||||||
t_error tokeniser(t_const_str raw);
|
t_error tokenize(t_const_str s, t_vec_token *out);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -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/02 19:22:37 by maiboyer ### ########.fr */
|
/* Updated: 2024/10/03 21:32:14 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -46,7 +46,7 @@ static const struct s_ts_pass_def g_ts_passes[] = {
|
||||||
{do_fuck_all, "does nothing lol"},
|
{do_fuck_all, "does nothing lol"},
|
||||||
};
|
};
|
||||||
|
|
||||||
t_error apply_all_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;
|
||||||
|
|
@ -58,6 +58,8 @@ t_error apply_all_passes(t_vec_token ts, t_vec_token *out)
|
||||||
return (vec_token_free(ts), ERROR);
|
return (vec_token_free(ts), ERROR);
|
||||||
if ((g_ts_passes[i].fn)(ts, &next))
|
if ((g_ts_passes[i].fn)(ts, &next))
|
||||||
return (me_eprintf("failed on %s token pass\n", g_ts_passes[i].name), ERROR);
|
return (me_eprintf("failed on %s token pass\n", g_ts_passes[i].name), ERROR);
|
||||||
|
else
|
||||||
|
me_printf("Applied '%s' pass\n", g_ts_passes[i].name);
|
||||||
ts = next;
|
ts = next;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
|
/* Created: 2024/10/02 19:04:32 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/10/02 19:13:31 by maiboyer ### ########.fr */
|
/* Updated: 2024/10/03 21:37:04 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -15,11 +15,22 @@
|
||||||
#include "me/vec/vec_token.h"
|
#include "me/vec/vec_token.h"
|
||||||
#include "parser/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 do_fuck_all(t_vec_token input, t_vec_token *output)
|
t_error do_fuck_all(t_vec_token input, t_vec_token *output)
|
||||||
{
|
{
|
||||||
t_vec_token out;
|
t_vec_token out;
|
||||||
t_usize i;
|
t_usize i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
out = vec_token_new(input.len, token_free);
|
out = vec_token_new(input.len, token_free);
|
||||||
while (i < input.len)
|
while (i < input.len)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/09/30 19:39:39 by maiboyer #+# #+# */
|
/* Created: 2024/09/30 19:39:39 by maiboyer #+# #+# */
|
||||||
/* Updated: 2024/10/02 18:02:59 by maiboyer ### ########.fr */
|
/* Updated: 2024/10/03 21:31:57 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -28,8 +28,16 @@ static void push_token_and_create_new(\
|
||||||
string_push(&tmp.string, s);
|
string_push(&tmp.string, s);
|
||||||
vec_token_push(tokens, tmp);
|
vec_token_push(tokens, tmp);
|
||||||
}
|
}
|
||||||
|
static void push_token_and_set_new(\
|
||||||
|
t_vec_token *tokens, t_token *tok, enum e_token ttype, t_const_str s)
|
||||||
|
{
|
||||||
|
if (tok->type != TOK_NONE)
|
||||||
|
vec_token_push(tokens, *tok);
|
||||||
|
*tok = token_new(ttype);
|
||||||
|
string_push(&tok->string, s);
|
||||||
|
}
|
||||||
|
|
||||||
void handle_quote(t_vec_token *ret, char chr, t_token *tok, char *quote)
|
static void handle_quote(t_vec_token *ret, char chr, t_token *tok, char *quote)
|
||||||
{
|
{
|
||||||
if (chr == *quote)
|
if (chr == *quote)
|
||||||
{
|
{
|
||||||
|
|
@ -42,7 +50,7 @@ void handle_quote(t_vec_token *ret, char chr, t_token *tok, char *quote)
|
||||||
string_push_char(&tok->string, chr);
|
string_push_char(&tok->string, chr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_noquote(t_vec_token *ret, char chr, t_token *tok, char *quote)
|
static void handle_noquote(t_vec_token *ret, char chr, t_token *tok, char *quote)
|
||||||
{
|
{
|
||||||
*quote = '\0';
|
*quote = '\0';
|
||||||
if (chr == '$')
|
if (chr == '$')
|
||||||
|
|
@ -71,15 +79,15 @@ void handle_noquote(t_vec_token *ret, char chr, t_token *tok, char *quote)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void tokenize_inner(t_vec_token *ret, char chr, t_token *tok, char *quote)
|
static void tokenize_inner(t_vec_token *ret, char chr, t_token *tok, char *quote)
|
||||||
{
|
{
|
||||||
if (*quote == '\0')
|
if (*quote == '\0')
|
||||||
{
|
{
|
||||||
*quote = chr;
|
*quote = chr;
|
||||||
if (chr == '\"')
|
if (chr == '\"')
|
||||||
push_token_and_create_new(ret, tok, TOK_DQUOTE, "");
|
push_token_and_set_new(ret, tok, TOK_DQUOTE, "");
|
||||||
else if (chr == '\'')
|
else if (chr == '\'')
|
||||||
push_token_and_create_new(ret, tok, TOK_SQUOTE, "");
|
push_token_and_set_new(ret, tok, TOK_SQUOTE, "");
|
||||||
else
|
else
|
||||||
handle_noquote(ret, chr, tok, quote);
|
handle_noquote(ret, chr, tok, quote);
|
||||||
}
|
}
|
||||||
|
|
@ -89,6 +97,10 @@ void tokenize_inner(t_vec_token *ret, char chr, t_token *tok, char *quote)
|
||||||
me_abort("invalid quote type");
|
me_abort("invalid quote type");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This should even be wrapped with the passes function so the consumer only
|
||||||
|
// see the last version of the tokenstream
|
||||||
|
//
|
||||||
|
// currently it is "Public" API though
|
||||||
t_error tokenize(t_const_str s, t_vec_token *out)
|
t_error tokenize(t_const_str s, t_vec_token *out)
|
||||||
{
|
{
|
||||||
t_usize i;
|
t_usize i;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/09/06 16:31:41 by rparodi #+# #+# */
|
/* Created: 2024/09/06 16:31:41 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/09/30 20:06:27 by maiboyer ### ########.fr */
|
/* Updated: 2024/10/03 21:09:35 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -47,7 +47,6 @@ t_error get_user_input(t_state *state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
line_edit_stop(&lstate);
|
line_edit_stop(&lstate);
|
||||||
printf("state->str_input = %s\n", state->str_input);
|
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
|
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/09/30 20:11:12 by maiboyer ### ########.fr */
|
/* Updated: 2024/10/03 21:32:41 by maiboyer ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -24,6 +24,8 @@
|
||||||
#include "me/types.h"
|
#include "me/types.h"
|
||||||
#include "me/vec/vec_str.h"
|
#include "me/vec/vec_str.h"
|
||||||
#include "me/vec/vec_token.h"
|
#include "me/vec/vec_token.h"
|
||||||
|
#include "parser/passes.h"
|
||||||
|
#include "parser/token.h"
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
|
@ -104,15 +106,16 @@ void func(t_usize i, t_token *token, void *state)
|
||||||
{
|
{
|
||||||
(void)(state);
|
(void)(state);
|
||||||
(void)(i);
|
(void)(i);
|
||||||
printf("%s => %s\n", token_name(token), token->string.buf);
|
printf("["COL_GREEN"%10s"RESET"] '"COL_YELLOW"%s"RESET"'\n", token_name(token), token->string.buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error tokenize(t_const_str s, t_vec_token *out);
|
|
||||||
void parse_str(t_state *state)
|
void parse_str(t_state *state)
|
||||||
{
|
{
|
||||||
t_vec_token tokens;
|
t_vec_token tokens;
|
||||||
if (tokenize(state->str_input, &tokens))
|
if (tokenize(state->str_input, &tokens))
|
||||||
return ;
|
return ;
|
||||||
|
if (ts_apply_passes(tokens, &tokens))
|
||||||
|
return ;
|
||||||
vec_token_iter(&tokens, func, NULL);
|
vec_token_iter(&tokens, func, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue