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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
use crate::chars::*;
use crate::err::ECode;
use crate::iter::LCChars;
use crate::ptrait::{ParseRes, Parser};
use crate::skip::skip_while;
use std::collections::BTreeMap;
use std::marker::PhantomData;
//use std::iter::FromIterator;

#[derive(Clone)]
pub struct Read<F> {
    f: F,
    min: usize,
}

//pub fn ident(Str)

///This is the return result for any function wishing to work with Read
#[derive(Clone)]
pub enum ReadResult<V> {
    ///Keep asking going
    Cont(V),
    ///Stop here
    Done(V),
    ///Stop, we've gone too far
    Back(V),
    ///There is still more required
    Req(V),
    ///There is an unresolveable problem
    Err(ECode),
}

impl<F> Parser<String> for Read<F>
where
    F: Fn(String, char) -> ReadResult<String>,
{
    fn parse<'a>(&self, i: &LCChars<'a>) -> ParseRes<'a, String> {
        let mut res = String::new();
        let mut i = i.clone();
        let mut i2 = i.clone();
        let mut req = self.min > 0;
        let mut n = 0;
        while let Some(p) = i.next() {
            match (self.f)(res, p) {
                ReadResult::Done(v) => {
                    return if n >= self.min {
                        Ok((i, v))
                    } else {
                        i.err_r("not enough read")
                    }
                }
                ReadResult::Back(v) => {
                    return if n >= self.min {
                        Ok((i2, v))
                    } else {
                        i.err_r("not enough read")
                    }
                }
                ReadResult::Cont(v) => {
                    res = v;
                    req = false;
                }
                ReadResult::Req(v) => {
                    res = v;
                    req = true
                }
                ReadResult::Err(e) => return i.err_cr(e),
            }
            i2 = i.clone();
            n += 1
        }
        if req {
            return i.err_r("Still more required for Read::parse");
        }
        if n >= self.min {
            Ok((i, res))
        } else {
            i.err_r("not enough read")
        }
    }
}

#[deprecated(
    since = "0.2.1",
    note = "Use a tuple instead: (CharBool,CharBool) implements CharBool"
)]
pub fn or_char<A: Fn(char) -> bool, B: Fn(char) -> bool>(a: A, b: B) -> impl Fn(char) -> bool {
    move |c| a(c) || b(c)
}

/// ```rust
/// use gobble::*;
/// let (rest,name )= s_(read_fs((Alpha,NumDigit),1)).parse_sn("    gobble ").unwrap();
/// assert_eq!(name,"gobble");
/// assert_eq!(rest,"");
/// ```
#[deprecated(since = "0.2.1", note = "Use CharBool::min_n(n) instead")]
pub fn read_fs<CB: CharBool>(cb: CB, min: usize) -> impl Parser<String> {
    cb.min_n(min)
}

#[deprecated(since = "0.2.1", note = "It's really not pretty")]
pub fn read<F>(f: F, min: usize) -> Read<F>
where
    F: Fn(String, char) -> ReadResult<String>,
{
    Read { f, min }
}

#[derive(Clone)]
pub struct Tag {
    s: &'static str,
}

/// Check for a specifig string
/// Returns the string, so that used with "or" you can see which result you got
#[deprecated(since = "0.1.8", note = "Use &str instead")]
pub fn tag(s: &'static str) -> Tag {
    Tag { s }
}

///Conveniece wrapper for tag, often you want to allow whitespace
/// around a tag of some kind
#[deprecated(since = "0.1.8", note = "Use s_(&str) instead")]
pub fn s_tag(s: &'static str) -> impl Parser<&'static str> {
    s_(s)
}

pub fn ws_<P: Parser<V>, V>(p: P) -> impl Parser<V> {
    ws(0).ig_then(p)
}

///Convenience wrapper to say allow whitespace around whatever I'm parsing.
pub fn s_<P: Parser<V>, V>(p: P) -> impl Parser<V> {
    crate::combi::wrap(ws(0), p)
}

///Take at least n white space characters
pub fn ws(min: usize) -> impl Parser<()> {
    skip_while(
        |c| match c {
            ' ' | '\t' | '\r' => true,
            _ => false,
        },
        min,
    )
}

impl<P: Parser<V>, V> Parser<V> for KeyWord<P, V> {
    fn parse<'a>(&self, it: &LCChars<'a>) -> ParseRes<'a, V> {
        let (t2, r) = self.p.parse(it)?;
        match t2.clone().next() {
            Some(c) => {
                if (Alpha, NumDigit, '_').char_bool(c) {
                    t2.err_cr(ECode::SMess("Not Keyword"))
                } else {
                    Ok((t2, r))
                }
            }
            None => Ok((t2, r)),
        }
    }
}

