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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
//! Types which facillitates the chaining of parsers and their results.

pub mod numbering;
#[cfg(feature = "tendril")]
pub mod tendril;

use primitives::{Guard, IntoInner};

/// The buffers yielded parsers consuming a sequence of the input.
///
/// This could either be an owned type or a slice reference depending on the `Input`
/// implementation.
pub trait Buffer: PartialEq<Self> {
    /// The token type of this buffer.
    type Token: Copy + PartialEq;

    /// Applies a function in order on all tokens present in the buffer carrying an accumulator
    /// value `B` between invocations. The buffer is consumed as part of the folding and the last
    /// value of the accumulator is returned.
    // Would be prefereable if there was a &self -> Iterator method, but that does not work for
    // owned or maybe owned since the lifetimes will be wrong for one or the other. Higher Ranked
    // Trait Bounds (HRTB) does not seem to work either since it is not possible to later
    // instantiate the type in a function signature with a concrete lifetime without running into
    // an "expected bound lifetime but found concrete lifetime" error. Instantiation for HRTBs seem
    // to only take place in the actual code, not when a type is used in eg. a where clause.
    fn fold<B, F>(self, B, F) -> B
      where F: FnMut(B, Self::Token) -> B;

    /// Runs the supplied function on a borrow of each token present in the buffer. Invoked in
    /// order and once per token.
    // Same reason for above for not returning an iterator.
    fn iterate<F>(&self, F)
      where F: FnMut(Self::Token);

    /// The number of tokens present in this buffer.
    fn len(&self) -> usize;

    /// Copies all the tokens in this buffer to a new `Vec`.
    #[cfg(feature="std")]
    fn to_vec(&self) -> Vec<Self::Token>;

    /// Consumes self to create an owned vector of tokens.
    ///
    /// Will allocate if the implementation borrows storage or does not use an owned type
    /// compatible with `Vec` internally.
    #[cfg(feature="std")]
    fn into_vec(self) -> Vec<Self::Token>;

    /// Returns true if this buffer is empty.
    fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<'a, I: Copy + PartialEq> Buffer for &'a [I] {
    type Token = I;

    fn fold<B, F>(self, init: B, f: F) -> B
      where F: FnMut(B, Self::Token) -> B {
        (&self[..]).iter().cloned().fold(init, f)
    }

    fn iterate<F>(&self, mut f: F)
      where F: FnMut(Self::Token) {
        for c in (&self[..]).iter().cloned() {
            f(c)
        }
    }

    fn len(&self) -> usize {
        // Slice to reach inherent method to prevent infinite recursion
        (&self[..]).len()
    }

    #[cfg(feature="std")]
    fn to_vec(&self) -> Vec<Self::Token> {
        (&self[..]).to_vec()
    }

    #[cfg(feature="std")]
    fn into_vec(self) -> Vec<Self::Token> {
        (&self[..]).to_vec()
    }
}

impl<'a> Buffer for &'a str {
    type Token = char;

    fn fold<B, F>(self, init: B, f: F) -> B
      where F: FnMut(B, Self::Token) -> B {
        self.chars().fold(init, f)
    }

    fn iterate<F>(&self, mut f: F)
      where F: FnMut(Self::Token) {
        for c in self.chars() {
            f(c)
        }
    }

    fn len(&self) -> usize {
        self.chars().count()
    }

    fn is_empty(&self) -> bool {
        (&self[..]).is_empty()
    }

    #[cfg(feature="std")]
    fn to_vec(&self) -> Vec<Self::Token> {
        (&self[..]).chars().collect()
    }

    #[cfg(feature="std")]
    fn into_vec(self) -> Vec<Self::Token> {
        (&self[..]).chars().collect()
    }
}

/// Linear type containing the parser state, this type is threaded though `bind` and is also the
/// initial type passed to a parser.
///
/// Coupled with the `ParseResult` type it forms the parser monad:
///
/// ```ignore
/// Fn*<I: Input>(I, ...) -> ParseResult<I, T, E>;
/// ```
///
/// where ``Fn*`` is the appropriate closure/function trait, `I` the input type (can be something
/// like `[u8]`), `...` additional parameters to the parser, `T` the carried success type and `E`
/// the potential error type.
pub trait Input: Sized {
    /// The token type of the input.
    type Token: Copy + PartialEq;

