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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//! Module containing tools for working with position aware parsers.
//!
//! ```
//! # #[macro_use] extern crate chomp;
//! # fn main() {
//! use chomp::types::{Input, ParseResult};
//! use chomp::types::numbering::{InputPosition, LineNumber, Numbering};
//! use chomp::combinators::many;
//! use chomp::parsers::{Error, any, take_while1, string};
//! use chomp::run_parser;
//!
//! // Let's count some lines
//! let i = InputPosition::new(&b"test a\ntest b\n\ntest c\n"[..], LineNumber::new());
//!
//! // We could use a concrete type P too if we want to restrict to a
//! // certain position-aware implementation
//! fn parser<I: Input<Token=u8>, P: Numbering<Token=u8>>(i: InputPosition<I, P>)
//!   -> ParseResult<InputPosition<I, P>, (char, P), Error<u8>> {
//!     parse!{i;
//!                      string(b"test");
//!                      take_while1(|c| c == b' ' || c == b'\t');
//!         let t_name = any();
//!         i -> {
//!             // Obtain current parser position
//!             let p = i.position();
//!
//!             i.ret((t_name as char, p))
//!             // We skip the take while below, because we want to determine the line right
//!             // after reading the t_name
//!         } <* take_while1(|c| c == b'\n');
//!     }
//! }
//!
//! let r = run_parser(i, |i| many(i, parser)).1;
//!
//! assert_eq!(r, Ok(vec![('a', LineNumber(0)),
//!                       ('b', LineNumber(1)),
//!                       // Note the two linebreaks in a row
//!                       ('c', LineNumber(3))]));
//! # }
//! ```

use primitives::{Guard, IntoInner};
use types::{Buffer, Input};

/// Trait for managing some kind of numbering over the parsed data.
pub trait Numbering: Clone {
    /// The token type accepted by the numbering.
    type Token: Copy + PartialEq;

    /// Updates the numbering based on the contents of the buffer, adding it to the current
    /// numbering.
    fn update<B>(&mut self, &B)
      where B: Buffer<Token=Self::Token>;

    /// Adds the token to the numbering.
    fn add(&mut self, Self::Token);
}

/// Struct counting the number of newlines (`b'\n'`).
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash)]
pub struct LineNumber(
    /// The current line, zero-indexed.
    pub u64,
);

impl LineNumber {
    /// Creates a new line-number counter with zero.
    pub fn new() -> Self {
        LineNumber(0)
    }
}

impl Default for LineNumber {
    fn default() -> Self {
        LineNumber::new()
    }
}

impl Numbering for LineNumber {
    type Token  = u8;

    fn update<B>(&mut self, b: &B)
      where B: Buffer<Token=Self::Token> {
        let mut n = 0;

        b.iterate(|c| if c == b'\n' {
            n += 1;
        });

        self.0 += n
    }

    fn add(&mut self, t: Self::Token) {
        if t == b'\n' {
            self.0 += 1
        }
    }
}

/// Wrapper around an `Input` implementation providing numbering support.
#[derive(Debug)]
pub struct InputPosition<I: Input, N: Numbering<Token=I::Token>> {
    input: I,
    num:   N,
}

impl<I: Input, N: Numbering<Token=I::Token>> InputPosition<I, N> {
    /// Creates a new input position instance.
    pub fn new(i: I, n: N) -> Self {
        InputPosition {
            input: i,
            num:   n,
        }
    }

    /// Obtains the current position of the numbering.
    pub fn position(&self) -> N {
        self.num.clone()
    }
}

impl<I: Input, N: Numbering<Token=I::Token>> IntoInner for InputPosition<I, N> {
    type Inner = (I, N);

    fn into_inner(self) -> Self::Inner {
        (self.input, self.num)
    }
}

impl<I: Input, N: Numbering<Token=I::Token>> Input for InputPosition<I, N> {
    type Token  = I::Token;
    type Marker = (N, I::Marker);
    type Buffer = I::Buffer;

    #[inline]
    fn _peek(&mut self, g: Guard) -> Option<Self::Token> {
        self.input._peek(g)
    }

    #[inline]
    fn _pop(&mut self, g: Guard) -> Option<Self::Token> {
        self.input._pop(g).map(|t| {
            self.num.add(t);

            t
        })
    }

    #[inline]
    fn _consume(&mut self, g: Guard, n: usize) -> Option<Self::Buffer> {
        self.input._consume(g, n).map(|b| {
            self.num.update(&b);

            b
        })
    }

    #[inline]
    fn _consume_while<F>(&mut self, g: Guard, f: F) -> Self::Buffer
      where F: FnMut(Self::Token) -> bool {
        let b = self.input._consume_while(g, f);

        self.num.update(&b);

        b
    }

    #[inline]
    fn _consume_from(&mut self, g: Guard, m: Self::Marker) -> Self::Buffer {
        // We have already counted to current position, no need to update
        self.input._consume_from(g, m.1)
    }

    #[inline]
    fn _consume_remaining(&mut self, g: Guard) -> Self::Buffer {
        let b = self.input._consume_remaining(g);

        self.num.update(&b);

        b
    }

    #[inline]
    fn _mark(&self, g: Guard) -> Self::Marker {
        (self.num.clone(), self.input._mark(g))
    }

    #[inline]
    fn _restore(self, g: Guard, m: Self::Marker) -> Self {
        InputPosition {
            input: self.input._restore(g, m.1),
            num:   m.0,
        }
    }
}

#[cfg(test)]
mod test {
    use types::{Input, ParseResult};
    use super::{InputPosition, LineNumber};
    use primitives::IntoInner;

    #[test]
    fn basic_test() {
        use combinators::many;
        use parsers::{Error, any, take_while1, string};

        let i = InputPosition::new(&b"test a\ntest b\n\ntest c\n"[..], Default::default());

        fn parser<I: Input<Token=u8>>(i: InputPosition<I, LineNumber>)
          -> ParseResult<InputPosition<I, LineNumber>, (char, LineNumber), Error<u8>> {
            parse!{i;
                string(b"test");
                take_while1(|c| c == b' ' || c == b'\t');
                let t_name = any();
                i -> {
                    let p = i.position();

                    i.ret((t_name as char, p))
                    // We skip the take while below, because we want to determine the line right
                    // after reading the t_name
                } <* take_while1(|c| c == b'\n');
            }
        }

        assert_eq!(many(i, parser).into_inner().1, Ok(vec![
                                                      ('a', LineNumber(0)),
                                                      ('b', LineNumber(1)),
                                                      // Note the two linebreaks in a row
                                                      ('c', LineNumber(3))]));
    }
}