aboutsummaryrefslogtreecommitdiff
path: root/lib/output.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/output.js')
-rw-r--r--lib/output.js230
1 files changed, 101 insertions, 129 deletions
diff --git a/lib/output.js b/lib/output.js
index 6d7b0723..1dbc1f6e 100644
--- a/lib/output.js
+++ b/lib/output.js
@@ -137,7 +137,7 @@ function OutputStream(options) {
function make_string(str, quote) {
var dq = 0, sq = 0;
str = str.replace(/[\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]/g,
- function(s, i){
+ function(s, i) {
switch (s) {
case '"': ++dq; return '"';
case "'": ++sq; return "'";
@@ -408,7 +408,7 @@ function OutputStream(options) {
var ret;
print("{");
newline();
- with_indent(next_indent(), function(){
+ with_indent(next_indent(), function() {
ret = cont();
});
indent();
@@ -631,7 +631,7 @@ function OutputStream(options) {
/* -----[ code generators ]----- */
-(function(){
+(function() {
/* -----[ utils ]----- */
@@ -643,7 +643,7 @@ function OutputStream(options) {
var active_scope = null;
var use_asm = null;
- AST_Node.DEFMETHOD("print", function(stream, force_parens){
+ AST_Node.DEFMETHOD("print", function(stream, force_parens) {
var self = this, generator = self._codegen;
if (self instanceof AST_Scope) {
active_scope = self;
@@ -670,7 +670,7 @@ function OutputStream(options) {
});
AST_Node.DEFMETHOD("_print", AST_Node.prototype.print);
- AST_Node.DEFMETHOD("print_to_string", function(options){
+ AST_Node.DEFMETHOD("print_to_string", function(options) {
var s = OutputStream(options);
this.print(s);
return s.get();
@@ -680,7 +680,7 @@ function OutputStream(options) {
function PARENS(nodetype, func) {
if (Array.isArray(nodetype)) {
- nodetype.forEach(function(nodetype){
+ nodetype.forEach(function(nodetype) {
PARENS(nodetype, func);
});
} else {
@@ -692,7 +692,7 @@ function OutputStream(options) {
// a function expression needs parens around it when it's provably
// the first token to appear in a statement.
- PARENS(AST_Function, function(output){
+ PARENS(AST_Function, function(output) {
if (!output.has_parens() && first_in_statement(output)) {
return true;
}
@@ -714,17 +714,17 @@ function OutputStream(options) {
// same goes for an object literal, because otherwise it would be
// interpreted as a block of code.
- PARENS(AST_Object, function(output){
+ PARENS(AST_Object, function(output) {
return !output.has_parens() && first_in_statement(output);
});
- PARENS(AST_Unary, function(output){
+ PARENS(AST_Unary, function(output) {
var p = output.parent();
return p instanceof AST_PropAccess && p.expression === this
|| p instanceof AST_Call && p.expression === this;
});
- PARENS(AST_Sequence, function(output){
+ PARENS(AST_Sequence, function(output) {
var p = output.parent();
return p instanceof AST_Call // (foo, bar)() or foo(1, (2, 3), 4)
|| p instanceof AST_Unary // !(foo, bar, baz)
@@ -738,7 +738,7 @@ function OutputStream(options) {
;
});
- PARENS(AST_Binary, function(output){
+ PARENS(AST_Binary, function(output) {
var p = output.parent();
// (foo && bar)()
if (p instanceof AST_Call && p.expression === this)
@@ -761,7 +761,7 @@ function OutputStream(options) {
}
});
- PARENS(AST_PropAccess, function(output){
+ PARENS(AST_PropAccess, function(output) {
var p = output.parent();
if (p instanceof AST_New && p.expression === this) {
// i.e. new (foo.bar().baz)
@@ -782,7 +782,7 @@ function OutputStream(options) {
}
});
- PARENS(AST_Call, function(output){
+ PARENS(AST_Call, function(output) {
var p = output.parent(), p1;
if (p instanceof AST_New && p.expression === this)
return true;
@@ -796,7 +796,7 @@ function OutputStream(options) {
&& p1.left === p;
});
- PARENS(AST_New, function(output){
+ PARENS(AST_New, function(output) {
var p = output.parent();
if (!need_constructor_parens(this, output)
&& (p instanceof AST_PropAccess // (new Date).getTime(), (new Date)["getTime"]()
@@ -804,7 +804,7 @@ function OutputStream(options) {
return true;
});
- PARENS(AST_Number, function(output){
+ PARENS(AST_Number, function(output) {
var p = output.parent();
if (p instanceof AST_PropAccess && p.expression === this) {
var value = this.getValue();
@@ -814,7 +814,7 @@ function OutputStream(options) {
}
});
- PARENS([ AST_Assign, AST_Conditional ], function(output){
+ PARENS([ AST_Assign, AST_Conditional ], function(output) {
var p = output.parent();
// !(a = false) → true
if (p instanceof AST_Unary)
@@ -835,11 +835,11 @@ function OutputStream(options) {
/* -----[ PRINTERS ]----- */
- DEFPRINT(AST_Directive, function(self, output){
+ DEFPRINT(AST_Directive, function(self, output) {
output.print_string(self.value, self.quote);
output.semicolon();
});
- DEFPRINT(AST_Debugger, function(self, output){
+ DEFPRINT(AST_Debugger, function(self, output) {
output.print("debugger");
output.semicolon();
});
@@ -849,7 +849,7 @@ function OutputStream(options) {
function display_body(body, is_toplevel, output, allow_directives) {
var last = body.length - 1;
in_directive = allow_directives;
- body.forEach(function(stmt, i){
+ body.forEach(function(stmt, i) {
if (in_directive === true && !(stmt instanceof AST_Directive ||
stmt instanceof AST_EmptyStatement ||
(stmt instanceof AST_SimpleStatement && stmt.body instanceof AST_String)
@@ -874,24 +874,24 @@ function OutputStream(options) {
in_directive = false;
}
- AST_StatementWithBody.DEFMETHOD("_do_print_body", function(output){
+ AST_StatementWithBody.DEFMETHOD("_do_print_body", function(output) {
force_statement(this.body, output);
});
- DEFPRINT(AST_Statement, function(self, output){
+ DEFPRINT(AST_Statement, function(self, output) {
self.body.print(output);
output.semicolon();
});
- DEFPRINT(AST_Toplevel, function(self, output){
+ DEFPRINT(AST_Toplevel, function(self, output) {
display_body(self.body, true, output, true);
output.print("");
});
- DEFPRINT(AST_LabeledStatement, function(self, output){
+ DEFPRINT(AST_LabeledStatement, function(self, output) {
self.label.print(output);
output.colon();
self.body.print(output);
});
- DEFPRINT(AST_SimpleStatement, function(self, output){
+ DEFPRINT(AST_SimpleStatement, function(self, output) {
self.body.print(output);
output.semicolon();
});
@@ -909,37 +909,37 @@ function OutputStream(options) {
});
} else print_braced_empty(self, output);
}
- DEFPRINT(AST_BlockStatement, function(self, output){
+ DEFPRINT(AST_BlockStatement, function(self, output) {
print_braced(self, output);
});
- DEFPRINT(AST_EmptyStatement, function(self, output){
+ DEFPRINT(AST_EmptyStatement, function(self, output) {
output.semicolon();
});
- DEFPRINT(AST_Do, function(self, output){
+ DEFPRINT(AST_Do, function(self, output) {
output.print("do");
output.space();
make_block(self.body, output);
output.space();
output.print("while");
output.space();
- output.with_parens(function(){
+ output.with_parens(function() {
self.condition.print(output);
});
output.semicolon();
});
- DEFPRINT(AST_While, function(self, output){
+ DEFPRINT(AST_While, function(self, output) {
output.print("while");
output.space();
- output.with_parens(function(){
+ output.with_parens(function() {
self.condition.print(output);
});
output.space();
self._do_print_body(output);
});
- DEFPRINT(AST_For, function(self, output){
+ DEFPRINT(AST_For, function(self, output) {
output.print("for");
output.space();
- output.with_parens(function(){
+ output.with_parens(function() {
if (self.init) {
if (self.init instanceof AST_Definitions) {
self.init.print(output);
@@ -965,10 +965,10 @@ function OutputStream(options) {
output.space();
self._do_print_body(output);
});
- DEFPRINT(AST_ForIn, function(self, output){
+ DEFPRINT(AST_ForIn, function(self, output) {
output.print("for");
output.space();
- output.with_parens(function(){
+ output.with_parens(function() {
self.init.print(output);
output.space();
output.print("in");
@@ -978,10 +978,10 @@ function OutputStream(options) {
output.space();
self._do_print_body(output);
});
- DEFPRINT(AST_With, function(self, output){
+ DEFPRINT(AST_With, function(self, output) {
output.print("with");
output.space();
- output.with_parens(function(){
+ output.with_parens(function() {
self.expression.print(output);
});
output.space();
@@ -989,7 +989,7 @@ function OutputStream(options) {
});
/* -----[ functions ]----- */
- AST_Lambda.DEFMETHOD("_do_print", function(output, nokeyword){
+ AST_Lambda.DEFMETHOD("_do_print", function(output, nokeyword) {
var self = this;
if (!nokeyword) {
output.print("function");
@@ -998,8 +998,8 @@ function OutputStream(options) {
output.space();
self.name.print(output);
}
- output.with_parens(function(){
- self.argnames.forEach(function(arg, i){
+ output.with_parens(function() {
+ self.argnames.forEach(function(arg, i) {
if (i) output.comma();
arg.print(output);
});
@@ -1007,40 +1007,31 @@ function OutputStream(options) {
output.space();
print_braced(self, output, true);
});
- DEFPRINT(AST_Lambda, function(self, output){
+ DEFPRINT(AST_Lambda, function(self, output) {
self._do_print(output);
});
- /* -----[ exits ]----- */
- AST_Exit.DEFMETHOD("_do_print", function(output, kind){
+ /* -----[ jumps ]----- */
+ function print_jump(output, kind, target) {
output.print(kind);
- if (this.value) {
+ if (target) {
output.space();
- this.value.print(output);
+ target.print(output);
}
output.semicolon();
- });
- DEFPRINT(AST_Return, function(self, output){
- self._do_print(output, "return");
- });
- DEFPRINT(AST_Throw, function(self, output){
- self._do_print(output, "throw");
- });
+ }
- /* -----[ loop control ]----- */
- AST_LoopControl.DEFMETHOD("_do_print", function(output, kind){
- output.print(kind);
- if (this.label) {
- output.space();
- this.label.print(output);
- }
- output.semicolon();
+ DEFPRINT(AST_Return, function(self, output) {
+ print_jump(output, "return", self.value);
+ });
+ DEFPRINT(AST_Throw, function(self, output) {
+ print_jump(output, "throw", self.value);
});
- DEFPRINT(AST_Break, function(self, output){
- self._do_print(output, "break");
+ DEFPRINT(AST_Break, function(self, output) {
+ print_jump(output, "break", self.label);
});
- DEFPRINT(AST_Continue, function(self, output){
- self._do_print(output, "continue");
+ DEFPRINT(AST_Continue, function(self, output) {
+ print_jump(output, "continue", self.label);
});
/* -----[ if ]----- */
@@ -1072,10 +1063,10 @@ function OutputStream(options) {
}
force_statement(self.body, output);
}
- DEFPRINT(AST_If, function(self, output){
+ DEFPRINT(AST_If, function(self, output) {
output.print("if");
output.space();
- output.with_parens(function(){
+ output.with_parens(function() {
self.condition.print(output);
});
output.space();
@@ -1094,17 +1085,17 @@ function OutputStream(options) {
});
/* -----[ switch ]----- */
- DEFPRINT(AST_Switch, function(self, output){
+ DEFPRINT(AST_Switch, function(self, output) {
output.print("switch");
output.space();
- output.with_parens(function(){
+ output.with_parens(function() {
self.expression.print(output);
});
output.space();
var last = self.body.length - 1;
if (last < 0) print_braced_empty(self, output);
- else output.with_block(function(){
- self.body.forEach(function(branch, i){
+ else output.with_block(function() {
+ self.body.forEach(function(branch, i) {
output.indent(true);
branch.print(output);
if (i < last && branch.body.length > 0)
@@ -1112,19 +1103,19 @@ function OutputStream(options) {
});
});
});
- AST_SwitchBranch.DEFMETHOD("_do_print_body", function(output){
+ AST_SwitchBranch.DEFMETHOD("_do_print_body", function(output) {
output.newline();
- this.body.forEach(function(stmt){
+ this.body.forEach(function(stmt) {
output.indent();
stmt.print(output);
output.newline();
});
});
- DEFPRINT(AST_Default, function(self, output){
+ DEFPRINT(AST_Default, function(self, output) {
output.print("default:");
self._do_print_body(output);
});
- DEFPRINT(AST_Case, function(self, output){
+ DEFPRINT(AST_Case, function(self, output) {
output.print("case");
output.space();
self.expression.print(output);
@@ -1133,7 +1124,7 @@ function OutputStream(options) {
});
/* -----[ exceptions ]----- */
- DEFPRINT(AST_Try, function(self, output){
+ DEFPRINT(AST_Try, function(self, output) {
output.print("try");
output.space();
print_braced(self, output);
@@ -1146,37 +1137,30 @@ function OutputStream(options) {
self.bfinally.print(output);
}
});
- DEFPRINT(AST_Catch, function(self, output){
+ DEFPRINT(AST_Catch, function(self, output) {
output.print("catch");
output.space();
- output.with_parens(function(){
+ output.with_parens(function() {
self.argname.print(output);
});
output.space();
print_braced(self, output);
});
- DEFPRINT(AST_Finally, function(self, output){
+ DEFPRINT(AST_Finally, function(self, output) {
output.print("finally");
output.space();
print_braced(self, output);
});
- /* -----[ var/const ]----- */
- AST_Definitions.DEFMETHOD("_do_print", function(output, kind){
- output.print(kind);
+ DEFPRINT(AST_Var, function(self, output) {
+ output.print("var");
output.space();
- this.definitions.forEach(function(def, i){
+ self.definitions.forEach(function(def, i) {
if (i) output.comma();
def.print(output);
});
var p = output.parent();
- var in_for = p instanceof AST_For || p instanceof AST_ForIn;
- var avoid_semicolon = in_for && p.init === this;
- if (!avoid_semicolon)
- output.semicolon();
- });
- DEFPRINT(AST_Var, function(self, output){
- self._do_print(output, "var");
+ if (p.init !== self || !(p instanceof AST_For || p instanceof AST_ForIn)) output.semicolon();
});
function parenthesize_for_noin(node, output, noin) {
@@ -1193,7 +1177,7 @@ function OutputStream(options) {
node.print(output, parens);
}
- DEFPRINT(AST_VarDef, function(self, output){
+ DEFPRINT(AST_VarDef, function(self, output) {
self.name.print(output);
if (self.value) {
output.space();
@@ -1206,28 +1190,27 @@ function OutputStream(options) {
});
/* -----[ other expressions ]----- */
- DEFPRINT(AST_Call, function(self, output){
+ DEFPRINT(AST_Call, function(self, output) {
self.expression.print(output);
if (self instanceof AST_New && !need_constructor_parens(self, output))
return;
if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) {
output.add_mapping(self.start);
}
- output.with_parens(function(){
- self.args.forEach(function(expr, i){
+ output.with_parens(function() {
+ self.args.forEach(function(expr, i) {
if (i) output.comma();
expr.print(output);
});
});
});
- DEFPRINT(AST_New, function(self, output){
+ DEFPRINT(AST_New, function(self, output) {
output.print("new");
output.space();
AST_Call.prototype._codegen(self, output);
});
-
- AST_Sequence.DEFMETHOD("_do_print", function(output){
- this.expressions.forEach(function(node, index) {
+ DEFPRINT(AST_Sequence, function(self, output) {
+ self.expressions.forEach(function(node, index) {
if (index > 0) {
output.comma();
if (output.should_break()) {
@@ -1238,18 +1221,7 @@ function OutputStream(options) {
node.print(output);
});
});
- DEFPRINT(AST_Sequence, function(self, output){
- self._do_print(output);
- // var p = output.parent();
- // if (p instanceof AST_Statement) {
- // output.with_indent(output.next_indent(), function(){
- // self._do_print(output);
- // });
- // } else {
- // self._do_print(output);
- // }
- });
- DEFPRINT(AST_Dot, function(self, output){
+ DEFPRINT(AST_Dot, function(self, output) {
var expr = self.expression;
expr.print(output);
var prop = self.property;
@@ -1270,13 +1242,13 @@ function OutputStream(options) {
output.print_name(prop);
}
});
- DEFPRINT(AST_Sub, function(self, output){
+ DEFPRINT(AST_Sub, function(self, output) {
self.expression.print(output);
output.print("[");
self.property.print(output);
output.print("]");
});
- DEFPRINT(AST_UnaryPrefix, function(self, output){
+ DEFPRINT(AST_UnaryPrefix, function(self, output) {
var op = self.operator;
output.print(op);
if (/^[a-z]/i.test(op)
@@ -1287,11 +1259,11 @@ function OutputStream(options) {
}
self.expression.print(output);
});
- DEFPRINT(AST_UnaryPostfix, function(self, output){
+ DEFPRINT(AST_UnaryPostfix, function(self, output) {
self.expression.print(output);
output.print(self.operator);
});
- DEFPRINT(AST_Binary, function(self, output){
+ DEFPRINT(AST_Binary, function(self, output) {
var op = self.operator;
self.left.print(output);
if (op[0] == ">" /* ">>" ">>>" ">" ">=" */
@@ -1317,7 +1289,7 @@ function OutputStream(options) {
}
self.right.print(output);
});
- DEFPRINT(AST_Conditional, function(self, output){
+ DEFPRINT(AST_Conditional, function(self, output) {
self.condition.print(output);
output.space();
output.print("?");
@@ -1329,11 +1301,11 @@ function OutputStream(options) {
});
/* -----[ literals ]----- */
- DEFPRINT(AST_Array, function(self, output){
- output.with_square(function(){
+ DEFPRINT(AST_Array, function(self, output) {
+ output.with_square(function() {
var a = self.elements, len = a.length;
if (len > 0) output.space();
- a.forEach(function(exp, i){
+ a.forEach(function(exp, i) {
if (i) output.comma();
exp.print(output);
// If the final element is a hole, we need to make sure it
@@ -1345,9 +1317,9 @@ function OutputStream(options) {
if (len > 0) output.space();
});
});
- DEFPRINT(AST_Object, function(self, output){
- if (self.properties.length > 0) output.with_block(function(){
- self.properties.forEach(function(prop, i){
+ DEFPRINT(AST_Object, function(self, output) {
+ if (self.properties.length > 0) output.with_block(function() {
+ self.properties.forEach(function(prop, i) {
if (i) {
output.print(",");
output.newline();
@@ -1376,7 +1348,7 @@ function OutputStream(options) {
}
}
- DEFPRINT(AST_ObjectKeyVal, function(self, output){
+ DEFPRINT(AST_ObjectKeyVal, function(self, output) {
print_property_name(self.key, self.quote, output);
output.colon();
self.value.print(output);
@@ -1387,27 +1359,27 @@ function OutputStream(options) {
print_property_name(this.key.name, this.quote, output);
this.value._do_print(output, true);
});
- DEFPRINT(AST_ObjectSetter, function(self, output){
+ DEFPRINT(AST_ObjectSetter, function(self, output) {
self._print_getter_setter("set", output);
});
- DEFPRINT(AST_ObjectGetter, function(self, output){
+ DEFPRINT(AST_ObjectGetter, function(self, output) {
self._print_getter_setter("get", output);
});
- DEFPRINT(AST_Symbol, function(self, output){
+ DEFPRINT(AST_Symbol, function(self, output) {
var def = self.definition();
output.print_name(def ? def.mangled_name || def.name : self.name);
});
DEFPRINT(AST_Hole, noop);
- DEFPRINT(AST_This, function(self, output){
+ DEFPRINT(AST_This, function(self, output) {
output.print("this");
});
- DEFPRINT(AST_Constant, function(self, output){
+ DEFPRINT(AST_Constant, function(self, output) {
output.print(self.getValue());
});
- DEFPRINT(AST_String, function(self, output){
+ DEFPRINT(AST_String, function(self, output) {
output.print_string(self.getValue(), self.quote, in_directive);
});
- DEFPRINT(AST_Number, function(self, output){
+ DEFPRINT(AST_Number, function(self, output) {
if (use_asm && self.start && self.start.raw != null) {
output.print(self.start.raw);
} else {
@@ -1415,7 +1387,7 @@ function OutputStream(options) {
}
});
- DEFPRINT(AST_RegExp, function(self, output){
+ DEFPRINT(AST_RegExp, function(self, output) {
var regexp = self.getValue();
var str = regexp.toString();
if (regexp.raw_source) {
@@ -1487,7 +1459,7 @@ function OutputStream(options) {
output.print("{}");
else if (stmt instanceof AST_BlockStatement)
stmt.print(output);
- else output.with_block(function(){
+ else output.with_block(function() {
output.indent();
stmt.print(output);
output.newline();