    /// A marker type which is used to backtrack using `_mark` and `_restore`.
    ///
    /// It should also be possible to use this type to consume the data from the marked position to
    /// the current position.
    #[doc(hidden)]
    type Marker;

    /// The buffer type yielded by this input when multiple tokens are consumed in sequence.
    ///
    /// Can eg. provide zero-copy parsing if the input type is built to support it.
    type Buffer: Buffer<Token=Self::Token>;

    /// Returns `t` as a success value in the parsing context.
    ///
    /// Equivalent to Haskell's `return` function in the `Monad` typeclass.
    ///
    /// # Example
    ///
    /// ```
    /// use chomp::prelude::{Input, parse_only};
    ///
    /// let r = parse_only(|i|
    ///     // Annotate the error type
    ///     i.ret::<_, ()>("Wohoo, success!"),
    ///     b"some input");
    ///
    /// assert_eq!(r, Ok("Wohoo, success!"));
    /// ```
    #[inline]
    fn ret<T, E>(self, t: T) -> ParseResult<Self, T, E> {
        ParseResult(self, Ok(t))
    }

    /// Returns `e` as an error value in the parsing context.
    ///
    /// A more general version of Haskell's `fail` function in the `Monad` typeclass.
    ///
    /// # Example
    ///
    /// ```
    /// use chomp::prelude::{Input, parse_only};
    ///
    /// let r = parse_only(|i|
    ///     // Annotate the value type
    ///     i.err::<(), _>("Something went wrong"),
    ///     b"some input");
    ///
    /// assert_eq!(r, Err((&b"some input"[..], "Something went wrong")));
    /// ```
    #[inline]
    fn err<T, E>(self, e: E) -> ParseResult<Self, T, E> {
        ParseResult(self, Err(e))
    }

    /// Converts a `Result` into a `ParseResult`, preserving parser state.
    ///
    /// To convert an `Option` into a `ParseResult` it is recommended to use
    /// [`Option::ok_or`](https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or)
    /// or [`Option::ok_or_else`](https://doc.rust-lang.org/std/option/enum.Option.html#method.ok_or_else)
    /// in combination with this method.
    ///
    /// # Examples
    ///
    /// ```
    /// use chomp::prelude::{Input, parse_only};
    ///
    /// let r = parse_only(|i| i.from_result::<_, ()>(Ok("foo")), b"test");
    ///
    /// assert_eq!(r, Ok("foo"));
    ///
    /// let r = parse_only(|i| i.from_result::<(), _>(Err("error message")), b"test");
    ///
    /// assert_eq!(r, Err((&b"test"[..], "error message")));
    /// ```
    #[inline]
    fn from_result<T, E>(self, r: Result<T, E>) -> ParseResult<Self, T, E> {
        ParseResult(self, r)
    }

    // Primitive methods

    /// **Primitive:** See `Primitives::peek` for documentation.
    #[inline]
    #[doc(hidden)]
    fn _peek(&mut self, Guard) -> Option<Self::Token>;

    /// **Primitive:** See `Primitives::pop` for documentation.
    #[inline]
    #[doc(hidden)]
    fn _pop(&mut self, Guard) -> Option<Self::Token>;

    /// **Primitive:** See `Primitives::consume` for documentation.
    #[inline]
    #[doc(hidden)]
    fn _consume(&mut self, Guard, usize) -> Option<Self::Buffer>;

    /// **Primitive:** See `Primitives::consume_while` for documentation.
    #[inline]
    #[doc(hidden)]
    fn _consume_while<F>(&mut self, Guard, F) -> Self::Buffer
      where F: FnMut(Self::Token) -> bool;

    /// **Primitive:** See `Primitives::consume_from for documentation.
    #[inline]
    #[doc(hidden)]
    fn _consume_from(&mut self, Guard, Self::Marker) -> Self::Buffer;

    /// **Primitive:** See `Primitives::consume_remaining` for documentation.
    #[inline]
    #[doc(hidden)]
    fn _consume_remaining(&mut self, Guard) -> Self::Buffer;

    /// **Primitive:** See `Primitives::skip_while` for documentation.
    #[inline]
    #[doc(hidden)]
    fn _skip_while<F>(&mut self, g: Guard, f: F)
      where F: FnMut(Self::Token) -> bool {
        self._consume_while(g, f);
    }

    /// **Primitive:** See `Primitives::mark` for documentation.
    #[inline]
    #[doc(hidden)]
    fn _mark(&self, Guard) -> Self::Marker;

