From: Lukas Jiriste Date: Sat, 15 Jun 2024 07:39:27 +0000 (+0200) Subject: Implement parse_header, minor fixes X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=ed898cfac465eff5ee0e9280cddb78d48eeaed87;p=Libft.git Implement parse_header, minor fixes --- diff --git a/ft_parse/ft_parse.c b/ft_parse/ft_parse.c index c2ead50..0e11486 100644 --- a/ft_parse/ft_parse.c +++ b/ft_parse/ft_parse.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/20 20:51:36 by ljiriste #+# #+# */ -/* Updated: 2024/06/14 15:54:42 by ljiriste ### ########.fr */ +/* Updated: 2024/06/15 09:24:56 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,6 +62,7 @@ static t_grammar_rule parse_rule(const char *line) while (!ft_isspace(line[i])) ++i; token.type = ft_strndup(line + j, i - j); + rule.result = token; while (ft_isspace(line[i])) ++i; if (!(line[i++] == '-' && line[i++] == '>')) @@ -172,10 +173,40 @@ static int add_line(t_vec *states, const char *line, size_t lookahead_size) goto_rule = ft_atoi(condensed_line + i); ft_vec_append(&state.gotos, &goto_rule); } + free(condensed_line); ft_vec_append(states, &state); return (0); } +static t_vec parse_header(const char *header) +{ + t_vec tokens; + t_token token; + char *condensed_line; + size_t i; + size_t j; + + condensed_line = ft_remove_space(header); + ft_vec_init(&tokens, sizeof(t_token)); + token.str = NULL; + i = 0; + while (condensed_line[i] && condensed_line[i++] != ';'); + while (condensed_line[i]) + { + j = i; + while (condensed_line[i] && condensed_line[i] != ';') + ++i; + token.type = ft_strndup(condensed_line + j, i - j); + if (!token.type || ft_vec_append(&tokens, &token) != success) + { + free(token.type); + ft_vec_free(&tokens, free_token); + return (tokens); + } + } + return (tokens); +} + t_parsing_table ft_load_parsing_table(const char *filename, const char *rules_filename) { @@ -183,6 +214,9 @@ t_parsing_table ft_load_parsing_table(const char *filename, char *line; t_parsing_table table; + ft_vec_init(&table.rules, sizeof(t_grammar_rule)); + ft_vec_init(&table.states, sizeof(t_parser_state)); + ft_vec_init(&table.tokens, sizeof(t_token)); if (load_rules(&table.rules, rules_filename)) return (table); fd = open(filename, O_RDONLY); diff --git a/inc/libft.h b/inc/libft.h index 33d0962..595e71b 100644 --- a/inc/libft.h +++ b/inc/libft.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/08/15 12:58:15 by ljiriste #+# #+# */ -/* Updated: 2023/12/11 10:17:16 by ljiriste ### ########.fr */ +/* Updated: 2024/06/15 09:05:22 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,5 +22,6 @@ # include "ft_io.h" # include "ft_lst.h" # include "ft_arr.h" +# include "ft_parse.h" #endif