mirror of
https://github.com/jashkenas/coffeescript.git
synced 2022-11-09 12:23:24 -05:00
adding a traverse method to the AST, so we can do fancy processing from external scripts.
This commit is contained in:
parent
f4cd0bdf29
commit
5e7f5f390a
2 changed files with 47 additions and 7 deletions
35
lib/nodes.js
35
lib/nodes.js
|
@ -142,12 +142,27 @@
|
||||||
if (block(node)) {
|
if (block(node)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (node instanceof BaseNode && node.contains(block)) {
|
if (node.contains && node.contains(block)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
// Perform an in-order traversal of the AST.
|
||||||
|
BaseNode.prototype.traverse = function traverse(block) {
|
||||||
|
var _a, _b, _c, _d, node;
|
||||||
|
_a = []; _b = this.children;
|
||||||
|
for (_c = 0, _d = _b.length; _c < _d; _c++) {
|
||||||
|
node = _b[_c];
|
||||||
|
_a.push((function() {
|
||||||
|
block(node);
|
||||||
|
if (node.traverse) {
|
||||||
|
return node.traverse(block);
|
||||||
|
}
|
||||||
|
}).call(this));
|
||||||
|
}
|
||||||
|
return _a;
|
||||||
|
};
|
||||||
// toString representation of the node, for inspecting the parse tree.
|
// toString representation of the node, for inspecting the parse tree.
|
||||||
BaseNode.prototype.toString = function toString(idt) {
|
BaseNode.prototype.toString = function toString(idt) {
|
||||||
var _a, _b, _c, _d, child;
|
var _a, _b, _c, _d, child;
|
||||||
|
@ -854,12 +869,24 @@
|
||||||
top_sensitive: function top_sensitive() {
|
top_sensitive: function top_sensitive() {
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
real_children: function real_children() {
|
||||||
|
return flatten([this.params, this.body.expressions]);
|
||||||
|
},
|
||||||
|
traverse: function traverse(block) {
|
||||||
|
var _a, _b, _c, _d, child;
|
||||||
|
block(this);
|
||||||
|
_a = []; _b = this.real_children();
|
||||||
|
for (_c = 0, _d = _b.length; _c < _d; _c++) {
|
||||||
|
child = _b[_c];
|
||||||
|
_a.push(block(child));
|
||||||
|
}
|
||||||
|
return _a;
|
||||||
|
},
|
||||||
toString: function toString(idt) {
|
toString: function toString(idt) {
|
||||||
var _a, _b, _c, _d, child, children;
|
var _a, _b, _c, _d, child;
|
||||||
idt = idt || '';
|
idt = idt || '';
|
||||||
children = flatten([this.params, this.body.expressions]);
|
|
||||||
return '\n' + idt + this.type + (function() {
|
return '\n' + idt + this.type + (function() {
|
||||||
_a = []; _b = children;
|
_a = []; _b = this.real_children();
|
||||||
for (_c = 0, _d = _b.length; _c < _d; _c++) {
|
for (_c = 0, _d = _b.length; _c < _d; _c++) {
|
||||||
child = _b[_c];
|
child = _b[_c];
|
||||||
_a.push(child.toString(idt + TAB));
|
_a.push(child.toString(idt + TAB));
|
||||||
|
|
|
@ -94,9 +94,16 @@ BaseNode::idt: (tabs) ->
|
||||||
BaseNode::contains: (block) ->
|
BaseNode::contains: (block) ->
|
||||||
for node in @children
|
for node in @children
|
||||||
return true if block(node)
|
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
|
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.
|
# toString representation of the node, for inspecting the parse tree.
|
||||||
BaseNode::toString: (idt) ->
|
BaseNode::toString: (idt) ->
|
||||||
idt ||= ''
|
idt ||= ''
|
||||||
|
@ -670,10 +677,16 @@ CodeNode: exports.CodeNode: inherit BaseNode, {
|
||||||
top_sensitive: ->
|
top_sensitive: ->
|
||||||
true
|
true
|
||||||
|
|
||||||
|
real_children: ->
|
||||||
|
flatten [@params, @body.expressions]
|
||||||
|
|
||||||
|
traverse: (block) ->
|
||||||
|
block this
|
||||||
|
block(child) for child in @real_children()
|
||||||
|
|
||||||
toString: (idt) ->
|
toString: (idt) ->
|
||||||
idt ||= ''
|
idt ||= ''
|
||||||
children: flatten [@params, @body.expressions]
|
'\n' + idt + @type + (child.toString(idt + TAB) for child in @real_children()).join('')
|
||||||
'\n' + idt + @type + (child.toString(idt + TAB) for child in children).join('')
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue