Files
rust/crates/ra_syntax/src/parsing/lexer/ptr.rs

163 lines
4.2 KiB
Rust
Raw Normal View History

2018-10-15 19:55:32 +03:00
use crate::TextUnit;
2017-12-29 23:33:04 +03:00
use std::str::Chars;
/// A simple view into the characters of a string.
2017-12-29 23:33:04 +03:00
pub(crate) struct Ptr<'s> {
text: &'s str,
len: TextUnit,
}
impl<'s> Ptr<'s> {
/// Creates a new `Ptr` from a string.
2017-12-29 23:33:04 +03:00
pub fn new(text: &'s str) -> Ptr<'s> {
2019-02-08 14:49:43 +03:00
Ptr { text, len: 0.into() }
2017-12-29 23:33:04 +03:00
}
/// Gets the length of the remaining string.
2017-12-29 23:33:04 +03:00
pub fn into_len(self) -> TextUnit {
self.len
}
/// Gets the current character, if one exists.
pub fn current(&self) -> Option<char> {
2017-12-29 23:33:04 +03:00
self.chars().next()
}
/// Gets the nth character from the current.
2018-12-05 21:46:43 +00:00
/// For example, 0 will return the current character, 1 will return the next, etc.
pub fn nth(&self, n: u32) -> Option<char> {
2018-11-07 11:35:33 +01:00
self.chars().nth(n as usize)
2017-12-29 23:33:04 +03:00
}
/// Checks whether the current character is `c`.
pub fn at(&self, c: char) -> bool {
self.current() == Some(c)
2017-12-30 15:22:40 +03:00
}
/// Checks whether the next characters match `s`.
pub fn at_str(&self, s: &str) -> bool {
let chars = self.chars();
chars.as_str().starts_with(s)
2017-12-30 15:22:40 +03:00
}
/// Checks whether the current character satisfies the predicate `p`.
pub fn at_p<P: Fn(char) -> bool>(&self, p: P) -> bool {
self.current().map(p) == Some(true)
2017-12-31 10:41:42 +03:00
}
/// Checks whether the nth character satisfies the predicate `p`.
pub fn nth_is_p<P: Fn(char) -> bool>(&self, n: u32, p: P) -> bool {
self.nth(n).map(p) == Some(true)
2017-12-30 15:22:40 +03:00
}
/// Moves to the next character.
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)
}
/// Moves to the next character as long as `pred` is satisfied.
2017-12-30 00:48:47 +03:00
pub fn bump_while<F: Fn(char) -> bool>(&mut self, pred: F) {
loop {
match self.current() {
2017-12-30 00:48:47 +03:00
Some(c) if pred(c) => {
self.bump();
2018-01-27 18:31:23 -05:00
}
2017-12-30 00:48:47 +03:00
_ => return,
}
}
}
/// Returns the text up to the current point.
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]
}
/// Returns an iterator over the remaining characters.
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
}
}
2018-09-04 23:26:11 -07:00
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_current() {
let ptr = Ptr::new("test");
assert_eq!(ptr.current(), Some('t'));
}
#[test]
fn test_nth() {
let ptr = Ptr::new("test");
assert_eq!(ptr.nth(0), Some('t'));
assert_eq!(ptr.nth(1), Some('e'));
assert_eq!(ptr.nth(2), Some('s'));
assert_eq!(ptr.nth(3), Some('t'));
assert_eq!(ptr.nth(4), None);
}
#[test]
fn test_at() {
let ptr = Ptr::new("test");
assert!(ptr.at('t'));
assert!(!ptr.at('a'));
}
#[test]
fn test_at_str() {
let ptr = Ptr::new("test");
assert!(ptr.at_str("t"));
assert!(ptr.at_str("te"));
assert!(ptr.at_str("test"));
assert!(!ptr.at_str("tests"));
assert!(!ptr.at_str("rust"));
}
#[test]
fn test_at_p() {
let ptr = Ptr::new("test");
assert!(ptr.at_p(|c| c == 't'));
assert!(!ptr.at_p(|c| c == 'e'));
}
#[test]
fn test_nth_is_p() {
let ptr = Ptr::new("test");
assert!(ptr.nth_is_p(0, |c| c == 't'));
assert!(!ptr.nth_is_p(1, |c| c == 't'));
assert!(ptr.nth_is_p(3, |c| c == 't'));
assert!(!ptr.nth_is_p(150, |c| c == 't'));
2018-09-04 23:26:11 -07:00
}
#[test]
fn test_bump() {
let mut ptr = Ptr::new("test");
assert_eq!(ptr.current(), Some('t'));
ptr.bump();
assert_eq!(ptr.current(), Some('e'));
ptr.bump();
assert_eq!(ptr.current(), Some('s'));
ptr.bump();
assert_eq!(ptr.current(), Some('t'));
ptr.bump();
assert_eq!(ptr.current(), None);
ptr.bump();
assert_eq!(ptr.current(), None);
}
#[test]
fn test_bump_while() {
let mut ptr = Ptr::new("test");
assert_eq!(ptr.current(), Some('t'));
ptr.bump_while(|c| c != 's');
assert_eq!(ptr.current(), Some('s'));
}
}