    /// **Primitive:** See `Primitives::restore` for documentation.
    #[inline]
    #[doc(hidden)]
    fn _restore(self, Guard, Self::Marker) -> Self;
}

impl<'a, I: Copy + PartialEq> Input for &'a [I] {
    type Token  = I;
    type Marker = &'a [I];
    type Buffer = &'a [I];

    #[inline]
    fn _peek(&mut self, _g: Guard) -> Option<Self::Token> {
        self.first().cloned()
    }

    #[inline]
    fn _pop(&mut self, _g: Guard) -> Option<Self::Token> {
        self.first().cloned().map(|c| {
            *self = &self[1..];

            c
        })
    }

    #[inline]
    fn _consume(&mut self, _g: Guard, n: usize) -> Option<Self::Buffer> {
        if n > self.len() {
            None
        } else {
            let b = &self[..n];

            *self = &self[n..];

            Some(b)
        }
    }

    #[inline]
    fn _consume_while<F>(&mut self, _g: Guard, mut f: F) -> Self::Buffer
      where F: FnMut(Self::Token) -> bool {
        if let Some(n) = self.iter().position(|c| !f(*c)) {
            let b = &self[..n];

            *self = &self[n..];

            b
        }  else {
            let b = &self[..];

            *self = &self[..0];

            b
        }
    }

    #[inline]
    fn _consume_from(&mut self, _g: Guard, m: Self::Marker) -> Self::Buffer {
        &m[..m.len() - self.len()]
    }

    #[inline]
    fn _consume_remaining(&mut self, _g: Guard) -> Self::Buffer {
        let b = &self[..];

        *self = &self[..0];

        b
    }

    #[inline]
    fn _mark(&self, _g: Guard) -> Self::Marker {
        self
    }

    #[inline]
    fn _restore(self, _g: Guard, m: Self::Marker) -> Self {
        m
    }
}

impl<'a> Input for &'a str {
    type Token  = char;
    type Marker = &'a str;
    type Buffer = &'a str;

    #[inline]
    fn _peek(&mut self, _g: Guard) -> Option<Self::Token> {
        self.chars().next()
    }

    #[inline]
    fn _pop(&mut self, _g: Guard) -> Option<Self::Token> {
        let mut iter = self.char_indices();

        iter.next().map(|(_, c)| {
            match iter.next().map(|(p, _)| p) {
                Some(n) => *self = &self[n..],
                None    => *self = &self[..0],
            }

            c
        })
    }

    #[inline]
    fn _consume(&mut self, _g: Guard, n: usize) -> Option<Self::Buffer> {
        match self.char_indices().enumerate().take(n + 1).last() {
            // num always equal to n if self contains more than n characters
            Some((num, (pos, _))) if n == num => {
                let b = &self[..pos];

                *self = &self[pos..];

                Some(b)
            },
            // num always equal to n - 1 if self contains exactly n characters
            Some((num, _)) if n == num + 1 => {
                let b = &self[..];

                *self = &self[..0];

                Some(b)
            },
            _ => None,
        }
    }

    #[inline]
    fn _consume_while<F>(&mut self, _g: Guard, mut f: F) -> Self::Buffer
      where F: FnMut(Self::Token) -> bool {
        // We need to find the character following the one which did not match
        if let Some((pos, _)) = self.char_indices().skip_while(|&(_, c)| f(c)).next() {
            let b = &self[..pos];

            *self = &self[pos..];

            b
        } else {
            let b = &self[..];

            *self = &self[..0];

            b
        }
    }

    #[inline]
    fn _consume_from(&mut self, _g: Guard, m: Self::Marker) -> Self::Buffer {
        &m[..m.len() - self.len()]
    }

    #[inline]
    fn _consume_remaining(&mut self, _g: Guard) -> Self::Buffer {
        let b = &self[..];

        *self = &self[..0];

        b
    }

    #[inline]
    fn _mark(&self, _g: Guard) -> Self::Marker {
        self
    }

