Created
March 13, 2013 10:08
-
-
Save gka/5150764 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
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