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
//! Utilities for parsing streams of data.
//!
//! # Examples
//!
//! ```
//! # #[macro_use] extern crate chomp;
//! # fn main() {
//! use std::fs::File;
//!
//! use chomp::buffer;
//! use chomp::buffer::Stream;
//! use chomp::prelude::{token, take_while, take_while1};
//! use chomp::ascii::is_whitespace;
//!
//! let f = File::open("./README.md").unwrap();
//!
//! let mut b = buffer::Source::from_read(f, buffer::FixedSizeBuffer::new());
//!
//! let r = b.parse(parser!{
//!     take_while(|c| c != b'#');
//!     token(b'#');
//!     take_while1(is_whitespace);
//!     take_while1(|c| c != b'\r' && c != b'\n')
//! });
//!
//! assert_eq!(r, Ok(&b"Chomp"[..]));
//! # }
//! ```

mod stateful;
mod slice;

pub mod data_source;

use std::io;
use std::ops;
use std::ptr;

use std::cell::Cell;

use types::{Input, ParseResult};
use types::Buffer as InputBuffer;
use primitives::Guard;

pub use self::slice::SliceStream;
pub use self::data_source::{DataSource, RWDataSource};
pub use self::stateful::Source;

const DEFAULT_BUFFER_SIZE: usize = 6 * 1024;

/// Error type for parsing using the `Stream` trait.
#[derive(Debug)]
pub enum StreamError<B: InputBuffer, E> {
    /// An error occurred in the parser, the given slice indicates the part which failed.
    ParseError(B, E),
    /// Parser failed to complete with the available data.
    Incomplete,
    /// An IO-error occurred while attempting to fill the buffer.
    IoError(io::Error),
    /// The last parser completed successfully and there is no more input to parse.
    EndOfInput,
    /// The last parser failed with an incomplete state, fill the buffer and try again.
    ///
    /// Filling the buffer is automatic by default.
    Retry,
}

impl<B: InputBuffer, E: PartialEq<E>> PartialEq for StreamError<B, E> {
    #[inline]
    fn eq(&self, other: &StreamError<B, E>) -> bool {
        match (self, other) {
            (&StreamError::ParseError(ref b1, ref e1), &StreamError::ParseError(ref b2, ref e2)) => b1 == b2 && e1 == e2,
            (&StreamError::Incomplete, &StreamError::Incomplete)
              | (&StreamError::EndOfInput, &StreamError::EndOfInput)
              | (&StreamError::Retry, &StreamError::Retry)           => true,
            _ => false,
        }
    }
}

/// Trait wrapping the state management in reading from a data source while parsing.
pub trait Stream<'a, 'i> {
    /// The input item type, usually depending on which `DataSource` is used.
    type Input: Input + 'i;

    /// Attempts to run the supplied parser `F` once on the currently populated data in this
    /// stream, providing a borrow of the inner data storage.
    ///
    /// If a `StreamError::Retry` is returned the consuming code it should just retry the action
    /// (the implementation might require a separate call to refill the stream).
    #[inline]
    fn parse<F, T, E>(&'a mut self, f: F) -> Result<T, StreamError<<Self::Input as Input>::Buffer, E>>
      where F: FnOnce(Self::Input) -> ParseResult<Self::Input, T, E>,
            T: 'i,
            E: 'i;
}

/// Input buffer type which contains a flag which tells if we might need to read more data.
#[must_use]
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct InputBuf<'a, I: 'a>(
    /// If this is set to true a parser has tried to read past the end of this buffer.
    bool,
    /// Current buffer slice
    &'a [I],
);

impl<'a, I: 'a> InputBuf<'a, I> {
    /// Creates a new input buffer with incomplete set to false.
    #[inline]
    pub fn new(buf: &'a [I]) -> Self {
        InputBuf(false, buf)
    }

    /// Returns true if parsers want to obtain more data.
    ///
    /// The result of the parsing is only accurate if this is false after completed parsing.
    #[inline]
    pub fn is_incomplete(&self) -> bool {
        self.0
    }

    /// Returns the length of the contained buffer, may be an incomplete buffer.
    #[inline]
    pub fn len(&self) -> usize {
        self.1.len()
    }

    /// Returns true if the contained buffer is empty, may return true even when incomplete.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

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

    #[inline]
    fn _peek(&mut self, _g: Guard) -> Option<Self::Token> {
        if let Some(c) = self.1.first() {
            Some(*c)
        } else {
            self.0 = true;

            None
        }
    }

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