    #[inline]
    fn _restore(self, _g: Guard, m: Self::Marker) -> Self {
        m
    }
}

/// A type alias for an `Input` with a token type of `u8`.
pub trait U8Input: Input<Token=u8> {}

impl<T> U8Input for T
  where T: Input<Token=u8> {}

/// The basic return type of a parser.
///
/// This type satisfies a variant of the `Monad` typeclass. Due to the limitations of Rust's
/// return types closures cannot be returned without boxing which has an unacceptable performance
/// impact.
///
/// To get around this issue and still provide a simple to use and safe (as in hard to accidentally
/// violate the monad laws or the assumptions taken by the parser type) an `Input` wrapper is
/// provided which ensures that the parser state is carried properly through every call to `bind`.
/// This is also known as a Linear Type (emulated through hiding destructors and using the
/// annotation `#[must_use]`).
///
/// Do-notation is provided by the macro `parse!`.
///
/// # Equivalence with Haskell's `Monad` typeclass:
///
/// ```text
/// f >>= g   ≡  f(m).bind(g)
/// f >> g    ≡  f(m).then(g)
/// return a  ≡  m.ret(a)
/// fail a    ≡  m.err(a)
/// ```
///
/// It also satisfies the monad laws:
///
/// ```text
/// return a >>= f   ≡  f a
/// m >>= return     ≡  m
/// (m >>= f) >>= g  ≡  m >>= (\x -> f x >>= g)
/// ```
#[must_use]
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct ParseResult<I: Input, T, E>(I, Result<T, E>);

impl<I: Input, T, E> ParseResult<I, T, E> {
    /// Sequentially composes the result with a parse action `f`, passing any produced value as
    /// the second parameter.
    ///
    /// The first parameter to the supplied function `f` is the parser state (`Input`). This
    /// state is then passed on to other parsers or used to return a value or an error.
    ///
    /// # Automatic conversion of `E`
    ///
    /// The error value `E` will automatically be converted using the `From` trait to the
    /// desired type. The downside with this using the current stable version of Rust (1.4) is that
    /// the type inferrence will currently not use the default value for the generic `V` and will
    /// therefore require extra type hint for the error.
    ///
    /// # Examples
    ///
    /// ```
    /// use chomp::prelude::{Input, parse_only};
    ///
    /// let r = parse_only(|i| {
    ///         i.ret("data".to_owned())
    ///         // Explicitly state the error type
    ///          .bind::<_, _, ()>(|i, x| i.ret(x + " here!"))
    ///     },
    ///     b"test");
    ///
    /// assert_eq!(r, Ok("data here!".to_owned()));
    /// ```
    ///
    /// Wrapping the expression in a function will both make it easier to compose and also provides
    /// the type-hint for the error in the function signature:
    ///
    /// ```
    /// use chomp::prelude::{Input, ParseResult, parse_only};
    ///
    /// fn parser<I: Input>(i: I, n: i32) -> ParseResult<I, i32, ()> {
    ///     i.ret(n + 10)
    /// }
    ///
    /// let r = parse_only(|i| i.ret(23).bind(parser), b"test");
    ///
    /// assert_eq!(r, Ok(33));
    /// ```
    #[inline]
    pub fn bind<F, U, V>(self, f: F) -> ParseResult<I, U, V>
      where F: FnOnce(I, T) -> ParseResult<I, U, V>,
            V: From<E> {
        match self.1 {
            Ok(t)  => f(self.0, t).map_err(From::from),
            Err(e) => ParseResult(self.0, Err(From::from(e))),
        }
    }

    /// Sequentially composes the result with a parse action `f`, discarding any produced value.
    ///
    /// The first parameter to the supplied function `f` is the parser state (`Input`). This
    /// state is then passed on to other parsers or used to return a value or an error.
    ///
    /// # Relation to `bind`
    ///
    /// ```text
    /// ParseResult::then(g)  ≡  ParseResult::bind(|i, _| g(i))
    /// ```
    ///
    /// # Example
    ///
    /// ```
    /// use chomp::prelude::{Input, SimpleResult, parse_only};
    ///
    /// fn g<I: Input>(i: I) -> SimpleResult<I, &'static str> {
    ///     i.ret("testing!")
    /// }
    ///
    /// let r1 = parse_only(|i| i.ret("initial state").bind(|i, _| g(i)), b"data");
    /// let r2 = parse_only(|i| i.ret("initial state").then(g), b"data");
    ///
    /// assert_eq!(r1, Ok("testing!"));
    /// assert_eq!(r2, Ok("testing!"));
    /// ```
    #[inline]
    pub fn then<F, U, V>(self, f: F) -> ParseResult<I, U, V>
      where F: FnOnce(I) -> ParseResult<I, U, V>,
            V: From<E> {
        self.bind(|i, _| f(i))
    }

