@@ -257,37 +257,25 @@ def token_prev(self, idx, skip_ws=True, skip_cm=False):
257257 """Returns the previous token relative to *idx*.
258258
259259 If *skip_ws* is ``True`` (the default) whitespace tokens are ignored.
260+ If *skip_cm* is ``True`` comments are ignored.
260261 ``None`` is returned if there's no previous token.
261262 """
262- if idx is None :
263- return None , None
264- idx += 1 # alot of code usage current pre-compensates for this
265- funcs = lambda tk : not ((skip_ws and tk .is_whitespace ()) or
266- (skip_cm and imt (tk , t = T .Comment , i = Comment )))
267- return self ._token_matching (funcs , idx , reverse = True )
263+ return self .token_next (idx , skip_ws , skip_cm , _reverse = True )
268264
269- # TODO: May need to implement skip_cm for upstream changes.
270265 # TODO: May need to re-add default value to idx
271- def token_next (self , idx , skip_ws = True , skip_cm = False ):
266+ def token_next (self , idx , skip_ws = True , skip_cm = False , _reverse = False ):
272267 """Returns the next token relative to *idx*.
273268
274269 If *skip_ws* is ``True`` (the default) whitespace tokens are ignored.
270+ If *skip_cm* is ``True`` comments are ignored.
275271 ``None`` is returned if there's no next token.
276272 """
277273 if idx is None :
278274 return None , None
279275 idx += 1 # alot of code usage current pre-compensates for this
280- try :
281- if not skip_ws :
282- return idx , self .tokens [idx ]
283- else :
284- while True :
285- token = self .tokens [idx ]
286- if not token .is_whitespace ():
287- return idx , token
288- idx += 1
289- except IndexError :
290- return None , None
276+ funcs = lambda tk : not ((skip_ws and tk .is_whitespace ()) or
277+ (skip_cm and imt (tk , t = T .Comment , i = Comment )))
278+ return self ._token_matching (funcs , idx , reverse = _reverse )
291279
292280 def token_index (self , token , start = 0 ):
293281 """Return list index of token."""
0 commit comments