use lib::fmt;
use lib::str::Chars;
#[cfg(feature = "std")]
use std::io::{Bytes, Read};
#[cfg(feature = "std")]
use stream::easy::Errors;
use Parser;
use error::FastResult::*;
use error::{
ConsumedResult, FastResult, ParseError, StreamError, StringStreamError, Tracked,
UnexpectedParse,
};
#[doc(hidden)]
#[macro_export]
macro_rules! clone_resetable {
(( $($params: tt)* ) $ty: ty) => {
impl<$($params)*> Resetable for $ty
{
type Checkpoint = Self;
fn checkpoint(&self) -> Self {
self.clone()
}
fn reset(&mut self, checkpoint: Self) {
*self = checkpoint;
}
}
}
}
#[cfg(feature = "std")]
pub mod buffered;
#[cfg(feature = "std")]
pub mod easy;
pub mod state;
pub trait Positioned: StreamOnce {
fn position(&self) -> Self::Position;
}
pub type StreamErrorFor<I> = <<I as StreamOnce>::Error as ParseError<
<I as StreamOnce>::Item,
<I as StreamOnce>::Range,
<I as StreamOnce>::Position,
>>::StreamError;
pub trait StreamOnce {
type Item: Clone + PartialEq;
type Range: Clone + PartialEq;
type Position: Clone + Ord;
type Error: ParseError<Self::Item, Self::Range, Self::Position>;
fn uncons(&mut self) -> Result<Self::Item, StreamErrorFor<Self>>;
fn is_partial(&self) -> bool {
false
}
}
pub trait Resetable {
type Checkpoint: Clone;
fn checkpoint(&self) -> Self::Checkpoint;
fn reset(&mut self, checkpoint: Self::Checkpoint);
}
clone_resetable! {('a) &'a str}
clone_resetable! {('a, T) &'a [T]}
clone_resetable! {('a, T) SliceStream<'a, T> }
clone_resetable! {(T: Clone) IteratorStream<T>}
pub trait Stream: StreamOnce + Resetable + Positioned {}
impl<I> Stream for I
where
I: StreamOnce + Positioned + Resetable,
I::Error: ParseError<I::Item, I::Range, I::Position>,
{
}
#[inline]
pub fn uncons<I>(input: &mut I) -> ConsumedResult<I::Item, I>
where
I: ?Sized + Stream,
{
match input.uncons() {
Ok(x) => ConsumedOk(x),
Err(err) => wrap_stream_error(input, err),
}
}
pub trait RangeStreamOnce: StreamOnce + Resetable {
fn uncons_range(&mut self, size: usize) -> Result<Self::Range, StreamErrorFor<Self>>;
fn uncons_while<F>(&mut self, f: F) -> Result<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Item) -> bool;
#[inline]
fn uncons_while1<F>(&mut self, mut f: F) -> FastResult<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Item) -> bool,
{
let mut consumed = false;
let result = self.uncons_while(|c| {
let ok = f(c);
consumed |= ok;
ok
});
if consumed {
match result {
Ok(x) => ConsumedOk(x),
Err(x) => ConsumedErr(x),
}
} else {
EmptyErr(Tracked::from(
StreamErrorFor::<Self>::unexpected_static_message(""),
))
}
}
fn distance(&self, end: &Self::Checkpoint) -> usize;
}
pub trait RangeStream: Stream + RangeStreamOnce {}
impl<I> RangeStream for I where I: RangeStreamOnce + Stream {}
pub trait FullRangeStream: RangeStream {
fn range(&self) -> Self::Range;
}
#[doc(hidden)]
#[inline]
pub fn wrap_stream_error<T, I>(
input: &I,
err: <I::Error as ParseError<I::Item, I::Range, I::Position>>::StreamError,
) -> ConsumedResult<T, I>
where
I: ?Sized + StreamOnce + Positioned,
{
let err = I::Error::from_error(input.position(), err);
if input.is_partial() {
ConsumedErr(err)
} else {
EmptyErr(err.into())
}
}
#[inline]
pub fn uncons_range<I>(input: &mut I, size: usize) -> ConsumedResult<I::Range, I>
where
I: ?Sized + RangeStream,
{
match input.uncons_range(size) {
Err(err) => wrap_stream_error(input, err),
Ok(x) => {
if size == 0 {
EmptyOk(x)
} else {
ConsumedOk(x)
}
}
}
}
#[doc(hidden)]
pub fn input_at_eof<I>(input: &mut I) -> bool
where
I: ?Sized + Stream,
{
let before = input.checkpoint();
let x = input.uncons() == Err(StreamError::end_of_input());
input.reset(before);
x
}
#[inline]
pub fn uncons_while<I, F>(input: &mut I, predicate: F) -> ConsumedResult<I::Range, I>
where
F: FnMut(I::Item) -> bool,
I: ?Sized + RangeStream,
I::Range: Range,
{
match input.uncons_while(predicate) {
Err(err) => wrap_stream_error(input, err),
Ok(x) => {
if input.is_partial() && input_at_eof(input) {
ConsumedErr(I::Error::from_error(
input.position(),
StreamError::end_of_input(),
))
} else if x.len() == 0 {
EmptyOk(x)
} else {
ConsumedOk(x)
}
}
}
}
#[inline]
pub fn uncons_while1<I, F>(input: &mut I, predicate: F) -> ConsumedResult<I::Range, I>
where
F: FnMut(I::Item) -> bool,
I: ?Sized + RangeStream,
{
match input.uncons_while1(predicate) {
ConsumedOk(x) => {
if input.is_partial() && input_at_eof(input) {
ConsumedErr(I::Error::from_error(
input.position(),
StreamError::end_of_input(),
))
} else {
ConsumedOk(x)
}
}
EmptyErr(_) => {
if input.is_partial() && input_at_eof(input) {
ConsumedErr(I::Error::from_error(
input.position(),
StreamError::end_of_input(),
))
} else {
EmptyErr(I::Error::empty(input.position()).into())
}
}
ConsumedErr(err) => {
if input.is_partial() && input_at_eof(input) {
ConsumedErr(I::Error::from_error(
input.position(),
StreamError::end_of_input(),
))
} else {
wrap_stream_error(input, err)
}
}
EmptyOk(_) => unreachable!(),
}
}
pub trait Range {
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
}
fn str_uncons_while<'a, F>(slice: &mut &'a str, mut chars: Chars<'a>, mut f: F) -> &'a str
where
F: FnMut(char) -> bool,
{
let mut last_char_size = 0;
macro_rules! test_next {
() => {
match chars.next() {
Some(c) => {
if !f(c) {
last_char_size = c.len_utf8();
break;
}
}
None => break,
}
};
}
loop {
test_next!();
test_next!();
test_next!();
test_next!();
test_next!();
test_next!();
test_next!();
test_next!();
}
let len = slice.len() - chars.as_str().len() - last_char_size;
let (result, rest) = slice.split_at(len);
*slice = rest;
result
}
impl<'a> RangeStreamOnce for &'a str {
fn uncons_while<F>(&mut self, f: F) -> Result<&'a str, StreamErrorFor<Self>>
where
F: FnMut(Self::Item) -> bool,
{
Ok(str_uncons_while(self, self.chars(), f))
}
#[inline]
fn uncons_while1<F>(&mut self, mut f: F) -> FastResult<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Item) -> bool,
{
let mut chars = self.chars();
match chars.next() {
Some(c) => {
if !f(c) {
return EmptyErr(Tracked::from(StringStreamError::UnexpectedParse));
}
}
None => return EmptyErr(Tracked::from(StringStreamError::UnexpectedParse)),
}
ConsumedOk(str_uncons_while(self, chars, f))
}
#[inline]
fn uncons_range(&mut self, size: usize) -> Result<&'a str, StreamErrorFor<Self>> {
fn is_char_boundary(s: &str, index: usize) -> bool {
if index == s.len() {
return true;
}
match s.as_bytes().get(index) {
None => false,
Some(&b) => b < 128 || b >= 192,
}
}
if size <= self.len() {
if is_char_boundary(self, size) {
let (result, remaining) = self.split_at(size);
*self = remaining;
Ok(result)
} else {
Err(StringStreamError::CharacterBoundary)
}
} else {
Err(StringStreamError::Eoi)
}
}
#[inline]
fn distance(&self, end: &Self) -> usize {
self.position().0 - end.position().0
}
}
impl<'a> FullRangeStream for &'a str {
fn range(&self) -> Self::Range {
self
}
}
impl<'a> Range for &'a str {
#[inline]
fn len(&self) -> usize {
str::len(self)
}
}
impl<'a, T> Range for &'a [T] {
#[inline]
fn len(&self) -> usize {
<[T]>::len(self)
}
}
fn slice_uncons_while<'a, T, F>(slice: &mut &'a [T], mut i: usize, mut f: F) -> &'a [T]
where
F: FnMut(T) -> bool,
T: Clone,
{
let len = slice.len();
let mut found = false;
macro_rules! check {
() => {
if !f(unsafe { slice.get_unchecked(i).clone() }) {
found = true;
break;
}
i += 1;
};
}
while len - i >= 8 {
check!();
check!();
check!();
check!();
check!();
check!();
check!();
check!();
}
if !found {
while i < len {
if !f(unsafe { slice.get_unchecked(i).clone() }) {
break;
}
i += 1;
}
}
let (result, remaining) = slice.split_at(i);
*slice = remaining;
result
}
impl<'a, T> RangeStreamOnce for &'a [T]
where
T: Clone + PartialEq,
{
#[inline]
fn uncons_range(&mut self, size: usize) -> Result<&'a [T], StreamErrorFor<Self>> {
if size <= self.len() {
let (result, remaining) = self.split_at(size);
*self = remaining;
Ok(result)
} else {
Err(UnexpectedParse::Eoi)
}
}
#[inline]
fn uncons_while<F>(&mut self, f: F) -> Result<&'a [T], StreamErrorFor<Self>>
where
F: FnMut(Self::Item) -> bool,
{
Ok(slice_uncons_while(self, 0, f))
}
#[inline]
fn uncons_while1<F>(&mut self, mut f: F) -> FastResult<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Item) -> bool,
{
if self.is_empty() || !f(unsafe { (*self.get_unchecked(0)).clone() }) {
return EmptyErr(Tracked::from(UnexpectedParse::Unexpected));
}
ConsumedOk(slice_uncons_while(self, 1, f))
}
#[inline]
fn distance(&self, end: &Self) -> usize {
end.len() - self.len()
}
}
impl<'a, T> FullRangeStream for &'a [T]
where
T: Clone + PartialEq,
{
fn range(&self) -> Self::Range {
self
}
}
impl<'a> Positioned for &'a str {
#[inline(always)]
fn position(&self) -> Self::Position {
self.as_bytes().position()
}
}
impl<'a> StreamOnce for &'a str {
type Item = char;
type Range = &'a str;
type Position = PointerOffset;
type Error = StringStreamError;
#[inline]
fn uncons(&mut self) -> Result<char, StreamErrorFor<Self>> {
let mut chars = self.chars();
match chars.next() {
Some(c) => {
*self = chars.as_str();
Ok(c)
}
None => Err(StringStreamError::Eoi),
}
}
}
impl<'a, T> Positioned for &'a [T]
where
T: Clone + PartialEq,
{
#[inline(always)]
fn position(&self) -> Self::Position {
PointerOffset(self.as_ptr() as usize)
}
}
impl<'a, T> StreamOnce for &'a [T]
where
T: Clone + PartialEq,
{
type Item = T;
type Range = &'a [T];
type Position = PointerOffset;
type Error = UnexpectedParse;
#[inline]
fn uncons(&mut self) -> Result<T, StreamErrorFor<Self>> {
match self.split_first() {
Some((first, rest)) => {
*self = rest;
Ok(first.clone())
}
None => Err(UnexpectedParse::Eoi),
}
}
}
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Debug)]
pub struct PartialStream<S>(pub S);
impl<S> Positioned for PartialStream<S>
where
S: Positioned,
{
#[inline(always)]
fn position(&self) -> Self::Position {
self.0.position()
}
}
impl<S> Resetable for PartialStream<S>
where
S: Resetable,
{
type Checkpoint = S::Checkpoint;
#[inline(always)]
fn checkpoint(&self) -> Self::Checkpoint {
self.0.checkpoint()
}
#[inline(always)]
fn reset(&mut self, checkpoint: Self::Checkpoint) {
self.0.reset(checkpoint);
}
}
impl<S> StreamOnce for PartialStream<S>
where
S: StreamOnce,
{
type Item = S::Item;
type Range = S::Range;
type Position = S::Position;
type Error = S::Error;
#[inline(always)]
fn uncons(&mut self) -> Result<S::Item, StreamErrorFor<Self>> {
self.0.uncons()
}
fn is_partial(&self) -> bool {
true
}
}
impl<S> RangeStreamOnce for PartialStream<S>
where
S: RangeStreamOnce,
{
#[inline(always)]
fn uncons_range(&mut self, size: usize) -> Result<Self::Range, StreamErrorFor<Self>> {
self.0.uncons_range(size)
}
#[inline(always)]
fn uncons_while<F>(&mut self, f: F) -> Result<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Item) -> bool,
{
self.0.uncons_while(f)
}
fn uncons_while1<F>(&mut self, f: F) -> FastResult<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Item) -> bool,
{
self.0.uncons_while1(f)
}
#[inline(always)]
fn distance(&self, end: &Self::Checkpoint) -> usize {
self.0.distance(end)
}
}
impl<S> FullRangeStream for PartialStream<S>
where
S: FullRangeStream,
{
#[inline(always)]
fn range(&self) -> Self::Range {
self.0.range()
}
}
#[derive(Copy, Eq, PartialEq, Ord, PartialOrd, Debug)]
pub struct SliceStream<'a, T: 'a>(pub &'a [T]);
impl<'a, T> Clone for SliceStream<'a, T> {
fn clone(&self) -> SliceStream<'a, T> {
SliceStream(self.0)
}
}
impl<'a, T> Positioned for SliceStream<'a, T>
where
T: PartialEq + 'a,
{
#[inline(always)]
fn position(&self) -> Self::Position {
PointerOffset(self.0.as_ptr() as usize)
}
}
impl<'a, T> StreamOnce for SliceStream<'a, T>
where
T: PartialEq + 'a,
{
type Item = &'a T;
type Range = &'a [T];
type Position = PointerOffset;
type Error = UnexpectedParse;
#[inline]
fn uncons(&mut self) -> Result<&'a T, StreamErrorFor<Self>> {
match self.0.split_first() {
Some((first, rest)) => {
self.0 = rest;
Ok(first)
}
None => Err(UnexpectedParse::Eoi),
}
}
}
fn slice_uncons_while_ref<'a, T, F>(slice: &mut &'a [T], mut i: usize, mut f: F) -> &'a [T]
where
F: FnMut(&'a T) -> bool,
{
let len = slice.len();
let mut found = false;
macro_rules! check {
() => {
if !f(unsafe { slice.get_unchecked(i) }) {
found = true;
break;
}
i += 1;
};
}
while len - i >= 8 {
check!();
check!();
check!();
check!();
check!();
check!();
check!();
check!();
}
if !found {
while i < len {
if !f(unsafe { slice.get_unchecked(i) }) {
break;
}
i += 1;
}
}
let (result, remaining) = slice.split_at(i);
*slice = remaining;
result
}
impl<'a, T> RangeStreamOnce for SliceStream<'a, T>
where
T: PartialEq + 'a,
{
#[inline]
fn uncons_range(&mut self, size: usize) -> Result<&'a [T], StreamErrorFor<Self>> {
if size <= self.0.len() {
let (range, rest) = self.0.split_at(size);
self.0 = rest;
Ok(range)
} else {
Err(UnexpectedParse::Eoi)
}
}
#[inline]
fn uncons_while<F>(&mut self, f: F) -> Result<&'a [T], StreamErrorFor<Self>>
where
F: FnMut(Self::Item) -> bool,
{
Ok(slice_uncons_while_ref(&mut self.0, 0, f))
}
#[inline]
fn uncons_while1<F>(&mut self, mut f: F) -> FastResult<Self::Range, StreamErrorFor<Self>>
where
F: FnMut(Self::Item) -> bool,
{
if self.0.is_empty() || !f(unsafe { self.0.get_unchecked(0) }) {
return EmptyErr(Tracked::from(UnexpectedParse::Unexpected));
}
ConsumedOk(slice_uncons_while_ref(&mut self.0, 1, f))
}
#[inline]
fn distance(&self, end: &Self) -> usize {
end.0.len() - self.0.len()
}
}
impl<'a, T> FullRangeStream for SliceStream<'a, T>
where
T: PartialEq + 'a,
{
fn range(&self) -> Self::Range {
self.0
}
}
#[derive(Copy, Clone, Debug)]
pub struct IteratorStream<I>(I);
impl<I> IteratorStream<I>
where
I: Iterator,
{
pub fn new<T>(iter: T) -> IteratorStream<I>
where
T: IntoIterator<IntoIter = I, Item = I::Item>,
{
IteratorStream(iter.into_iter())
}
}
impl<I> Iterator for IteratorStream<I>
where
I: Iterator,
{
type Item = I::Item;
fn next(&mut self) -> Option<I::Item> {
self.0.next()
}
}
impl<I: Iterator> StreamOnce for IteratorStream<I>
where
I::Item: Clone + PartialEq,
{
type Item = I::Item;
type Range = I::Item;
type Position = ();
type Error = UnexpectedParse;
#[inline]
fn uncons(&mut self) -> Result<I::Item, StreamErrorFor<Self>> {
match self.next() {
Some(x) => Ok(x),
None => Err(UnexpectedParse::Eoi),
}
}
}
#[cfg(feature = "std")]
pub struct ReadStream<R> {
bytes: Bytes<R>,
}
#[cfg(feature = "std")]
impl<R: Read> StreamOnce for ReadStream<R> {
type Item = u8;
type Range = u8;
type Position = usize;
type Error = Errors<u8, u8, usize>;
#[inline]
fn uncons(&mut self) -> Result<u8, StreamErrorFor<Self>> {
match self.bytes.next() {
Some(Ok(b)) => Ok(b),
Some(Err(err)) => Err(StreamErrorFor::<Self>::other(err)),
None => Err(StreamErrorFor::<Self>::end_of_input()),
}
}
}
#[cfg(feature = "std")]
impl<R> ReadStream<R>
where
R: Read,
{
pub fn new(read: R) -> ReadStream<R> {
ReadStream {
bytes: read.bytes(),
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
pub struct PointerOffset(pub usize);
impl fmt::Display for PointerOffset {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self.0 as *const ())
}
}
impl PointerOffset {
pub fn translate_position<T>(mut self, initial_string: &T) -> usize
where
T: ?Sized,
{
self.0 -= initial_string as *const T as *const () as usize;
self.0
}
}
pub fn decode<P>(
mut parser: P,
mut input: P::Input,
partial_state: &mut P::PartialState,
) -> Result<(Option<P::Output>, usize), <P::Input as StreamOnce>::Error>
where
P: Parser,
P::Input: RangeStream,
{
let start = input.checkpoint();
match parser.parse_with_state(&mut input, partial_state) {
Ok(message) => Ok((Some(message), input.distance(&start))),
Err(err) => {
if input.is_partial() && err.is_unexpected_end_of_input() {
Ok((None, input.distance(&start)))
} else {
Err(err)
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[inline]
fn uncons_range_at_end() {
assert_eq!("".uncons_range(0), Ok(""));
assert_eq!("123".uncons_range(3), Ok("123"));
assert_eq!((&[1][..]).uncons_range(1), Ok(&[1][..]));
let s: &[u8] = &[];
assert_eq!(SliceStream(s).uncons_range(0), Ok(&[][..]));
}
#[test]
fn larger_than_1_byte_items_return_correct_distance() {
let mut input = &[123i32, 0i32][..];
let before = input.checkpoint();
assert_eq!(input.distance(&before), 0);
input.uncons().unwrap();
assert_eq!(input.distance(&before), 1);
input.uncons().unwrap();
assert_eq!(input.distance(&before), 2);
input.reset(before.clone());
assert_eq!(input.distance(&before), 0);
}
}