Refactoring over!
This commit is contained in:
parent
d461f9d927
commit
ea892ef28c
|
|
@ -288,7 +288,7 @@ Cola.AST_Scope = Cola.DEFNODE("Scope", "directives variables functions uses_with
|
||||||
},
|
},
|
||||||
}, Cola.AST_Block);
|
}, Cola.AST_Block);
|
||||||
|
|
||||||
Cola.AST_Toplevel = Cola.DEFNODE("Toplevel", "globals", {
|
Cola.AST_Toplevel = Cola.DEFNODE("Toplevel", "globals language", {
|
||||||
$documentation: "The toplevel scope",
|
$documentation: "The toplevel scope",
|
||||||
$propdoc: {
|
$propdoc: {
|
||||||
globals: "[Object/S] a map of name -> SymbolDef for all undeclared names",
|
globals: "[Object/S] a map of name -> SymbolDef for all undeclared names",
|
||||||
|
|
@ -307,7 +307,7 @@ Cola.AST_Toplevel = Cola.DEFNODE("Toplevel", "globals", {
|
||||||
|
|
||||||
var wrapped_tl = "(function(" + parameters.join(",") + "){ '$ORIG'; })(" + args.join(",") + ")";
|
var wrapped_tl = "(function(" + parameters.join(",") + "){ '$ORIG'; })(" + args.join(",") + ")";
|
||||||
wrapped_tl = Cola.parse(wrapped_tl);
|
wrapped_tl = Cola.parse(wrapped_tl);
|
||||||
wrapped_tl = wrapped_tl.transform(new TreeTransformer(function before(node){
|
wrapped_tl = wrapped_tl.transform(new Cola.TreeTransformer(function before(node){
|
||||||
if (node instanceof Cola.AST_Directive && node.value == "$ORIG") {
|
if (node instanceof Cola.AST_Directive && node.value == "$ORIG") {
|
||||||
return Cola.MAP.splice(self.body);
|
return Cola.MAP.splice(self.body);
|
||||||
}
|
}
|
||||||
|
|
@ -328,7 +328,7 @@ Cola.AST_Toplevel = Cola.DEFNODE("Toplevel", "globals", {
|
||||||
}
|
}
|
||||||
var wrapped_tl = "(function(exports, global){ global['" + name + "'] = exports; '$ORIG'; '$EXPORTS'; }({}, (function(){return this}())))";
|
var wrapped_tl = "(function(exports, global){ global['" + name + "'] = exports; '$ORIG'; '$EXPORTS'; }({}, (function(){return this}())))";
|
||||||
wrapped_tl = Cola.parse(wrapped_tl);
|
wrapped_tl = Cola.parse(wrapped_tl);
|
||||||
wrapped_tl = wrapped_tl.transform(new TreeTransformer(function before(node){
|
wrapped_tl = wrapped_tl.transform(new Cola.TreeTransformer(function before(node){
|
||||||
if (node instanceof Cola.AST_SimpleStatement) {
|
if (node instanceof Cola.AST_SimpleStatement) {
|
||||||
node = node.body;
|
node = node.body;
|
||||||
if (node instanceof Cola.AST_String) switch (node.getValue()) {
|
if (node instanceof Cola.AST_String) switch (node.getValue()) {
|
||||||
|
|
|
||||||
|
|
@ -42,11 +42,12 @@
|
||||||
***********************************************************************/
|
***********************************************************************/
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
!window.Cola && (window.Cola = {});
|
||||||
|
|
||||||
function Compressor(options, false_by_default) {
|
Cola.Compressor = function (options, false_by_default) {
|
||||||
if (!(this instanceof Compressor))
|
if (!(this instanceof Cola.Compressor))
|
||||||
return new Compressor(options, false_by_default);
|
return new Cola.Compressor(options, false_by_default);
|
||||||
TreeTransformer.call(this, this.before, this.after);
|
Cola.TreeTransformer.call(this, this.before, this.after);
|
||||||
this.options = Cola.defaults(options, {
|
this.options = Cola.defaults(options, {
|
||||||
sequences : !false_by_default,
|
sequences : !false_by_default,
|
||||||
properties : !false_by_default,
|
properties : !false_by_default,
|
||||||
|
|
@ -81,8 +82,8 @@ function Compressor(options, false_by_default) {
|
||||||
}, true);
|
}, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
Compressor.prototype = new TreeTransformer;
|
Cola.Compressor.prototype = new Cola.TreeTransformer;
|
||||||
Cola.merge(Compressor.prototype, {
|
Cola.merge(Cola.Compressor.prototype, {
|
||||||
option: function(key) { return this.options[key] },
|
option: function(key) { return this.options[key] },
|
||||||
warn: function() {
|
warn: function() {
|
||||||
if (this.options.warnings)
|
if (this.options.warnings)
|
||||||
|
|
@ -139,7 +140,7 @@ Cola.merge(Compressor.prototype, {
|
||||||
|
|
||||||
function make_node_from_constant(compressor, val, orig) {
|
function make_node_from_constant(compressor, val, orig) {
|
||||||
// XXX: WIP.
|
// XXX: WIP.
|
||||||
// if (val instanceof Cola.AST_Node) return val.transform(new TreeTransformer(null, function(node){
|
// if (val instanceof Cola.AST_Node) return val.transform(new Cola.TreeTransformer(null, function(node){
|
||||||
// if (node instanceof Cola.AST_SymbolRef) {
|
// if (node instanceof Cola.AST_SymbolRef) {
|
||||||
// var scope = compressor.find_parent(Cola.AST_Scope);
|
// var scope = compressor.find_parent(Cola.AST_Scope);
|
||||||
// var def = scope.find_variable(node);
|
// var def = scope.find_variable(node);
|
||||||
|
|
@ -555,7 +556,7 @@ Cola.merge(Compressor.prototype, {
|
||||||
statements.forEach(function(stat){
|
statements.forEach(function(stat){
|
||||||
if (stat instanceof Cola.AST_SimpleStatement) {
|
if (stat instanceof Cola.AST_SimpleStatement) {
|
||||||
stat.body = (function transform(thing) {
|
stat.body = (function transform(thing) {
|
||||||
return thing.transform(new TreeTransformer(function(node){
|
return thing.transform(new Cola.TreeTransformer(function(node){
|
||||||
if (node instanceof Cola.AST_Call && node.expression instanceof Cola.AST_Function) {
|
if (node instanceof Cola.AST_Call && node.expression instanceof Cola.AST_Function) {
|
||||||
return make_node(Cola.AST_UnaryPrefix, node, {
|
return make_node(Cola.AST_UnaryPrefix, node, {
|
||||||
operator: "!",
|
operator: "!",
|
||||||
|
|
@ -1044,7 +1045,7 @@ Cola.merge(Compressor.prototype, {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
// pass 3: we should drop declarations not in_use
|
// pass 3: we should drop declarations not in_use
|
||||||
var tt = new TreeTransformer(
|
var tt = new Cola.TreeTransformer(
|
||||||
function before(node, descend, in_list) {
|
function before(node, descend, in_list) {
|
||||||
if (node instanceof Cola.AST_Lambda && !(node instanceof Cola.AST_Accessor)) {
|
if (node instanceof Cola.AST_Lambda && !(node instanceof Cola.AST_Accessor)) {
|
||||||
if (!compressor.option("keep_fargs")) {
|
if (!compressor.option("keep_fargs")) {
|
||||||
|
|
@ -1183,7 +1184,7 @@ Cola.merge(Compressor.prototype, {
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
hoist_vars = hoist_vars && var_decl > 1;
|
hoist_vars = hoist_vars && var_decl > 1;
|
||||||
var tt = new TreeTransformer(
|
var tt = new Cola.TreeTransformer(
|
||||||
function before(node) {
|
function before(node) {
|
||||||
if (node !== self) {
|
if (node !== self) {
|
||||||
if (node instanceof Cola.AST_Directive) {
|
if (node instanceof Cola.AST_Directive) {
|
||||||
|
|
@ -1555,7 +1556,7 @@ Cola.merge(Compressor.prototype, {
|
||||||
var started = false;
|
var started = false;
|
||||||
var stopped = false;
|
var stopped = false;
|
||||||
var ruined = false;
|
var ruined = false;
|
||||||
var tt = new TreeTransformer(function(node, descend, in_list){
|
var tt = new Cola.TreeTransformer(function(node, descend, in_list){
|
||||||
if (node instanceof Cola.AST_Lambda || node instanceof Cola.AST_SimpleStatement) {
|
if (node instanceof Cola.AST_Lambda || node instanceof Cola.AST_SimpleStatement) {
|
||||||
// no need to descend these node types
|
// no need to descend these node types
|
||||||
return node;
|
return node;
|
||||||
|
|
@ -1724,7 +1725,7 @@ Cola.merge(Compressor.prototype, {
|
||||||
}).join(",") + "){" + self.args[self.args.length - 1].value + "})()";
|
}).join(",") + "){" + self.args[self.args.length - 1].value + "})()";
|
||||||
var ast = Cola.parse(code);
|
var ast = Cola.parse(code);
|
||||||
ast.figure_out_scope({ screw_ie8: compressor.option("screw_ie8") });
|
ast.figure_out_scope({ screw_ie8: compressor.option("screw_ie8") });
|
||||||
var comp = new Compressor(compressor.options);
|
var comp = new Cola.Compressor(compressor.options);
|
||||||
ast = ast.transform(comp);
|
ast = ast.transform(comp);
|
||||||
ast.figure_out_scope({ screw_ie8: compressor.option("screw_ie8") });
|
ast.figure_out_scope({ screw_ie8: compressor.option("screw_ie8") });
|
||||||
ast.mangle_names();
|
ast.mangle_names();
|
||||||
|
|
@ -1744,7 +1745,7 @@ Cola.merge(Compressor.prototype, {
|
||||||
value: arg.print_to_string()
|
value: arg.print_to_string()
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
var code = OutputStream();
|
var code = new Cola.OutputStream();
|
||||||
Cola.AST_BlockStatement.prototype._codegen.call(fun, fun, code);
|
Cola.AST_BlockStatement.prototype._codegen.call(fun, fun, code);
|
||||||
code = code.toString().replace(/^\{|\}$/g, "");
|
code = code.toString().replace(/^\{|\}$/g, "");
|
||||||
args.push(make_node(Cola.AST_String, self.args[self.args.length - 1], {
|
args.push(make_node(Cola.AST_String, self.args[self.args.length - 1], {
|
||||||
|
|
|
||||||
|
|
@ -63,13 +63,13 @@
|
||||||
localStorage.source = source;
|
localStorage.source = source;
|
||||||
localStorage.isjs = isjs.checked ? "t" : "f";
|
localStorage.isjs = isjs.checked ? "t" : "f";
|
||||||
|
|
||||||
stream = OutputStream({ beautify : true, is_js : isjs.checked });
|
stream = new Cola.OutputStream({ beautify : true, is_js : isjs.checked });
|
||||||
compressor = Compressor({ is_js : isjs.checked });
|
compressor = Cola.Compressor({ is_js : isjs.checked });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. compile
|
// 1. compile
|
||||||
ast = Cola.parse(source, null, isjs.checked);
|
ast = Cola.parse(source, null, isjs.checked);
|
||||||
if(!isjs.checked) ast = translate(ast);
|
if(!isjs.checked) ast = ast.toJavaScript();
|
||||||
ast.print(stream);
|
ast.print(stream);
|
||||||
translationArea.value = stream.toString();
|
translationArea.value = stream.toString();
|
||||||
|
|
||||||
|
|
@ -82,7 +82,7 @@
|
||||||
ast.compute_char_frequency();
|
ast.compute_char_frequency();
|
||||||
ast.mangle_names({ is_js : isjs.checked, sort : true, toplevel : true });
|
ast.mangle_names({ is_js : isjs.checked, sort : true, toplevel : true });
|
||||||
|
|
||||||
stream = OutputStream();
|
stream = new Cola.OutputStream();
|
||||||
ast.print(stream);
|
ast.print(stream);
|
||||||
resultArea.value = stream.toString();
|
resultArea.value = stream.toString();
|
||||||
} catch(e){
|
} catch(e){
|
||||||
|
|
@ -98,7 +98,7 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function Translate(){
|
function Translate(){
|
||||||
stream = OutputStream({ beautify : true });
|
stream = new Cola.OutputStream({ beautify : true });
|
||||||
translate(Cola.parse(source, null, isjs.checked)).print(stream);
|
translate(Cola.parse(source, null, isjs.checked)).print(stream);
|
||||||
return stream.toString();
|
return stream.toString();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
303
lib/output.js
303
lib/output.js
|
|
@ -42,10 +42,11 @@
|
||||||
***********************************************************************/
|
***********************************************************************/
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
!window.Cola && (window.Cola = {});
|
||||||
|
|
||||||
function OutputStream(options) {
|
Cola.OutputStream = function (options) {
|
||||||
|
|
||||||
options = Cola.defaults(options, {
|
this.options = Cola.defaults(options, {
|
||||||
indent_start : 0,
|
indent_start : 0,
|
||||||
indent_level : 4,
|
indent_level : 4,
|
||||||
quote_keys : false,
|
quote_keys : false,
|
||||||
|
|
@ -66,13 +67,26 @@ function OutputStream(options) {
|
||||||
is_js : false
|
is_js : false
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
var indentation = 0;
|
this.indentation = 0;
|
||||||
var current_col = 0;
|
this.current_col = 0;
|
||||||
var current_line = 1;
|
this.current_line = 1;
|
||||||
var current_pos = 0;
|
this.current_pos = 0;
|
||||||
var OUTPUT = "";
|
this.OUTPUT = "";
|
||||||
|
|
||||||
function to_ascii(str, identifier) {
|
this.might_need_space = false;
|
||||||
|
this.might_need_semicolon = false;
|
||||||
|
this.last = null;
|
||||||
|
|
||||||
|
this.requireSemicolonChars = Cola.makePredicate("( [ + * / - , .");
|
||||||
|
|
||||||
|
if (this.options.preamble) {
|
||||||
|
this.print(this.options.preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
this.stack = [];
|
||||||
|
};
|
||||||
|
|
||||||
|
Cola.OutputStream.prototype.to_ascii = function (str, identifier) {
|
||||||
return str.replace(/[\u0080-\uffff]/g, function(ch) {
|
return str.replace(/[\u0080-\uffff]/g, function(ch) {
|
||||||
var code = ch.charCodeAt(0).toString(16);
|
var code = ch.charCodeAt(0).toString(16);
|
||||||
if (code.length <= 2 && !identifier) {
|
if (code.length <= 2 && !identifier) {
|
||||||
|
|
@ -85,7 +99,7 @@ function OutputStream(options) {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function make_string(str) {
|
Cola.OutputStream.prototype.make_string = function (str) {
|
||||||
var dq = 0, sq = 0;
|
var dq = 0, sq = 0;
|
||||||
str = str.replace(/[\\\b\f\n\r\t\x22\x27\u2028\u2029\0]/g, function(s){
|
str = str.replace(/[\\\b\f\n\r\t\x22\x27\u2028\u2029\0]/g, function(s){
|
||||||
switch (s) {
|
switch (s) {
|
||||||
|
|
@ -102,187 +116,181 @@ function OutputStream(options) {
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
});
|
});
|
||||||
if (options.ascii_only) str = to_ascii(str);
|
if (this.options.ascii_only) str = this.to_ascii(str);
|
||||||
if (dq > sq) return "'" + str.replace(/\x27/g, "\\'") + "'";
|
if (dq > sq) return "'" + str.replace(/\x27/g, "\\'") + "'";
|
||||||
else return '"' + str.replace(/\x22/g, '\\"') + '"';
|
else return '"' + str.replace(/\x22/g, '\\"') + '"';
|
||||||
};
|
};
|
||||||
|
|
||||||
function encode_string(str) {
|
Cola.OutputStream.prototype.encode_string = function (str) {
|
||||||
var ret = make_string(str);
|
var ret = this.make_string(str);
|
||||||
if (options.inline_script)
|
if (this.options.inline_script)
|
||||||
ret = ret.replace(/<\x2fscript([>\/\t\n\f\r ])/gi, "<\\/script$1");
|
ret = ret.replace(/<\x2fscript([>\/\t\n\f\r ])/gi, "<\\/script$1");
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
function make_name(name) {
|
Cola.OutputStream.prototype.make_name = function (name) {
|
||||||
name = name.toString();
|
name = name.toString();
|
||||||
if (options.ascii_only)
|
if (this.options.ascii_only)
|
||||||
name = to_ascii(name, true);
|
name = this.to_ascii(name, true);
|
||||||
return name;
|
return name;
|
||||||
};
|
};
|
||||||
|
|
||||||
function make_indent(back) {
|
Cola.OutputStream.prototype.make_indent = function (back) {
|
||||||
return Cola.repeat_string(" ", options.indent_start + indentation - back * options.indent_level);
|
return Cola.repeat_string(" ", this.options.indent_start + this.indentation - back * this.options.indent_level);
|
||||||
};
|
};
|
||||||
|
|
||||||
/* -----[ beautification/minification ]----- */
|
/* -----[ beautification/minification ]----- */
|
||||||
|
|
||||||
var might_need_space = false;
|
Cola.OutputStream.prototype.last_char = function () {
|
||||||
var might_need_semicolon = false;
|
return this.last.charAt(this.last.length - 1);
|
||||||
var last = null;
|
|
||||||
|
|
||||||
function last_char() {
|
|
||||||
return last.charAt(last.length - 1);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function maybe_newline() {
|
Cola.OutputStream.prototype.maybe_newline = function () {
|
||||||
if (options.max_line_len && current_col > options.max_line_len)
|
if (this.options.max_line_len && this.current_col > this.options.max_line_len)
|
||||||
print("\n");
|
this.print("\n");
|
||||||
};
|
};
|
||||||
|
|
||||||
var requireSemicolonChars = Cola.makePredicate("( [ + * / - , .");
|
Cola.OutputStream.prototype.print = function (str) {
|
||||||
|
|
||||||
function print(str) {
|
|
||||||
str = String(str);
|
str = String(str);
|
||||||
var ch = str.charAt(0);
|
var ch = str.charAt(0);
|
||||||
if (might_need_semicolon) {
|
if (this.might_need_semicolon) {
|
||||||
if ((!ch || ";}".indexOf(ch) < 0) && !/[;]$/.test(last)) {
|
if ((!ch || ";}".indexOf(ch) < 0) && !/[;]$/.test(this.last)) {
|
||||||
if (options.semicolons || requireSemicolonChars(ch)) {
|
if (this.options.semicolons || this.requireSemicolonChars(ch)) {
|
||||||
OUTPUT += ";";
|
this.OUTPUT += ";";
|
||||||
current_col++;
|
this.current_col++;
|
||||||
current_pos++;
|
this.current_pos++;
|
||||||
} else {
|
} else {
|
||||||
OUTPUT += "\n";
|
this.OUTPUT += "\n";
|
||||||
current_pos++;
|
this.current_pos++;
|
||||||
current_line++;
|
this.current_line++;
|
||||||
current_col = 0;
|
this.current_col = 0;
|
||||||
}
|
}
|
||||||
if (!options.beautify)
|
if (!this.options.beautify)
|
||||||
might_need_space = false;
|
this.might_need_space = false;
|
||||||
}
|
}
|
||||||
might_need_semicolon = false;
|
this.might_need_semicolon = false;
|
||||||
maybe_newline();
|
this.maybe_newline();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!options.beautify && options.preserve_line && stack[stack.length - 1]) {
|
if (!this.options.beautify && this.options.preserve_line && this.stack[stack.length - 1]) {
|
||||||
var target_line = stack[stack.length - 1].start.line;
|
var target_line = this.stack[stack.length - 1].start.line;
|
||||||
while (current_line < target_line) {
|
while (this.current_line < target_line) {
|
||||||
OUTPUT += "\n";
|
this.OUTPUT += "\n";
|
||||||
current_pos++;
|
this.current_pos++;
|
||||||
current_line++;
|
this.current_line++;
|
||||||
current_col = 0;
|
this.current_col = 0;
|
||||||
might_need_space = false;
|
this.might_need_space = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (might_need_space) {
|
if (this.might_need_space) {
|
||||||
var prev = last_char();
|
var prev = this.last_char();
|
||||||
if ((Cola.is_identifier_char(prev)
|
if ((Cola.is_identifier_char(prev)
|
||||||
&& (Cola.is_identifier_char(ch) || ch == "\\"))
|
&& (Cola.is_identifier_char(ch) || ch == "\\"))
|
||||||
|| (/^[\+\-\/]$/.test(ch) && ch == prev))
|
|| (/^[\+\-\/]$/.test(ch) && ch == prev))
|
||||||
{
|
{
|
||||||
OUTPUT += " ";
|
this.OUTPUT += " ";
|
||||||
current_col++;
|
this.current_col++;
|
||||||
current_pos++;
|
this.current_pos++;
|
||||||
}
|
}
|
||||||
might_need_space = false;
|
this.might_need_space = false;
|
||||||
}
|
}
|
||||||
var a = str.split(/\r?\n/), n = a.length - 1;
|
var a = str.split(/\r?\n/), n = a.length - 1;
|
||||||
current_line += n;
|
this.current_line += n;
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
current_col += a[n].length;
|
this.current_col += a[n].length;
|
||||||
} else {
|
} else {
|
||||||
current_col = a[n].length;
|
this.current_col = a[n].length;
|
||||||
}
|
}
|
||||||
current_pos += str.length;
|
this.current_pos += str.length;
|
||||||
last = str;
|
this.last = str;
|
||||||
OUTPUT += str;
|
this.OUTPUT += str;
|
||||||
};
|
};
|
||||||
|
|
||||||
var space = options.beautify ? function() {
|
Cola.OutputStream.prototype.space = function () {
|
||||||
print(" ");
|
this.options.beautify ? this.print(" ") : this.might_need_space = true;
|
||||||
} : function() {
|
|
||||||
might_need_space = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
var indent = options.beautify ? function(half) {
|
|
||||||
if (options.beautify) {
|
|
||||||
print(make_indent(half ? 0.5 : 0));
|
|
||||||
}
|
}
|
||||||
} : Cola.noop;
|
|
||||||
|
|
||||||
var with_indent = options.beautify ? function(col, cont) {
|
Cola.OutputStream.prototype.indent = function (half) {
|
||||||
if (col === true) col = next_indent();
|
if (this.options.beautify) {
|
||||||
var save_indentation = indentation;
|
this.print(this.make_indent(half ? 0.5 : 0));
|
||||||
indentation = col;
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Cola.OutputStream.prototype.with_indent = function (col, cont) {
|
||||||
|
if(!this.options.beautify) return cont();
|
||||||
|
|
||||||
|
if (col === true) col = this.next_indent();
|
||||||
|
var save_indentation = this.indentation;
|
||||||
|
this.indentation = col;
|
||||||
var ret = cont();
|
var ret = cont();
|
||||||
indentation = save_indentation;
|
this.indentation = save_indentation;
|
||||||
return ret;
|
return ret;
|
||||||
} : function(col, cont) { return cont() };
|
}
|
||||||
|
|
||||||
var newline = options.beautify ? function() {
|
Cola.OutputStream.prototype.newline = function () {
|
||||||
print("\n");
|
if (this.options.beautify) this.print("\n");
|
||||||
} : Cola.noop;
|
}
|
||||||
|
|
||||||
var semicolon = options.beautify ? function() {
|
Cola.OutputStream.prototype.semicolon = function () {
|
||||||
print(";");
|
this.options.beautify ? this.print(";") : this.might_need_semicolon = true;
|
||||||
} : function() {
|
}
|
||||||
might_need_semicolon = true;
|
|
||||||
|
Cola.OutputStream.prototype.force_semicolon = function () {
|
||||||
|
this.might_need_semicolon = false;
|
||||||
|
this.print(";");
|
||||||
};
|
};
|
||||||
|
|
||||||
function force_semicolon() {
|
Cola.OutputStream.prototype.next_indent = function () {
|
||||||
might_need_semicolon = false;
|
return this.indentation + this.options.indent_level;
|
||||||
print(";");
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function next_indent() {
|
Cola.OutputStream.prototype.with_block = function (cont) {
|
||||||
return indentation + options.indent_level;
|
|
||||||
};
|
|
||||||
|
|
||||||
function with_block(cont) {
|
|
||||||
var ret;
|
var ret;
|
||||||
print("{");
|
this.print("{");
|
||||||
newline();
|
this.newline();
|
||||||
with_indent(next_indent(), function(){
|
this.with_indent(this.next_indent(), function(){
|
||||||
ret = cont();
|
ret = cont();
|
||||||
});
|
});
|
||||||
indent();
|
this.indent();
|
||||||
print("}");
|
this.print("}");
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
function with_parens(cont) {
|
Cola.OutputStream.prototype.with_parens = function (cont) {
|
||||||
print("(");
|
this.print("(");
|
||||||
//XXX: still nice to have that for argument lists
|
//XXX: still nice to have that for argument lists
|
||||||
//var ret = with_indent(current_col, cont);
|
//var ret = this.with_indent(this.current_col, cont);
|
||||||
var ret = cont();
|
var ret = cont();
|
||||||
print(")");
|
this.print(")");
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
function with_square(cont) {
|
Cola.OutputStream.prototype.with_square = function (cont) {
|
||||||
print("[");
|
this.print("[");
|
||||||
//var ret = with_indent(current_col, cont);
|
//var ret = this.with_indent(this.current_col, cont);
|
||||||
var ret = cont();
|
var ret = cont();
|
||||||
print("]");
|
this.print("]");
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
function comma() {
|
Cola.OutputStream.prototype.comma = function () {
|
||||||
print(",");
|
this.print(",");
|
||||||
space();
|
this.space();
|
||||||
};
|
};
|
||||||
|
|
||||||
function colon() {
|
Cola.OutputStream.prototype.colon = function () {
|
||||||
print(":");
|
this.print(":");
|
||||||
if (options.space_colon) space();
|
if (this.options.space_colon) this.space();
|
||||||
};
|
};
|
||||||
|
|
||||||
var add_mapping = options.source_map ? function(token, name) {
|
Cola.OutputStream.prototype.add_mapping = function (token, name) {
|
||||||
|
if(!this.options.source_map) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (token) options.source_map.add(
|
if (token) this.options.source_map.add(
|
||||||
token.file || "?",
|
token.file || "?",
|
||||||
current_line, current_col,
|
this.current_line, this.current_col,
|
||||||
token.line, token.col,
|
token.line, token.col,
|
||||||
(!name && token.type == "name") ? token.value : name
|
(!name && token.type == "name") ? token.value : name
|
||||||
);
|
);
|
||||||
|
|
@ -291,58 +299,27 @@ function OutputStream(options) {
|
||||||
file: token.file,
|
file: token.file,
|
||||||
line: token.line,
|
line: token.line,
|
||||||
col: token.col,
|
col: token.col,
|
||||||
cline: current_line,
|
cline: this.current_line,
|
||||||
ccol: current_col,
|
ccol: this.current_col,
|
||||||
name: name || ""
|
name: name || ""
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} : Cola.noop;
|
|
||||||
|
|
||||||
function get() {
|
|
||||||
return OUTPUT;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (options.preamble) {
|
|
||||||
print(options.preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var stack = [];
|
Cola.OutputStream.prototype.get = function () {
|
||||||
return {
|
return this.OUTPUT;
|
||||||
get : get,
|
|
||||||
toString : get,
|
|
||||||
indent : indent,
|
|
||||||
indentation : function() { return indentation },
|
|
||||||
current_width : function() { return current_col - indentation },
|
|
||||||
should_break : function() { return options.width && this.current_width() >= options.width },
|
|
||||||
newline : newline,
|
|
||||||
print : print,
|
|
||||||
space : space,
|
|
||||||
comma : comma,
|
|
||||||
colon : colon,
|
|
||||||
last : function() { return last },
|
|
||||||
semicolon : semicolon,
|
|
||||||
force_semicolon : force_semicolon,
|
|
||||||
to_ascii : to_ascii,
|
|
||||||
print_name : function(name) { print(make_name(name)) },
|
|
||||||
print_string : function(str) { print(encode_string(str)) },
|
|
||||||
next_indent : next_indent,
|
|
||||||
with_indent : with_indent,
|
|
||||||
with_block : with_block,
|
|
||||||
with_parens : with_parens,
|
|
||||||
with_square : with_square,
|
|
||||||
add_mapping : add_mapping,
|
|
||||||
option : function(opt) { return options[opt] },
|
|
||||||
line : function() { return current_line },
|
|
||||||
col : function() { return current_col },
|
|
||||||
pos : function() { return current_pos },
|
|
||||||
push_node : function(node) { stack.push(node) },
|
|
||||||
pop_node : function() { return stack.pop() },
|
|
||||||
stack : function() { return stack },
|
|
||||||
parent : function(n) {
|
|
||||||
return stack[stack.length - 2 - (n || 0)];
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Cola.OutputStream.prototype.toString = function() { return this.OUTPUT };
|
||||||
|
Cola.OutputStream.prototype.current_width = function() { return this.current_col - this.indentation };
|
||||||
|
Cola.OutputStream.prototype.should_break = function() { return this.options.width && this.current_width() >= this.options.width };
|
||||||
|
Cola.OutputStream.prototype.print_name = function(name) { this.print(this.make_name(name)) };
|
||||||
|
Cola.OutputStream.prototype.print_string = function(str) { this.print(this.encode_string(str)) };
|
||||||
|
Cola.OutputStream.prototype.option = function(opt) { return this.options[opt] };
|
||||||
|
Cola.OutputStream.prototype.push_node = function(node) { this.stack.push(node) };
|
||||||
|
Cola.OutputStream.prototype.pop_node = function() { return this.stack.pop() };
|
||||||
|
Cola.OutputStream.prototype.parent = function(n) {
|
||||||
|
return this.stack[this.stack.length - 2 - (n || 0)];
|
||||||
};
|
};
|
||||||
|
|
||||||
/* -----[ code generators ]----- */
|
/* -----[ code generators ]----- */
|
||||||
|
|
@ -372,7 +349,7 @@ function OutputStream(options) {
|
||||||
});
|
});
|
||||||
|
|
||||||
Cola.AST_Node.DEFMETHOD("print_to_string", function(options){
|
Cola.AST_Node.DEFMETHOD("print_to_string", function(options){
|
||||||
var s = OutputStream(options);
|
var s = new Cola.OutputStream(options);
|
||||||
this.print(s);
|
this.print(s);
|
||||||
return s.get();
|
return s.get();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
79
lib/scope.js
79
lib/scope.js
|
|
@ -42,8 +42,9 @@
|
||||||
***********************************************************************/
|
***********************************************************************/
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
!window.Cola && (window.Cola = {});
|
||||||
|
|
||||||
function SymbolDef(scope, index, orig) {
|
Cola.SymbolDef = function (scope, index, orig) {
|
||||||
this.name = orig.name;
|
this.name = orig.name;
|
||||||
this.orig = [ orig ];
|
this.orig = [ orig ];
|
||||||
this.scope = scope;
|
this.scope = scope;
|
||||||
|
|
@ -55,7 +56,7 @@ function SymbolDef(scope, index, orig) {
|
||||||
this.index = index;
|
this.index = index;
|
||||||
};
|
};
|
||||||
|
|
||||||
SymbolDef.prototype = {
|
Cola.SymbolDef.prototype = {
|
||||||
unmangleable: function(options) {
|
unmangleable: function(options) {
|
||||||
return (this.global && !(options && options.toplevel))
|
return (this.global && !(options && options.toplevel))
|
||||||
|| this.undeclared
|
|| this.undeclared
|
||||||
|
|
@ -157,7 +158,7 @@ Cola.AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
|
||||||
if (globals.has(name)) {
|
if (globals.has(name)) {
|
||||||
g = globals.get(name);
|
g = globals.get(name);
|
||||||
} else {
|
} else {
|
||||||
g = new SymbolDef(self, globals.size(), node);
|
g = new Cola.SymbolDef(self, globals.size(), node);
|
||||||
g.undeclared = true;
|
g.undeclared = true;
|
||||||
g.global = true;
|
g.global = true;
|
||||||
globals.set(name, g);
|
globals.set(name, g);
|
||||||
|
|
@ -231,7 +232,7 @@ Cola.AST_Scope.DEFMETHOD("def_function", function(symbol){
|
||||||
Cola.AST_Scope.DEFMETHOD("def_variable", function(symbol){
|
Cola.AST_Scope.DEFMETHOD("def_variable", function(symbol){
|
||||||
var def;
|
var def;
|
||||||
if (!this.variables.has(symbol.name)) {
|
if (!this.variables.has(symbol.name)) {
|
||||||
def = new SymbolDef(this, this.variables.size(), symbol);
|
def = new Cola.SymbolDef(this, this.variables.size(), symbol);
|
||||||
this.variables.set(symbol.name, def);
|
this.variables.set(symbol.name, def);
|
||||||
def.global = !this.parent_scope;
|
def.global = !this.parent_scope;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -244,7 +245,7 @@ Cola.AST_Scope.DEFMETHOD("def_variable", function(symbol){
|
||||||
Cola.AST_Scope.DEFMETHOD("next_mangled", function(options){
|
Cola.AST_Scope.DEFMETHOD("next_mangled", function(options){
|
||||||
var ext = this.enclosed;
|
var ext = this.enclosed;
|
||||||
out: while (true) {
|
out: while (true) {
|
||||||
var m = base54(++this.cname);
|
var m = Cola.base54(++this.cname);
|
||||||
if (!Cola.is_identifier(m, options.is_js)) continue; // skip over "do"
|
if (!Cola.is_identifier(m, options.is_js)) continue; // skip over "do"
|
||||||
|
|
||||||
// https://github.com/mishoo/UglifyJS2/issues/242 -- do not
|
// https://github.com/mishoo/UglifyJS2/issues/242 -- do not
|
||||||
|
|
@ -362,7 +363,7 @@ Cola.AST_Toplevel.DEFMETHOD("mangle_names", function(options){
|
||||||
}
|
}
|
||||||
if (node instanceof Cola.AST_Label) {
|
if (node instanceof Cola.AST_Label) {
|
||||||
var name;
|
var name;
|
||||||
do name = base54(++lname); while (!Cola.is_identifier(name, options.is_js));
|
do name = Cola.base54(++lname); while (!Cola.is_identifier(name, options.is_js));
|
||||||
node.mangled_name = name;
|
node.mangled_name = name;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -379,73 +380,73 @@ Cola.AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options){
|
||||||
options = this._default_mangler_options(options);
|
options = this._default_mangler_options(options);
|
||||||
var tw = new Cola.TreeWalker(function(node){
|
var tw = new Cola.TreeWalker(function(node){
|
||||||
if (node instanceof Cola.AST_Constant)
|
if (node instanceof Cola.AST_Constant)
|
||||||
base54.consider(node.print_to_string());
|
Cola.base54.consider(node.print_to_string());
|
||||||
else if (node instanceof Cola.AST_Return)
|
else if (node instanceof Cola.AST_Return)
|
||||||
base54.consider("return");
|
Cola.base54.consider("return");
|
||||||
else if (node instanceof Cola.AST_Throw)
|
else if (node instanceof Cola.AST_Throw)
|
||||||
base54.consider("throw");
|
Cola.base54.consider("throw");
|
||||||
else if (node instanceof Cola.AST_Continue)
|
else if (node instanceof Cola.AST_Continue)
|
||||||
base54.consider("continue");
|
Cola.base54.consider("continue");
|
||||||
else if (node instanceof Cola.AST_Break)
|
else if (node instanceof Cola.AST_Break)
|
||||||
base54.consider("break");
|
Cola.base54.consider("break");
|
||||||
else if (node instanceof Cola.AST_Debugger)
|
else if (node instanceof Cola.AST_Debugger)
|
||||||
base54.consider("debugger");
|
Cola.base54.consider("debugger");
|
||||||
else if (node instanceof Cola.AST_Directive)
|
else if (node instanceof Cola.AST_Directive)
|
||||||
base54.consider(node.value);
|
Cola.base54.consider(node.value);
|
||||||
else if (node instanceof Cola.AST_While)
|
else if (node instanceof Cola.AST_While)
|
||||||
base54.consider("while");
|
Cola.base54.consider("while");
|
||||||
else if (node instanceof Cola.AST_Do)
|
else if (node instanceof Cola.AST_Do)
|
||||||
base54.consider("do while");
|
Cola.base54.consider("do while");
|
||||||
else if (node instanceof Cola.AST_If) {
|
else if (node instanceof Cola.AST_If) {
|
||||||
base54.consider("if");
|
Cola.base54.consider("if");
|
||||||
if (node.alternative) base54.consider("else");
|
if (node.alternative) Cola.base54.consider("else");
|
||||||
}
|
}
|
||||||
else if (node instanceof Cola.AST_Var)
|
else if (node instanceof Cola.AST_Var)
|
||||||
base54.consider("var");
|
Cola.base54.consider("var");
|
||||||
else if (node instanceof Cola.AST_Const)
|
else if (node instanceof Cola.AST_Const)
|
||||||
base54.consider("const");
|
Cola.base54.consider("const");
|
||||||
else if (node instanceof Cola.AST_Lambda)
|
else if (node instanceof Cola.AST_Lambda)
|
||||||
base54.consider("function");
|
Cola.base54.consider("function");
|
||||||
else if (node instanceof Cola.AST_For)
|
else if (node instanceof Cola.AST_For)
|
||||||
base54.consider("for");
|
Cola.base54.consider("for");
|
||||||
else if (node instanceof Cola.AST_ForIn)
|
else if (node instanceof Cola.AST_ForIn)
|
||||||
base54.consider("for in");
|
Cola.base54.consider("for in");
|
||||||
else if (node instanceof Cola.AST_Switch)
|
else if (node instanceof Cola.AST_Switch)
|
||||||
base54.consider("switch");
|
Cola.base54.consider("switch");
|
||||||
else if (node instanceof Cola.AST_Case)
|
else if (node instanceof Cola.AST_Case)
|
||||||
base54.consider("case");
|
Cola.base54.consider("case");
|
||||||
else if (node instanceof Cola.AST_Default)
|
else if (node instanceof Cola.AST_Default)
|
||||||
base54.consider("default");
|
Cola.base54.consider("default");
|
||||||
else if (node instanceof Cola.AST_With)
|
else if (node instanceof Cola.AST_With)
|
||||||
base54.consider("with");
|
Cola.base54.consider("with");
|
||||||
else if (node instanceof Cola.AST_ObjectSetter)
|
else if (node instanceof Cola.AST_ObjectSetter)
|
||||||
base54.consider("set" + node.key);
|
Cola.base54.consider("set" + node.key);
|
||||||
else if (node instanceof Cola.AST_ObjectGetter)
|
else if (node instanceof Cola.AST_ObjectGetter)
|
||||||
base54.consider("get" + node.key);
|
Cola.base54.consider("get" + node.key);
|
||||||
else if (node instanceof Cola.AST_ObjectKeyVal)
|
else if (node instanceof Cola.AST_ObjectKeyVal)
|
||||||
base54.consider(node.key);
|
Cola.base54.consider(node.key);
|
||||||
else if (node instanceof Cola.AST_New)
|
else if (node instanceof Cola.AST_New)
|
||||||
base54.consider("new");
|
Cola.base54.consider("new");
|
||||||
else if (node instanceof Cola.AST_This)
|
else if (node instanceof Cola.AST_This)
|
||||||
base54.consider("this");
|
Cola.base54.consider("this");
|
||||||
else if (node instanceof Cola.AST_Try)
|
else if (node instanceof Cola.AST_Try)
|
||||||
base54.consider("try");
|
Cola.base54.consider("try");
|
||||||
else if (node instanceof Cola.AST_Catch)
|
else if (node instanceof Cola.AST_Catch)
|
||||||
base54.consider("catch");
|
Cola.base54.consider("catch");
|
||||||
else if (node instanceof Cola.AST_Finally)
|
else if (node instanceof Cola.AST_Finally)
|
||||||
base54.consider("finally");
|
Cola.base54.consider("finally");
|
||||||
else if (node instanceof Cola.AST_Symbol && node.unmangleable(options))
|
else if (node instanceof Cola.AST_Symbol && node.unmangleable(options))
|
||||||
base54.consider(node.name);
|
Cola.base54.consider(node.name);
|
||||||
else if (node instanceof Cola.AST_Unary || node instanceof Cola.AST_Binary)
|
else if (node instanceof Cola.AST_Unary || node instanceof Cola.AST_Binary)
|
||||||
base54.consider(node.operator);
|
Cola.base54.consider(node.operator);
|
||||||
else if (node instanceof Cola.AST_Dot)
|
else if (node instanceof Cola.AST_Dot)
|
||||||
base54.consider(node.property);
|
Cola.base54.consider(node.property);
|
||||||
});
|
});
|
||||||
this.walk(tw);
|
this.walk(tw);
|
||||||
base54.sort();
|
Cola.base54.sort();
|
||||||
});
|
});
|
||||||
|
|
||||||
var base54 = (function() {
|
Cola.base54 = (function() {
|
||||||
var string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789";
|
var string = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789";
|
||||||
var chars, frequency;
|
var chars, frequency;
|
||||||
function reset() {
|
function reset() {
|
||||||
|
|
|
||||||
|
|
@ -42,10 +42,11 @@
|
||||||
***********************************************************************/
|
***********************************************************************/
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
!window.Cola && (window.Cola = {});
|
||||||
|
|
||||||
// a small wrapper around fitzgen's source-map library
|
// a small wrapper around fitzgen's source-map library
|
||||||
function SourceMap(options) {
|
Cola.SourceMap = function (options) {
|
||||||
options = Cola.defaults(options, {
|
this.options = Cola.defaults(options, {
|
||||||
file : null,
|
file : null,
|
||||||
root : null,
|
root : null,
|
||||||
orig : null,
|
orig : null,
|
||||||
|
|
@ -53,14 +54,16 @@ function SourceMap(options) {
|
||||||
orig_line_diff : 0,
|
orig_line_diff : 0,
|
||||||
dest_line_diff : 0,
|
dest_line_diff : 0,
|
||||||
});
|
});
|
||||||
var generator = new MOZ_SourceMap.SourceMapGenerator({
|
this.generator = new MOZ_SourceMap.SourceMapGenerator({
|
||||||
file : options.file,
|
file : this.options.file,
|
||||||
sourceRoot : options.root
|
sourceRoot : this.options.root
|
||||||
});
|
});
|
||||||
var orig_map = options.orig && new MOZ_SourceMap.SourceMapConsumer(options.orig);
|
this.orig_map = this.options.orig && new MOZ_SourceMap.SourceMapConsumer(this.options.orig);
|
||||||
function add(source, gen_line, gen_col, orig_line, orig_col, name) {
|
}
|
||||||
if (orig_map) {
|
|
||||||
var info = orig_map.originalPositionFor({
|
Cola.SourceMap.prototype.add = function (source, gen_line, gen_col, orig_line, orig_col, name) {
|
||||||
|
if (this.orig_map) {
|
||||||
|
var info = this.orig_map.originalPositionFor({
|
||||||
line: orig_line,
|
line: orig_line,
|
||||||
column: orig_col
|
column: orig_col
|
||||||
});
|
});
|
||||||
|
|
@ -72,16 +75,13 @@ function SourceMap(options) {
|
||||||
orig_col = info.column;
|
orig_col = info.column;
|
||||||
name = info.name;
|
name = info.name;
|
||||||
}
|
}
|
||||||
generator.addMapping({
|
this.generator.addMapping({
|
||||||
generated : { line: gen_line + options.dest_line_diff, column: gen_col },
|
generated : { line: gen_line + this.options.dest_line_diff, column: gen_col },
|
||||||
original : { line: orig_line + options.orig_line_diff, column: orig_col },
|
original : { line: orig_line + this.options.orig_line_diff, column: orig_col },
|
||||||
source : source,
|
source : source,
|
||||||
name : name
|
name : name
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
return {
|
|
||||||
add : add,
|
Cola.SourceMap.prototype.get = function() { return this.generator };
|
||||||
get : function() { return generator },
|
Cola.SourceMap.prototype.toString = function() { return this.generator.toString() };
|
||||||
toString : function() { return generator.toString() }
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
|
||||||
|
|
@ -42,15 +42,16 @@
|
||||||
***********************************************************************/
|
***********************************************************************/
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
!window.Cola && (window.Cola = {});
|
||||||
|
|
||||||
// Tree transformer helpers.
|
// Tree transformer helpers.
|
||||||
|
|
||||||
function TreeTransformer(before, after) {
|
Cola.TreeTransformer = function (before, after) {
|
||||||
Cola.TreeWalker.call(this);
|
Cola.TreeWalker.call(this);
|
||||||
this.before = before;
|
this.before = before;
|
||||||
this.after = after;
|
this.after = after;
|
||||||
}
|
}
|
||||||
TreeTransformer.prototype = new Cola.TreeWalker;
|
Cola.TreeTransformer.prototype = new Cola.TreeWalker;
|
||||||
|
|
||||||
(function(undefined){
|
(function(undefined){
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,10 +35,13 @@
|
||||||
***********************************************************************/
|
***********************************************************************/
|
||||||
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
!window.Cola && (window.Cola = {});
|
||||||
|
|
||||||
function translate(tree){
|
Cola.AST_Toplevel.prototype.toJavaScript = function(){
|
||||||
|
if(this.language == 'js') return this;
|
||||||
|
this.language = 'js';
|
||||||
|
|
||||||
var tt = new TreeTransformer(null, function(node){
|
var tt = new Cola.TreeTransformer(null, function(node){
|
||||||
var newNode, props;
|
var newNode, props;
|
||||||
|
|
||||||
if(node instanceof Cola.AST_Binary && node.operator == '**'){
|
if(node instanceof Cola.AST_Binary && node.operator == '**'){
|
||||||
|
|
@ -174,5 +177,5 @@ function translate(tree){
|
||||||
return node;
|
return node;
|
||||||
});
|
});
|
||||||
|
|
||||||
return tree.transform(tt);
|
return this.transform(tt);
|
||||||
}
|
};
|
||||||
Loading…
Reference in New Issue
Block a user