aboutsummaryrefslogtreecommitdiff
path: root/include/parser.h
blob: b00407cc284868c7fd17c9195c67fee01790e905 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   parser.h                                           :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <cacharle@student.42.fr>          +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/28 09:00:00 by cacharle          #+#    #+#             */
/*   Updated: 2020/10/07 16:33:16 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#ifndef PARSE_H
# define PARSE_H

/*
** \file   parser.h
** \brief  Input parsing and AST manipulation
**
** Context free grammar:
** ```
** redir_in     ::= '<' <string>
** redir_out    ::= '>' <string>
** redir_append ::= '>>' <string>
** string       ::= '\'' .+ '\'' | '"' .+ '"' | [^ ]+
** sep          ::= '&&' | '||' | '|' | ';'
** expr         ::= <redir_in> | <redir_out> | <redir_append> | <string>
** cmd          ::= <expr>+
** line         ::= <cmd> <sep> <line> | <cmd>
** ```
*/

# include <stdlib.h>
# include <stdbool.h>
# include "libft_lst.h"
# include "lexer.h"
# include "error.h"

struct s_ast;

/*
** \brief            Operation struct
** \param left       AST to the left of separator
** \param right      AST to the right of separator
** \param sep        Type of separator
*/


typedef struct			s_op
{
	struct s_ast		*left;
	struct s_ast		*right;
	enum e_tok	sep;
}						t_op;

/*
** \brief              AST node tag (type)
** \param TAG_CMD      Command AST node
** \param TAG_OP       Operation AST node
** \param TAG_PARENT   Parenthesis AST node
** \param TAG_PIPELINE Pipeline AST node
*/

enum					e_ast
{
	AST_CMD,
	AST_OP,
	AST_PARENT,
	AST_PIPELINE,
};

/*
** \brief             AST node struct
** \param tag         Node tag
** \param op          Operation struct
** \param cmd_argv    Command argv tokens
** \param pipline     List of commands in a pipeline
** \param parend_ast  AST inside parenthesis
** \param redirs      Redirections tokens
*/

typedef struct			s_ast
{
	enum e_ast			tag;
	union
	{
		t_op			op;
		t_tok_lst		*cmd_argv;
		t_ftlst			*pipeline;
		struct s_ast	*parent_ast;
	};
	t_tok_lst			*redirs;
}						t_ast;

/*
** ast.c
*/

t_ast					*ast_new(enum e_ast tag);
void					ast_destroy(t_ast *ast);

/*
** parsed.c
*/

typedef struct			s_parsed
{
	bool				syntax_error;
	t_ast				*ast;
	t_tok_lst			*rest;
}						t_parsed;

t_parsed				*parsed_new(t_ast *ast, t_tok_lst *rest);
t_parsed				*parsed_error(const char *format, ...);

/*
** parse.c
*/

t_parsed				*parse(t_tok_lst *input);
t_parsed		        *parse_op(t_tok_lst *input);
t_parsed		        *parse_expr(t_tok_lst *input);
t_parsed               	*parse_cmd(t_tok_lst *input);


#endif