lib/atp/ast/node.rb in atp-0.3.3 vs lib/atp/ast/node.rb in atp-0.4.0

- old
+ new

@@ -27,15 +27,20 @@ @parser.string_to_ast(sexp) end # Adds an empty node of the given type to the children unless another # node of the same type is already present - def ensure_node_present(type) + def ensure_node_present(type, child_nodes = nil) if children.any? { |n| n.type == type } self else - updated(nil, children + [n0(type)]) + if child_nodes + node = n(type, *child_nodes) + else + node = n0(type) + end + updated(nil, children + [node]) end end # Returns the value at the root of an AST node like this: # @@ -54,9 +59,14 @@ end # Add the given nodes to the children def add(*nodes) updated(nil, children + nodes) + end + + # Remove the given nodes from the children + def remove(*nodes) + updated(nil, children - nodes) end # Returns the first child node of the given type that is found def find(type) children.find { |c| c.try(:type) == type }