Skip to content

Instantly share code, notes, and snippets.

@adamsanderson
Created August 14, 2008 04:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamsanderson/5367 to your computer and use it in GitHub Desktop.
Save adamsanderson/5367 to your computer and use it in GitHub Desktop.
require 'jruby'
import org.jruby.ast.Node
import org.jruby.ast.DefnNode
import org.jruby.ast.ClassNode
# Quick test of the JRuby AST, actually much easier to work with than
# ParseTree
code = <<-END
class Feline::Cat
def name
puts "mowser"
end
def likes
raise "Nothing, \#{name} hates everything!"
end
end
END
node = JRuby::parse(code, "", true)
class Node
def visit(&callback)
callback[self]
child_nodes.each do |node|
node.visit(&callback)
end
end
end
puts node.inspect
puts "--"
#puts((node.methods - Object.methods).sort.inspect)
node.visit do |n|
case n
when ClassNode then full_name = (n.getCPath.childNodes.map{|c| c.getName} << n.getCPath.getName).join('::')
puts "class #{full_name}"
when DefnNode then puts "defines #{n.getName}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment