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
//! This module contains types which implement the [`Tokens`] interface. You
//! won't often need to import this module unless you wish to explicitly name
//! the types in question.
//!
//! You should be able to remain generic by using `t: &mut impl Tokens<Item=char>` as a
//! function argument instead of naming concrete types like the ones here.
use super::{IntoTokens, TokenLocation, Tokens};

/// This is what we are given back if we call `into_tokens()` on
/// a `&[T]`. It implements the [`Tokens`] interface.
pub struct SliceTokens<'a, Item> {
    slice: &'a [Item],
    cursor: usize,
}

/// This implements [`TokenLocation`] and stores the location of
/// our current cursor into some slice.
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Debug)]
pub struct SliceTokensLocation(usize);

impl TokenLocation for SliceTokensLocation {
    fn offset(&self) -> usize {
        self.0
    }
}

impl<'a, Item> SliceTokens<'a, Item> {
    /// Return the parsed portion of the slice.
    pub fn consumed(&self) -> &'a [Item] {
        &self.slice[..self.cursor]
    }

    /// Return the unparsed remainder of the slice.
    pub fn remaining(&self) -> &'a [Item] {
        &self.slice[self.cursor..]
    }
}

impl<'a, Item> From<SliceTokens<'a, Item>> for &'a [Item] {
    fn from(toks: SliceTokens<'a, Item>) -> Self {
        toks.slice
    }
}

impl<'a, Item> Tokens for SliceTokens<'a, Item> {
    type Item = &'a Item;
    type Location = SliceTokensLocation;

    fn next(&mut self) -> Option<Self::Item> {
        let res = self.slice.get(self.cursor);
        self.cursor += 1;
        res
    }
    fn location(&self) -> Self::Location {
        SliceTokensLocation(self.cursor)
    }
    fn set_location(&mut self, location: Self::Location) {
        self.cursor = location.0;
    }
    fn is_at_location(&self, location: &Self::Location) -> bool {
        self.cursor == location.0
    }
}

impl<'a, Item> IntoTokens<&'a Item> for SliceTokens<'a, Item> {
    type Tokens = Self;
    fn into_tokens(self) -> Self {
        self
    }
}

impl<'a, Item> IntoTokens<&'a Item> for &'a [Item] {
    type Tokens = SliceTokens<'a, Item>;
    fn into_tokens(self) -> Self::Tokens {
        SliceTokens {
            slice: self,
            cursor: 0,
        }
    }
}

/// This is what we are given back if we call `into_tokens()` on
/// a `&str`. It implements the [`Tokens`] interface.
pub struct StrTokens<'a> {
    str: &'a str,
    cursor: usize,
}

/// This implements [`TokenLocation`] and stores the location of
/// our current cursor into some string. The location is the byte index
/// into the string and not the nth character we're up to (a character
/// may be represented by several bytes).
#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Debug)]
pub struct StrTokensLocation(usize);

impl TokenLocation for StrTokensLocation {
    fn offset(&self) -> usize {
        self.0
    }
}

impl<'a> StrTokens<'a> {
    /// Return the parsed portion of the str.
    pub fn consumed(&self) -> &'a str {
        &self.str[..self.cursor]
    }

    /// Return the unparsed remainder of the str.
    pub fn remaining(&self) -> &'a str {
        &self.str[self.cursor..]
    }
}

impl<'a> From<StrTokens<'a>> for &'a str {
    fn from(toks: StrTokens<'a>) -> Self {
        toks.str
    }
}

impl<'a> Tokens for StrTokens<'a> {
    type Item = char;
    type Location = StrTokensLocation;

    fn next(&mut self) -> Option<Self::Item> {
        if self.cursor == self.str.len() {
            return None;
        }

        // Cursor should always start at a valid char boundary.
        // So, we just find the next char boundary and return the
        // char between those two.
        let mut next_char_boundary = self.cursor + 1;
        while !self.str.is_char_boundary(next_char_boundary) {
            next_char_boundary += 1;
        }

        // We have to go to &str and then char. Unchecked because we know
        // that we are on a valid boundary. There's probably a quicker way..
        // To check that bounds detection works even on exotic characters, there's a test included
        // at the end of the file.
        let next_char = unsafe { self.str.get_unchecked(self.cursor..next_char_boundary) }
            .chars()
            .next()
            .unwrap();

        self.cursor = next_char_boundary;
        Some(next_char)
    }
    fn location(&self) -> Self::Location {
        StrTokensLocation(self.cursor)
    }
    fn set_location(&mut self, location: Self::Location) {
        self.cursor = location.0;
    }
    fn is_at_location(&self, location: &Self::Location) -> bool {
        self.cursor == location.0
    }
}

impl<'a> IntoTokens<char> for StrTokens<'a> {
    type Tokens = Self;
    fn into_tokens(self) -> Self {
        self
    }
}

impl<'a> IntoTokens<char> for &'a str {
    type Tokens = StrTokens<'a>;
    fn into_tokens(self) -> Self::Tokens {
        StrTokens {
            str: self,
            cursor: 0,
        }
    }
}

/// This is what we are given back if we call [`IterTokens::into_tokens(iter)`] on
/// an `impl Iterator + Clone`. It implements the [`Tokens`] interface.
#[derive(Clone)]
pub struct IterTokens<I> {
    iter: I,
    cursor: usize,
}

/// This implements [`TokenLocation`] and stores the location and state of
/// our current cursor into some iterator. The location is equivalent to `offset`
/// in [`Iterator::nth(offset)`].
#[derive(Clone)]
pub struct IterTokensLocation<I>(IterTokens<I>);

impl<I> core::fmt::Debug for IterTokensLocation<I> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "IterTokensLocation(cursor = {})", self.0.cursor)
    }
}

// Locations match as long as the cursors do. This is as strong as the guarantee
// for string or slice locations, and in all cases, locations from StrTokens/SliceTokens
// may be equal even if the underlying tokens are different.
impl<I> PartialEq for IterTokensLocation<I> {
    fn eq(&self, other: &Self) -> bool {
        self.0.cursor == other.0.cursor
    }
}

impl<I> TokenLocation for IterTokensLocation<I> {
    fn offset(&self) -> usize {
        self.0.cursor
    }
}

impl<I: Iterator + Clone> IterTokens<I> {
    /// We can't define a blanket impl for [`IntoTokens`] on all `impl Iterator + Clone` without
    /// [specialization](https://rust-lang.github.io/rfcs/1210-impl-specialization.html).
    ///
    /// Instead, use this method to convert a suitable iterator into [`Tokens`].
    ///
    /// # Example
    ///
    /// ```rust
    /// use yap::{ Tokens, types::IterTokens };
    ///
    /// // In normal usage, "hello \n\t world".into_tokens()
    /// // would be preferred here (which would give StrTokens).
    /// // This is just to demonstrate using IterTokens:
    /// let chars_iter = "hello \n\t world".chars();
    /// let mut tokens = IterTokens::into_tokens(chars_iter);
    ///
    /// // now we have tokens, we can do some parsing:
    /// assert!(tokens.tokens("hello".chars()));
    /// tokens.skip_tokens_while(|c| c.is_whitespace());
    /// assert!(tokens.tokens("world".chars()));
    /// ```
    pub fn into_tokens(iter: I) -> Self {
        IterTokens { iter, cursor: 0 }
    }
}

impl<I> Tokens for IterTokens<I>
where
    I: Iterator + Clone,
{
    type Item = I::Item;
    type Location = IterTokensLocation<I>;

    fn next(&mut self) -> Option<Self::Item> {
        self.cursor += 1;
        self.iter.next()
    }
    fn location(&self) -> Self::Location {
        IterTokensLocation(self.clone())
    }
    fn set_location(&mut self, location: Self::Location) {
        *self = location.0;
    }
    fn is_at_location(&self, location: &Self::Location) -> bool {
        self.cursor == location.0.cursor
    }
}

impl<I> IntoTokens<I::Item> for IterTokens<I>
where
    I: Iterator + Clone,
{
    type Tokens = Self;
    fn into_tokens(self) -> Self {
        self
    }
}

/// Embed some context with your [`Tokens`] implementation to
/// access at any time. Use [`Tokens::with_context`] to produce this.
pub struct WithContext<T, C> {
    tokens: T,
    context: C,
}

/// Embed some context with a mutable reference to your [`Tokens`] to
/// access at any time. Use [`Tokens::with_context`] to produce this.
pub struct WithContextMut<T, C> {
    tokens: T,
    context: C,
}

// `WithContext` and `WithContextMut` have almost identical looking impls,
// but one only works with `Tokens`, and one with `&mut Tokens` (because
// those impls would conflict if both on the same struct).
macro_rules! with_context_impls {
    ($name:ident $( $($mut:tt)+ )?) => {
        impl <T, C> $name<T, C> {
            /// Provide something that implements [`Tokens`] and
            /// some arbitrary context.
            pub(crate) fn new(tokens: T, context: C) -> Self {
                Self { tokens, context }
            }

            /// Return the original tokens and context
            pub fn into_parts(self) -> (T, C) {
                (self.tokens, self.context)
            }

            /// Access the context
            pub fn context(&self) -> &C {
                &self.context
            }

            /// Mutably access the context
            pub fn context_mut(&mut self) -> &mut C {
                &mut self.context
            }
        }

        impl <T, C> Tokens for $name<$( $($mut)+ )? T, C>
        where T: Tokens {
            type Item = T::Item;
            type Location = T::Location;

            fn next(&mut self) -> Option<Self::Item> {
                self.tokens.next()
            }
            fn location(&self) -> Self::Location {
                self.tokens.location()
            }
            fn set_location(&mut self, location: Self::Location) {
                self.tokens.set_location(location)
            }
            fn is_at_location(&self, location: &Self::Location) -> bool {
                self.tokens.is_at_location(location)
            }
        }
    }
}

with_context_impls!(WithContext);
with_context_impls!(WithContextMut &mut);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn exotic_character_bounds() {
        let mut tokens = "🗻∈🌏".into_tokens();

        assert_eq!(tokens.next(), Some('🗻'));
        assert_eq!(tokens.next(), Some('∈'));
        assert_eq!(tokens.next(), Some('🌏'));
    }

    #[test]
    fn iterator_tokens_sanity_check() {
        // In reality, one should always prefer to use StrTokens for strings:
        let chars = "hello \n\t world".chars();
        let mut tokens = IterTokens::into_tokens(chars);

        let loc = tokens.location();
        assert!(tokens.tokens("hello".chars()));

        tokens.set_location(loc.clone());
        assert!(tokens.tokens("hello".chars()));

        tokens.skip_tokens_while(|c| c.is_whitespace());

        assert!(tokens.tokens("world".chars()));

        tokens.set_location(loc);
        assert!(tokens.tokens("hello".chars()));
    }
}