    /// Applies the function `f` on the contained data if the parser is in a success state.
    ///
    /// # Example
    ///
    /// ```
    /// use chomp::prelude::{parse_only, any};
    ///
    /// let r = parse_only(|i| any(i).map(|c| c + 12), b"abc");
    ///
    /// assert_eq!(r, Ok(b'm'));
    /// ```
    #[inline]
    pub fn map<U, F>(self, f: F) -> ParseResult<I, U, E>
      where F: FnOnce(T) -> U {
        match self {
            ParseResult(i, Ok(t))  => ParseResult(i, Ok(f(t))),
            ParseResult(i, Err(e)) => ParseResult(i, Err(e)),
        }
    }

    /// Applies the function `f` on the contained error if the parser is in an error state.
    ///
    /// # Example
    ///
    /// ```
    /// use chomp::prelude::{Input, parse_only};
    ///
    /// let r = parse_only(|i| i.err::<(), _>("this is")
    ///          .map_err(|e| e.to_owned() + " an error"),
    ///          b"foo");
    ///
    /// assert_eq!(r, Err((&b"foo"[..], "this is an error".to_owned())));
    /// ```
    #[inline]
    pub fn map_err<V, F>(self, f: F) -> ParseResult<I, T, V>
      where F: FnOnce(E) -> V {
        match self {
            ParseResult(i, Ok(t))  => ParseResult(i, Ok(t)),
            ParseResult(i, Err(e)) => ParseResult(i, Err(f(e))),
        }
    }

    /// Calls the function `f` with a reference of the contained data if the parser is in a success
    /// state.
    ///
    /// # Example
    ///
    /// ```
    /// use chomp::prelude::{parse_only, take_while};
    ///
    /// let r = parse_only(|i| take_while(i, |c| c != b' ').inspect(|b| {
    ///     println!("{:?}", b); // Prints "test"
    /// }), b"test and more");
    ///
    /// assert_eq!(r, Ok(&b"test"[..]));
    /// ```
    #[inline]
    pub fn inspect<F>(self, f: F) -> ParseResult<I, T, E>
      where F: FnOnce(&T) {
        if let Ok(ref t) = self.1 {
             f(t)
        }

        self
    }
}

/// **Primitive:** Consumes the `ParseResult` and exposes the internal state.
///
/// # Primitive
///
/// Only used by fundamental parsers and combinators.
///
/// # Motivation
///
/// The `ParseResult` type is a semi-linear type, supposed to act like a linear type while used in
/// a parsing context to carry the state. Normally it should be as restrictive as the `Input` type
/// in terms of how much it exposes its internals, but the `IntoInner` trait implementation
/// allows fundamental parsers and combinators to expose the inner `Result` of the `ParseResult`
/// and act on this.
impl<I: Input, T, E> IntoInner for ParseResult<I, T, E> {
    type Inner = (I, Result<T, E>);

    #[inline(always)]
    fn into_inner(self) -> Self::Inner {
        (self.0, self.1)
    }
}

#[cfg(test)]
pub mod test {
    use super::{Buffer, Input, ParseResult};
    use primitives::IntoInner;
    use std::fmt::Debug;

    #[test]
    fn ret() {
        let r1: ParseResult<_, u32, ()>   = b"in1".ret::<_, ()>(23u32);
        let r2: ParseResult<_, i32, &str> = b"in2".ret::<_, &str>(23i32);

        assert_eq!(r1.into_inner(), (&b"in1"[..], Ok(23u32)));
        assert_eq!(r2.into_inner(), (&b"in2"[..], Ok(23i32)));
    }

    #[test]
    fn err() {
        let r1: ParseResult<_, (), u32>   = b"in1".err::<(), _>(23u32);
        let r2: ParseResult<_, &str, i32> = b"in2".err::<&str, _>(23i32);

        assert_eq!(r1.into_inner(), (&b"in1"[..], Err(23u32)));
        assert_eq!(r2.into_inner(), (&b"in2"[..], Err(23i32)));
    }

    #[test]
    fn from_result() {
        let i1: Result<u32, &str> = Ok(23);
        let i2: Result<&str, &str> = Err("foobar");

        let r1 = b"in1".from_result(i1);
        let r2 = b"in2".from_result(i2);

        assert_eq!(r1.into_inner(), (&b"in1"[..], Ok(23u32)));
        assert_eq!(r2.into_inner(), (&b"in2"[..], Err("foobar")));
    }

    #[test]
    fn monad_left_identity() {
        fn f<I: Input>(i: I, n: u32) -> ParseResult<I, u32, ()> {
            i.ret(n + 1)
        }

        let a = 123;
        // return a >>= f
        let lhs = b"test".ret(a).bind(f);
        // f a
        let rhs = f(&b"test"[..], a);

        assert_eq!((lhs.0, lhs.1), (&b"test"[..], Ok(124)));
        assert_eq!((rhs.0, rhs.1), (&b"test"[..], Ok(124)));
    }

    #[test]
    fn monad_right_identity() {
        let m1 = b"test".ret::<_, ()>(1);
        let m2 = b"test".ret::<_, ()>(1);

        // m1 >>= ret === m2
        let lhs = m1.bind::<_, _, ()>(Input::ret);
        let rhs = m2;

        assert_eq!((lhs.0, rhs.1), (&b"test"[..], Ok(1)));
        assert_eq!((rhs.0, lhs.1), (&b"test"[..], Ok(1)));
    }

    #[test]
    fn monad_associativity() {
         fn f<I: Input>(i: I, num: u32) -> ParseResult<I, u64, ()> {
            i.ret((num + 1) as u64)
        }

        fn g<I: Input>(i: I, num: u64) -> ParseResult<I, u64, ()> {
            i.ret(num * 2)
        }

        let lhs_m = b"test".ret::<_, ()>(2);
        let rhs_m = b"test".ret::<_, ()>(2);

        // (m >>= f) >>= g
        let lhs = lhs_m.bind(f).bind(g);
        // m >>= (\x -> f x >>= g)
        let rhs = rhs_m.bind(|i, x| f(i, x).bind(g));

        assert_eq!((lhs.0, lhs.1), (&b"test"[..], Ok(6)));
        assert_eq!((rhs.0, rhs.1), (&b"test"[..], Ok(6)));
    }

    #[test]
    fn parse_result_inspect() {
        use primitives::IntoInner;

        let mut n1 = 0;
        let mut n2 = 0;
        let i1     = b"test ".ret::<u32, ()>(23);
        let i2     = b"test ".ret::<u32, ()>(23);

        let r1 = i1.inspect(|d: &u32| {
            assert_eq!(d, &23);

            n1 += 1;
        });
        let r2 = i2.inspect(|d: &u32| {
            assert_eq!(d, &23);

            n2 += 1;
        });

        assert_eq!(r1.into_inner(), (&b"test "[..], Ok(23)));
        assert_eq!(n1, 1);
        assert_eq!(r2.into_inner(), (&b"test "[..], Ok(23)));
        assert_eq!(n2, 1);
    }

    #[test]
    fn input_propagation() {
        let mut n_calls = 0;

        let i = b"test1".ret::<_, ()>(23);

        assert_eq!(i.0, b"test1");
        assert_eq!(i.1, Ok(23));

        let r: ParseResult<_, _, ()> = i.bind(|i, t| { n_calls += 1; i.ret(t) });

        assert_eq!((r.0, r.1), (&b"test1"[..], Ok(23)));
        assert_eq!(n_calls, 1);
    }

    #[test]
    fn error_propagation() {
        let mut n_calls = 0;

        let i = b"test1".err::<(), _>(23);

        assert_eq!(i.0, b"test1");
        assert_eq!(i.1, Err(23));

        let r = i.bind(|i, t| { n_calls += 1; i.ret(t) });

        assert_eq!((r.0, r.1), (&b"test1"[..], Err(23)));
        assert_eq!(n_calls, 0);
    }

    #[test]
    fn slice() {
        fn f<I: Input>(i: I, n: u32) -> ParseResult<I, u32, ()> { i.ret(n + 1) }

        let lhs = (&b"test"[..]).ret(123).bind(f);
        let rhs = f(&b"test"[..], 123);

        assert_eq!((lhs.0, lhs.1), (&b"test"[..], Ok(124)));
        assert_eq!((rhs.0, rhs.1), (&b"test"[..], Ok(124)));
    }

    #[test]
    fn test_slice() {
        run_primitives_test(&b"abc"[..], |x| x);
    }

    #[test]
    fn test_string() {
        run_primitives_test(&"abc"[..], |c| c as char);
    }

