-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_list.c
117 lines (108 loc) · 2.83 KB
/
token_list.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* token_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: almelo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/19 13:40:12 by almelo #+# #+# */
/* Updated: 2023/03/31 23:53:05 by almelo ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static enum e_label get_label(char *input, size_t i)
{
if (input[i] == '|')
return (PIPE);
else if (input[i] == '<')
{
if (input[i + 1] == '<')
return (HEREDOC);
else
return (IN);
}
else if (input[i] == '>')
{
if (input[i + 1] == '>')
return (APPEND);
else
return (OUT);
}
else
return (WORD);
}
enum e_bool get_state(char *input, int i, enum e_bool is_quoted, size_t *count)
{
static char quote;
if (is_quote(input[i]))
{
if (!is_quoted)
{
quote = input[i];
is_quoted = TRUE;
}
if (is_quoted && input[i] == quote)
*count -= 1;
if (*count == 0)
is_quoted = FALSE;
i++;
}
return (is_quoted);
}
static size_t count_quote(char *input)
{
size_t counter;
size_t i;
char quote;
enum e_bool is_quoted;
is_quoted = FALSE;
counter = 0;
i = 0;
while (input[i])
{
if (is_quote(input[i]))
{
if (!is_quoted)
{
quote = input[i];
is_quoted = TRUE;
counter++;
}
else if (is_quoted && input[i] == quote)
counter++;
}
i++;
}
return (counter);
}
static void update_state(t_lexer_state *state, size_t i)
{
state->is_word = TRUE;
state->curr = i;
}
void tokenize_input(t_tokenl *token_lst, char *input, t_lexer_state *state)
{
size_t i;
static size_t count;
token_lst->head = NULL;
count = count_quote(input);
i = 0;
while (i <= ft_strlen(input))
{
state->is_quoted = get_state(input, i, state->is_quoted, &count);
if ((is_metachar(input[i]) || input[i] == '\0')
&& (state->is_word && !state->is_quoted))
{
state->input[i] = '\0';
queue_token(token_lst, new_token(state->input + state->curr, WORD));
if (is_operator(input[i]))
queue_token(token_lst, new_token(NULL, get_label(input, i)));
state->is_word = FALSE;
}
else if (is_operator(input[i]) && !state->is_word)
queue_token(token_lst, new_token(NULL, get_label(input, i)));
if (!(is_metachar(input[i])) && !state->is_word && input[i] != '\0')
update_state(state, i);
i++;
}
}