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
use std::iter::Peekable;
use source_span::Span;
use crate::Located;
use crate::syntax::{
	token,
	Token,
	Parsable,
	Error,
	Result,
	peek,
	consume,
	consume_token,
	parse_list,
	parse_numeral
};

mod ast;
pub mod display;

pub use ast::*;

fn server_error<L>(lexer: &mut Peekable<L>, mut loc: Span) -> Result<Located<Error>> where L: Iterator<Item=Result<Located<Token>>> {
	use Token::*;

	let token = consume(lexer)?;
	let id_loc = token.span();
	match token.into_inner() {
		Ident(name) => {
			match name.as_str() {
				"error" => {
					let token = consume(lexer)?;
					let message_loc = token.span();
					match token.into_inner() {
						Litteral(token::Litteral::String(message)) => {
							loc = loc.union(consume_token(lexer, End)?);
							Ok(Error::Server(message).at(loc))
						},
						unexpected => Err(Error::UnexpectedToken(Ident(unexpected.to_string()), None).at(message_loc))
					}
				},
				unexpected => Err(Error::UnexpectedToken(Ident(unexpected.to_string()), None).at(id_loc))
			}
		},
		unexpected => Err(Error::UnexpectedToken(unexpected, None).at(id_loc))
	}
}

fn peek_server_error<L>(lexer: &mut Peekable<L>, loc: &Span) -> Result<()> where L: Iterator<Item=Result<Located<Token>>> {
	use Token::*;

	let token = peek(lexer)?;
	match token.as_ref() {
		Ident(name) => {
			match name.as_str() {
				"error" => Err(server_error(lexer, loc.clone())?),
				_ => Ok(())
			}
		},
		_ => Ok(())
	}
}

impl Parsable for CheckSat {
	fn parse<L>(lexer: &mut Peekable<L>) -> Result<Located<CheckSat>> where L: Iterator<Item=Result<Located<Token>>> {
		use Token::*;

		let token = consume(lexer)?;
		let loc = token.span();
		match token.into_inner() {
			Ident(name) => {
				match name.as_str() {
					"sat" => {
						Ok(Located::new(crate::response::CheckSat::Sat, loc))
					},
					"unsat" => {
						Ok(Located::new(crate::response::CheckSat::Unsat, loc))
					},
					"unknown" => {
						Ok(Located::new(crate::response::CheckSat::Unknown, loc))
					},
					unexpected => Err(Error::UnexpectedToken(Ident(unexpected.to_string()), None).at(loc))
				}
			},
			Begin => Err(server_error(lexer, loc)?),
			unexpected => Err(Error::UnexpectedToken(unexpected, None).at(loc))
		}
	}
}

impl Parsable for Model {
	fn parse<L>(lexer: &mut Peekable<L>) -> Result<Located<Model>> where L: Iterator<Item=Result<Located<Token>>> {
		use Token::*;

		let mut loc = consume_token(lexer, Begin)?;
		peek_server_error(lexer, &loc)?;

		let mut definitions;
		let mut sorts = Vec::new();

		match peek(lexer)?.as_ref() {
			// this "model" keyword token does not appear in the SMT2-lib specification.
			// however it seems to be pretty standard... In this case we can also have sorts and
			// functions declarations in the model.
			Ident(ref name) if name.as_str() == "model" => {
				consume(lexer)?;
				definitions = Vec::new();
				loop {
					let token = consume(lexer)?;
					let item_loc = token.span();
					match token.into_inner() {
						Begin => {
							let token = peek(lexer)?;
							let id_loc = token.span();
							match token.as_ref() {
								Ident(ref name) => {
									match name.as_ref() {
										"define-fun" | "define-fun-rec" | "define-funs-rec" => {
											let def = Definition::parse_at(lexer, item_loc)?;
											definitions.push(def);
										},
										"declare-sort" => { // non standard
											consume(lexer)?;
											let id = Symbol::parse(lexer)?;
											let arity = parse_numeral(lexer)?;

											let decl_loc = item_loc.union(consume_token(lexer, End)?);

											let decl = Located::new(SortDeclaration {
												id: id,
												arity: arity
											}, decl_loc);

											sorts.push(decl);
										},
										"declare-fun" => { // non standard
											consume(lexer)?;
											let _id = Symbol::parse(lexer)?;
											consume_token(lexer, Begin)?;
											let _args: Vec<Located<Sort>> = parse_list(lexer, &mut loc)?;
											let _return_sort = Sort::parse(lexer)?;

											let _decl_loc = item_loc.union(consume_token(lexer, End)?);

											// TODO should we keep the non-standard function declaration?
										},
										unexpected => return Err(Error::UnexpectedToken(Ident(unexpected.to_string()), None).at(id_loc))
									}
								},
								unexpected => return Err(Error::UnexpectedToken(unexpected.clone(), None).at(id_loc))
							}
						},
						End => {
							break
						},
						unexpected => return Err(Error::UnexpectedToken(unexpected, None).at(item_loc))
					}
				}
			},
			_ => {
				definitions = parse_list(lexer, &mut loc)?;
			}
		}

		Ok(Located::new(Model {
			sorts: sorts,
			definitions: definitions
		}, loc))
	}
}

