forked from zhanghai/zhsh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathline_parser.y
168 lines (154 loc) · 4.37 KB
/
line_parser.y
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
%include {
#include "line_parser.h"
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "line_syntax.h"
#include "util.h"
#define BREAK_IF_ERRNO \
if (errno) { \
break; \
}
}
%name LineParser
%token_type { char * }
%token_destructor {
free($$);
}
%type redirection { redir_t * }
%destructor redirection {
redir_free($$);
}
%type command { cmd_t * }
%destructor command {
cmd_free($$);
}
%type commandList { cmd_list_t * }
%destructor commandList {
cmd_list_free($$);
}
%type start { void * }
%destructor start {}
%extra_argument { cmd_list_t **cmd_list_p }
%parse_failure {
errno = EINVAL;
}
%start_symbol start
redirection(REDIR) ::= REDIRECT_INPUT_FROM_FILE(RIFF) ARGUMENT(ARG) . {
BREAK_IF_ERRNO
REDIR = redir_alloc();
BREAK_IF_ERRNO
REDIR->left_fd = redir_parse_left_fd(RIFF, 1, STDIN_FILENO);
REDIR->type = REDIRECT_INPUT_FROM_FILE;
REDIR->right_file = ARG;
}
redirection(REDIR) ::= REDIRECT_INPUT_FROM_FILE_DESCRIPTOR(RIFFD) ARGUMENT(ARG) . {
BREAK_IF_ERRNO
REDIR = redir_alloc();
BREAK_IF_ERRNO
REDIR->left_fd = redir_parse_left_fd(RIFFD, 2, STDIN_FILENO);
REDIR->type = REDIRECT_INPUT_FROM_FILE_DESCRIPTOR;
REDIR->right_fd = redir_parse_fd(ARG);
free(ARG);
}
redirection(REDIR) ::= REDIRECT_OUTPUT_TO_FILE(ROTF) ARGUMENT(ARG) . {
BREAK_IF_ERRNO
REDIR = redir_alloc();
BREAK_IF_ERRNO
REDIR->left_fd = redir_parse_left_fd(ROTF, 1, STDOUT_FILENO);
REDIR->type = REDIRECT_OUTPUT_TO_FILE;
REDIR->right_file = ARG;
}
redirection(REDIR) ::= REDIRECT_OUTPUT_TO_FILE_DESCRIPTOR(ROTFD) ARGUMENT(ARG) . {
BREAK_IF_ERRNO
REDIR = redir_alloc();
BREAK_IF_ERRNO
REDIR->left_fd = redir_parse_left_fd(ROTFD, 2, STDOUT_FILENO);
REDIR->type = REDIRECT_OUTPUT_TO_FILE_DESCRIPTOR;
REDIR->right_fd = redir_parse_fd(ARG);
free(ARG);
}
redirection(REDIR) ::= REDIRECT_OUTPUT_APPEND_TO_FILE(ROATF) ARGUMENT(ARG) . {
BREAK_IF_ERRNO
REDIR = redir_alloc();
BREAK_IF_ERRNO
REDIR->left_fd = redir_parse_left_fd(ROATF, 2, STDOUT_FILENO);
REDIR->type = REDIRECT_OUTPUT_APPEND_TO_FILE;
REDIR->right_file = ARG;
}
command(CMD) ::= ARGUMENT(ARG) . {
BREAK_IF_ERRNO
CMD = cmd_alloc();
BREAK_IF_ERRNO
strarr_append(&(CMD->args), ARG);
}
command(CMD) ::= redirection(REDIR) . {
BREAK_IF_ERRNO
CMD = cmd_alloc();
BREAK_IF_ERRNO
ptrarr_append(&(CMD->redirs), REDIR);
}
command(CMD) ::= command(CMD_) ARGUMENT(ARG) . {
BREAK_IF_ERRNO
CMD = CMD_;
strarr_append(&(CMD->args), ARG);
}
command(CMD) ::= command(CMD_) redirection(REDIR) . {
BREAK_IF_ERRNO
CMD = CMD_;
ptrarr_append(&(CMD->redirs), REDIR);
}
commandList(CMD_LIST) ::= command(CMD) . {
BREAK_IF_ERRNO
CMD_LIST = cmd_list_alloc();
BREAK_IF_ERRNO
ptrarr_append(&(CMD_LIST->cmds), CMD);
}
commandList(CMD_LIST) ::= commandList(CMD_LIST_) PIPE command(CMD) . {
BREAK_IF_ERRNO
CMD_LIST = CMD_LIST_;
intarr_append(&(CMD_LIST->ops), PIPE);
ptrarr_append(&(CMD_LIST->cmds), CMD);
}
commandList(CMD_LIST) ::= commandList(CMD_LIST_) OR command(CMD) . {
BREAK_IF_ERRNO
CMD_LIST = CMD_LIST_;
intarr_append(&(CMD_LIST->ops), OR);
ptrarr_append(&(CMD_LIST->cmds), CMD);
}
commandList(CMD_LIST) ::= commandList(CMD_LIST_) AND command(CMD) . {
BREAK_IF_ERRNO
CMD_LIST = CMD_LIST_;
intarr_append(&(CMD_LIST->ops), AND);
ptrarr_append(&(CMD_LIST->cmds), CMD);
}
commandList(CMD_LIST) ::= commandList(CMD_LIST_) BACKGROUND command(CMD). {
BREAK_IF_ERRNO
CMD_LIST = CMD_LIST_;
intarr_append(&(CMD_LIST->ops), BACKGROUND);
ptrarr_append(&(CMD_LIST->cmds), CMD);
}
commandList(CMD_LIST) ::= commandList(CMD_LIST_) BACKGROUND . {
BREAK_IF_ERRNO
CMD_LIST = CMD_LIST_;
intarr_append(&(CMD_LIST->ops), BACKGROUND);
}
commandList(CMD_LIST) ::= commandList(CMD_LIST_) SEMICOLON command(CMD). {
BREAK_IF_ERRNO
CMD_LIST = CMD_LIST_;
intarr_append(&(CMD_LIST->ops), SEMICOLON);
ptrarr_append(&(CMD_LIST->cmds), CMD);
}
commandList(CMD_LIST) ::= commandList(CMD_LIST_) SEMICOLON . {
BREAK_IF_ERRNO
CMD_LIST = CMD_LIST_;
intarr_append(&(CMD_LIST->ops), SEMICOLON);
}
start ::= commandList(CMD_LIST) . {
BREAK_IF_ERRNO
// Save our AST from being freed by Lemon!
*cmd_list_p = CMD_LIST;
}