Skip to content

Commit 44e9eaa

Browse files
committed
chore: add documentation
1 parent 62d6f7f commit 44e9eaa

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

parse-it/src/lexer.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ pub struct Span {
1717
pub end: usize,
1818
}
1919

20+
/// A trait for types that can be converted to another type.
2021
pub trait TryConvert<T> {
22+
/// Try to convert the value to the target type.
2123
fn try_convert(&self) -> Option<T>;
2224
}
2325

@@ -27,6 +29,7 @@ impl<T: Copy> TryConvert<T> for T {
2729
}
2830
}
2931

32+
/// Cursor position in the input.
3033
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
3134
pub struct Cursor {
3235
cursor: usize,
@@ -42,6 +45,7 @@ pub struct LexerState<'a> {
4245
}
4346

4447
impl<'a> LexerState<'a> {
48+
/// Create a new lexer state.
4549
pub fn new(input: &'a str) -> Self {
4650
Self {
4751
start: 0,
@@ -50,7 +54,7 @@ impl<'a> LexerState<'a> {
5054
}
5155
}
5256

53-
/// TODO
57+
/// Run the lexer against the given regex.
5458
pub fn run(&mut self, regex: &Regex) -> Option<PatternID> {
5559
let input = Input::new(self.input)
5660
.range(self.cursor..)
@@ -61,7 +65,7 @@ impl<'a> LexerState<'a> {
6165
Some(end.pattern())
6266
}
6367

64-
/// TODO
68+
/// Get the lexeme of the current token.
6569
pub fn lexeme(&self) -> &'a str {
6670
&self.input[self.start..self.cursor]
6771
}
@@ -74,17 +78,20 @@ impl<'a> LexerState<'a> {
7478
}
7579
}
7680

81+
/// Get the span of the current token.
7782
pub fn span(&self) -> Span {
7883
Span {
7984
start: self.start,
8085
end: self.cursor,
8186
}
8287
}
8388

89+
/// Check if the lexer is at the end of the input.
8490
pub fn is_empty(&self) -> bool {
8591
self.cursor >= self.input.len()
8692
}
8793

94+
/// Advance the lexer to the given cursor position.
8895
pub fn advance_to_cursor(&mut self, cursor: Cursor) {
8996
self.start = cursor.start;
9097
self.cursor = cursor.cursor;

parse-it/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
//! println!("{:?}", instrs);
4747
//! }
4848
//! ```
49-
// #![warn(missing_docs)]
49+
#![warn(missing_docs)]
5050
#![allow(clippy::needless_doctest_main)]
5151

5252
pub mod lexer;
@@ -61,12 +61,15 @@ pub use crate::{
6161
parser::{Error, ParserState},
6262
};
6363

64-
/// A lexer for the parser.
64+
/// A lexer.
6565
pub trait LexIt {
66+
/// The token type.
6667
type Token<'a>;
6768

69+
/// Create a new lexer instance.
6870
fn new() -> Self;
6971

72+
/// Get the next token from the lexer.
7073
fn next<'a>(&self, lexbuf: &mut LexerState<'a>) -> Option<Self::Token<'a>>;
7174
}
7275

parse-it/src/parser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl<'a, L: LexIt + Clone> ParserState<'a, L> {
127127
self.next().and_then(matches).ok_or_else(|| self.error())
128128
}
129129

130+
/// Parse a token that can be converted to the given type.
130131
pub fn parse_type<T>(&mut self) -> Result<T, Error>
131132
where
132133
L::Token<'a>: TryConvert<T>,
@@ -135,6 +136,7 @@ impl<'a, L: LexIt + Clone> ParserState<'a, L> {
135136
self.parse_with(|tt| tt.try_convert())
136137
}
137138

139+
/// Parse a token that exactly matches the given character.
138140
pub fn parse_char(&mut self, c: char) -> Result<char, Error> {
139141
self.next().ok_or_else(|| self.error())?;
140142
let lexeme = self.lexbuf.lexeme();
@@ -147,6 +149,7 @@ impl<'a, L: LexIt + Clone> ParserState<'a, L> {
147149
}
148150
}
149151

152+
/// Parse a token that exactly matches the given string.
150153
pub fn parse_str(&mut self, literal: &'a str) -> Result<&str, Error> {
151154
self.next().ok_or_else(|| self.error())?;
152155
let lexeme = self.lexbuf.lexeme();

0 commit comments

Comments
 (0)