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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
use core::ops::{Deref, Range, RangeFrom, RangeFull, RangeTo};
use core::str::{CharIndices, Chars, FromStr};
use nom::error::{ErrorKind, ParseError};
use nom::{
    AsBytes, Compare, CompareResult, Err, ExtendInto, FindSubstring, FindToken, IResult, InputIter,
    InputLength, InputTake, InputTakeAtPosition, Offset, ParseTo, Slice,
};

/// Tracks location information and user-defined metadata for nom's source input.
#[derive(Debug, Clone)]
pub struct TrackedLocation<T, X> {
    /// The offset represents the current byte position relative to the original input
    offset: usize,
    /// Tracks the current line number (starts at 1)
    line: usize,
    /// Tracks the current character number (starts at 1, UTF8-aware)
    char: usize,
    /// A slice representing the remaining input
    input: T,
    /// Any user-defined metadata that should be tracked in addition to the location
    pub metadata: X,
    /// Tracks a reference to the remaining input from the heredoc's start line
    pub(crate) remaining_input: Option<Box<Self>>,
}

impl<T, X> Deref for TrackedLocation<T, X> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        &self.input
    }
}

impl<T: AsBytes, X> TrackedLocation<T, X> {
    pub fn new_with_metadata(program: T, metadata: X) -> Self {
        Self {
            offset: 0,
            line: 1,
            char: 1,
            input: program,
            metadata: metadata,
            remaining_input: None,
        }
    }
    pub fn offset(&self) -> usize {
        self.offset
    }
    pub fn line(&self) -> usize {
        self.line
    }
    pub fn char(&self) -> usize {
        self.char
    }
    pub fn input(&self) -> &T {
        &self.input
    }
    pub fn metadata(&self) -> &X {
        &self.metadata
    }
    pub fn beginning_of_line(&self) -> bool {
        self.char == 1
    }
}

impl<T: AsBytes, X: Default> TrackedLocation<T, X> {
    pub fn new(program: T) -> Self {
        Self {
            offset: 0,
            line: 1,
            char: 1,
            input: program,
            metadata: X::default(),
            remaining_input: None,
        }
    }
    pub fn new_with_pos(program: T, offset: usize, line: usize, char: usize) -> Self {
        Self {
            offset: offset,
            line: line,
            char: char,
            input: program,
            metadata: X::default(),
            remaining_input: None,
        }
    }
}

impl<T: AsBytes, X> TrackedLocation<T, X> {
    pub fn new_with_pos_and_meta(
        program: T,
        offset: usize,
        line: usize,
        char: usize,
        metadata: X,
    ) -> Self {
        Self {
            offset: offset,
            line: line,
            char: char,
            input: program,
            metadata: metadata,
            remaining_input: None,
        }
    }
}

impl<T: AsBytes, X: Default> From<T> for TrackedLocation<T, X> {
    fn from(program: T) -> Self {
        Self::new_with_metadata(program, X::default())
    }
}

impl<T: PartialEq, X> PartialEq for TrackedLocation<T, X> {
    fn eq(&self, other: &Self) -> bool {
        self.offset == other.offset
            && self.line == other.line
            && self.char == other.char
            && self.input == other.input
    }
}

impl<T: Eq, X> Eq for TrackedLocation<T, X> {}

impl<T: AsBytes, X> AsBytes for TrackedLocation<T, X> {
    fn as_bytes(&self) -> &[u8] {
        self.input.as_bytes()
    }
}

impl<T: InputLength, X> InputLength for TrackedLocation<T, X> {
    fn input_len(&self) -> usize {
        self.input.input_len()
    }
}

impl<T, X> InputTake for TrackedLocation<T, X>
where
    Self: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>>,
{
    fn take(&self, count: usize) -> Self {
        self.slice(..count)
    }
    fn take_split(&self, count: usize) -> (Self, Self) {
        (self.slice(count..), self.slice(..count))
    }
}

