Skip to content

Instantly share code, notes, and snippets.

@tenderlove
Forked from jbarnette/meta.rb
Created September 26, 2008 18:32
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 tenderlove/13169 to your computer and use it in GitHub Desktop.
Save tenderlove/13169 to your computer and use it in GitHub Desktop.
require "rubygems"
require "johnson"
require "./visitor.rb"
js =<<-END
function top() {
"top docstring", { command: true }
doSomething();
};
function justADocstring() {
"justADocstring docstring"
doSomething();
}
function noMeta() {
doSomething();
};
Namespace = {
nested: function() {
"nested docstring", { command: true }
doSomething();
},
Doubly: {
nested: function() {
"doubly nested docstring", { command: true }
doSomething();
}
}
};
var NamespaceWithVar = {
nested: function() {
"nested with var docstring", { command: true }
doSomething();
}
};
END
ast = Johnson.parse(js);
visitor = MetaVisitor.new do |node|
end
ast.accept(visitor)
class MetaVisitor < Johnson::Visitors::EnumeratingVisitor
attr_accessor :block
def initialize(&block)
@block = block
@namespace = []
end
def visit_Function(o)
name = (@namespace + [o.name]).compact.join(".")
puts name
super
end
%w{ OpEqual AssignExpr Property }.each do |type|
define_method(:"visit_#{type}") do |o|
@namespace.push(o.left.value)
super
@namespace.pop
self
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment