For parsing: CharIndices, peek() with offset() and as_str()?

I'm trying to write a parser. A string's char_indices() -> CharIndices seems like a useful iterator for this. I can walk the chars and have their positions for error reporting. But when parsing it's also useful to look ahead, so I called peekable() on that iterator. That returned iterator has a peek() function, but it loses the offset() and as_str() functions in CharIndices. I don't see a way to get the underlying iterator in the public interface.

What do people use for parsing a file? Maybe I should create my own iterator that wraps the str slice and provides all these functions.

I've now moved to something manual like:

struct Input<'source> {
    source: &'source str,
    position: usize,
}

impl Input<'_> {
    fn peek(&self) -> Option<char> {
        self.source[self.position..].chars().next()
    }

    fn next(&mut self) -> Option<char> {
        let result = self.peek();
        if let Some(c) = result {
            self.position += c.len_utf8();
        }
        result
    }
}

You can just use the peek approach above on the char_indices().as_str() if you prefer though!

4 Likes