11use 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.
37pub ( 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