pub struct KeyWord<P: Parser<V>, V> {
    p: P,
    phv: PhantomData<V>,
}

///```rust
/// use gobble::*;
/// assert_eq!(keyword("let").parse_s("let"), Ok("let"));
/// assert_eq!(keyword("let").parse_s("let "), Ok("let"));
/// assert_eq!(keyword("let").parse_s("let*"), Ok("let"));
/// assert!(keyword("let").parse_s("letl").is_err());
///```
pub fn keyword<P: Parser<V>, V>(p: P) -> KeyWord<P, V> {
    KeyWord {
        p,
        phv: PhantomData,
    }
}

pub fn do_tag<'a>(it: &LCChars<'a>, tg: &'static str) -> ParseRes<'a, &'static str> {
    let mut i = it.clone();
    let mut s_it = tg.chars();
    while let Some(c) = s_it.next() {
        match i.next() {
            None => return i.err_cr(ECode::Tag(tg)),
            Some(ic) => {
                if ic != c {
                    return i.err_cr(ECode::Tag(tg));
                }
            }
        }
    }
    Ok((i, tg))
}

impl Parser<&'static str> for Tag {
    fn parse<'a>(&self, i: &LCChars<'a>) -> ParseRes<'a, &'static str> {
        do_tag(i, self.s)
    }
}

///A reader for strings, that allows escaping one char and mapping to another char. The
///returned string has already had the escape replace done
#[derive(Clone)]
pub struct Escape {
    esc: char,
    map: BTreeMap<char, char>,
    close: char,
}
impl Parser<String> for Escape {
    fn parse<'a>(&self, i: &LCChars<'a>) -> ParseRes<'a, String> {
        let mut i = i.clone();
        let mut res = String::new();
        while let Some(c) = i.next() {
            if c == self.close {
                return Ok((i, res));
            }
            if c == self.esc {
                if let Some(c2) = i.next() {
                    match self.map.get(&c2) {
                        Some(cr) => res.push(*cr),
                        None => res.push(c2),
                    }
                }
            } else {
                res.push(c);
            }
        }
        i.err_r("un closed escape")
    }
}
#[deprecated(
    since = "0.1.7",
    note = "see common::common_str() for how to handle escapes"
)]
/// Build an escaper - used to complete a string, you will already have called checked for the
/// opening part of the string
pub fn esc(close: char, esc: char) -> Escape {
    Escape {
        close,
        esc,
        map: BTreeMap::new(),
    }
}

impl Escape {
    /// Add a character to the map, in a builder pattern way.
    /// ```rust
    /// use gobble::*;
    /// let s = tag("\"").ig_then(esc('\"','\\')
    ///     .e_map('t','\t').e_map('p','$'))
    ///     .parse_s(r#""my \t \pstring""#).unwrap();
    /// assert_eq!(s,"my \t $string")
    /// ```
    pub fn e_map(mut self, f: char, t: char) -> Self {
        self.map.insert(f, t);
        self
    }
}

impl<F> Parser<()> for Take<F>
where
    F: Fn(char) -> bool,
{
    fn parse<'a>(&self, i: &LCChars<'a>) -> ParseRes<'a, ()> {
        let mut n = 0;
        let mut i = i.clone();
        let mut i2 = i.clone();
        while let Some(c) = i.next() {
            if !(self.f)(c) {
                if n < self.min {
                    return i.err_r("not enough to take");
                }
                return Ok((i2, ()));
            }
            n += 1;
            i2.next();
        }
        if n < self.min {
            return i.err_r("End of str before end of take");
        }
        Ok((i2, ()))
    }
}

#[derive(Clone)]
pub struct Take<F> {
    f: F,
    min: usize,
}

pub fn take<F>(f: F, min: usize) -> Take<F>
where
    F: Fn(char) -> bool,
{
    Take { f, min }
}

pub fn eoi<'a>(i: &LCChars<'a>) -> ParseRes<'a, ()> {
    let mut r = i.clone();
    if r.next() == None {
        return Ok((r, ()));
    }
    i.err_r("Still More Input")
}

