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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use super::core::Parser;
use super::core::Result;
use super::error::Error;
use super::error::SyntaxError;
use super::lex::Keyword::{Case, Do, Done, For, If, OpenBrace, Until, While};
use super::lex::Operator::OpenParen;
use super::lex::TokenId::{Operator, Token};
use crate::syntax::CompoundCommand;
use crate::syntax::FullCompoundCommand;
use crate::syntax::List;
impl Parser<'_, '_> {
pub async fn do_clause(&mut self) -> Result<Option<List>> {
if self.peek_token().await?.id != Token(Some(Do)) {
return Ok(None);
}
let open = self.take_token_raw().await?;
let list = self.maybe_compound_list_boxed().await?;
let close = self.take_token_raw().await?;
if close.id != Token(Some(Done)) {
let opening_location = open.word.location;
let cause = SyntaxError::UnclosedDoClause { opening_location }.into();
let location = close.word.location;
return Err(Error { cause, location });
}
if list.0.is_empty() {
let cause = SyntaxError::EmptyDoClause.into();
let location = close.word.location;
return Err(Error { cause, location });
}
Ok(Some(list))
}
pub async fn compound_command(&mut self) -> Result<Option<CompoundCommand>> {
match self.peek_token().await?.id {
Token(Some(OpenBrace)) => self.grouping().await.map(Some),
Operator(OpenParen) => self.subshell().await.map(Some),
Token(Some(For)) => self.for_loop().await.map(Some),
Token(Some(While)) => self.while_loop().await.map(Some),
Token(Some(Until)) => self.until_loop().await.map(Some),
Token(Some(If)) => self.if_command().await.map(Some),
Token(Some(Case)) => self.case_command().await.map(Some),
_ => Ok(None),
}
}
pub async fn full_compound_command(&mut self) -> Result<Option<FullCompoundCommand>> {
let command = match self.compound_command().await? {
Some(command) => command,
None => return Ok(None),
};
let redirs = self.redirections().await?;
Ok(Some(FullCompoundCommand { command, redirs }))
}
}
#[allow(clippy::bool_assert_comparison)]
#[cfg(test)]
mod tests {
use super::super::error::ErrorCause;
use super::super::lex::Lexer;
use super::super::lex::Operator::Semicolon;
use super::super::lex::TokenId::EndOfInput;
use super::*;
use crate::alias::{AliasSet, HashEntry};
use crate::source::Location;
use crate::source::Source;
use crate::syntax::Command;
use crate::syntax::SimpleCommand;
use assert_matches::assert_matches;
use futures_executor::block_on;
#[test]
fn parser_do_clause_none() {
let mut lexer = Lexer::from_memory("done", Source::Unknown);
let aliases = Default::default();
let mut parser = Parser::new(&mut lexer, &aliases);
let result = block_on(parser.do_clause()).unwrap();
assert!(result.is_none(), "result should be none: {:?}", result);
}
#[test]
fn parser_do_clause_short() {
let mut lexer = Lexer::from_memory("do :; done", Source::Unknown);
let aliases = Default::default();
let mut parser = Parser::new(&mut lexer, &aliases);
let result = block_on(parser.do_clause()).unwrap().unwrap();
assert_eq!(result.to_string(), ":");
let next = block_on(parser.peek_token()).unwrap();
assert_eq!(next.id, EndOfInput);
}
#[test]
fn parser_do_clause_long() {
let mut lexer = Lexer::from_memory("do foo; bar& done", Source::Unknown);
let aliases = Default::default();
let mut parser = Parser::new(&mut lexer, &aliases);
let result = block_on(parser.do_clause()).unwrap().unwrap();
assert_eq!(result.to_string(), "foo; bar&");
let next = block_on(parser.peek_token()).unwrap();
assert_eq!(next.id, EndOfInput);
}
#[test]
fn parser_do_clause_unclosed() {
let mut lexer = Lexer::from_memory(" do not close ", Source::Unknown);
let aliases = Default::default();
let mut parser = Parser::new(&mut lexer, &aliases);
let e = block_on(parser.do_clause()).unwrap_err();
assert_matches!(e.cause,
ErrorCause::Syntax(SyntaxError::UnclosedDoClause { opening_location }) => {
assert_eq!(*opening_location.code.value.borrow(), " do not close ");
assert_eq!(opening_location.code.start_line_number.get(), 1);
assert_eq!(opening_location.code.source, Source::Unknown);
assert_eq!(opening_location.index, 1);
});
assert_eq!(*e.location.code.value.borrow(), " do not close ");
assert_eq!(e.location.code.start_line_number.get(), 1);
assert_eq!(e.location.code.source, Source::Unknown);
assert_eq!(e.location.index, 14);
}
#[test]
fn parser_do_clause_empty_posix() {
let mut lexer = Lexer::from_memory("do done", Source::Unknown);
let aliases = Default::default();
let mut parser = Parser::new(&mut lexer, &aliases);
let e = block_on(parser.do_clause()).unwrap_err();
assert_eq!(e.cause, ErrorCause::Syntax(SyntaxError::EmptyDoClause));
assert_eq!(*e.location.code.value.borrow(), "do done");
assert_eq!(e.location.code.start_line_number.get(), 1);
assert_eq!(e.location.code.source, Source::Unknown);
assert_eq!(e.location.index, 3);
}
#[test]
fn parser_do_clause_aliasing() {
let mut lexer = Lexer::from_memory(" do :; end ", Source::Unknown);
let mut aliases = AliasSet::new();
let origin = Location::dummy("");
aliases.insert(HashEntry::new(
"do".to_string(),
"".to_string(),
false,
origin.clone(),
));
aliases.insert(HashEntry::new(
"done".to_string(),
"".to_string(),
false,
origin.clone(),
));
aliases.insert(HashEntry::new(
"end".to_string(),
"done".to_string(),
false,
origin,
));
let mut parser = Parser::new(&mut lexer, &aliases);
let result = block_on(parser.do_clause()).unwrap().unwrap();
assert_eq!(result.to_string(), ":");
let next = block_on(parser.peek_token()).unwrap();
assert_eq!(next.id, EndOfInput);
}
#[test]
fn parser_compound_command_none() {
let mut lexer = Lexer::from_memory("}", Source::Unknown);
let aliases = Default::default();
let mut parser = Parser::new(&mut lexer, &aliases);
let option = block_on(parser.compound_command()).unwrap();
assert_eq!(option, None);
}
#[test]
fn parser_full_compound_command_without_redirections() {
let mut lexer = Lexer::from_memory("(:)", Source::Unknown);
let aliases = Default::default();
let mut parser = Parser::new(&mut lexer, &aliases);
let FullCompoundCommand { command, redirs } =
block_on(parser.full_compound_command()).unwrap().unwrap();
assert_eq!(command.to_string(), "(:)");
assert_eq!(redirs, []);
}
#[test]
fn parser_full_compound_command_with_redirections() {
let mut lexer = Lexer::from_memory("(command) <foo >bar ;", Source::Unknown);
let aliases = Default::default();
let mut parser = Parser::new(&mut lexer, &aliases);
let FullCompoundCommand { command, redirs } =
block_on(parser.full_compound_command()).unwrap().unwrap();
assert_eq!(command.to_string(), "(command)");
assert_eq!(redirs.len(), 2);
assert_eq!(redirs[0].to_string(), "<foo");
assert_eq!(redirs[1].to_string(), ">bar");
let next = block_on(parser.peek_token()).unwrap();
assert_eq!(next.id, Operator(Semicolon));
}
#[test]
fn parser_full_compound_command_none() {
let mut lexer = Lexer::from_memory("}", Source::Unknown);
let aliases = Default::default();
let mut parser = Parser::new(&mut lexer, &aliases);
let option = block_on(parser.full_compound_command()).unwrap();
assert_eq!(option, None);
}
#[test]
fn parser_short_function_definition_ok() {
let mut lexer = Lexer::from_memory(" ( ) ( : ) > /dev/null ", Source::Unknown);
let aliases = Default::default();
let mut parser = Parser::new(&mut lexer, &aliases);
let c = SimpleCommand {
assigns: vec![],
words: vec!["foo".parse().unwrap()],
redirs: vec![].into(),
};
let result = block_on(parser.short_function_definition(c)).unwrap();
assert_matches!(result, Command::Function(f) => {
assert_eq!(f.has_keyword, false);
assert_eq!(f.name.to_string(), "foo");
assert_eq!(f.body.to_string(), "(:) >/dev/null");
});
let next = block_on(parser.peek_token()).unwrap();
assert_eq!(next.id, EndOfInput);
}
}