Files
rust/src/lexer/ptr.rs

72 lines
1.5 KiB
Rust
Raw Normal View History

2017-12-29 23:33:04 +03:00
use {TextUnit};
use std::str::Chars;
pub(crate) struct Ptr<'s> {
text: &'s str,
len: TextUnit,
}
impl<'s> Ptr<'s> {
pub fn new(text: &'s str) -> Ptr<'s> {
Ptr { text, len: TextUnit::new(0) }
}
pub fn into_len(self) -> TextUnit {
self.len
}
pub fn next(&self) -> Option<char> {
self.chars().next()
}
pub fn nnext(&self) -> Option<char> {
let mut chars = self.chars();
chars.next()?;
chars.next()
}
2017-12-30 15:22:40 +03:00
pub fn next_is(&self, c: char) -> bool {
self.next() == Some(c)
}
pub fn nnext_is(&self, c: char) -> bool {
self.nnext() == Some(c)
}
2017-12-31 10:41:42 +03:00
pub fn next_is_p<P: Fn(char) -> bool>(&self, p: P) -> bool {
self.next().map(p) == Some(true)
}
2017-12-30 15:22:40 +03:00
pub fn nnext_is_p<P: Fn(char) -> bool>(&self, p: P) -> bool {
self.nnext().map(p) == Some(true)
}
2017-12-29 23:33:04 +03:00
pub fn bump(&mut self) -> Option<char> {
let ch = self.chars().next()?;
self.len += TextUnit::len_of_char(ch);
Some(ch)
}
2017-12-30 00:48:47 +03:00
pub fn bump_while<F: Fn(char) -> bool>(&mut self, pred: F) {
loop {
match self.next() {
Some(c) if pred(c) => {
self.bump();
},
_ => return,
}
}
}
2018-01-01 18:58:46 +03:00
pub fn current_token_text(&self) -> &str {
let len: u32 = self.len.into();
&self.text[..len as usize]
}
2017-12-29 23:33:04 +03:00
fn chars(&self) -> Chars {
2017-12-30 15:29:09 +03:00
let len: u32 = self.len.into();
self.text[len as usize ..].chars()
2017-12-29 23:33:04 +03:00
}
}