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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
use std::{mem, fmt};
use std::error::Error;
use std::collections::HashMap;
use super::*;
use synth::*;
/*
macro_rules! dprintln {
( $( $x:expr ),* ) => { eprintln!( $( $x ),* ) }
}
*/
macro_rules! dprintln {
( $( $x:expr ),* ) => { () }
}
#[derive(Debug)]
pub enum ErrorKind {
Unexpected(TokType, TokType),
Unparseable(TokType, String),
ExpectedOp(char, TokType),
UnknownGen(String),
}
#[derive(Debug)]
pub struct ErrorType {
pub kind: ErrorKind,
desc: String,
}
impl ErrorType {
pub fn new(kind: ErrorKind) -> ErrorType {
let mut ret = ErrorType {
kind: kind,
desc: "".to_string(),
};
ret.desc = match ret.kind {
ErrorKind::Unexpected(found, expected) => format!("Found {:?}, expected {:?}", found, expected),
ErrorKind::Unparseable(found, ref term) => format!("Cannot consume {:?} token in {}", found, term),
ErrorKind::ExpectedOp(c, found) => format!("Expected {:?}, found {:?}", c, found),
ErrorKind::UnknownGen(ref s) => format!("Unknown generator name {}", s),
};
ret
}
pub fn with_description(kind: ErrorKind, desc: String) -> ErrorType {
ErrorType {
kind: kind,
desc: desc,
}
}
}
impl Error for ErrorType {
fn description(&self) -> &str {
&self.desc
}
}
impl fmt::Display for ErrorType {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self.description())
}
}
pub struct Parser<T: Iterator<Item=char>> {
tzr: Tokenizer<T>,
env: Environment,
token: Token,
pushback: Option<Token>,
factories: HashMap<String, &'static GeneratorFactory>,
}
impl<T: Iterator<Item=char>> Parser<T> {
pub fn new(mut tzr: Tokenizer<T>, env: Environment) -> Result<Parser<T>, Box<Error>> {
let token = tzr.next_token()?;
Ok(Parser {
tzr: tzr,
env: env,
token: token,
pushback: None,
factories: all_factories(),
})
}
pub fn push_back(&mut self, tok: Token) {
match self.pushback {
None => {
self.pushback = Some(tok);
},
Some(_) => panic!("too many pushbacks on Parser"),
}
}
pub fn cur_token(&self) -> &Token {
match self.pushback {
Some(ref tok) => tok,
None => &self.token,
}
}
pub fn expect(&mut self, ty: TokType) -> Result<Token, Box<Error>> {
if ty != self.cur_token().to_type() {
Err(ErrorType::new(ErrorKind::Unexpected(self.token.to_type(), ty)).into())
} else {
Ok(match self.pushback {
Some(_) => mem::replace(&mut self.pushback, None).unwrap(),
None => mem::replace(&mut self.token, self.tzr.next_token()?),
})
}
}
pub fn expect_ident(&mut self) -> Result<String, Box<Error>> {
match self.expect(TokType::Ident)? {
Token::Ident(s) => Ok(s),
_ => unreachable!(),
}
}
pub fn expect_op(&mut self, oper: char) -> Result<(), Box<Error>> {
dprintln!("expect_op: {:?} ({})", self.cur_token(), oper);
match *self.cur_token() {
Token::Oper(c) if c == oper => { self.expect(TokType::Oper)?; Ok(()) },
_ => Err(ErrorType::new(ErrorKind::ExpectedOp(oper, self.cur_token().to_type())).into()),
}
}
pub fn peek_op(&self, oper: char) -> bool {
dprintln!("peek_op: {:?} ({})", self.cur_token(), oper);
match *self.cur_token() {
Token::Oper(c) if c == oper => true,
_ => false
}
}
pub fn parse_gen_vec(&mut self) -> Result<Vec<GenBox>, Box<Error>> {
let mut ret: Vec<GenBox> = Vec::new();
self.expect_op('[')?;
loop {
if self.expect_op(']').is_ok() {
break;
}
ret.push(self.parse_gen_rel()?);
if self.expect_op(',').is_err() {
self.expect_op(']')?;
break;
}
}
Ok(ret)
}
pub fn parse_gen_rel(&mut self) -> Result<GenBox, Box<Error>> {
let left = self.parse_gen_terms()?;
match *self.cur_token() {
Token::Oper(c) => {
if c == '>' || c == '!' || c == '<' || c == '=' { // TODO: Conflict with param name
self.expect(TokType::Oper)?;
let relop = match (c, self.cur_token()) {
('<', &Token::Oper('=')) => { self.expect(TokType::Oper)?; RelOp::LessEqual },
('=', &Token::Oper('=')) => { self.expect(TokType::Oper)?; RelOp::Equal },
('>', &Token::Oper('=')) => { self.expect(TokType::Oper)?; RelOp::Greater },
('!', &Token::Oper('=')) => { self.expect(TokType::Oper)?; RelOp::NotEqual },
('<', _) => RelOp::Less,
('>', _) => RelOp::Greater,
_ => return Err(ErrorType::new(ErrorKind::Unparseable(TokType::Oper, "rel expr".to_string())).into()),
};
let mut params = FactoryParameters { env: self.env.clone(), ..Default::default() };
params.vars.insert("0".to_string(), ParamValue::Generator(left));
params.vars.insert("1".to_string(), ParamValue::String(relop.to_param_string().to_string()));
params.vars.insert("2".to_string(), ParamValue::Generator(self.parse_gen_rel()?));
let factory = self.factories.get("rel").ok_or(ErrorType::new(ErrorKind::UnknownGen("rel".to_string())))?;
factory.new(&mut params).map_err(Into::into)
} else {
Ok(left)
}
},
_ => Ok(left),
}
}
pub fn parse_gen_terms(&mut self) -> Result<GenBox, Box<Error>> {
let mut gens: Vec<GenBox> = Vec::new();
gens.push(self.parse_gen_factors()?);
loop {
match *self.cur_token() {
Token::Oper('+') => {
self.expect_op('+')?;
gens.push(self.parse_gen_factors()?);
},
Token::Oper('-') => {
self.expect_op('-')?;
let mut params = FactoryParameters { env: self.env.clone(), ..Default::default() };
params.vars.insert("0".to_string(), ParamValue::Generator(self.parse_gen_factors()?));
let factory = self.factories.get("negate").ok_or(ErrorType::new(ErrorKind::UnknownGen("negate".to_string())))?;
gens.push(factory.new(&mut params).map_err(GenFactoryErrorType::from)?);
},
_ => break,
}
}
if gens.len() == 1 {
return Ok(gens.pop().unwrap());
}
let mut params = FactoryParameters { env: self.env.clone(), ..Default::default() };
for (idx, gen) in gens.into_iter().enumerate() {
params.vars.insert(idx.to_string(), ParamValue::Generator(gen));
}
let factory = self.factories.get("add").ok_or(ErrorType::new(ErrorKind::UnknownGen("add".to_string())))?;
factory.new(&mut params).map_err(Into::into)
}
pub fn parse_gen_factors(&mut self) -> Result<GenBox, Box<Error>> {
let mut gens: Vec<GenBox> = Vec::new();
gens.push(self.parse_gen()?);
loop {
match *self.cur_token() {
Token::Oper('*') => {
self.expect_op('*')?;
gens.push(self.parse_gen()?);
},
Token::Oper('/') => {
self.expect_op('/')?;
let mut params = FactoryParameters { env: self.env.clone(), ..Default::default() };
params.vars.insert("0".to_string(), ParamValue::Generator(self.parse_gen()?));
let factory = self.factories.get("reciprocate").ok_or(ErrorType::new(ErrorKind::UnknownGen("reciprocate".to_string())))?;
gens.push(factory.new(&mut params).map_err(GenFactoryErrorType::from)?);
},
_ => break,
}
}
if gens.len() == 1 {
return Ok(gens.pop().unwrap());
}
let mut params = FactoryParameters { env: self.env.clone(), ..Default::default() };
for (idx, gen) in gens.into_iter().enumerate() {
params.vars.insert(idx.to_string(), ParamValue::Generator(gen));
}
let factory = self.factories.get("mul").ok_or(ErrorType::new(ErrorKind::UnknownGen("mul".to_string())))?;
factory.new(&mut params).map_err(Into::into)
}
pub fn parse_gen(&mut self) -> Result<GenBox, Box<Error>> {
match *self.cur_token() {
Token::Integer(v) => {
self.expect(TokType::Integer)?;
let mut params = FactoryParameters { env: self.env.clone(), ..Default::default() };
params.vars.insert("0".to_string(), ParamValue::String("_".to_string()));
params.vars.insert("1".to_string(), ParamValue::Integer(v));
let factory = self.factories.get("param").ok_or(ErrorType::new(ErrorKind::UnknownGen("param".to_string())))?;
factory.new(&mut params).map_err(Into::into)
},
Token::Float(v) => {
self.expect(TokType::Float)?;
let mut params = FactoryParameters { env: self.env.clone(), ..Default::default() };
params.vars.insert("0".to_string(), ParamValue::String("_".to_string()));
params.vars.insert("1".to_string(), ParamValue::Float(v));
let factory = self.factories.get("param").ok_or(ErrorType::new(ErrorKind::UnknownGen("param".to_string())))?;
factory.new(&mut params).map_err(Into::into)
},
Token::Ident(_) => {
let name = self.expect_ident()?;
if self.peek_op('(') {
let mut params = self.parse_factory_params()?;
let factory = match self.factories.get(&name) {
Some(fac) => fac,
None => return Err(ErrorType::new(ErrorKind::UnknownGen(name)).into()),
};
factory.new(&mut params).map_err(Into::into)
} else {
let mut params = FactoryParameters { env: self.env.clone(), ..Default::default() };
params.vars.insert("0".to_string(), ParamValue::String(name));
let factory = self.factories.get("param").ok_or(ErrorType::new(ErrorKind::UnknownGen("param".to_string())))?;
factory.new(&mut params).map_err(Into::into)
}
},
Token::Oper('(') => {
dprintln!("consuming paren in parse_gen");
self.expect(TokType::Oper)?;
let ret = self.parse_gen_rel()?;
dprintln!("parenthesized generator is concluding");
self.expect_op(')')?;
Ok(ret)
},
_ => Err(ErrorType::new(ErrorKind::Unparseable(self.cur_token().to_type(), "gen".to_string())).into()),
}
}
pub fn parse_factory_params(&mut self) -> Result<FactoryParameters, Box<Error>> {
dprintln!("consuming paren in factory_params");
self.expect_op('(')?;
let mut params: FactoryParameters = FactoryParameters { env: self.env.clone(), ..Default::default() };
let mut ctr = 0;
loop {
if self.expect_op(')').is_ok() {
break;
}
let (nm, vl, new_ctr) = self.parse_param(ctr)?;
params.vars.insert(nm, vl);
ctr = new_ctr;
dprintln!("before factory_params comma, tok is {:?}", self.cur_token());
if self.expect_op(',').map_err(|e| dprintln!("factory_params consume comma failed: {:?}", e)).is_err() {
dprintln!("factory_params is concluding");
self.expect_op(')')?;
break;
}
}
Ok(params)
}
pub fn parse_param(&mut self, pos: usize) -> Result<(String, ParamValue, usize), Box<Error>> {
let mut ctr = pos;
let name = match self.expect_ident() {
Ok(nm) => {
if self.expect_op('=').is_ok() {
nm
} else {
self.push_back(Token::Ident(nm));
ctr += 1;
(ctr - 1).to_string()
}
},
Err(_) => {
ctr += 1;
(ctr - 1).to_string()
},
};
dprintln!("about to consume param value, token is {:?}", self.cur_token());
match self.cur_token().clone() { // FIXME: Does this really need to be cloned?
Token::String(ref v) => { self.expect(TokType::String)?; Ok((name, ParamValue::String(v.clone()), ctr)) },
Token::Integer(_) | Token::Float(_) | Token::Ident(_) | Token::Oper('(') => Ok((name, ParamValue::Generator(self.parse_gen_rel()?), ctr)),
_ => Err(ErrorType::new(ErrorKind::Unparseable(self.cur_token().to_type(), "param value".to_string())).into()),
}
}
}
|