-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer.c
106 lines (97 loc) · 3.29 KB
/
lexer.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jaesjeon <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/20 15:55:53 by jaesjeon #+# #+# */
/* Updated: 2022/09/27 01:12:21 by minsuki2 ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell_info.h"
#include "minishell.h"
#include "ft_print.h"
#include "ft_string.h"
#include "ft_token.h"
#include "ft_check.h"
static void *_handle_encounter_eof(t_oflag *oflag)
{
if (oflag->quote == QUOTE)
print_error_not_close("'");
if (oflag->quote == DQUOTE)
print_error_not_close("\"");
if (oflag->parentheses > 0)
print_error_not_close("(");
if (oflag->parentheses < 0)
print_error_syntax(")");
return (NULL);
}
static int _check_doubled_operator(char **line)
{
if (**line == '|' && *(*line + 1) == '|')
return (OR_IF);
else if (**line == '&' && *(*line + 1) == '&')
return (AND_IF);
else if (**line == '<' && *(*line + 1) == '<')
return (HERE_DOC);
else if (**line == '>' && *(*line + 1) == '>')
return (RED_APD_OUT);
return (FALSE);
}
static int _make_operator(t_lx_token *token_node, char **line, int token_type)
{
const char *str_startpoint = *line;
if (token_type == FALSE)
return (FALSE);
(*line) += 2;
token_node->token_str = ft_strcpy(str_startpoint, *line);
token_node->token_type = token_type;
return (SUCCESS);
}
t_lx_token *set_token(char **line, t_oflag *oflag)
{
t_lx_token *token_node;
const int token_split_flag = is_token_seperator(**line);
const char *str_startpoint = *line;
token_node = make_token_node(NULL, UNDEFINED);
if (_make_operator(token_node, line, _check_doubled_operator(line)))
return (token_node);
while (**line && (oflag->quote || (token_node->token_type == UNDEFINED \
|| !is_token_seperator(**line))))
{
set_quote_flag(**line, &oflag->quote);
set_token_type(token_node, **line);
set_need_translate_symbol(token_node, **line, &oflag->quote);
(*line)++;
if (token_split_flag || (!oflag->quote && **line == '&' \
&& *(*line + 1) == '&'))
break ;
}
token_node->token_str = ft_strcpy(str_startpoint, *line);
return (token_node);
}
t_lx_token *lexer(char *full_line, t_oflag *oflag)
{
t_lx_token *token_head;
t_lx_token *token_cur;
if (oflag->parentheses || oflag->quote)
return (_handle_encounter_eof(oflag));
token_head = NULL;
while (*full_line || token_cur)
{
if (ft_isspace(*full_line) && full_line++)
continue ;
if (token_head == NULL)
{
token_head = set_token(&full_line, oflag);
token_head->prev = NULL;
token_cur = token_head;
}
else if (*full_line && token_cur->next == NULL)
token_cur->next = set_token(&full_line, oflag);
else
token_cur = connect_token(token_cur);
}
return (token_head);
}