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
#![forbid(unsafe_code)]
#[macro_use]
extern crate pomelo;
pub mod concrete;
mod lexer;
mod parser;
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 lexer::Position;
pub struct CommandStream<R, T>
where
R: std::io::BufRead,
T: visitors::Smt2Visitor,
{
lexer: lexer::Lexer<R>,
visitor: T,
}
impl<R, T> CommandStream<R, T>
where
R: std::io::BufRead,
T: visitors::Smt2Visitor,
{
pub fn new(reader: R, visitor: T) -> Self {
Self {
lexer: lexer::Lexer::new(reader),
visitor,
}
}
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, Position>;
fn next(&mut self) -> Option<Result<T::Command, Position>> {
let mut parser = parser::Parser::new(&mut self.visitor);
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;
}
}
_ => (),
}
if let Err(()) = parser.parse(token) {
return Some(Err(self.lexer.current_position()));
}
if unmatched_paren == 0 {
return match parser.end_of_input() {
Ok((command, _)) => Some(Ok(command)),
Err(()) => Some(Err(self.lexer.current_position())),
};
}
}
None
}
}