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
use crate::lib::marker::PhantomData;

use crate::{
    error::{ParseErrorInto, ParseResult, StreamErrorInto},
    stream::{ResetStream, StreamErrorFor},
    Positioned, RangeStream, RangeStreamOnce, StreamOnce,
};

#[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Span<P> {
    pub start: P,
    pub end: P,
}

impl<P> From<P> for Span<P>
where
    P: Clone,
{
    #[inline]
    fn from(p: P) -> Self {
        Self {
            start: p.clone(),
            end: p,
        }
    }
}

impl<P> Span<P> {
    pub fn map<Q>(self, mut f: impl FnMut(P) -> Q) -> Span<Q> {
        Span {
            start: f(self.start),
            end: f(self.end),
        }
    }
}

#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub struct Stream<S, E>(pub S, PhantomData<fn(E) -> E>);

impl<S, E> From<S> for Stream<S, E> {
    fn from(stream: S) -> Self {
        Stream(stream, PhantomData)
    }
}

impl<S, E> ResetStream for Stream<S, E>
where
    S: ResetStream + Positioned,
    S::Token: PartialEq,
    S::Range: PartialEq,
    E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>,
    S::Error: ParseErrorInto<S::Token, S::Range, S::Position>,
    <S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError:
        StreamErrorInto<S::Token, S::Range>,
{
    type Checkpoint = S::Checkpoint;

    #[inline]
    fn checkpoint(&self) -> Self::Checkpoint {
        self.0.checkpoint()
    }

    #[inline]
    fn reset(&mut self, checkpoint: Self::Checkpoint) -> Result<(), Self::Error> {
        self.0
            .reset(checkpoint)
            .map_err(ParseErrorInto::into_other_error)
    }
}

impl<S, E> StreamOnce for Stream<S, E>
where
    S: StreamOnce + Positioned,
    S::Token: PartialEq,
    S::Range: PartialEq,
    E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>,
    S::Error: ParseErrorInto<S::Token, S::Range, S::Position>,
    <S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError:
        StreamErrorInto<S::Token, S::Range>,
{
    type Token = S::Token;
    type Range = S::Range;
    type Position = Span<S::Position>;
    type Error = E;

    #[inline]
    fn uncons(&mut self) -> Result<Self::Token, StreamErrorFor<Self>> {
        self.0.uncons().map_err(StreamErrorInto::into_other_error)
    }

    #[inline]
    fn is_partial(&self) -> bool {
        self.0.is_partial()
    }
}

impl<S, E> RangeStreamOnce for Stream<S, E>
where
    S: RangeStream,
    S::Token: PartialEq,
    S::Range: PartialEq,
    E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>,
    S::Error: ParseErrorInto<S::Token, S::Range, S::Position>,
    <S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError:
        StreamErrorInto<S::Token, S::Range>,
{
    #[inline]
    fn uncons_range(&mut self, size: usize) -> Result<Self::Range, StreamErrorFor<Self>> {
        self.0
            .uncons_range(size)
            .map_err(StreamErrorInto::into_other_error)
    }

    #[inline]
    fn uncons_while<F>(&mut self, f: F) -> Result<Self::Range, StreamErrorFor<Self>>
    where
        F: FnMut(Self::Token) -> bool,
    {
        self.0
            .uncons_while(f)
            .map_err(StreamErrorInto::into_other_error)
    }

    #[inline]
    fn uncons_while1<F>(&mut self, f: F) -> ParseResult<Self::Range, StreamErrorFor<Self>>
    where
        F: FnMut(Self::Token) -> bool,
    {
        self.0
            .uncons_while1(f)
            .map_err(StreamErrorInto::into_other_error)
    }

    #[inline]
    fn distance(&self, end: &Self::Checkpoint) -> usize {
        self.0.distance(end)
    }

    fn range(&self) -> Self::Range {
        self.0.range()
    }
}

impl<S, E> Positioned for Stream<S, E>
where
    S: StreamOnce + Positioned,
    S::Token: PartialEq,
    S::Range: PartialEq,
    E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>,
    S::Error: ParseErrorInto<S::Token, S::Range, S::Position>,
    <S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError:
        StreamErrorInto<S::Token, S::Range>,
{
    fn position(&self) -> Span<S::Position> {
        Span::from(self.0.position())
    }
}