mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-02-18 11:31:20 -05:00
adding a traverse method to the AST, so we can do fancy processing from external scripts.
This commit is contained in:
@@ -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('')
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user