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
use std::{
    fmt,
    io::{self, Bytes, Read},
};

use crate::{
    error::{ParseError, StreamError, Tracked},
    stream::{StreamErrorFor, StreamOnce},
};

#[derive(Debug)]
pub enum Error {
    Unexpected,
    EndOfInput,
    Io(io::Error),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Unexpected => write!(f, "unexpected parse"),
            Error::EndOfInput => write!(f, "unexpected end of input"),
            Error::Io(err) => write!(f, "{}", err),
        }
    }
}

impl PartialEq for Error {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Error::Unexpected, Error::Unexpected) => true,
            (Error::EndOfInput, Error::EndOfInput) => true,
            _ => false,
        }
    }
}

impl<Item, Range> StreamError<Item, Range> for Error {
    #[inline]
    fn unexpected_token(_: Item) -> Self {
        Error::Unexpected
    }
    #[inline]
    fn unexpected_range(_: Range) -> Self {
        Error::Unexpected
    }
    #[inline]
    fn unexpected_format<T>(_: T) -> Self
    where
        T: fmt::Display,
    {
        Error::Unexpected
    }

    #[inline]
    fn expected_token(_: Item) -> Self {
        Error::Unexpected
    }
    #[inline]
    fn expected_range(_: Range) -> Self {
        Error::Unexpected
    }
    #[inline]
    fn expected_format<T>(_: T) -> Self
    where
        T: fmt::Display,
    {
        Error::Unexpected
    }
    #[inline]
    fn message_format<T>(_: T) -> Self
    where
        T: fmt::Display,
    {
        Error::Unexpected
    }
    #[inline]
    fn message_token(_: Item) -> Self {
        Error::Unexpected
    }
    #[inline]
    fn message_range(_: Range) -> Self {
        Error::Unexpected
    }

    #[inline]
    fn end_of_input() -> Self {
        Error::EndOfInput
    }

    #[inline]
    fn is_unexpected_end_of_input(&self) -> bool {
        *self == Error::EndOfInput
    }

    #[inline]
    fn into_other<T>(self) -> T
    where
        T: StreamError<Item, Range>,
    {
        match self {
            Error::Unexpected => T::unexpected_static_message("parse"),
            Error::EndOfInput => T::end_of_input(),
            Error::Io(err) => T::other(err),
        }
    }
}

impl<Item, Range, Position> ParseError<Item, Range, Position> for Error
where
    Position: Default,
{
    type StreamError = Self;
    #[inline]
    fn empty(_position: Position) -> Self {
        Error::Unexpected
    }

    #[inline]
    fn from_error(_: Position, err: Self::StreamError) -> Self {
        err
    }

    #[inline]
    fn set_position(&mut self, _position: Position) {}

    #[inline]
    fn add(&mut self, err: Self::StreamError) {
        *self = match (&*self, err) {
            (Error::EndOfInput, _) => Error::EndOfInput,
            (_, err) => err,
        };
    }

    #[inline]
    fn set_expected<F>(self_: &mut Tracked<Self>, info: Self::StreamError, f: F)
    where
        F: FnOnce(&mut Tracked<Self>),
    {
        f(self_);
        self_.error = info;
    }

    fn is_unexpected_end_of_input(&self) -> bool {
        *self == Error::EndOfInput
    }

    #[inline]
    fn into_other<T>(self) -> T
    where
        T: ParseError<Item, Range, Position>,
    {
        T::from_error(Position::default(), StreamError::into_other(self))
    }
}

pub struct Stream<R> {
    bytes: Bytes<R>,
}

impl<R: Read> StreamOnce for Stream<R> {
    type Token = u8;
    type Range = &'static [u8];
    type Position = usize;
    type Error = Error;

    #[inline]
    fn uncons(&mut self) -> Result<u8, StreamErrorFor<Self>> {
        match self.bytes.next() {
            Some(Ok(b)) => Ok(b),
            Some(Err(err)) => Err(Error::Io(err)),
            None => Err(Error::EndOfInput),
        }
    }
}

impl<R> Stream<R>
where
    R: Read,
{
    /// Creates a `StreamOnce` instance from a value implementing `std::io::Read`.
    ///
    /// NOTE: This type do not implement `Positioned` and `Clone` and must be wrapped with types
    ///     such as `BufferedStreamRef` and `State` to become a `Stream` which can be parsed
    ///
    /// ```rust
    /// # #![cfg(feature = "std")]
    /// # extern crate combine;
    /// use combine::*;
    /// use combine::parser::byte::*;
    /// use combine::stream::read;
    /// use combine::stream::buffered;
    /// use combine::stream::position;
    /// use std::io::Read;
    ///
    /// # fn main() {
    /// let input: &[u8] = b"123,";
    /// let stream = buffered::Stream::new(position::Stream::new(read::Stream::new(input)), 1);
    /// let result = (many(digit()), byte(b','))
    ///     .parse(stream)
    ///     .map(|t| t.0);
    /// assert_eq!(result, Ok((vec![b'1', b'2', b'3'], b',')));
    /// # }
    /// ```
    pub fn new(read: R) -> Stream<R> {
        Stream {
            bytes: read.bytes(),
        }
    }
}