1 use crate::lib::marker::PhantomData; 2 3 use crate::{ 4 error::{ParseErrorInto, ParseResult, StreamErrorInto}, 5 stream::{ResetStream, StreamErrorFor}, 6 Positioned, RangeStream, RangeStreamOnce, StreamOnce, 7 }; 8 9 #[derive(Copy, Clone, Default, Debug, PartialEq, Eq, PartialOrd, Ord)] 10 pub struct Span<P> { 11 pub start: P, 12 pub end: P, 13 } 14 15 impl<P> From<P> for Span<P> 16 where 17 P: Clone, 18 { 19 #[inline] from(p: P) -> Self20 fn from(p: P) -> Self { 21 Self { 22 start: p.clone(), 23 end: p, 24 } 25 } 26 } 27 28 impl<P> Span<P> { map<Q>(self, mut f: impl FnMut(P) -> Q) -> Span<Q>29 pub fn map<Q>(self, mut f: impl FnMut(P) -> Q) -> Span<Q> { 30 Span { 31 start: f(self.start), 32 end: f(self.end), 33 } 34 } 35 } 36 37 #[derive(PartialEq, Eq, Copy, Clone, Debug)] 38 pub struct Stream<S, E>(pub S, PhantomData<fn(E) -> E>); 39 40 impl<S, E> From<S> for Stream<S, E> { from(stream: S) -> Self41 fn from(stream: S) -> Self { 42 Stream(stream, PhantomData) 43 } 44 } 45 46 impl<S, E> ResetStream for Stream<S, E> 47 where 48 S: ResetStream + Positioned, 49 S::Token: PartialEq, 50 S::Range: PartialEq, 51 E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>, 52 S::Error: ParseErrorInto<S::Token, S::Range, S::Position>, 53 <S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError: 54 StreamErrorInto<S::Token, S::Range>, 55 { 56 type Checkpoint = S::Checkpoint; 57 58 #[inline] checkpoint(&self) -> Self::Checkpoint59 fn checkpoint(&self) -> Self::Checkpoint { 60 self.0.checkpoint() 61 } 62 63 #[inline] reset(&mut self, checkpoint: Self::Checkpoint) -> Result<(), Self::Error>64 fn reset(&mut self, checkpoint: Self::Checkpoint) -> Result<(), Self::Error> { 65 self.0 66 .reset(checkpoint) 67 .map_err(ParseErrorInto::into_other_error) 68 } 69 } 70 71 impl<S, E> StreamOnce for Stream<S, E> 72 where 73 S: StreamOnce + Positioned, 74 S::Token: PartialEq, 75 S::Range: PartialEq, 76 E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>, 77 S::Error: ParseErrorInto<S::Token, S::Range, S::Position>, 78 <S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError: 79 StreamErrorInto<S::Token, S::Range>, 80 { 81 type Token = S::Token; 82 type Range = S::Range; 83 type Position = Span<S::Position>; 84 type Error = E; 85 86 #[inline] uncons(&mut self) -> Result<Self::Token, StreamErrorFor<Self>>87 fn uncons(&mut self) -> Result<Self::Token, StreamErrorFor<Self>> { 88 self.0.uncons().map_err(StreamErrorInto::into_other_error) 89 } 90 91 #[inline] is_partial(&self) -> bool92 fn is_partial(&self) -> bool { 93 self.0.is_partial() 94 } 95 } 96 97 impl<S, E> RangeStreamOnce for Stream<S, E> 98 where 99 S: RangeStream, 100 S::Token: PartialEq, 101 S::Range: PartialEq, 102 E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>, 103 S::Error: ParseErrorInto<S::Token, S::Range, S::Position>, 104 <S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError: 105 StreamErrorInto<S::Token, S::Range>, 106 { 107 #[inline] uncons_range(&mut self, size: usize) -> Result<Self::Range, StreamErrorFor<Self>>108 fn uncons_range(&mut self, size: usize) -> Result<Self::Range, StreamErrorFor<Self>> { 109 self.0 110 .uncons_range(size) 111 .map_err(StreamErrorInto::into_other_error) 112 } 113 114 #[inline] uncons_while<F>(&mut self, f: F) -> Result<Self::Range, StreamErrorFor<Self>> where F: FnMut(Self::Token) -> bool,115 fn uncons_while<F>(&mut self, f: F) -> Result<Self::Range, StreamErrorFor<Self>> 116 where 117 F: FnMut(Self::Token) -> bool, 118 { 119 self.0 120 .uncons_while(f) 121 .map_err(StreamErrorInto::into_other_error) 122 } 123 124 #[inline] uncons_while1<F>(&mut self, f: F) -> ParseResult<Self::Range, StreamErrorFor<Self>> where F: FnMut(Self::Token) -> bool,125 fn uncons_while1<F>(&mut self, f: F) -> ParseResult<Self::Range, StreamErrorFor<Self>> 126 where 127 F: FnMut(Self::Token) -> bool, 128 { 129 self.0 130 .uncons_while1(f) 131 .map_err(StreamErrorInto::into_other_error) 132 } 133 134 #[inline] distance(&self, end: &Self::Checkpoint) -> usize135 fn distance(&self, end: &Self::Checkpoint) -> usize { 136 self.0.distance(end) 137 } 138 range(&self) -> Self::Range139 fn range(&self) -> Self::Range { 140 self.0.range() 141 } 142 } 143 144 impl<S, E> Positioned for Stream<S, E> 145 where 146 S: StreamOnce + Positioned, 147 S::Token: PartialEq, 148 S::Range: PartialEq, 149 E: crate::error::ParseError<S::Token, S::Range, Span<S::Position>>, 150 S::Error: ParseErrorInto<S::Token, S::Range, S::Position>, 151 <S::Error as crate::error::ParseError<S::Token, S::Range, S::Position>>::StreamError: 152 StreamErrorInto<S::Token, S::Range>, 153 { position(&self) -> Span<S::Position>154 fn position(&self) -> Span<S::Position> { 155 Span::from(self.0.position()) 156 } 157 } 158