Minishell 1.0
42 School Minishell Project - A simple shell implementation
Yüklüyor...
Arıyor...
Eşleşme Yok
lexer.c
Bu dosyanın dokümantasyonuna git.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* lexer.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: bkinali <bkinali@student.42.fr> #+# +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025-11-14 10:16:57 by bkinali #+# #+# */
9/* Updated: 2025-11-14 10:16:57 by bkinali ### ########.tr */
10/* */
11/* ************************************************************************** */
12
13/**
14 * @file lexer.c
15 * @brief Lexer (Sözcüksel Analiz) modülü
16 *
17 * Kullanıcı girişini token'lara ayırır. Shell pipeline'ının ilk aşaması.
18 * Girdi: shell->cmd_line (string)
19 * Çıktı: shell->token_list (linked list of t_token)
20 */
21
23#include <stdio.h>
24
25/**
26 * @brief Bir sonraki token'ı işler
27 * @param shell Shell yapısı
28 * @param i Mevcut indeks
29 * @return int Tüketilen karakter sayısı veya hata için -1
30 */
31int process_next_token(t_shell *shell, int i)
32{
33 t_char_category c_cat;
34 int consumed_len;
35
36 c_cat = assess_char_category(shell->cmd_line[i]);
37 consumed_len = category_dispatcher(shell, c_cat, i);
38 return (consumed_len);
39}
40
41/**
42 * @brief Ardışık newline ve whitespace'leri atlayıp TOKEN_NEWLINE üretir
43 * @return int Başarı: 1, Hata: 0
44 */
45static int handle_newline(t_shell *shell, int *i, int *token_end)
46{
47 while (shell->cmd_line[*i] == '\n' || shell->cmd_line[*i] == ' '
48 || shell->cmd_line[*i] == '\t')
49 (*i)++;
50 *token_end = -1;
51 if (shell->cmd_line[*i])
52 return (append_token(shell, TOKEN_NEWLINE, "\n", QUOTE_NONE));
53 return (1);
54}
55
56/**
57 * @brief Lexer ana fonksiyonu — komut satırını token'lara ayırır
58 * @return int Başarı: 1, Hata: 0
59 */
60int lexer(t_shell *shell)
61{
62 int i;
63 int consumed_len;
64 int token_end;
65
66 i = 0;
67 token_end = -1;
68 while (shell->cmd_line[i])
69 {
70 i = skip_whitespace(shell->cmd_line, i);
71 if (!shell->cmd_line[i])
72 break ;
73 if (shell->cmd_line[i] == '\n')
74 {
75 if (!handle_newline(shell, &i, &token_end))
76 return (0);
77 continue ;
78 }
79 consumed_len = process_next_token(shell, i);
80 if (consumed_len < 0)
81 return (0);
82 if (token_end == i)
84 i += consumed_len;
85 token_end = i;
86 }
87 return (append_token(shell, TOKEN_EOF, NULL, QUOTE_NONE));
88}
static int handle_newline(t_shell *shell, int *i, int *token_end)
Ardışık newline ve whitespace'leri atlayıp TOKEN_NEWLINE üretir.
Definition lexer.c:45
int process_next_token(t_shell *shell, int i)
Bir sonraki token'ı işler.
Definition lexer.c:31
int lexer(t_shell *shell)
Lexer ana fonksiyonu — komut satırını token'lara ayırır.
Definition lexer.c:60
t_char_category assess_char_category(char c)
int category_dispatcher(t_shell *shell, t_char_category category, int i)
void mark_last_token_adjacent(t_shell *shell)
int append_token(t_shell *shell, t_token_type type, char *value, t_quote_type quote_type)
Minishell ana header dosyası
enum e_char_category t_char_category
@ QUOTE_NONE
Definition minishell.h:77
@ TOKEN_NEWLINE
Definition minishell.h:58
@ TOKEN_EOF
Definition minishell.h:56
struct s_shell t_shell
char * cmd_line
Definition minishell.h:152
int skip_whitespace(char *cmd, int i)
Definition utils.c:16