            c
        })
    }

    #[inline]
    fn _consume(&mut self, _g: Guard, n: usize) -> Option<Self::Buffer> {
        if n > self.1.len() {
            self.0 = true;

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

            self.1 = &self.1[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.1.iter().position(|c| !f(*c)) {
            let b = &self.1[..n];

            self.1 = &self.1[n..];

            b
        } else {
            self._consume_remaining(g)
        }
    }

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

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

        let b = self.1;

        self.1 = &self.1[..0];

        b
    }

    #[inline]
    fn _mark(&self, _g: Guard) -> Self::Marker {
        // Incomplete state is separate from the parsed state, no matter how we hit incomplete we
        // want to keep it.
        self.1
    }

    #[inline]
    fn _restore(mut self, _g: Guard, m: Self::Marker) -> Self {
        self.1 = m;

        self
    }
}

/// Trait all parser buffers implement.
///
/// Enables the consumer to request specific amounts of data and only consume partial parts of the
/// buffer.
pub trait Buffer<I: Copy + PartialEq>: ops::Deref<Target=[I]> {
    /// Attempt to fill the buffer using the closure `F`.
    ///
    /// The successful return from `F` should contain the number of items successfully written to
    /// the slice.
    ///
    /// # Notes
    ///
    /// * The returned value must *NOT* be larger than the length of the given slice.
    ///
    /// * Return `0` if no more data is available or if the slice is of zero length.
    ///
    /// * The slice might contain uninitialized memory, do not read from the slice.
    #[inline]
    fn fill<S: DataSource<Item=I>>(&mut self, &mut S) -> io::Result<usize>;

    /// Buffer attempts to clear space for additional items.
    #[inline]
    fn request_space(&mut self, usize);

    /// Consumes the given amount of bytes, must be less than or equal to `len()`.
    ///
    /// Does not invalidate any borrow of data from self.
    #[inline]
    fn consume(&self, items: usize);

    /// Returns the number of bytes left in the buffer.
    #[inline]
    fn len(&self) -> usize;

    /// If the buffer has no more data.
    #[inline]
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the maximum amount of data which can be stored
    #[inline]
    fn capacity(&self) -> usize;
}

/// A fixed size buffer.
///
/// Only allocates when created.
// TODO: Tests
#[derive(Debug, Eq, PartialEq)]
pub struct FixedSizeBuffer<I: Copy + PartialEq> {
    /// Backing memory.
    buffer:    Vec<I>,
    /// Number of items of `buffer` which contain actual data.
    populated: usize,
    /// The number of bytes from the start of the buffer which are used.
    ///
    /// As long as used <= populated it is safe.
    used:      Cell<usize>,
}

impl<I: Copy + PartialEq> FixedSizeBuffer<I> {
    /// Creates a fixed-size buffer with the default buffer size.
    #[inline]
    pub fn new() -> Self {
        Self::with_size(DEFAULT_BUFFER_SIZE)
    }

    /// Creates a fixed-size buffer with the supplied buffer size.
    #[inline]
    pub fn with_size(size: usize) -> Self {
        assert!(size > 0);

        let mut buf = Vec::with_capacity(size);

        // TODO: Would it be better with a Default requirement on I?
        // We set the length here to allow fill() to hand out a slice of uninitialized memory
        // to be populated.
        // NOTE: We cannot actually expose this memory to the parser since self.populated will
        // be the upper limit for the deref to slice.
        unsafe {
            buf.set_len(size);
        }

        FixedSizeBuffer {
            buffer:    buf,
            populated: 0,
            used:      Cell::new(0),
        }
    }
}

impl<I: Copy + PartialEq> ops::Deref for FixedSizeBuffer<I> {
    type Target = [I];

    #[inline]
    fn deref(&self) -> &[I] {
        &self.buffer[self.used.get()..self.populated]
    }
}

impl<I: Copy + PartialEq> ops::DerefMut for FixedSizeBuffer<I> {
    #[inline]
    fn deref_mut(&mut self) -> &mut [I] {
        &mut self.buffer[self.used.get()..self.populated]
    }
}

impl<I: Copy + PartialEq> Buffer<I> for FixedSizeBuffer<I> {
    #[inline]
    fn fill<S: DataSource<Item=I>>(&mut self, s: &mut S) -> io::Result<usize> {
        s.read(&mut self.buffer[self.populated..]).map(|n| {
            debug_assert!(self.populated + n <= self.buffer.len());

            self.populated += n;

            n
        })
    }

    #[inline]
    fn request_space(&mut self, items: usize) {
        use std::ptr;

        assert!(self.populated >= self.used.get());

        // Only copy if we actually need to free the space
        if self.buffer.len() - self.populated < items {
            unsafe {
                ptr::copy(self.buffer.as_ptr().offset(self.used.get() as isize), self.buffer.as_mut_ptr(), self.populated - self.used.get());
            }

            self.populated -= self.used.get();
            self.used.set(0);
        }
    }

