aboutsummaryrefslogtreecommitdiff
path: root/src/parser/parser.c
blob: 0e3fbafd43bdc7e18518e4f3294d1735b75e4398 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   parser.c                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: nahaddac <nahaddac@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/06/17 18:09:04 by nahaddac          #+#    #+#             */
/*   Updated: 2020/10/06 11:22:14 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

/*
** \file   parse.c
** \brief  Parser
*/

#include "parser.h"

static char *g_sep_str_lookup[] = {
	[TAG_END] = ";",
	[TAG_AND] = "&&",
	[TAG_OR] = "||",
	[TAG_PIPE] = "|",
	[TAG_REDIR_IN] = "<",
	[TAG_REDIR_OUT] = ">",
	[TAG_REDIR_APPEND] = ">>",
	[TAG_PARENT_OPEN] = "(",
	[TAG_PARENT_CLOSE] = ")",
};

/*
** push redirection token
** push while token is str and stick
** push last str token
*/

t_parsed	*parse_redir(t_tok_lst *input, t_tok_lst **redirs)
{
	tok_lst_push_back(redirs, tok_lst_uncons(&input));
	if (input == NULL)
		return (parsed_error("syntax error near unexpected token `newline'\n"));
	// FIXME big condition could be avoided with lexer not putting STICK
	// if the next tokens isn't a string
	while (input != NULL
			&& input->tag & TAG_IS_STR && input->tag & TAG_STICK
			&& input->next != NULL && input->next->tag & TAG_IS_STR)
		tok_lst_push_back(redirs, tok_lst_uncons(&input));
	if (input == NULL)
		return (NULL);
	if (!(input->tag & TAG_IS_STR))
		return (parsed_error("syntax error near unexpected token `%s'\n", input->content));
	tok_lst_push_back(redirs, tok_lst_uncons(&input));
	return (parsed_new(NULL, input));
}

t_parsed	*parse_cmd(t_tok_lst *input)
{
	t_ast		*ast;
	t_parsed	*tmp;

	if (input->tag & TAG_IS_SEP)
		return (parsed_error("syntax error near unexpected token `%s'\n", input->content));
	if ((ast = ast_new(AST_CMD)) == NULL)
		return (NULL);
	while (input != NULL)
	{
		if (input->tag & TAG_IS_STR)
			tok_lst_push_back(&ast->cmd_argv, tok_lst_uncons(&input));
		else if (input->tag & TAG_IS_REDIR)
		{
			tmp = parse_redir(input, &ast->redirs);
			if (tmp == NULL || tmp->syntax_error)
				return (tmp);
			input = tmp->rest;
		}
		else
			break;
	}
	return (parsed_new(ast, input));
}

t_parsed	*parse_pipeline(t_tok_lst *input)
{
	t_parsed	*expr;
	t_parsed	*tail;
	t_ast		*expr_ast;

	expr = parse_expr(input);
	if (expr == NULL || expr->syntax_error ||
		expr->rest == NULL || expr->rest->tag != TAG_PIPE)
		return (expr);
	tail = parse_pipeline(expr->rest->next);
	if (tail == NULL || tail->syntax_error)
		return (tail);
	expr_ast = expr->ast;
	free(expr);
	t_ast *pipeline_ast;
	if (tail->ast->tag == AST_CMD)
	{
		pipeline_ast = ast_new(AST_PIPELINE);
		if ((pipeline_ast->pipeline = ft_lstnew(tail->ast)) == NULL)
		{
			/* ast_destroy(expr_ast); */
			/* ft_lstdestroy(&tail, NULL); */
			return (NULL);
		}
	}
	else
		pipeline_ast = tail->ast;
	if (ft_lstpush_front_node(&pipeline_ast->pipeline, expr_ast) == NULL)
	{
		/* ast_destroy(expr_ast); */
		/* ft_lstdestroy(&tail, NULL); */
		return (NULL);
	}
	return (parsed_new(pipeline_ast, tail->rest));
}

/*
** parse and operation (| ; && ||)
*/

t_parsed	*parse_op(t_tok_lst *input)
{
	t_ast		*ast;
	t_parsed	*left;
	t_parsed	*right;
	enum e_tok	sep_tag;

	left = parse_pipeline(input);
	if (left == NULL || left->syntax_error)
		return (left);
	input = left->rest;
	if (input == NULL || input->tag & TAG_PARENT_CLOSE)
		return (left);
	sep_tag = input->tag;
	if (!(sep_tag & TAG_IS_SEP))
		return (parsed_error("syntax error near unexpected token `%s'\n",
				g_sep_str_lookup[sep_tag]));
	ft_lstpop_front((t_ftlst**)&input, free);
	if (input == NULL && sep_tag == TAG_END)
		return (left);
	if (input == NULL)
		return (parsed_error("syntax error expected token\n"));
	right = parse_op(input);
	if (right == NULL  || right->syntax_error)
		return (right);
	if ((ast = ast_new(AST_OP)) == NULL)
		return (NULL);
	ast->op.left = left->ast;
	ast->op.right = right->ast;
	ast->op.sep = sep_tag;
	free(left);
	left = parsed_new(ast, right->rest);
	free(right);
	return (left);
}

/*
** try to parse parenthesis, if fail parse a command.
** parenthesis can be followed by redirections
*/

t_parsed	*parse_expr(t_tok_lst *input)
{
    t_parsed	*parsed;
    t_parsed	*tmp;
	t_ast		*ast;

    if (input->tag & TAG_PARENT_OPEN)
    {
        if (!(parsed = parse_op(input->next)) || parsed->syntax_error)
			return (parsed);
        input = parsed->rest;
		if (input == NULL || !(input->tag & TAG_PARENT_CLOSE))
			return (parsed_error("syntax error expected token\n"));
        input = input->next;
        if ((ast = ast_new(AST_PARENT)) == NULL)
			return (NULL);
        ast->parent_ast = parsed->ast;
        parsed->ast = ast;
		while (input != NULL && input->tag & TAG_IS_REDIR)
		{
			tmp = parse_redir(input, &parsed->ast->redirs);
			if (tmp == NULL || tmp->syntax_error)
				return (tmp);
			input = tmp->rest;
		}
        parsed->rest = input;
        return (parsed);
    }
    return (parse_cmd(input));
}

t_parsed	*parse(t_tok_lst *input)
{
	if (input == NULL)
		return (NULL);
	return (parse_op(input));
}