mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 06:41:48 +00:00
organizize
This commit is contained in:
parent
26262aaf05
commit
7c67612b8a
376 changed files with 27 additions and 145 deletions
74
crates/libsyntax2/src/lexer/ptr.rs
Normal file
74
crates/libsyntax2/src/lexer/ptr.rs
Normal file
|
@ -0,0 +1,74 @@
|
|||
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: 0.into(),
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
pub fn next_is(&self, c: char) -> bool {
|
||||
self.next() == Some(c)
|
||||
}
|
||||
|
||||
pub fn nnext_is(&self, c: char) -> bool {
|
||||
self.nnext() == Some(c)
|
||||
}
|
||||
|
||||
pub fn next_is_p<P: Fn(char) -> bool>(&self, p: P) -> bool {
|
||||
self.next().map(p) == Some(true)
|
||||
}
|
||||
|
||||
pub fn nnext_is_p<P: Fn(char) -> bool>(&self, p: P) -> bool {
|
||||
self.nnext().map(p) == Some(true)
|
||||
}
|
||||
|
||||
pub fn bump(&mut self) -> Option<char> {
|
||||
let ch = self.chars().next()?;
|
||||
self.len += TextUnit::of_char(ch);
|
||||
Some(ch)
|
||||
}
|
||||
|
||||
pub fn bump_while<F: Fn(char) -> bool>(&mut self, pred: F) {
|
||||
loop {
|
||||
match self.next() {
|
||||
Some(c) if pred(c) => {
|
||||
self.bump();
|
||||
}
|
||||
_ => return,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_token_text(&self) -> &str {
|
||||
let len: u32 = self.len.into();
|
||||
&self.text[..len as usize]
|
||||
}
|
||||
|
||||
fn chars(&self) -> Chars {
|
||||
let len: u32 = self.len.into();
|
||||
self.text[len as usize..].chars()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue