adding a traverse method to the AST, so we can do fancy processing from external scripts.

This commit is contained in:
Jeremy Ashkenas
2010-02-27 01:22:21 -05:00
parent f4cd0bdf29
commit 5e7f5f390a
2 changed files with 47 additions and 7 deletions

View File

@@ -94,9 +94,16 @@ BaseNode::idt: (tabs) ->
BaseNode::contains: (block) ->
for node in @children
return true if block(node)
return true if node instanceof BaseNode and node.contains block
return true if node.contains and node.contains block
false
# Perform an in-order traversal of the AST.
BaseNode::traverse: (block) ->
for node in @children
block node
node.traverse block if node.traverse
# toString representation of the node, for inspecting the parse tree.
BaseNode::toString: (idt) ->
idt ||= ''
@@ -670,10 +677,16 @@ CodeNode: exports.CodeNode: inherit BaseNode, {
top_sensitive: ->
true
real_children: ->
flatten [@params, @body.expressions]
traverse: (block) ->
block this
block(child) for child in @real_children()
toString: (idt) ->
idt ||= ''
children: flatten [@params, @body.expressions]
'\n' + idt + @type + (child.toString(idt + TAB) for child in children).join('')
'\n' + idt + @type + (child.toString(idt + TAB) for child in @real_children()).join('')
}