Skip to main content

slk_tokenstream/
bookmark.rs

1/// A mark struct used to mark positions in a `TokenStream` for backtracking    
2/// 
3/// # Examples
4/// ``` rust
5/// use slk_tokenstream::TokenStream;
6/// use slk_tokenstream::Mark;
7/// 
8/// let tokens = &[1, 2, 3];
9/// let mut token_stream = TokenStream::new(tokens);
10/// let mark: Mark = token_stream.mark();
11/// 
12/// token_stream.advance(5);
13/// assert_eq!(token_stream.peek(), None);
14/// token_stream.reset(&mark);
15/// assert_eq!(token_stream.peek(), Some(&1));
16/// ```
17#[derive(Debug)]
18pub struct Mark {
19    position: usize,
20}
21
22impl Mark {
23    /// Creates a new mark with the position
24    pub(crate) fn new(position: usize) -> Self {
25        Self { position }
26    }
27    /// Returns the position
28    /// 
29    /// # Example
30    /// 
31    /// ```
32    /// use slk_tokenstream::Mark;
33    /// use slk_tokenstream::TokenStream;
34    /// 
35    /// let mut token_stream = TokenStream::new(&[0; 12]);
36    /// token_stream.advance(12);
37    /// 
38    ///
39    /// assert_eq!(token_stream.mark().position(), 12);
40    /// ```
41    pub fn position(&self) -> usize {
42        self.position
43    }
44}