lexer_rs/posn_and_span/
posn_in_char_stream.rs

1//a Imports
2use crate::UserPosn;
3
4//a PosnIn traits
5//tt PosnInCharStream
6/// Trait for location within a character stream
7///
8/// This tracks a byte offset within the stream so that strings can be
9/// retrieved from the stream. Byte offsets *must* always be on UTF8
10/// boundaries.
11pub trait PosnInCharStream: UserPosn {
12    //mp byte_ofs
13    /// Return the byte offset into the stream of the position.
14    ///
15    /// This must *always* be a UTF8 character boundary; it will be so
16    fn byte_ofs(&self) -> usize;
17
18    //mp move_by_char
19    /// Move on by a character
20    fn move_by_char(self, ch: char) -> Self {
21        let n = ch.len_utf8();
22        if ch == '\n' {
23            self.advance_line(n)
24        } else {
25            self.advance_cols(n, 1)
26        }
27    }
28}
29
30//ip PosnInCharStream for usize
31impl PosnInCharStream for usize {
32    fn byte_ofs(&self) -> usize {
33        *self
34    }
35}