Skip to content

Instantly share code, notes, and snippets.

@gka
Created March 13, 2013 10:08
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 gka/5150764 to your computer and use it in GitHub Desktop.
Save gka/5150764 to your computer and use it in GitHub Desktop.
from slimit.parser import Parser
from slimit.visitors.nodevisitor import ASTVisitor
from slimit.ast import *
fn = '../d3/src/scale/log.js'
parser = Parser()
tree = parser.parse(open(fn).read())
class MyVisitor(ASTVisitor):
def __init__(self):
self.funcNestLevel = 0
def visit_FuncDecl(self, node):
self.funcNestLevel += 1
for el in node.elements:
self.visit(el)
self.funcNestLevel -= 1
def visit_FuncExpr(self, node):
self.funcNestLevel += 1
for el in node.elements:
self.visit(el)
self.funcNestLevel -= 1
def visit_FunctionCall(self, node):
func = get_func(node.identifier)
if func[:3] == 'd3.' or func[:3] == 'd3_':
print 'called', func+'()', 'at level', self.funcNestLevel
def visit_Identifier(self, node):
if node.value[:3] == 'd3_':
print 'accessed', node.value, 'at level', self.funcNestLevel
def get_func(node):
if isinstance(node, Identifier):
return node.value
if isinstance(node, DotAccessor):
return get_func(node.node) + '.' + get_func(node.identifier)
if isinstance(node, FunctionCall):
return get_func(node.identifier)
print type(node)
visitor = MyVisitor()
visitor.visit(tree)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment