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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use std::collections::{HashMap,BTreeSet};
use std::io::prelude::*;

use source::Src;
use var::Var;
use logic::LogicFn;
use def::DefBlock;
use env::Env;

#[derive(Debug,PartialEq)]
pub struct SrcBlock {
    pub name: String,
    pub src: Vec<Src>,
    pub idx: usize,
    pub visited: bool,
    pub or_valid: bool,

    pub logic: HashMap<String,LogicFn>,
}

#[derive(Debug,PartialEq)]
pub enum Block {
    Src(SrcBlock),
    Def(DefBlock),
}

/// Intermediate Representation
///
/// This is used during the parsing stage
#[derive(Debug,Clone,PartialEq)]
pub enum IR {
    String(String),
    /// A non-quoted string turns into a symbol/token
    Sym(String),
    /// Key-Value, pre-parsed
    Map(Vec<IR>),
}

impl From<IR> for String {
    fn from(t:IR) -> String {
        match t {
            IR::String(s) => s,
            IR::Sym(s) => s,
            IR::Map(mut v) => {
                let mut s = "{".to_owned();
                for n in v.drain(..) {
                    let n:String = n.into();
                    s.push_str(&n);
                }

                s.push('}');

                s
            }
        }
    }
}
impl From<Var> for IR {
    fn from(t:Var) -> IR {
        match t {
            Var::String(t) => { IR::String(t) }
            _ => { IR::Sym(t.to_string()) }
        }
    }
}


/// Map object for Selects
pub type Map = HashMap<String,Vec<Var>>;

pub struct Parser(Vec<Block>);

use std::ops::Deref;
impl Deref for Parser {
    type Target = Vec<Block>;
    fn deref(&self) -> &Self::Target { &self.0 }
}

impl Parser {
    pub fn parse_blocks (src: &str) -> Result<Parser,&'static str> {
        let mut v = vec!();
        let mut exp = String::new();
        let mut exps: Vec<IR> = vec!();
        let mut map_ir: Vec<IR> = vec!(); //contains pre-parsed map
        let mut block: Option<Block> = None;

        let mut in_string = false;
        let mut in_comment = false;
        let mut in_vec = false;
        let mut in_map = false;
        let mut was_if = false;

        
        let mut usyms = BTreeSet::new(); //unique set, remove dupes
                            

        for c in src.chars() {
            if !in_comment && !in_string {
                if c == '[' { in_vec = true; continue }
                else if c == ']' { in_vec = false; }
                
            }
            
            if c == '#' && !in_string { in_comment = true; continue }
            else if  c == '\n' && in_comment && !in_string { in_comment = false; }

            if c == '\n' && (in_vec || in_map) && !in_comment && !in_string { continue }
            
            if (c == ']' ||
                c == '}' ||
                c == '#' ||
                c == '\n')
                && !in_string && !in_comment
            {
                for n in exp.split_whitespace() {
                    let sym = IR::Sym(n.trim().to_owned());
                    if in_map { map_ir.push(sym); }
                    else { exps.push(sym); }
                }
                
                if c == '}' && in_map && !in_comment{
                    in_map = false;
                    exps.push(IR::Map(map_ir));
                    map_ir = vec![];
                }
                
                exp = String::new();

                if exps.len() < 1 { continue }
                
                
                // determine block type
                if block.is_none() {
                    let name = exps.remove(0).into();
                    if name == "def" {
                        let b = DefBlock {
                            name: exps.pop().unwrap().into(),
                            data: HashMap::new(),
                        };
                        
                        block = Some(Block::Def(b));
                    }
                    else {
                        let b = SrcBlock {
                            name: name,
                            src: vec!(),
                            idx: 0,
                            visited: false,
                            or_valid: false,
                            logic: HashMap::new()
                        };
                        
                        block = Some(Block::Src(b));
                    }
                    
                }
                else { // build block type
                    let mut qsyms:Vec<(String,String)> = vec!();
                    let adjust_sym = |qsyms: &mut Vec<(String,String)>, s: &mut String| {
                        if s.chars().next() == Some('!') {
                            let mut sym = "not_".to_owned();
                            sym.push_str(s[1..].trim());
                            
                            let osym = s.trim().to_owned();
                            
                            qsyms.push((sym.clone(),osym));
                            *s = sym;
                        }
                    };
                    
                    // this builds symbol refs as a convenience
                    for n in exps.iter_mut() {
                        match n {
                            &mut IR::Sym(ref mut s) => {
                                adjust_sym(&mut qsyms,s);
                            },
                            &mut IR::Map(ref mut v) => {
                                for n in v.iter_mut() {
                                    match n {
                                        &mut IR::Sym(ref mut s) => {
                                            adjust_sym(&mut qsyms,s);
                                        },
                                        _ => {},
                                    }
                                }
                            },
                            _ => {},
                        }
                    }
                    
                    match block {
                        Some(Block::Def(ref mut b)) => {
                            let v = exps.pop().unwrap();
                            let r = try!(Var::parse(v));
                            b.data.insert(exps.pop().unwrap().into(),
                                         r);
                        },
                        Some(Block::Src(ref mut b)) => {
                            let mut srcs: Vec<Src> = vec![];
                            
                            for (qsym,sym) in qsyms.drain(..) {
                                if usyms.contains(&qsym) { continue }
                                usyms.insert(qsym.clone());
                                
                                let src = try!(Src::parse(vec![IR::Sym(qsym),
                                                               IR::Sym(sym)]));

                                srcs.push(src);
                            }

                            let src = try!(Src::parse(exps));
                            srcs.push(src);

                            for src in srcs.drain(..) {
                                match &src {
                                    &Src::If(_,_,_) => { was_if = true; },
                                    &Src::Or(_,_) => {
                                        if !was_if {
                                            return Err("If must prepend Or")
                                        }
                                    },
                                    _ => { was_if = false; },
                                }

                                
                                b.src.push(src);
                            }
                        },
                        _ => {}
                    }

                    exps = vec!();
                }
            }
            else if c == '"' && !in_comment {
                in_string = !in_string;
                if in_string { //starting a new quoted string? let's push this sym
                    for n in exp.split_whitespace() {
                        let sym = IR::Sym(n.trim().to_owned());
                        if in_map { map_ir.push(sym); }
                        else { exps.push(sym); }
                    }
                    exp = String::new();
                }
                else if !in_string { //finished the quoted string?
                    let sym = IR::String(exp);
                    if in_map { map_ir.push(sym); }
                    else { exps.push(sym); }
                    exp = String::new();
                }
            }
            else if c == ';' && !in_string && !in_comment {
                //fail otherwise, block should be built!
                if let Some(block_) = block {
                    v.push(block_);
                    usyms.clear(); //clear out on new block
                    block = None;
                }
                else { return Err("Parse Block not built")}
            }
            else {
                if c == '{' && !in_comment && !in_string {
                    in_map = true;
                    // push previous symbol
                    let sym = IR::Sym(exp.trim().to_owned());
                    exps.push(sym);
                    exp.clear();
                }
                else if !in_comment {
                    exp.push(c);
                }
            }
        }
        
        Ok(Parser(v))
    }

    /// Consumes parser, pushes blocks onto existing vec
    ///
    /// Returns starting index of where it was pushed onto vec
    pub fn sink (mut self, v: &mut Vec<Block>) -> Option<usize> {
        if self.0.len() > 0 {
            let start = Some(v.len());
            for b in self.0.drain(..) {
                v.push(b);
            }

            return start
        }

        None
    }

    /// Consumes parser, builds environment
    pub fn into_env (self) -> Env {
        let mut env = Env::empty();
        env.insert(self.0);
        env
    }

    /// Parses a map from IR
    ///
    /// Parsed using commas for variable sized maps
    pub fn parse_map (map_ir: IR) -> Result<Map,&'static str> {
        let mut map: Map = HashMap::new(); // optionally unbounded val-lengths

        match map_ir {
            IR::Map(mut exps) => {
                let mut key = "".to_owned();
                let mut vals = vec![];
                
                for n in exps.drain(..) {
                    if key.is_empty() { key = n.into(); continue }

                    match n {
                        IR::Sym(mut s) => {
                            if s.chars().last() == Some(',') {
                                let _ = s.pop();
                                let var = Var::parse(IR::Sym(s))?;
                                vals.push(var);

                                map.insert(key,vals);
                                vals = vec![];
                                key = "".to_owned();

                                continue
                            }

                            let var = Var::parse(IR::Sym(s))?;
                            vals.push(var);
                        },
                        _ => { vals.push(Var::parse(n)?); },
                    }
                }

                if !key.is_empty() && vals.len() > 0 {
                    map.insert(key,vals);
                }
                else if !key.is_empty() {
                    return Err("Map contains unbalanced braclets")
                }
                
                
                
                Ok(map)
            },
            _=> { return Err("Map type not found") }
        }
    }
}



pub struct StreamParser<S:Read> {
    /// The non-parsed leftovers of a stream that is being buffered actively
    buf: String,
    pub stream: S,
    size: usize,
    pub blocks: Vec<Block>,
    curr_block: String,
}

impl<S:Read> Iterator for StreamParser<S> {
    type Item=usize;
    fn next(&mut self) -> Option<Self::Item> {
        self.parse()
    }
}

impl<S:Read> StreamParser<S> {
    /// Creates a new Parser for readable streams
    ///
    /// Optionally specify chunk size on buffering
    pub fn new (s: S, size: Option<usize>) -> StreamParser<S> {
        StreamParser {
            buf: String::new(),
            stream: s,
            blocks: vec![],
            size: { if let Some(size) = size { size }
                    else { 1024 } },
            curr_block: "".to_owned(),
        }
    }

    /// Moves parsed blocks into existing environment
    pub fn sink (&mut self, v: &mut Env) -> Result<(),&str> {
        if !self.curr_block.is_empty() { return Err(&self.curr_block) }
            
        for b in self.blocks.drain(..) {
            match b {
                Block::Src(b) => { v.src.insert(b.name.clone(), b); },
                Block::Def(b) => { v.def.insert(b.name.clone(), b); },
            }
        }

        Ok(())
    }

    /// Parses blocks from stream, returns the index of the new starting block
    pub fn parse (&mut self) -> Option<usize> {
        let mut buf = vec![0u8;self.size];
        if let Ok(n) = self.stream.read(&mut buf[..]) {
            if n > 0 {
                let _ = buf.truncate(n);
                if let Ok(s) = String::from_utf8(buf) {
                    self.buf.push_str(&s);

                    let mut block = String::new();
                    let mut start = None;
                    for c in self.buf.drain(..) {
                        block.push(c);
                        if self.curr_block.is_empty() && c == '\n' {
                            self.curr_block = block.clone();
                        }
                        
                        if c == ';' {
                            if let Ok(p) = Parser::parse_blocks(&block) {
                                start = p.sink(&mut self.blocks);
                                self.curr_block.clear();
                            }
                            else { return None } //end iteration when parsing fails
                            
                            block.clear();
                        }
                    }

                    if !block.is_empty() { self.buf = block; } //put back anything if needed
                    
                    return start
                }
            }
        }

        
        None
    }

}