@@ -178,6 +178,47 @@ impl<'txt> Cursor<'txt> {
178178 }
179179 }
180180
181+ /// Consumes all tokens until the specified identifier is found and returns its
182+ /// position. Returns `None` if the identifier could not be found.
183+ ///
184+ /// The cursor will be positioned immediately after the identifier, or at the end if
185+ /// it is not.
186+ pub fn find_ident ( & mut self , ident : & str ) -> Option < u32 > {
187+ loop {
188+ match self . next_token . kind {
189+ TokenKind :: Ident if self . peek_text ( ) == ident => {
190+ let pos = self . pos ;
191+ self . step ( ) ;
192+ return Some ( pos) ;
193+ } ,
194+ TokenKind :: Eof => return None ,
195+ _ => self . step ( ) ,
196+ }
197+ }
198+ }
199+
200+ /// Consumes all tokens until the next identifier is found and captures it. Returns
201+ /// `None` if no identifier could be found.
202+ ///
203+ /// The cursor will be positioned immediately after the identifier, or at the end if
204+ /// it is not.
205+ pub fn find_any_ident ( & mut self ) -> Option < Capture > {
206+ loop {
207+ match self . next_token . kind {
208+ TokenKind :: Ident => {
209+ let res = Capture {
210+ pos : self . pos ,
211+ len : self . next_token . len ,
212+ } ;
213+ self . step ( ) ;
214+ return Some ( res) ;
215+ } ,
216+ TokenKind :: Eof => return None ,
217+ _ => self . step ( ) ,
218+ }
219+ }
220+ }
221+
181222 /// Continually attempt to match the pattern on subsequent tokens until a match is
182223 /// found. Returns whether the pattern was successfully matched.
183224 ///
@@ -195,20 +236,6 @@ impl<'txt> Cursor<'txt> {
195236 true
196237 }
197238
198- /// The same as [`Self::find_pat`], but returns a capture as well.
199- #[ must_use]
200- pub fn find_capture_pat ( & mut self , pat : Pat < ' _ > ) -> Option < & ' txt str > {
201- let mut capture = Capture :: EMPTY ;
202- let mut captures = slice:: from_mut ( & mut capture) . iter_mut ( ) ;
203- while !self . match_impl ( pat, & mut captures) {
204- self . step ( ) ;
205- if self . at_end ( ) {
206- return None ;
207- }
208- }
209- Some ( self . get_text ( capture) )
210- }
211-
212239 /// Attempts to match a sequence of patterns at the current position. Returns whether
213240 /// all patterns were successfully matched.
214241 ///
@@ -221,6 +248,16 @@ impl<'txt> Cursor<'txt> {
221248 #[ must_use]
222249 pub fn match_all ( & mut self , pats : & [ Pat < ' _ > ] , captures : & mut [ Capture ] ) -> bool {
223250 let mut captures = captures. iter_mut ( ) ;
224- pats. iter ( ) . all ( |& t| self . match_impl ( t, & mut captures) )
251+ pats. iter ( ) . all ( |& p| self . match_impl ( p, & mut captures) )
252+ }
253+
254+ /// Attempts to match a single pattern at the current position. Returns whether the
255+ /// pattern was successfully matched.
256+ ///
257+ /// If the pattern attempts to capture anything this will panic. If the match fails
258+ /// the cursor will be positioned at the first failing token.
259+ #[ must_use]
260+ pub fn match_pat ( & mut self , pat : Pat < ' _ > ) -> bool {
261+ self . match_impl ( pat, & mut [ ] . iter_mut ( ) )
225262 }
226263}
0 commit comments