pub fn to_end() -> impl Parser<()> {
    ws(0).then_ig(eoi)
}

pub struct TakeN {
    n: usize,
}

impl Parser<String> for TakeN {
    fn parse<'a>(&self, i: &LCChars<'a>) -> ParseRes<'a, String> {
        let mut res = String::new();
        let mut it = i.clone();
        for _ in 0..self.n {
            res.push(it.next().ok_or(it.err_c(ECode::EOF))?);
        }
        Ok((it, res))
    }
}
pub fn take_n(n: usize) -> TakeN {
    TakeN { n }
}

pub fn take_char<'a>(i: &LCChars<'a>) -> ParseRes<'a, char> {
    let mut ri = i.clone();
    let c = ri.next().ok_or(ri.err_c(ECode::EOF))?;
    Ok((ri, c))
}

pub struct Peek<P: Parser<V>, V> {
    p: P,
    phv: PhantomData<V>,
}

impl<P: Parser<V>, V> Parser<V> for Peek<P, V> {
    fn parse<'a>(&self, it: &LCChars<'a>) -> ParseRes<'a, V> {
        let (_, v) = self.p.parse(it)?;
        Ok((it.clone(), v))
    }
}

pub fn peek<P: Parser<V>, V>(p: P) -> Peek<P, V> {
    Peek {
        p,
        phv: PhantomData,
    }
}

pub struct CharF<F: Fn(char) -> bool> {
    f: F,
}

impl<F: Fn(char) -> bool> Parser<char> for CharF<F> {
    fn parse<'a>(&self, i: &LCChars<'a>) -> ParseRes<'a, char> {
        let mut i2 = i.clone();
        match i2.next() {
            Some(c) if (self.f)(c) => Ok((i2, c)),
            v => i2.err_cr(ECode::Char('?', v)),
        }
    }
}

pub fn char_f<F: Fn(char) -> bool>(f: F) -> CharF<F> {
    CharF { f }
}

pub struct CharsUntil<A: Parser<char>, B: Parser<BV>, BV> {
    a: A,
    b: B,
    phb: PhantomData<BV>,
}

impl<A: Parser<char>, B: Parser<BV>, BV> Parser<String> for CharsUntil<A, B, BV> {
    fn parse<'a>(&self, it: &LCChars<'a>) -> ParseRes<'a, String> {
        let mut res = String::new();
        let mut it = it.clone();
        loop {
            //let it2 = it.clone();
            if let Ok((i, _)) = self.b.parse(&it) {
                return Ok((i, res));
            }
            it = match self.a.parse(&it) {
                Ok((i, c)) => {
                    res.push(c);
                    i
                }
                Err(e) => return Err(e),
            };
        }
    }
}

pub fn chars_until<A: Parser<char>, B: Parser<BV>, BV>(a: A, b: B) -> CharsUntil<A, B, BV> {
    CharsUntil {
        a,
        b,
        phb: PhantomData,
    }
}

pub struct StringRepeat<A: Parser<AV>, AV: Into<String> + AsRef<str>> {
    a: A,
    pha: PhantomData<AV>,
    min: usize,
}

impl<A: Parser<AV>, AV: Into<String> + AsRef<str>> Parser<String> for StringRepeat<A, AV> {
    fn parse<'a>(&self, it: &LCChars<'a>) -> ParseRes<'a, String> {
        let (mut nit, mut res) = match self.a.parse(it) {
            Ok((it2, ss)) => (it2, ss.into()),
            Err(e) => {
                if self.min == 0 {
                    return Ok((it.clone(), String::new()));
                } else {
                    return Err(e);
                }
            }
        };
        let mut done = 1;
        loop {
            match self.a.parse(&nit) {
                Ok((it, r)) => {
                    res.push_str(r.as_ref());
                    nit = it;
                }
                Err(e) => {
                    if done < self.min {
                        return Err(e);
                    } else {
                        return Ok((nit, res));
                    }
                }
            }
            done += 1;
        }
    }
}

pub fn string_repeat<A: Parser<AV>, AV: Into<String> + AsRef<str>>(
    a: A,
    min: usize,
) -> StringRepeat<A, AV> {
    StringRepeat {
        a,
        min,
        pha: PhantomData,
    }
}