    #[inline]
    fn consume(&self, items: usize) {
        debug_assert!(self.used.get() + items <= self.populated);

        self.used.set(self.used.get() + items)
    }

    #[inline]
    fn len(&self) -> usize {
        self.populated - self.used.get()
    }

    #[inline]
    fn capacity(&self) -> usize {
        self.buffer.len()
    }
}

/// A buffer which will reallocate to fit the requested amount of data.
///
/// # Note:
///
/// Will not decrease in size.
// TODO: Tests
#[derive(Debug)]
pub struct GrowingBuffer<I: Copy + PartialEq> {
    /// Backing memory.
    buffer:    Vec<I>,
    /// Number of items of `buffer` which contain actual data.
    populated: usize,
    /// Maximal size of the buffer, 0 means infinity.
    limit:     usize,
    /// The number of bytes from the start of the buffer which are used.
    ///
    /// As long as used <= populated it is safe.
    used:      Cell<usize>,
}

impl<I: Copy + PartialEq> GrowingBuffer<I> {
    /// Creates a new unlimited `GrowingBuffer`.
    #[inline]
    pub fn new() -> Self {
        Self::with_limit(0)
    }

    /// Creates a new `GrowingBuffer` with the specified limit.
    ///
    /// # Note
    ///
    /// The actual amount of allocated memory might be larger than the specified limit, depends on
    /// the allocator.
    #[inline]
    pub fn with_limit(limit: usize) -> Self {
        GrowingBuffer {
            buffer:    Vec::new(),
            populated: 0,
            limit:     limit,
            used:      Cell::new(0),
        }
    }
}

impl<I: Copy + PartialEq> ops::Deref for GrowingBuffer<I> {
    type Target = [I];

    #[inline]
    fn deref(&self) -> &[I] {
        &self.buffer[self.used.get()..self.populated]
    }
}

impl<I: Copy + PartialEq> ops::DerefMut for GrowingBuffer<I> {
    #[inline]
    fn deref_mut(&mut self) -> &mut [I] {
        &mut self.buffer[self.used.get()..self.populated]
    }
}

impl<I: Copy + PartialEq> Buffer<I> for GrowingBuffer<I> {
    #[inline]
    fn fill<S: DataSource<Item=I>>(&mut self, s: &mut S) -> io::Result<usize> {
        s.read(&mut self.buffer[self.populated..]).map(|n| {
            debug_assert!(self.populated + n <= self.buffer.len());

            self.populated += n;

            n
        })
    }

    #[inline]
    fn request_space(&mut self, items: usize) {
        // If we are over the limit, refuse
        if self.limit != 0 && self.buffer.capacity() > self.limit {
            return;
        }

        if items + self.len() > self.buffer.capacity() {
            // We do not have enough space for the new items, reallocate
            self.buffer.reserve(items);

            let cap = self.buffer.capacity();

            // TODO: Would it be better with a Default requirement on I?
            // We set the length here to allow fill() to hand out a slice of uninitialized memory
            // to be populated.
            // NOTE: We cannot actually expose this memory to the parser since self.populated will
            // be the upper limit for the deref to slice.
            unsafe {
                self.buffer.set_len(cap);
            }
        }

        // Only copy if we actually need to free the space
        if self.buffer.len() - self.populated < items {
            unsafe {
                ptr::copy(self.buffer.as_ptr().offset(self.used.get() as isize), self.buffer.as_mut_ptr(), self.populated - self.used.get());
            }

            self.populated -= self.used.get();
            self.used.set(0);
        }
    }

    #[inline]
    fn consume(&self, items: usize) {
        debug_assert!(self.used.get() + items <= self.populated);

        self.used.set(self.used.get() + items)
    }

    #[inline]
    fn len(&self) -> usize {
        self.populated - self.used.get()
    }

    #[inline]
    fn capacity(&self) -> usize {
        self.buffer.len()
    }
}

#[cfg(test)]
mod test {
    use super::InputBuf;
    use types::{Input, ParseResult};
    use primitives::{IntoInner, Primitives};

    use types::test::run_primitives_test;

    #[test]
    fn ret() {
        let i1: InputBuf<u8> = InputBuf::new(b"in1");
        let i2: InputBuf<u8> = InputBuf::new(b"in2");

        let r1: ParseResult<_, u32, ()> = i1.ret::<_, ()>(23u32);
        let r2: ParseResult<_, i32, &str> = i2.ret::<_, &str>(23i32);

        assert_eq!(r1.into_inner(), (InputBuf::new(b"in1"), Ok(23u32)));
        assert_eq!(r2.into_inner(), (InputBuf::new(b"in2"), Ok(23i32)));
    }

    #[test]
    fn err() {
        let i1: InputBuf<u8> = InputBuf::new(b"in1");
        let i2: InputBuf<u8> = InputBuf::new(b"in2");

        let r1: ParseResult<_, (), u32>   = i1.err::<(), _>(23u32);
        let r2: ParseResult<_, &str, i32> = i2.err::<&str, _>(23i32);

        assert_eq!(r1.into_inner(), (InputBuf::new(b"in1"), Err(23u32)));
        assert_eq!(r2.into_inner(), (InputBuf::new(b"in2"), Err(23i32)));
    }

    #[test]
    fn test_input_buf() {

        run_primitives_test(InputBuf::new(b"abc"), |x| x);

        let mut b = InputBuf::new(b"a");
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 1);
        assert_eq!(b.is_empty(), false);
        b.peek();
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 1);
        assert_eq!(b.is_empty(), false);
        b.pop();
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 0);
        assert_eq!(b.is_empty(), true);
        assert_eq!(b.peek(), None);
        assert_eq!(b.is_incomplete(), true);
        assert_eq!(b.len(), 0);
        assert_eq!(b.is_empty(), true);
        assert_eq!(b.pop(), None);
        assert_eq!(b.is_incomplete(), true);
        assert_eq!(b.len(), 0);
        assert_eq!(b.is_empty(), true);

        let mut b = InputBuf::new(b"ab");
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 2);
        assert_eq!(b.is_empty(), false);
        b.consume(1);
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 1);
        assert_eq!(b.is_empty(), false);
        b.consume(1);
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 0);
        assert_eq!(b.is_empty(), true);
        assert_eq!(b.consume(1), None);
        assert_eq!(b.is_incomplete(), true);
        assert_eq!(b.len(), 0);
        assert_eq!(b.is_empty(), true);

        let mut b = InputBuf::new(b"ab");
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 2);
        assert_eq!(b.is_empty(), false);
        assert_eq!(b.consume(3), None);
        assert_eq!(b.is_incomplete(), true);
        assert_eq!(b.len(), 2);
        assert_eq!(b.is_empty(), false);

        let mut b = InputBuf::new(b"ab");
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 2);
        assert_eq!(b.is_empty(), false);
        assert_eq!(b.consume_while(|_| true), &b"ab"[..]);
        assert_eq!(b.is_incomplete(), true);
        assert_eq!(b.len(), 0);
        assert_eq!(b.is_empty(), true);

        let mut b = InputBuf::new(b"ab");
        assert_eq!(b.consume_while(|c| c == b'a'), &b"a"[..]);
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 1);
        assert_eq!(b.is_empty(), false);
        assert_eq!(b.consume_while(|c| c == b'b'), &b"b"[..]);
        assert_eq!(b.is_incomplete(), true);
        assert_eq!(b.len(), 0);
        assert_eq!(b.is_empty(), true);
        assert_eq!(b.consume_while(|c| c == b'b'), &b""[..]);
        assert_eq!(b.is_incomplete(), true);
        assert_eq!(b.len(), 0);
        assert_eq!(b.is_empty(), true);

        let mut b = InputBuf::new(b"abc");
        let m = b.mark();
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 3);
        assert_eq!(b.is_empty(), false);
        b.consume(3);
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 0);
        assert_eq!(b.is_empty(), true);
        assert_eq!(b.consume_from(m), &b"abc"[..]);
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 0);
        assert_eq!(b.is_empty(), true);

        let mut b = InputBuf::new(b"abc");
        let m = b.mark();
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 3);
        assert_eq!(b.is_empty(), false);
        b.consume(2);
        b.consume(2);
        assert_eq!(b.is_incomplete(), true);
        assert_eq!(b.len(), 1);
        assert_eq!(b.is_empty(), false);
        let b = b.restore(m);
        assert_eq!(b.is_incomplete(), true);
        assert_eq!(b.len(), 3);
        assert_eq!(b.is_empty(), false);

        let mut b = InputBuf::new(b"abc");
        assert_eq!(b.is_incomplete(), false);
        assert_eq!(b.len(), 3);
        assert_eq!(b.is_empty(), false);
        b.consume_remaining();
        assert_eq!(b.is_incomplete(), true);
        assert_eq!(b.len(), 0);
        assert_eq!(b.is_empty(), true);
    }
}