Files
rust/crates/libsyntax2/src/lexer/ptr.rs

75 lines
1.5 KiB
Rust
Raw Normal View History

2018-01-27 18:31:23 -05:00
use TextUnit;
2017-12-29 23:33:04 +03:00
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> {
2018-01-27 18:31:23 -05:00
Ptr {
text,
2018-07-28 13:07:10 +03:00
len: 0.into(),
2018-01-27 18:31:23 -05:00
}
2017-12-29 23:33:04 +03:00
}
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()?;
2018-07-28 13:07:10 +03:00
self.len += TextUnit::of_char(ch);
2017-12-29 23:33:04 +03:00
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();
2018-01-27 18:31:23 -05:00
}
2017-12-30 00:48:47 +03:00
_ => 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();
2018-01-27 18:31:23 -05:00
self.text[len as usize..].chars()
2017-12-29 23:33:04 +03:00
}
}