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
#![forbid(unsafe_code)]
#[macro_use]
extern crate pomelo;
pub mod concrete;
mod lexer;
mod parser;
pub mod renaming;
pub mod rewriter;
pub mod stats;
pub mod visitors;
pub type Numeral = num::bigint::BigUint;
pub type Decimal = num::rational::BigRational;
pub type Nibble = u8;
pub type Hexadecimal = Vec<Nibble>;
pub type Binary = Vec<bool>;
pub use concrete::Error;
pub use lexer::Position;
pub struct CommandStream<R, T>
where
R: std::io::BufRead,
T: visitors::Smt2Visitor,
{
lexer: lexer::Lexer<R>,
visitor: T,
position: Position,
}
impl<R, T> CommandStream<R, T>
where
R: std::io::BufRead,
T: visitors::Smt2Visitor,
{
pub fn new(reader: R, visitor: T, path: Option<String>) -> Self {
Self {
lexer: lexer::Lexer::new(reader),
visitor,
position: Position {
path,
..Position::default()
},
}
}
pub fn visitor(&self) -> &T {
&self.visitor
}
pub fn visitor_mut(&mut self) -> &mut T {
&mut self.visitor
}
pub fn into_visitor(self) -> T {
self.visitor
}
}
impl<R, T> Iterator for CommandStream<R, T>
where
R: std::io::BufRead,
T: visitors::Smt2Visitor,
{
type Item = Result<T::Command, T::Error>;
#[allow(clippy::while_let_on_iterator)]
fn next(&mut self) -> Option<Result<T::Command, T::Error>> {
let mut parser = parser::Parser::new((&mut self.visitor, &mut self.position));
let mut unmatched_paren = 0;
while let Some(token) = self.lexer.next() {
match &token {
parser::Token::LeftParen => unmatched_paren += 1,
parser::Token::RightParen => {
if unmatched_paren > 0 {
unmatched_paren -= 1;
}
}
_ => (),
}
self.lexer.update_position(parser.extra_mut().1);
if let Err(err) = parser.parse(token) {
return Some(Err(err));
}
if unmatched_paren == 0 {
return match parser.end_of_input() {
Ok((command, _)) => Some(Ok(command)),
Err(err) => Some(Err(err)),
};
}
}
if unmatched_paren > 0 {
let extra = parser.into_extra();
Some(Err(extra.0.parsing_error(
extra.1.clone(),
"unexpected end of input".into(),
)))
} else {
None
}
}
}
#[test]
fn test_command_stream_error() {
let input = b"(echo \"Hello world!\")(exit f)";
let builder = concrete::SyntaxBuilder::default();
let stream = CommandStream::new(&input[..], builder, None);
let commands = stream.collect::<Vec<_>>();
assert!(matches!(
commands[..],
[
Ok(concrete::Command::Echo { .. }),
Err(concrete::Error::SyntaxError(..)),
Err(concrete::Error::SyntaxError(..)),
]
));
assert_eq!(
commands[0].as_ref().unwrap().to_string(),
"(echo \"Hello world!\")"
);
}
#[test]
fn test_command_stream_invalid_token() {
let input = b"(echo \"Hello world!\")(exit \000)";
let builder = concrete::SyntaxBuilder::default();
let stream = CommandStream::new(&input[..], builder, None);
let commands = stream.collect::<Vec<_>>();
assert!(matches!(
commands[..],
[
Ok(concrete::Command::Echo { .. }),
Err(concrete::Error::ParsingError(..)),
]
));
assert_eq!(
commands[0].as_ref().unwrap().to_string(),
"(echo \"Hello world!\")"
);
}