Recognize tokenization errors (as parsing errors)
authorLukas Jiriste <ljiriste@student.42prague.com>
Sat, 18 Jan 2025 14:41:15 +0000 (15:41 +0100)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 18 Jan 2025 14:41:15 +0000 (15:41 +0100)
src/tokenize.c

index 49f53064afcb1f76c9899e54b77e553a444efd83..dd98dd552c46dc28a58e715f4d033d33870ab828 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2025/01/14 14:49:32 by ljiriste          #+#    #+#             */
-/*   Updated: 2025/01/17 12:05:04 by ljiriste         ###   ########.fr       */
+/*   Updated: 2025/01/18 15:34:18 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -61,21 +61,22 @@ static int  create_token(t_token *token, const char *line, size_t *i)
                token->type = "INT";
                token->str = extract_int(line, i);
                if (!token->str)
-                       return (1);
+                       return (ALLOCATION_FAILURE);
        }
        else
        {
                token->type = (char *)extract_keyword(line, i);
                if (!token->type)
-                       return (1);
+                       return (PARSING_ERROR);
        }
-       return (0);
+       return (SUCCESS);
 }
 
-static int     tokenize_line(const char *line, t_vec *tokens)
+static t_res   tokenize_line(const char *line, t_vec *tokens)
 {
        size_t  i;
        t_token token;
+       t_res   res;
 
        i = 0;
        while (line[i])
@@ -85,12 +86,13 @@ static int  tokenize_line(const char *line, t_vec *tokens)
                        ++i;
                if (!line[i])
                        break ;
-               if (create_token(&token, line, &i))
-                       return (1);
+               res = create_token(&token, line, &i);
+               if (res != SUCCESS)
+                       return (res);
                if (ft_vec_append(tokens, &token) != success)
                {
                        free_token(&token);
-                       return (1);
+                       return (ALLOCATION_FAILURE);
                }
                ++i;
        }
@@ -101,6 +103,7 @@ t_res       tokenize(const char *filename, t_vec *tokens)
 {
        int             fd;
        char    *line;
+       t_res   res;
 
        fd = open(filename, O_RDONLY);
        if (fd < 0)
@@ -108,14 +111,17 @@ t_res     tokenize(const char *filename, t_vec *tokens)
        line = get_next_line(fd);
        while (line)
        {
-               if (ft_strncmp(line, "//", 2) && tokenize_line(line, tokens))
+               if (ft_strncmp(line, "//", 2))
                {
+                       res = tokenize_line(line, tokens);
                        free(line);
-                       get_next_line(-1);
-                       close(fd);
-                       return (ALLOCATION_FAILURE);
+                       if (res != SUCCESS)
+                       {
+                               get_next_line(-1);
+                               close(fd);
+                               return (res);
+                       }
                }
-               free(line);
                line = get_next_line(fd);
        }
        close(fd);