Implement parse_header, minor fixes
authorLukas Jiriste <ljiriste@student.42prague.com>
Sat, 15 Jun 2024 07:39:27 +0000 (09:39 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 15 Jun 2024 07:39:27 +0000 (09:39 +0200)
ft_parse/ft_parse.c
inc/libft.h

index c2ead50a5af2a269f596e5497329cc4ef757ae52..0e1148655c7fac04926aa93efae612e0349b371e 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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);
index 33d09625a043184ee34496462cd8bed636c93eb9..595e71b63811f22cf7581bf5590cc1a98e46e963 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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