From af0ae9ee0409ce2296dafebf977e353a5b871d80 Mon Sep 17 00:00:00 2001 From: Zach Lute Date: Tue, 4 Sep 2018 22:56:16 -0700 Subject: Updated Ptr methods to better match Parser method names. --- crates/libsyntax2/src/lexer/ptr.rs | 42 +++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 14 deletions(-) (limited to 'crates/libsyntax2/src/lexer/ptr.rs') diff --git a/crates/libsyntax2/src/lexer/ptr.rs b/crates/libsyntax2/src/lexer/ptr.rs index d1391fd5f..aa59e33cc 100644 --- a/crates/libsyntax2/src/lexer/ptr.rs +++ b/crates/libsyntax2/src/lexer/ptr.rs @@ -2,12 +2,14 @@ use TextUnit; use std::str::Chars; +/// A simple view into the characters of a string. pub(crate) struct Ptr<'s> { text: &'s str, len: TextUnit, } impl<'s> Ptr<'s> { + /// Creates a new `Ptr` from a string. pub fn new(text: &'s str) -> Ptr<'s> { Ptr { text, @@ -15,45 +17,55 @@ impl<'s> Ptr<'s> { } } + /// Gets the length of the remaining string. pub fn into_len(self) -> TextUnit { self.len } - pub fn next(&self) -> Option { + /// Gets the current character, if one exists. + pub fn current(&self) -> Option { self.chars().next() } - pub fn nnext(&self) -> Option { - let mut chars = self.chars(); - chars.next()?; - chars.next() + /// Gets the nth character from the current. + /// For example, 0 will return the current token, 1 will return the next, etc. + pub fn nth(&self, n: u32) -> Option { + let mut chars = self.chars().peekable(); + chars.by_ref().skip(n as usize).next() } - pub fn next_is(&self, c: char) -> bool { - self.next() == Some(c) + /// Checks whether the current character is `c`. + pub fn at(&self, c: char) -> bool { + self.current() == Some(c) } - pub fn nnext_is(&self, c: char) -> bool { - self.nnext() == Some(c) + /// 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) } - pub fn next_is_p bool>(&self, p: P) -> bool { - self.next().map(p) == Some(true) + /// Checks whether the current character satisfies the predicate `p`. + pub fn at_p bool>(&self, p: P) -> bool { + self.current().map(p) == Some(true) } - pub fn nnext_is_p bool>(&self, p: P) -> bool { - self.nnext().map(p) == Some(true) + /// Checks whether the nth character satisfies the predicate `p`. + pub fn nth_is_p bool>(&self, n: u32, p: P) -> bool { + self.nth(n).map(p) == Some(true) } + /// Moves to the next character. pub fn bump(&mut self) -> Option { let ch = self.chars().next()?; self.len += TextUnit::of_char(ch); Some(ch) } + /// Moves to the next character as long as `pred` is satisfied. pub fn bump_while bool>(&mut self, pred: F) { loop { - match self.next() { + match self.current() { Some(c) if pred(c) => { self.bump(); } @@ -62,11 +74,13 @@ impl<'s> Ptr<'s> { } } + /// Returns the text up to the current point. 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. fn chars(&self) -> Chars { let len: u32 = self.len.into(); self.text[len as usize..].chars() -- cgit v1.2.3