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
use super::core::Parser;
use super::core::Rec;
use super::core::Result;
use super::error::Error;
use super::error::SyntaxError;
use super::fill::MissingHereDoc;
use super::lex::Operator::{AndAnd, BarBar};
use super::lex::TokenId::Operator;
use crate::syntax::AndOr;
use crate::syntax::AndOrList;
impl Parser<'_, '_> {
pub async fn and_or_list(&mut self) -> Result<Rec<Option<AndOrList<MissingHereDoc>>>> {
let first = match self.pipeline().await? {
Rec::AliasSubstituted => return Ok(Rec::AliasSubstituted),
Rec::Parsed(None) => return Ok(Rec::Parsed(None)),
Rec::Parsed(Some(p)) => p,
};
let mut rest = vec![];
loop {
let condition = match self.peek_token().await?.id {
Operator(AndAnd) => AndOr::AndThen,
Operator(BarBar) => AndOr::OrElse,
_ => break,
};
self.take_token_raw().await?;
while self.newline_and_here_doc_contents().await? {}
let maybe_pipeline = loop {
if let Rec::Parsed(maybe_pipeline) = self.pipeline().await? {
break maybe_pipeline;
}
};
let pipeline = match maybe_pipeline {
None => {
let cause = SyntaxError::MissingPipeline(condition).into();
let location = self.peek_token().await?.word.location.clone();
return Err(Error { cause, location });
}
Some(pipeline) => pipeline,
};
rest.push((condition, pipeline));
}
Ok(Rec::Parsed(Some(AndOrList { first, rest })))
}
}
#[cfg(test)]
mod tests {
use super::super::error::ErrorCause;
use super::super::fill::Fill;
use super::super::lex::Lexer;
use super::*;
use crate::source::Source;
use futures_executor::block_on;
#[test]
fn parser_and_or_list_eof() {
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.and_or_list()).unwrap().unwrap();
assert_eq!(option, None);
}
#[test]
fn parser_and_or_list_one() {
let mut lexer = Lexer::from_memory("foo", Source::Unknown);
let aliases = Default::default();
let mut parser = Parser::new(&mut lexer, &aliases);
let aol = block_on(parser.and_or_list()).unwrap().unwrap().unwrap();
let aol = aol.fill(&mut std::iter::empty()).unwrap();
assert_eq!(aol.first.to_string(), "foo");
assert_eq!(aol.rest, vec![]);
}
#[test]
fn parser_and_or_list_many() {
let mut lexer = Lexer::from_memory("first && second || \n\n third;", Source::Unknown);
let aliases = Default::default();
let mut parser = Parser::new(&mut lexer, &aliases);
let aol = block_on(parser.and_or_list()).unwrap().unwrap().unwrap();
let aol = aol.fill(&mut std::iter::empty()).unwrap();
assert_eq!(aol.first.to_string(), "first");
assert_eq!(aol.rest.len(), 2);
assert_eq!(aol.rest[0].0, AndOr::AndThen);
assert_eq!(aol.rest[0].1.to_string(), "second");
assert_eq!(aol.rest[1].0, AndOr::OrElse);
assert_eq!(aol.rest[1].1.to_string(), "third");
}
#[test]
fn parser_and_or_list_missing_command_after_and_and() {
let mut lexer = Lexer::from_memory("foo &&", Source::Unknown);
let aliases = Default::default();
let mut parser = Parser::new(&mut lexer, &aliases);
let e = block_on(parser.and_or_list()).unwrap_err();
assert_eq!(
e.cause,
ErrorCause::Syntax(SyntaxError::MissingPipeline(AndOr::AndThen))
);
assert_eq!(*e.location.code.value.borrow(), "foo &&");
assert_eq!(e.location.code.start_line_number.get(), 1);
assert_eq!(e.location.code.source, Source::Unknown);
assert_eq!(e.location.index, 6);
}
}