@@ -228,6 +228,7 @@ struct DepthFirstIterator {
228228 depth : usize ,
229229}
230230
231+ // TODO: Provide a function to move backward.
231232impl DepthFirstIterator {
232233 fn new ( tree_or_node : TreeOrNode ) -> Self {
233234 Self {
@@ -286,44 +287,62 @@ impl Iterator for DepthFirstIterator {
286287 }
287288}
288289
290+ /// Create a new depth-first iterator from the given TREE-OR-NODE.
291+ /// The traversal is pre-order.
289292#[ defun( user_ptr) ]
290293fn _iter ( tree_or_node : TreeOrNode ) -> Result < DepthFirstIterator > {
291294 Ok ( DepthFirstIterator :: new ( tree_or_node) )
292295}
293296
297+ /// Move ITERATOR to the next node.
298+ /// Return t if ITERATOR successfully moved, nil if there was no next node, or if
299+ /// ITERATOR was closed.
294300#[ defun]
295- fn _iter_next ( iter : & mut DepthFirstIterator ) -> Result < bool > {
296- Ok ( iter . next ( ) . is_some ( ) )
301+ fn _iter_next ( iterator : & mut DepthFirstIterator ) -> Result < bool > {
302+ Ok ( iterator . next ( ) . is_some ( ) )
297303}
298304
305+ /// Close ITERATOR.
299306#[ defun]
300- fn _iter_close ( iter : & mut DepthFirstIterator ) -> Result < ( ) > {
301- Ok ( iter . close ( ) )
307+ fn _iter_close ( iterator : & mut DepthFirstIterator ) -> Result < ( ) > {
308+ Ok ( iterator . close ( ) )
302309}
303310
311+ /// Retrieve properties of the node that ITERATOR is currently on.
312+ ///
313+ /// PROPS is a vector of property names to retrieve.
314+ /// OUTPUT is a vector where the properties will be written to.
304315#[ defun]
305- fn _iter_current_node ( iter : & mut DepthFirstIterator , props : Vector , output : Vector ) -> Result < ( ) > {
316+ fn _iter_current_node ( iterator : & mut DepthFirstIterator , props : Vector , output : Vector ) -> Result < ( ) > {
306317 let env = output. value ( ) . env ;
307- let cursor = & iter . cursor ;
318+ let cursor = & iterator . cursor ;
308319 let _ = _current_node ( cursor, Some ( props) , Some ( output) , env) ?;
309320 for ( i, prop) in props. into_iter ( ) . enumerate ( ) {
310321 if prop. eq ( _depth. bind ( env) ) {
311- output. set ( i, iter . depth ) ?;
322+ output. set ( i, iterator . depth ) ?;
312323 }
313324 }
314325 Ok ( ( ) )
315326}
316327
328+ /// Move ITERATOR to the next node, and retrieve its properties.
329+ ///
330+ /// This a combination of `tsc--iter-next' and `tsc--iter-current-node'.
317331#[ defun]
318- fn _iter_next_node ( iter : & mut DepthFirstIterator , props : Vector , output : Vector ) -> Result < bool > {
319- if iter . next ( ) . is_some ( ) {
320- _iter_current_node ( iter , props, output) ?;
332+ fn _iter_next_node ( iterator : & mut DepthFirstIterator , props : Vector , output : Vector ) -> Result < bool > {
333+ if iterator . next ( ) . is_some ( ) {
334+ _iter_current_node ( iterator , props, output) ?;
321335 Ok ( true )
322336 } else {
323337 Ok ( false )
324338 }
325339}
326340
341+ /// Return CURSOR's current node, if PROPS is nil.
342+ ///
343+ /// If PROPS is a vector of keywords, this function returns a vector containing the
344+ /// corresponding node properties instead of the node itself. If OUTPUT is also a
345+ /// vector, this function overwrites its contents instead of creating a new vector.
327346#[ defun]
328347fn _current_node < ' e > ( cursor : & RCursor , props : Option < Vector < ' e > > , output : Option < Vector < ' e > > , env : & ' e Env ) -> Result < Value < ' e > > {
329348 macro_rules! sugar {
@@ -378,6 +397,8 @@ fn _current_node<'e>(cursor: &RCursor, props: Option<Vector<'e>>, output: Option
378397 }
379398}
380399
400+ /// Actual logic of `tsc-traverse-mapc'. The wrapper is needed because
401+ /// `emacs-module-rs' doesn't currently support optional arguments.
381402#[ defun]
382403fn _traverse_mapc ( func : Value , tree_or_node : TreeOrNode , props : Option < Vector > ) -> Result < ( ) > {
383404 let mut iterator = DepthFirstIterator :: new ( tree_or_node) ;
0 commit comments