impl<T, X> InputTakeAtPosition for TrackedLocation<T, X>
where
    T: InputTakeAtPosition + InputLength + InputIter,
    Self: Slice<RangeFrom<usize>> + Slice<RangeTo<usize>> + Clone,
{
    type Item = <T as InputIter>::Item;

    fn split_at_position_complete<P, E: ParseError<Self>>(
        &self,
        predicate: P,
    ) -> IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.split_at_position(predicate) {
            Err(Err::Incomplete(_)) => Ok(self.take_split(self.input_len())),
            res => res,
        }
    }

    fn split_at_position<P, E: ParseError<Self>>(&self, predicate: P) -> IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.input.position(predicate) {
            Some(n) => Ok(self.take_split(n)),
            None => Err(Err::Incomplete(nom::Needed::Size(unsafe {
                std::num::NonZeroUsize::new_unchecked(1)
            }))),
        }
    }

    fn split_at_position1<P, E: ParseError<Self>>(
        &self,
        predicate: P,
        e: ErrorKind,
    ) -> IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.input.position(predicate) {
            Some(0) => Err(Err::Error(E::from_error_kind(self.clone(), e))),
            Some(n) => Ok(self.take_split(n)),
            None => Err(Err::Incomplete(nom::Needed::Size(unsafe {
                std::num::NonZeroUsize::new_unchecked(1)
            }))),
        }
    }

    fn split_at_position1_complete<P, E: ParseError<Self>>(
        &self,
        predicate: P,
        e: ErrorKind,
    ) -> IResult<Self, Self, E>
    where
        P: Fn(Self::Item) -> bool,
    {
        match self.input.position(predicate) {
            Some(0) => Err(Err::Error(E::from_error_kind(self.clone(), e))),
            Some(n) => Ok(self.take_split(n)),
            None => {
                if self.input.input_len() == 0 {
                    Err(Err::Error(E::from_error_kind(self.clone(), e)))
                } else {
                    Ok(self.take_split(self.input_len()))
                }
            }
        }
    }
}

impl<'a, X> InputIter for TrackedLocation<&'a str, X> {
    type Item = char;
    type Iter = CharIndices<'a>;
    type IterElem = Chars<'a>;

    fn iter_indices(&self) -> Self::Iter {
        self.input.iter_indices()
    }
    fn iter_elements(&self) -> Self::IterElem {
        self.input.iter_elements()
    }
    fn position<P>(&self, predicate: P) -> Option<usize>
    where
        P: Fn(Self::Item) -> bool,
    {
        self.input.position(predicate)
    }
    fn slice_index(&self, count: usize) -> Option<usize> {
        self.input.slice_index(count)
    }
}

impl<'a, X> IntoIterator for TrackedLocation<&'a str, X> {
    type Item = char;
    type IntoIter = Chars<'a>;
    fn into_iter(self) -> Self::IntoIter {
        self.input.chars()
    }
}

impl<'a, A: Compare<B>, B: Into<TrackedLocation<B, X>>, X> Compare<B> for TrackedLocation<A, X> {
    fn compare(&self, other: B) -> CompareResult {
        self.input.compare(other.into().input)
    }
    fn compare_no_case(&self, other: B) -> CompareResult {
        self.input.compare_no_case(other.into().input)
    }
}

impl<T, X> Offset for TrackedLocation<T, X> {
    fn offset(&self, second: &Self) -> usize {
        second.offset - self.offset
    }
}

impl<'a, T: Clone, X: Clone> Slice<RangeFull> for TrackedLocation<T, X> {
    fn slice(&self, _range: RangeFull) -> Self {
        self.clone()
    }
}

impl<'a, X: Clone> Slice<RangeFrom<usize>> for TrackedLocation<&'a str, X> {
    fn slice(&self, range: RangeFrom<usize>) -> Self {
        if range.start == 0 {
            return self.clone();
        }
        let next_fragment = self.input.slice(range);
        if let Some(j) = &self.remaining_input {
            if next_fragment.input_len() == 0 {
                return (**j).to_owned();
            }
        };
        self.next_from_slice(next_fragment)
    }
}

