-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse1.c
91 lines (82 loc) · 2.38 KB
/
parse1.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
#include <stdlib.h>
#include "parse.h"
/*
**command_line ::=
** "\n"
** | sequential_commands "\n"
*/
t_parse_ast *parse_command_line(
t_parse_buffer *buf, t_token *tok)
{
t_parse_ast *cmdline_node;
t_parse_ast *seqcmd_node;
t_parse_node_cmdline *content_node;
seqcmd_node = parse_sequential_commands(buf, tok);
if (!seqcmd_node)
return (NULL);
parse_skip_spaces(buf, tok);
if (tok->type != TOKTYPE_NEWLINE)
return (NULL);
content_node = malloc(sizeof(t_parse_node_cmdline));
cmdline_node = parse_new_ast_node(ASTNODE_COMMAND_LINE, content_node);
content_node->seqcmd_node = seqcmd_node;
cmdline_node->error = cmdline_node->error || seqcmd_node->error;
if (cmdline_node->error)
return (NULL);
cmdline_node->heredocs = seqcmd_node->heredocs;
return (cmdline_node);
}
/*
**delimiter ::=
** ";"
** | (bonus) "&"
*/
t_parse_ast *parse_delimiter(
t_parse_buffer *buf, t_token *tok)
{
t_parse_ast *delim_node;
t_parse_node_delimiter *content_node;
parse_skip_spaces(buf, tok);
if (tok->type != TOKTYPE_SEMICOLON)
return (NULL);
content_node = malloc(sizeof(t_parse_node_delimiter));
delim_node = parse_new_ast_node(ASTNODE_DELIMITER, content_node);
content_node->type = TOKTYPE_SEMICOLON;
return (delim_node);
}
/*
**sequential_commands ::=
** piped_commands delimiter sequential_commands
** | piped_commands delimiter
** | piped_commands
** | (bonus) piped_commands "&&" sequential_commands
** | (bonus) piped_commands "||" sequential_commands
*/
t_parse_ast *parse_sequential_commands(
t_parse_buffer *buf, t_token *tok)
{
t_parse_ast *seq_node;
t_parse_ast *pipcmd_node;
t_parse_ast *rest_node;
t_parse_node_seqcmds *content;
parse_skip_spaces(buf, tok);
pipcmd_node = parse_piped_commands(buf, tok);
if (!pipcmd_node)
return (NULL);
content = malloc(sizeof(t_parse_node_seqcmds));
seq_node = parse_new_ast_node(ASTNODE_SEQ_COMMANDS, content);
content->pipcmd_node = pipcmd_node;
content->delimiter_node = parse_delimiter(buf, tok);
rest_node = NULL;
if (content->delimiter_node)
{
lex_get_token(buf, tok);
parse_skip_spaces(buf, tok);
rest_node = parse_sequential_commands(buf, tok);
}
content->rest_node = rest_node;
seq_node->error = seq_node->error || pipcmd_node->error;
seq_node->error = seq_node->error || (rest_node && rest_node->error);
seq_node->heredocs = parse_concat_heredocs(pipcmd_node, rest_node);
return (seq_node);
}