Module: Paru::PandocFilter::ASTManipulation

Included in:
Node
Defined in:
lib/paru/filter/ast_manipulation.rb

Overview

ASTManipulation is a mixin for Node with some standard tree manipulation methods such as inserting or removing nodes, replacing nodes, and so on.

Instance Method Summary collapse

Instance Method Details

#append(child) ⇒ Object Also known as: <<

Append a child to the list with this node's children.

Parameters:

  • child (Node)

    the child to append.



70
71
72
# File 'lib/paru/filter/ast_manipulation.rb', line 70

def append(child)
    @children.push child
end

#delete(child) ⇒ Object

Delete child from this node's children.

Parameters:

  • child (Node)

    the child node to delete.



56
57
58
# File 'lib/paru/filter/ast_manipulation.rb', line 56

def delete(child)
    @children.delete child
end

#each_depth_first(&block) {|Node| ... } ⇒ Object

Walk the node tree starting at this node, depth first, and apply block to each node in the tree

Parameters:

  • block (Proc)

    the block to apply to each node in this node tree

Yields:



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/paru/filter/ast_manipulation.rb', line 109

def each_depth_first(&block)    
    yield self

    if has_been_replaced?
        node = get_replacement
    else
        node = self
    end

    node.each {|child| child.each_depth_first(&block)} if node.has_children?
end

#find_index(child) ⇒ Number

Find index of child

Parameters:

  • child (Node)

    the child to find the index for

Returns:

  • (Number)

    the index of child or nil



31
32
33
# File 'lib/paru/filter/ast_manipulation.rb', line 31

def find_index(child)
    @children.find_index child
end

#get(index) ⇒ Node

Get the child node at index

Parameters:

  • index (Number)

    the index of the child to get

Returns:

  • (Node)

    the child at index



41
42
43
# File 'lib/paru/filter/ast_manipulation.rb', line 41

def get(index)
    @children[index]
end

#insert(index, child) ⇒ Object

Insert child node among this node's children at position index.

Parameters:

  • index (Integer)

    the position to insert the child

  • child (Node)

    the child to insert



49
50
51
# File 'lib/paru/filter/ast_manipulation.rb', line 49

def insert(index, child)
    @children.insert index, child
end

#prepend(child) ⇒ Object

Prepend a child to the list with this node's children.

Parameters:

  • child (Node)

    the child to prepend.



78
79
80
# File 'lib/paru/filter/ast_manipulation.rb', line 78

def prepend(child)
    insert 0, child
end

#remove_at(index) ⇒ Object

Remove the child at position index from this node's children

Parameters:

  • index (Integer)

    the position of the child to remove



63
64
65
# File 'lib/paru/filter/ast_manipulation.rb', line 63

def remove_at(index)
    @children.delete_at index
end

#replace(old_child, new_child) ⇒ Object

Replace a child from this node's children with a new child.

Parameters:

  • old_child (Node)

    the child to replace

  • new_child (Node)

    the replacement child



86
87
88
89
90
91
# File 'lib/paru/filter/ast_manipulation.rb', line 86

def replace(old_child, new_child)
    old_child_index = find_index old_child
    if old_child_index then
        replace_at old_child_index, new_child
    end
end

#replace_at(index, new_child) ⇒ Object

Replace the child at position index from this node's children with a new child.

Parameters:

  • index (Integer)

    the position of the child to replace

  • new_child (Node)

    the replacement child



98
99
100
# File 'lib/paru/filter/ast_manipulation.rb', line 98

def replace_at(index, new_child)
    @children[index] = new_child
end