    /// Should recieve an Input with the tokens 'a', 'b' and 'c' remaining.
    pub fn run_primitives_test<I: Input, F: Fn(u8) -> I::Token>(mut s: I, f: F)
      where I::Token:  Debug,
            I::Buffer: Clone {
        use primitives::Primitives;

        fn buffer_eq_slice<B: Buffer + Clone, F: Fn(u8) -> B::Token>(b: B, s: &[u8], f: F)
          where B::Token: Debug {
            assert_eq!(b.len(), s.len());
            assert_eq!(b.is_empty(), s.is_empty());
            assert_eq!(b.clone().fold(0, |n, c| {
                assert_eq!(c, f(s[n]));

                n + 1
            }), s.iter().count());
            buffer_to_vec(b, s, f);
        }

        #[cfg(feature="std")]
        fn buffer_to_vec<B: Buffer + Clone, F: Fn(u8) -> B::Token>(b: B, s: &[u8], f: F)
          where B::Token: Debug {
            assert_eq!(b.to_vec(), s.iter().cloned().map(f).collect::<Vec<_>>());
        }

        #[cfg(not(feature="std"))]
        fn buffer_to_vec<B: Buffer + Clone, F: Fn(u8) -> B::Token>(_: B, _: &[u8], _: F)
          where B::Token: Debug {}

        let m = s.mark();
        assert_eq!(s.peek(), Some(f(b'a')));
        assert_eq!(s.pop(),  Some(f(b'a')));
        assert_eq!(s.peek(), Some(f(b'b')));
        assert_eq!(s.pop(),  Some(f(b'b')));
        assert_eq!(s.peek(), Some(f(b'c')));
        assert_eq!(s.pop(),  Some(f(b'c')));
        assert_eq!(s.peek(), None);
        assert_eq!(s.pop(),  None);
        assert_eq!(s.peek(), None);
        assert_eq!(s.pop(),  None);
        assert!(s.consume(1).is_none());
        buffer_eq_slice(s.consume_remaining(), &b""[..], &f);

        {
            let mut n = 0;

            let b = s.consume_while(|_| { n += 1; true });

            assert_eq!(n, 0);
            buffer_eq_slice(b, &b""[..], &f);
        }

        let mut s = s.restore(m);
        assert_eq!(s.peek(), Some(f(b'a')));
        let m = s.mark();
        buffer_eq_slice(s.consume_remaining(), &b"abc"[..], &f);
        assert_eq!(s.peek(), None);
        let mut s = s.restore(m);
        assert_eq!(s.peek(), Some(f(b'a')));
        let m = s.mark();

        {
            let b = s.consume(2);

            assert_eq!(b.is_some(), true);
            buffer_eq_slice(b.unwrap(), &b"ab"[..], &f);
        }

        assert_eq!(s.peek(), Some(f(b'c')));

        let mut s = s.restore(m);
        assert_eq!(s.peek(), Some(f(b'a')));
        let m = s.mark();

        {
            let b = s.consume(3);

            assert_eq!(b.is_some(), true);
            buffer_eq_slice(b.unwrap(), &b"abc"[..], &f);
        }

        assert_eq!(s.peek(), None);
        let mut s = s.restore(m);
        let m = s.mark();

        if let Some(b) = s.consume(3) {
            let mut v = Vec::new();

            assert_eq!(b.len(), 3);
            assert_eq!(b.is_empty(), false);

            b.iterate(|c| {
                v.push(c);
            });

            assert_eq!(v, [f(b'a'), f(b'b'), f(b'c')]);
            assert_eq!(b.len(), 3);
            assert_eq!(b.is_empty(), false);
        }
        else {
            panic!("s.consume(3) failed");
        }

        assert_eq!(s.peek(), None);
        let mut s = s.restore(m);
        assert_eq!(s.peek(), Some(f(b'a')));
        let m = s.mark();

        {
            let mut n = 0;

            let b = s.consume_while(|c| {
                assert_eq!(c, f(b"abc"[n]));

                n += 1;

                n < 3
            });

            assert_eq!(n, 3);
            buffer_eq_slice(b, &b"ab"[..], &f);
        }

        assert_eq!(s.peek(), Some(f(b'c')));
        assert_eq!(s.pop(),  Some(f(b'c')));
        assert_eq!(s.peek(), None);
        assert_eq!(s.pop(),  None);

        buffer_eq_slice(s.consume_from(m), &b"abc"[..], &f);
    }
}