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
mod stream;
pub use self::stream::Stream;

mod scanner;
pub use self::scanner::Scanner;

/// Collection of all of the available combinators
pub mod adapters;

pub use Either::{Left, Right};

pub(crate) use self::adapters::*;

mod error;
pub(crate) use self::error::Res;
pub use self::error::{Error, Expected};

/// Result is a wrapped result type. It wraps an error with the internal [Error](crate::Error) tracking
pub type Result<O, I> = std::result::Result<O, Error<I>>;

/// Convience macro for [or](crate::Scanner::or)ing together many combinators
#[macro_export]
macro_rules! or  {
    ($x:expr) => {
        $x
    };
    ($x:expr, $($xs:tt)+) => {
        $x.or(or!($($xs)+))
    }
}

/// Scans the stream with a production function
pub fn scan_with<F, A, B>(f: F) -> ScanWith<F, A, B>
where
    F: Fn(&mut Stream<A>) -> Result<B, A>,
{
    ScanWith::new(f)
}

/// Produce any token
pub fn any<T>() -> Any<T>
where
    T: Clone,
{
    Any::new()
}

/// Produce a specific value
pub fn value<T, I>(x: T) -> Value<T, I>
where
    T: Clone,
{
    Value::new(x)
}

/// Produce the end of input
pub fn end<T>() -> End<T>
where
    T: Clone,
{
    End::new()
}

/// Expect the clsoure, producing the value or an error
pub fn expect<T, F>(f: F) -> Expect<T, F>
where
    T: Clone,
    F: Fn(&T) -> bool,
{
    Expect::new(f)
}

/// Produce a failure
pub fn fail<A: Clone, B>() -> Fail<A, B> {
    Fail::new()
}

/// Produce a single token
pub fn token<A>(a: A) -> Token<A>
where
    A: Clone + PartialEq,
{
    Token::new(a)
}

/// Produce an collection of tokens
pub fn tokens<I, A>(iter: I) -> Tokens<A>
where
    I: IntoIterator<Item = A>,
    A: Clone + PartialEq,
{
    Tokens::new(iter.into_iter().collect())
}