Class: SwiftAST::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/swift-ast-dump/swift_ast_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, parameters = [], children = []) ⇒ Node

Returns a new instance of Node.



171
172
173
174
175
# File 'lib/swift-ast-dump/swift_ast_parser.rb', line 171

def initialize(name, parameters = [], children = [])
  @name = name
  @parameters = parameters
  @children = children
end

Instance Method Details

#childrenObject



185
186
187
# File 'lib/swift-ast-dump/swift_ast_parser.rb', line 185

def children
  @children
end

#dump(level = 0) ⇒ Object



189
190
191
192
193
194
195
# File 'lib/swift-ast-dump/swift_ast_parser.rb', line 189

def dump(level = 0)
  @@line = 0 if level == 0
  puts "\n" if level == 0
  puts " " * level + "[#{@@line}][#{@name} #{@parameters}"
  @@line = @@line + 1
  @children.each { |child| child.dump(level + 1) }
end

#find_nodes(type) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
# File 'lib/swift-ast-dump/swift_ast_parser.rb', line 197

def find_nodes(type)
  found_nodes = []
  @children.each { |child| 
    if child.name == type
       found_nodes << child
    else
       found_nodes += child.find_nodes(type)
    end      
  }
  found_nodes
end

#nameObject



177
178
179
# File 'lib/swift-ast-dump/swift_ast_parser.rb', line 177

def name
  @name
end

#on_node(type, &block) ⇒ Object



209
210
211
212
213
214
# File 'lib/swift-ast-dump/swift_ast_parser.rb', line 209

def on_node(type, &block)
  @children.each { |child|
    yield child if child.name == type
    child.on_node(type, &block)
  }
end

#parametersObject



181
182
183
# File 'lib/swift-ast-dump/swift_ast_parser.rb', line 181

def parameters
  @parameters
end