pear/input/
input.rs

1use crate::input::{Show, Length};
2
3pub trait Token<I: Input>: Show + PartialEq<I::Token> { }
4
5pub trait Slice<I: Input>: Show + Length + PartialEq<I::Slice> { }
6
7impl<I: Input, T> Token<I> for T where T: Show + PartialEq<I::Token> { }
8
9impl<I: Input, S> Slice<I> for S where S: Show + Length + PartialEq<I::Slice> { }
10
11#[derive(Debug, Copy, Clone)]
12pub struct ParserInfo {
13    pub name: &'static str,
14    pub raw: bool,
15}
16
17pub trait Rewind: Sized + Input {
18    /// Resets `self` to the position identified by `marker`.
19    fn rewind_to(&mut self, marker: Self::Marker);
20}
21
22pub trait Input: Sized {
23    type Token: Token<Self>;
24    type Slice: Slice<Self>;
25    type Many: Length;
26
27    type Marker: Copy;
28    type Context: Show;
29
30    /// Returns a copy of the current token, if there is one.
31    fn token(&mut self) -> Option<Self::Token>;
32
33    /// Returns a copy of the current slice of size `n`, if there is one.
34    fn slice(&mut self, n: usize) -> Option<Self::Slice>;
35
36    /// Checks if the current token fulfills `cond`.
37    fn peek<F>(&mut self, cond: F) -> bool
38        where F: FnMut(&Self::Token) -> bool;
39
40    /// Checks if the current slice of size `n` (if any) fulfills `cond`.
41    fn peek_slice<F>(&mut self, n: usize, cond: F) -> bool
42        where F: FnMut(&Self::Slice) -> bool;
43
44    /// Checks if the current token fulfills `cond`. If so, the token is
45    /// consumed and returned. Otherwise, returns `None`.
46    fn eat<F>(&mut self, cond: F) -> Option<Self::Token>
47        where F: FnMut(&Self::Token) -> bool;
48
49    /// Checks if the current slice of size `n` (if any) fulfills `cond`. If so,
50    /// the slice is consumed and returned. Otherwise, returns `None`.
51    fn eat_slice<F>(&mut self, n: usize, cond: F) -> Option<Self::Slice>
52        where F: FnMut(&Self::Slice) -> bool;
53
54    /// Takes tokens while `cond` returns true, collecting them into a
55    /// `Self::Many` and returning it.
56    fn take<F>(&mut self, cond: F) -> Self::Many
57        where F: FnMut(&Self::Token) -> bool;
58
59    /// Skips tokens while `cond` returns true. Returns the number of skipped
60    /// tokens.
61    fn skip<F>(&mut self, cond: F) -> usize
62        where F: FnMut(&Self::Token) -> bool;
63
64    /// Returns `true` if there are at least `n` tokens remaining.
65    fn has(&mut self, n: usize) -> bool;
66
67    /// Emits a marker that represents the current parse position.
68    #[allow(unused_variables)]
69    fn mark(&mut self, info: &ParserInfo) -> Self::Marker;
70
71    /// Returns a context to identify the input spanning from `mark` until but
72    /// excluding the current position.
73    fn context(&mut self, _mark: Self::Marker) -> Self::Context;
74}