///  1. Pop a node off the stack
///  2. Look up all of it's children in parent_to_children
///  3. Append the children to this node
///  4. Move on to the next node (as in, go back to step 1)
1. Pop a node off the stack
2. Look up all of it's children in parent_to_children
3. Append the children to this node
4. Move on to the next node (as in, go back to step 1)
1 . Pop a node off the stack 2 . Look up all of it ' s children in parent_to_children 3 . Append the children to this node 4 . Move on to the next node ( as in go back to step 1 )
pub fn finish(&mut self) -> proc_macro2::TokenStream {
        let parent_stack = &mut self.parent_stack;
        let node_order = &mut self.node_order;
        let parent_to_children = &mut self.parent_to_children;
        let tokens = &mut self.tokens;
        if node_order.len() > 1 {
            for _ in 0..(node_order.len()) {
                let parent_idx = node_order.pop().unwrap();
                // TODO: Figure out how to really use spans
                let parent_name =
                    Ident::new(format!("node_{}", parent_idx).as_str(), Span::call_site());
                let parent_to_children_indices = match parent_to_children.get(&parent_idx) {
                    Some(children) => children,
                    None => continue,
                };
                if parent_to_children_indices.len() > 0 {
                    for child_idx in parent_to_children_indices.iter() {
                        let children =
                            Ident::new(format!("node_{}", child_idx).as_str(), Span::call_site());
                        let unreachable = quote_spanned!(Span::call_site() => {
                            unreachable!("Non-elements cannot have children");
                        });
                        let push_children = quote! {
                            if let Some(ref mut element_node) =  #parent_name.as_velement_mut() {
                                element_node.children.extend(#children.into_iter());
                            } else {
                                #unreachable;
                            }
                        };
                        tokens.push(push_children);
                    }
                }
            }
        }
        // Create a virtual node tree
        let node = quote! {
            {
                #(#tokens)*
                // Root node is always named node_0
                node_0
            }
        };
        node
    }
pub fn finish ( & mut self ) -> proc_macro2 :: TokenStream { let parent_stack = & mut self . parent_stack ; let node_order = & mut self . node_order ; let parent_to_children = & mut self . parent_to_children ; let tokens = & mut self . tokens ; if node_order . len ( ) > 1 { for _ in 0 .. ( node_order . len ( ) ) { let parent_idx = node_order . pop ( ) . unwrap ( ) ; // TODO: Figure out how to really use spans let parent_name = Ident :: new ( format ! ( " " parent_idx ) . as_str ( ) , Span :: call_site ( ) ) ; let parent_to_children_indices = match parent_to_children . get ( & parent_idx ) { Some ( children ) => children , None => continue , } ; if parent_to_children_indices . len ( ) > 0 { for child_idx in parent_to_children_indices . iter ( ) { let children = Ident :: new ( format ! ( " " child_idx ) . as_str ( ) , Span :: call_site ( ) ) ; let unreachable = quote_spanned ! ( Span call_site ( ) { unreachable ( " " ) } ) ; let push_children = quote ! { if let Some ( ref mut element_node ) parent_name as_velement_mut ( ) { element_node children extend ( children into_iter ( ) ) } else { unreachable } } ; tokens . push ( push_children ) ; } } } } // Create a virtual node tree let node = quote ! { { ( tokens ) // Root node is always named node_0 node_0 } } ; node }
1.
1 .
&mut self