Skip to content

Commit 2cee7ba

Browse files
authored
Rollup merge of rust-lang#65849 - popzxc:document-librustc_lexer, r=petrochenkov
librustc_lexer: Enhance documentation This PR enhances documentation state of the `librustc_lexer` (as initiative caused by [rustc-guide#474](rust-lang/rustc-dev-guide#474)), by adding: - Module documentation. - Doc-comments (and a bit of usual comments) in non-obvious (as for me) places. r? @petrochenkov cc @Centril
2 parents 7e8599d + 993b920 commit 2cee7ba

File tree

3 files changed

+225
-24
lines changed

3 files changed

+225
-24
lines changed

src/librustc_lexer/src/cursor.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
use std::str::Chars;
22

3+
/// Peekable iterator over a char sequence.
4+
///
5+
/// Next characters can be peeked via `nth_char` method,
6+
/// and position can be shifted forward via `bump` method.
37
pub(crate) struct Cursor<'a> {
48
initial_len: usize,
59
chars: Chars<'a>,
@@ -18,7 +22,9 @@ impl<'a> Cursor<'a> {
1822
prev: EOF_CHAR,
1923
}
2024
}
25+
2126
/// For debug assertions only
27+
/// Returns the last eaten symbol (or '\0' in release builds).
2228
pub(crate) fn prev(&self) -> char {
2329
#[cfg(debug_assertions)]
2430
{
@@ -30,19 +36,30 @@ impl<'a> Cursor<'a> {
3036
'\0'
3137
}
3238
}
39+
40+
/// Returns nth character relative to the current cursor position.
41+
/// If requested position doesn't exist, `EOF_CHAR` is returned.
42+
/// However, getting `EOF_CHAR` doesn't always mean actual end of file,
43+
/// it should be checked with `is_eof` method.
3344
pub(crate) fn nth_char(&self, n: usize) -> char {
3445
self.chars().nth(n).unwrap_or(EOF_CHAR)
3546
}
47+
48+
/// Checks if there is nothing more to consume.
3649
pub(crate) fn is_eof(&self) -> bool {
3750
self.chars.as_str().is_empty()
3851
}
52+
53+
/// Returns amount of already consumed symbols.
3954
pub(crate) fn len_consumed(&self) -> usize {
4055
self.initial_len - self.chars.as_str().len()
4156
}
42-
/// Returns an iterator over the remaining characters.
57+
58+
/// Returns a `Chars` iterator over the remaining characters.
4359
fn chars(&self) -> Chars<'a> {
4460
self.chars.clone()
4561
}
62+
4663
/// Moves to the next character.
4764
pub(crate) fn bump(&mut self) -> Option<char> {
4865
let c = self.chars.next()?;

0 commit comments

Comments
 (0)