impl<'a, X: Clone> Slice<RangeTo<usize>> for TrackedLocation<&'a str, X> {
    fn slice(&self, range: RangeTo<usize>) -> Self {
        self.next_from_slice(self.input.slice(range))
    }
}

impl<'a, X: Clone> Slice<Range<usize>> for TrackedLocation<&'a str, X> {
    fn slice(&self, range: Range<usize>) -> Self {
        self.next_from_slice(self.input.slice(range))
    }
}

impl<'a, X: Clone> TrackedLocation<&'a str, X> {
    fn next_from_slice(&self, next_fragment: &'a str) -> Self {
        let consumed_len = self.input.offset(&next_fragment);
        if let Some(j) = &self.remaining_input {
            if self.input.input_len() == 0 {
                return (**j).to_owned();
            }
        };
        if consumed_len == 0 {
            return Self {
                line: self.line,
                char: self.char,
                offset: self.offset,
                input: next_fragment,
                metadata: self.metadata.clone(),
                remaining_input: self.remaining_input.clone(),
            };
        }

        let consumed = self.input.slice(..consumed_len);
        let next_offset = self.offset + consumed_len;

        let consumed_as_bytes = consumed.as_bytes();
        let iter = memchr::Memchr::new(b'\n', consumed_as_bytes);
        let number_of_lines = iter.count();
        let next_line = self.line + number_of_lines;
        let next_char = if number_of_lines == 0 {
            self.char + consumed.chars().count()
        } else {
            1 + consumed.chars().rev().position(|c| c == '\n').unwrap()
        };

        Self {
            line: next_line,
            char: next_char,
            offset: next_offset,
            input: next_fragment,
            metadata: self.metadata.clone(),
            remaining_input: self.remaining_input.clone(),
        }
    }
}

impl<T: FindToken<Token>, Token, X> FindToken<Token> for TrackedLocation<T, X> {
    fn find_token(&self, token: Token) -> bool {
        self.input.find_token(token)
    }
}

impl<'a, T: FindSubstring<&'a str>, X> FindSubstring<&'a str> for TrackedLocation<T, X> {
    fn find_substring(&self, substr: &'a str) -> Option<usize> {
        self.input.find_substring(substr)
    }
}

impl<R: FromStr, T: ParseTo<R>, X> ParseTo<R> for TrackedLocation<T, X> {
    fn parse_to(&self) -> Option<R> {
        self.input.parse_to()
    }
}

impl<T: ToString, X> std::fmt::Display for TrackedLocation<T, X> {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        fmt.write_str(&self.input.to_string())
    }
}

impl<'a, I, E, T, X> ExtendInto for TrackedLocation<T, X>
where
    E: Default + Extend<I>,
    T: ExtendInto<Item = I, Extender = E>,
    Self: Clone + IntoIterator<Item = I>,
{
    type Item = I;
    type Extender = E;

    fn new_builder(&self) -> Self::Extender {
        Self::Extender::default()
    }

    fn extend_into(&self, acc: &mut Self::Extender) {
        acc.extend(self.clone().into_iter())
    }
}

/// Captures the current position within the input
#[allow(unused)]
pub fn position<T, E>(i: T) -> IResult<T, T, E>
where
    E: ParseError<T>,
    T: InputIter + InputTake,
{
    nom::bytes::complete::take(0usize)(i)
}

#[cfg(test)]
mod tests {
    use super::*;
    type Input<'a> = TrackedLocation<&'a str, ()>;
    #[test]
    fn test_continuations() {
        let mut i = Input::new("foobar");
        let j = Input::new_with_pos("baz", 12, 4, 0);
        assert_eq!(6, i.input_len());
        i.remaining_input = Some(Box::new(j));
        assert_eq!(6, i.input_len());
        assert_eq!(
            Input::new_with_pos("baz", 12, 4, 0),
            nom::combinator::rest::<Input, (Input, nom::error::ErrorKind)>(i)
                .unwrap()
                .0
        );
    }
}