From 39a569cee6b4a4584439fd6b1188c130f24f3482 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Sun, 16 Jun 2024 10:27:37 +0200 Subject: [PATCH] Refactor to make functions comply with 42 Norm --- ft_parse/ft_parse.c | 157 +++++++++++++++++++++++++++++--------------- 1 file changed, 105 insertions(+), 52 deletions(-) diff --git a/ft_parse/ft_parse.c b/ft_parse/ft_parse.c index 6903469..968e2ce 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/16 07:15:38 by ljiriste ### ########.fr */ +/* Updated: 2024/06/16 10:26:21 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,6 +46,28 @@ static void free_state(void *v_state) return ; } +static void parse_constituents(t_vec *constituents, const char *line) +{ + size_t i; + size_t j; + t_token token; + + token.str = NULL; + i = 0; + while (line[i]) + { + while (ft_isspace(line[i])) + ++i; + j = i; + while (!ft_isspace(line[i]) && line[i]) + ++i; + if (j == i) + break ; + token.type = ft_strndup(line + j, i - j); + ft_vec_append(constituents, &token); + } +} + static t_grammar_rule parse_rule(const char *line) { t_grammar_rule rule; @@ -67,18 +89,7 @@ static t_grammar_rule parse_rule(const char *line) ++i; if (!(line[i++] == '-' && line[i++] == '>')) return (rule); - while (line[i]) - { - while (ft_isspace(line[i])) - ++i; - j = i; - while (!ft_isspace(line[i]) && line[i]) - ++i; - if (j == i) - break ; - token.type = ft_strndup(line + j, i - j); - ft_vec_append(&rule.constituents, &token); - } + parse_constituents(&rule.constituents, line + i); return (rule); } @@ -139,81 +150,126 @@ static size_t get_lookahead_size(t_vec *tokens) return (0); } -static int add_line(t_vec *states, const char *line, size_t lookahead_size) +static t_vec parse_lookahead(const char *line, size_t lookahead_size) { - t_parser_state state; t_parser_action action; - char *condensed_line; - size_t i; - ssize_t goto_rule; + t_vec lookahead; - condensed_line = ft_remove_space(line); - ft_vec_init(&state.lookahead, sizeof(t_parser_action)); - ft_vec_init(&state.gotos, sizeof(ssize_t)); - i = 0; + ft_vec_init(&lookahead, sizeof(t_parser_action)); while (lookahead_size > 0) { - while (condensed_line[i] && condensed_line[i++] != ';'); - action.number = ft_atoi(condensed_line + i + 1); - if (condensed_line[i] == 'r') + while (*line && *line != ';') + ++line; + if (!*line) + break ; + ++line; + action.number = ft_atoi(line + 1); + if (*line == 'r') action.type = parser_reduce; - else if (condensed_line[i] == 's') + else if (*line == 's') action.type = parser_shift; - else if (!ft_strncmp(condensed_line + i, "acc", 3)) + else if (!ft_strncmp(line, "acc", 3)) action.type = parser_accept; else action.type = parser_refuse; - ft_vec_append(&state.lookahead, &action); + ft_vec_append(&lookahead, &action); --lookahead_size; } - while (condensed_line[i]) + return (lookahead); +} + +static t_vec parse_goto(const char *line) +{ + ssize_t goto_rule; + t_vec gotos; + + ft_vec_init(&gotos, sizeof(ssize_t)); + while (*line) { - while (condensed_line[i] && condensed_line[i++] != ';'); - if (!condensed_line[i]) + while (*line && *line != ';') + ++line; + if (!*line) + break ; + else + ++line; + if (!*line) break ; - else if (condensed_line[i] == ';') + else if (*line == ';') goto_rule = -1; else - goto_rule = ft_atoi(condensed_line + i); - ft_vec_append(&state.gotos, &goto_rule); + goto_rule = ft_atoi(line); + ft_vec_append(&gotos, &goto_rule); + } + return (gotos); +} + +static int add_line(t_vec *states, const char *line, size_t lookahead_size) +{ + t_parser_state state; + char *condensed_line; + size_t i; + + condensed_line = ft_remove_space(line); + state.lookahead = parse_lookahead(condensed_line, lookahead_size); + while (lookahead_size > 0) + { + while (condensed_line[i] && condensed_line[i] != ';') + ++i; + if (!condensed_line[i]) + ++i; + --lookahead_size; } + state.gotos = parse_goto(condensed_line + i); free(condensed_line); ft_vec_append(states, &state); return (0); } +static char *get_token_type(const char *line) +{ + size_t i; + char *type; + + i = 0; + while (line[i] && line[i] != ';') + ++i; + type = ft_strndup(line, i); + return (type); +} + 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] && condensed_line[i] != ';') + ++i; + if (condensed_line[i]) + ++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); - free(condensed_line); - ft_vec_free(&tokens, free_token); - return (tokens); - } + token.type = get_token_type(condensed_line + i); + ft_vec_append(&tokens, &token); ++i; } free(condensed_line); return (tokens); } +static void ft_init_parsing_table(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)); + return ; +} + t_parsing_table ft_load_parsing_table(const char *filename, const char *rules_filename) { @@ -221,11 +277,8 @@ 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); + ft_init_parsing_table(&table); + load_rules(&table.rules, rules_filename); fd = open(filename, O_RDONLY); if (fd < 0) return (table); -- 2.30.2