Created
August 14, 2008 04:07
-
-
Save adamsanderson/5367 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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