impl Definition {
	fn parse_at<L>(lexer: &mut Peekable<L>, mut loc: Span) -> Result<Located<Definition>> where L: Iterator<Item=Result<Located<Token>>> {
		use Token::*;

		let token = consume(lexer)?;
		let id_loc = token.span();

		let rec;
		let declarations;
		let bodies;

		match token.into_inner() {
			Ident(name) => {
				match name.as_str() {
					"define-fun" => {
						rec = true;
						let id = Symbol::parse(lexer)?;
						consume_token(lexer, Begin)?;
						let args = parse_list(lexer, &mut loc)?;
						let return_sort = Sort::parse(lexer)?;
						let body = Term::parse(lexer)?;

						let def_loc = id.span().union(body.span());

						declarations = vec![Located::new(Declaration {
							id: id,
							args: args,
							return_sort: return_sort
						}, def_loc)];

						bodies = vec![body];
					},
					"define-fun-rec" => {
						rec = true;
						let id = Symbol::parse(lexer)?;
						consume_token(lexer, Begin)?;
						let args = parse_list(lexer, &mut loc)?;
						let return_sort = Sort::parse(lexer)?;
						let body = Term::parse(lexer)?;

						let def_loc = id.span().union(body.span());

						declarations = vec![Located::new(Declaration {
							id: id,
							args: args,
							return_sort: return_sort
						}, def_loc)];

						bodies = vec![body];
					},
					"define-funs-rec" => {
						rec = true;
						consume_token(lexer, Begin)?;
						declarations = parse_list(lexer, &mut loc)?;
						consume_token(lexer, Begin)?;
						bodies = parse_list(lexer, &mut loc)?;
					},
					// "declare-sort" => {
					//	 let id = Symbol::parse(lexer)?;
					//	 let arity = parse_numeral(lexer)?;
					// },
					unexpected => return Err(Error::UnexpectedToken(Ident(unexpected.to_string()), None).at(id_loc))
				}
			},
			unexpected => return Err(Error::UnexpectedToken(unexpected, None).at(id_loc))
		}

		loc = loc.union(consume_token(lexer, End)?);

		Ok(Located::new(Definition {
			rec: rec,
			declarations: declarations,
			bodies: bodies,
			comments: String::new()
		}, loc))
	}
}

impl Parsable for Definition {
	fn parse<L>(lexer: &mut Peekable<L>) -> Result<Located<Definition>> where L: Iterator<Item=Result<Located<Token>>> {
		use Token::*;

		let loc = consume_token(lexer, Begin)?;
		Self::parse_at(lexer, loc)
	}
}

impl Parsable for Declaration {
	fn parse<L>(lexer: &mut Peekable<L>) -> Result<Located<Declaration>> where L: Iterator<Item=Result<Located<Token>>> {
		use Token::*;

		let mut loc = consume_token(lexer, Begin)?;
		let id = Symbol::parse(lexer)?;
		consume_token(lexer, Begin)?;
		let args = parse_list(lexer, &mut loc)?;
		let return_sort = Sort::parse(lexer)?;
		loc = loc.union(consume_token(lexer, End)?);

		Ok(Located::new(Declaration {
			id: id,
			args: args,
			return_sort: return_sort
		}, loc))
	}
}