@@ -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.
2021pub 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 ) ]
3134pub struct Cursor {
3235 cursor : usize ,
@@ -42,6 +45,7 @@ pub struct LexerState<'a> {
4245}
4346
4447impl < ' 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 ;
0 commit comments