use Underscore's flatten -- start to make a nicer tree printer

This commit is contained in:
Jeremy Ashkenas
2010-02-13 09:13:50 -05:00
parent dd6be80fca
commit a62923ff97
2 changed files with 34 additions and 43 deletions

View File

@@ -1,5 +1,5 @@
(function(){
var AccessorNode, ArrayNode, AssignNode, CallNode, ClosureNode, CodeNode, CommentNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IfNode, IndexNode, LiteralNode, Node, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThisNode, ThrowNode, TryNode, ValueNode, WhileNode, _, del, flatten, inherit, merge, statement;
var AccessorNode, ArrayNode, AssignNode, CallNode, ClosureNode, CodeNode, CommentNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IfNode, IndexNode, LiteralNode, Node, ObjectNode, OpNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, SliceNode, SplatNode, TAB, TRAILING_WHITESPACE, ThisNode, ThrowNode, TryNode, ValueNode, WhileNode, _, del, inherit, merge, statement;
var __hasProp = Object.prototype.hasOwnProperty;
process.mixin(require('./scope'));
_ = require('./underscore')._;
@@ -9,21 +9,6 @@
TRAILING_WHITESPACE = /\s+$/gm;
// Keep the identifier regex in sync with the Lexer.
IDENTIFIER = /^[a-zA-Z$_](\w|\$)*$/;
// Flatten nested arrays recursively.
flatten = function flatten(list) {
var __a, __b, item, memo;
memo = [];
__a = list;
for (__b = 0; __b < __a.length; __b++) {
item = __a[__b];
if (item instanceof Array) {
return memo.concat(flatten(item));
}
memo.push(item);
memo;
}
return memo;
};
// Merge objects.
merge = function merge(options, overrides) {
return _.tap({}, function(fresh) {
@@ -123,16 +108,10 @@
};
// toString representation of the node, for inspecting the parse tree.
Node.prototype.toString = function toString(idt) {
var __a, __b, __c, child;
idt = (idt || '') + TAB;
return this.type + "\n" + ((function() {
__a = []; __b = this.children;
for (__c = 0; __c < __b.length; __c++) {
child = __b[__c];
__a.push(idt + child.toString(idt));
}
return __a;
}).call(this)).join('');
return this.type + '\n' + _.map(this.children, function(child) {
return idt + child.toString(idt);
}).join('');
};
// Default implementations of the common node methods.
Node.prototype.unwrap = function unwrap() {
@@ -152,7 +131,7 @@
Expressions = (exports.Expressions = inherit(Node, {
type: 'Expressions',
constructor: function constructor(nodes) {
this.children = (this.expressions = _.compact(flatten(nodes || [])));
this.children = (this.expressions = _.compact(_.flatten(nodes || [])));
return this;
},
// Tack an expression on to the end of this expression list.
@@ -274,6 +253,9 @@
idt = this.is_statement() ? this.idt() : '';
end = this.is_statement() ? ';' : '';
return idt + this.value + end;
},
toString: function toString(idt) {
return '"' + this.value + '"' + '\n';
}
}));
LiteralNode.prototype.is_statement_only = LiteralNode.prototype.is_statement;
@@ -299,7 +281,7 @@
type: 'Value',
SOAK: " == undefined ? undefined : ",
constructor: function constructor(base, properties) {
this.children = flatten((this.base = base), (this.properties = (properties || [])));
this.children = _.flatten([(this.base = base), (this.properties = (properties || []))]);
return this;
},
push: function push(prop) {
@@ -380,7 +362,7 @@
CallNode = (exports.CallNode = inherit(Node, {
type: 'Call',
constructor: function constructor(variable, args) {
this.children = flatten([(this.variable = variable), (this.args = (args || []))]);
this.children = _.flatten([(this.variable = variable), (this.args = (args || []))]);
this.prefix = '';
return this;
},
@@ -821,6 +803,16 @@
},
top_sensitive: function top_sensitive() {
return true;
},
toString: function toString(idt) {
var children;
idt = (idt || '') + TAB;
puts(this.body.expressions.length);
children = _.flatten([this.params, this.body.expressions]);
puts(children.length);
return this.type + '\n' + _.map(children, function(child) {
return idt + child.toString(idt);
}).join('');
}
}));
// A splat, either as a parameter to a function, an argument to a call,
@@ -1206,7 +1198,7 @@
compile_condition: function compile_condition(o) {
var __a, __b, __c, cond;
return ((function() {
__a = []; __b = flatten([this.condition]);
__a = []; __b = _.flatten([this.condition]);
for (__c = 0; __c < __b.length; __c++) {
cond = __b[__c];
__a.push(cond.compile(o));