parse.js refectory is done
This commit is contained in:
parent
db5ea25e30
commit
d461f9d927
|
|
@ -75,7 +75,9 @@ function Compressor(options, false_by_default) {
|
||||||
angular : false,
|
angular : false,
|
||||||
|
|
||||||
warnings : true,
|
warnings : true,
|
||||||
global_defs : {}
|
global_defs : {},
|
||||||
|
|
||||||
|
is_js : false
|
||||||
}, true);
|
}, true);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -1996,7 +1998,7 @@ Cola.merge(Compressor.prototype, {
|
||||||
// result. hence, force switch.
|
// result. hence, force switch.
|
||||||
|
|
||||||
if (!(self.left instanceof Cola.AST_Binary
|
if (!(self.left instanceof Cola.AST_Binary
|
||||||
&& Cola.PRECEDENCE[self.left.operator] >= Cola.PRECEDENCE[self.operator])) {
|
&& (compressor.option("is_js") ? Cola.PRECEDENCE : Cola.cPRECEDENCE)[self.left.operator] >= (compressor.option("is_js") ? Cola.PRECEDENCE : Cola.cPRECEDENCE)[self.operator])) {
|
||||||
reverse(null, true);
|
reverse(null, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2345,7 +2347,7 @@ Cola.merge(Compressor.prototype, {
|
||||||
var prop = self.property;
|
var prop = self.property;
|
||||||
if (prop instanceof Cola.AST_String && compressor.option("properties")) {
|
if (prop instanceof Cola.AST_String && compressor.option("properties")) {
|
||||||
prop = prop.getValue();
|
prop = prop.getValue();
|
||||||
if (Cola.RESERVED_WORDS(prop) ? compressor.option("screw_ie8") : Cola.is_identifier_string(prop)) {
|
if ((compressor.option("is_js") ? Cola.RESERVED_WORDS : Cola.cRESERVED_WORDS)(prop) ? compressor.option("screw_ie8") : Cola.is_identifier_string(prop)) {
|
||||||
return make_node(Cola.AST_Dot, self, {
|
return make_node(Cola.AST_Dot, self, {
|
||||||
expression : self.expression,
|
expression : self.expression,
|
||||||
property : prop
|
property : prop
|
||||||
|
|
|
||||||
|
|
@ -63,8 +63,8 @@
|
||||||
localStorage.source = source;
|
localStorage.source = source;
|
||||||
localStorage.isjs = isjs.checked ? "t" : "f";
|
localStorage.isjs = isjs.checked ? "t" : "f";
|
||||||
|
|
||||||
stream = OutputStream({ beautify : true });
|
stream = OutputStream({ beautify : true, is_js : isjs.checked });
|
||||||
compressor = Compressor();
|
compressor = Compressor({ is_js : isjs.checked });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// 1. compile
|
// 1. compile
|
||||||
|
|
@ -80,7 +80,7 @@
|
||||||
// 3. mangle
|
// 3. mangle
|
||||||
ast.figure_out_scope();
|
ast.figure_out_scope();
|
||||||
ast.compute_char_frequency();
|
ast.compute_char_frequency();
|
||||||
ast.mangle_names({ sort : true, toplevel : true });
|
ast.mangle_names({ is_js : isjs.checked, sort : true, toplevel : true });
|
||||||
|
|
||||||
stream = OutputStream();
|
stream = OutputStream();
|
||||||
ast.print(stream);
|
ast.print(stream);
|
||||||
|
|
|
||||||
|
|
@ -63,6 +63,7 @@ function OutputStream(options) {
|
||||||
preserve_line : false,
|
preserve_line : false,
|
||||||
screw_ie8 : false,
|
screw_ie8 : false,
|
||||||
preamble : null,
|
preamble : null,
|
||||||
|
is_js : false
|
||||||
}, true);
|
}, true);
|
||||||
|
|
||||||
var indentation = 0;
|
var indentation = 0;
|
||||||
|
|
@ -485,8 +486,8 @@ function OutputStream(options) {
|
||||||
return true;
|
return true;
|
||||||
// this deals with precedence: 3 * (2 + 1)
|
// this deals with precedence: 3 * (2 + 1)
|
||||||
if (p instanceof Cola.AST_Binary) {
|
if (p instanceof Cola.AST_Binary) {
|
||||||
var po = p.operator, pp = Cola.PRECEDENCE[po];
|
var po = p.operator, pp = (output.option("is_js") ? Cola.PRECEDENCE : Cola.cPRECEDENCE)[po];
|
||||||
var so = this.operator, sp = Cola.PRECEDENCE[so];
|
var so = this.operator, sp = (output.option("is_js") ? Cola.PRECEDENCE : Cola.cPRECEDENCE)[so];
|
||||||
if (pp > sp
|
if (pp > sp
|
||||||
|| (pp == sp
|
|| (pp == sp
|
||||||
&& this === p.right)) {
|
&& this === p.right)) {
|
||||||
|
|
@ -1073,7 +1074,7 @@ function OutputStream(options) {
|
||||||
&& +key + "" == key)
|
&& +key + "" == key)
|
||||||
&& parseFloat(key) >= 0) {
|
&& parseFloat(key) >= 0) {
|
||||||
output.print(make_num(key));
|
output.print(make_num(key));
|
||||||
} else if (Cola.RESERVED_WORDS(key) ? output.option("screw_ie8") : Cola.is_identifier_string(key)) {
|
} else if ((output.option("is_js") ? Cola.RESERVED_WORDS : Cola.cRESERVED_WORDS)(key) ? output.option("screw_ie8") : Cola.is_identifier_string(key)) {
|
||||||
output.print_name(key);
|
output.print_name(key);
|
||||||
} else {
|
} else {
|
||||||
output.print_string(key);
|
output.print_string(key);
|
||||||
|
|
|
||||||
59
lib/parse.js
59
lib/parse.js
|
|
@ -177,8 +177,8 @@ Cola.is_unicode_connector_punctuation = function (ch) {
|
||||||
return Cola.UNICODE.connector_punctuation.test(ch);
|
return Cola.UNICODE.connector_punctuation.test(ch);
|
||||||
};
|
};
|
||||||
|
|
||||||
Cola.is_identifier = function (name) {
|
Cola.is_identifier = function (name, is_js) {
|
||||||
return !Cola.RESERVED_WORDS(name) && /^[a-z_$][a-z0-9_$]*$/i.test(name);
|
return (is_js && !Cola.RESERVED_WORDS(name) || !is_js && !Cola.cRESERVED_WORDS(name)) && /^[a-z_$][a-z0-9_$]*$/i.test(name);
|
||||||
};
|
};
|
||||||
|
|
||||||
Cola.is_identifier_start = function (code) {
|
Cola.is_identifier_start = function (code) {
|
||||||
|
|
@ -259,6 +259,16 @@ Cola.Tokenizer = function ($TEXT, filename, is_js, html5_comments) {
|
||||||
level : 0
|
level : 0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if(is_js){
|
||||||
|
this.KEYWORDS = Cola.KEYWORDS;
|
||||||
|
this.KEYWORDS_ATOM = Cola.KEYWORDS_ATOM;
|
||||||
|
this.OPERATORS = Cola.OPERATORS;
|
||||||
|
} else {
|
||||||
|
this.KEYWORDS = Cola.cKEYWORDS;
|
||||||
|
this.KEYWORDS_ATOM = Cola.cKEYWORDS_ATOM;
|
||||||
|
this.OPERATORS = Cola.cOPERATORS;
|
||||||
|
}
|
||||||
|
|
||||||
this.is_js = !!is_js;
|
this.is_js = !!is_js;
|
||||||
this.html5_comments = html5_comments;
|
this.html5_comments = html5_comments;
|
||||||
this.prev_was_dot = false;
|
this.prev_was_dot = false;
|
||||||
|
|
@ -558,7 +568,7 @@ Cola.Tokenizer.prototype.read_name = function () {
|
||||||
backslash = false;
|
backslash = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (Cola.KEYWORDS(name) && escaped) {
|
if (this.KEYWORDS(name) && escaped) {
|
||||||
hex = name.charCodeAt(0).toString(16).toUpperCase();
|
hex = name.charCodeAt(0).toString(16).toUpperCase();
|
||||||
name = "\\u" + "0000".substr(hex.length) + hex + name.slice(1);
|
name = "\\u" + "0000".substr(hex.length) + hex + name.slice(1);
|
||||||
}
|
}
|
||||||
|
|
@ -593,7 +603,7 @@ Cola.Tokenizer.prototype.read_operator = function (prefix) {
|
||||||
function grow(op) {
|
function grow(op) {
|
||||||
if (!_this.peek()) return op;
|
if (!_this.peek()) return op;
|
||||||
var bigger = op + _this.peek();
|
var bigger = op + _this.peek();
|
||||||
if (Cola.OPERATORS(bigger)) {
|
if (_this.OPERATORS(bigger)) {
|
||||||
_this.next();
|
_this.next();
|
||||||
return grow(bigger);
|
return grow(bigger);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -626,9 +636,9 @@ Cola.Tokenizer.prototype.handle_dot = function () {
|
||||||
Cola.Tokenizer.prototype.read_word = function () {
|
Cola.Tokenizer.prototype.read_word = function () {
|
||||||
var word = this.read_name();
|
var word = this.read_name();
|
||||||
if (this.prev_was_dot) return this.token("name", word);
|
if (this.prev_was_dot) return this.token("name", word);
|
||||||
return Cola.KEYWORDS_ATOM(word) ? this.token("atom", word)
|
return this.KEYWORDS_ATOM(word) ? this.token("atom", word)
|
||||||
: !Cola.KEYWORDS(word) ? this.token("name", word)
|
: !this.KEYWORDS(word) ? this.token("name", word)
|
||||||
: Cola.OPERATORS(word) ? this.token("operator", word)
|
: this.OPERATORS(word) ? this.token("operator", word)
|
||||||
: this.token("keyword", word);
|
: this.token("keyword", word);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -659,8 +669,8 @@ Cola.Tokenizer.prototype.next_token = function (force_regexp) {
|
||||||
ch = this.peek();
|
ch = this.peek();
|
||||||
if (!ch) return this.token("eof");
|
if (!ch) return this.token("eof");
|
||||||
var code = ch.charCodeAt(0);
|
var code = ch.charCodeAt(0);
|
||||||
if(!this.is_js && code == 96) this.read_string();
|
if(!this.is_js && code == 96) return this.read_string();
|
||||||
else switch (code) {
|
switch (code) {
|
||||||
case 34: case 39: return this.read_string();
|
case 34: case 39: return this.read_string();
|
||||||
case 46: return this.handle_dot();
|
case 46: return this.handle_dot();
|
||||||
case 47: return this.handle_slash();
|
case 47: return this.handle_slash();
|
||||||
|
|
@ -800,8 +810,17 @@ Cola.Parser = function ($TEXT, options, is_js) {
|
||||||
labels : []
|
labels : []
|
||||||
};
|
};
|
||||||
this.S.input.context = function(){ return _this.tokenizer.context() };
|
this.S.input.context = function(){ return _this.tokenizer.context() };
|
||||||
|
|
||||||
this.S.token = this.next();
|
this.S.token = this.next();
|
||||||
|
|
||||||
|
if(is_js){
|
||||||
|
this.UNARY_PREFIX = Cola.UNARY_PREFIX;
|
||||||
|
this.PRECEDENCE = Cola.PRECEDENCE;
|
||||||
|
this.ASSIGNMENT = Cola.ASSIGNMENT;
|
||||||
|
} else {
|
||||||
|
this.UNARY_PREFIX = Cola.cUNARY_PREFIX;
|
||||||
|
this.PRECEDENCE = Cola.cPRECEDENCE;
|
||||||
|
this.ASSIGNMENT = Cola.cASSIGNMENT;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Cola.Parser.prototype.parse = function () {
|
Cola.Parser.prototype.parse = function () {
|
||||||
|
|
@ -881,7 +900,7 @@ Cola.Parser.prototype.expect_token = function (type, val) {
|
||||||
Cola.Parser.prototype.expect = function (punc) { return this.expect_token("punc", punc); };
|
Cola.Parser.prototype.expect = function (punc) { return this.expect_token("punc", punc); };
|
||||||
|
|
||||||
Cola.Parser.prototype.can_insert_semicolon = function () {
|
Cola.Parser.prototype.can_insert_semicolon = function () {
|
||||||
return false; // ColaScript
|
if(!this.is_js) return false; // ColaScript
|
||||||
return !this.options.strict && (
|
return !this.options.strict && (
|
||||||
this.S.token.nlb || this.is("eof") || this.is("punc", "}")
|
this.S.token.nlb || this.is("eof") || this.is("punc", "}")
|
||||||
);
|
);
|
||||||
|
|
@ -918,7 +937,7 @@ Cola.Parser.prototype.handle_regexp = function () {
|
||||||
};
|
};
|
||||||
|
|
||||||
Cola.Parser.prototype.statement = Cola.Parser.embed_tokens(function() {
|
Cola.Parser.prototype.statement = Cola.Parser.embed_tokens(function() {
|
||||||
var tmp;
|
var tmp, _this = this;
|
||||||
this.handle_regexp();
|
this.handle_regexp();
|
||||||
switch (this.S.token.type) {
|
switch (this.S.token.type) {
|
||||||
case "string":
|
case "string":
|
||||||
|
|
@ -970,14 +989,14 @@ Cola.Parser.prototype.statement = Cola.Parser.embed_tokens(function() {
|
||||||
|
|
||||||
case "do":
|
case "do":
|
||||||
return new Cola.AST_Do({
|
return new Cola.AST_Do({
|
||||||
body : this.in_loop(statement),
|
body : this.in_loop(function(){ return _this.statement() }),
|
||||||
condition : (this.expect_token("keyword", "while"), tmp = this.parenthesised(), this.semicolon(), tmp)
|
condition : (this.expect_token("keyword", "while"), tmp = this.parenthesised(), this.semicolon(), tmp)
|
||||||
});
|
});
|
||||||
|
|
||||||
case "while":
|
case "while":
|
||||||
return new Cola.AST_While({
|
return new Cola.AST_While({
|
||||||
condition : this.parenthesised(),
|
condition : this.parenthesised(),
|
||||||
body : this.in_loop(statement)
|
body : this.in_loop(function(){ return _this.statement() })
|
||||||
});
|
});
|
||||||
|
|
||||||
case "for":
|
case "for":
|
||||||
|
|
@ -1108,11 +1127,12 @@ Cola.Parser.prototype.regular_for = function (init) {
|
||||||
this.expect(";");
|
this.expect(";");
|
||||||
var step = this.is("punc", ")") ? null : this.expression(true);
|
var step = this.is("punc", ")") ? null : this.expression(true);
|
||||||
this.expect(")");
|
this.expect(")");
|
||||||
|
var _this = this;
|
||||||
return new Cola.AST_For({
|
return new Cola.AST_For({
|
||||||
init : init,
|
init : init,
|
||||||
condition : test,
|
condition : test,
|
||||||
step : step,
|
step : step,
|
||||||
body : this.in_loop(statement)
|
body : this.in_loop(function(){ return _this.statement() })
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -1120,11 +1140,12 @@ Cola.Parser.prototype.for_in = function (init) {
|
||||||
var lhs = init instanceof Cola.AST_Var ? init.definitions[0].name : null;
|
var lhs = init instanceof Cola.AST_Var ? init.definitions[0].name : null;
|
||||||
var obj = this.expression(true);
|
var obj = this.expression(true);
|
||||||
this.expect(")");
|
this.expect(")");
|
||||||
|
var _this = this;
|
||||||
return new Cola.AST_ForIn({
|
return new Cola.AST_ForIn({
|
||||||
init : init,
|
init : init,
|
||||||
name : lhs,
|
name : lhs,
|
||||||
object : obj,
|
object : obj,
|
||||||
body : this.in_loop(statement)
|
body : this.in_loop(function(){ return _this.statement() })
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -1578,7 +1599,7 @@ Cola.Parser.prototype.subscripts = function(expr, allow_calls) {
|
||||||
|
|
||||||
Cola.Parser.prototype.maybe_unary = function(allow_calls) {
|
Cola.Parser.prototype.maybe_unary = function(allow_calls) {
|
||||||
var start = this.S.token;
|
var start = this.S.token;
|
||||||
if (this.is("operator") && Cola.UNARY_PREFIX(start.value)) {
|
if (this.is("operator") && this.UNARY_PREFIX(start.value)) {
|
||||||
this.next();
|
this.next();
|
||||||
this.handle_regexp();
|
this.handle_regexp();
|
||||||
var ex = this.make_unary(Cola.AST_UnaryPrefix, start.value, this.maybe_unary(allow_calls));
|
var ex = this.make_unary(Cola.AST_UnaryPrefix, start.value, this.maybe_unary(allow_calls));
|
||||||
|
|
@ -1605,7 +1626,7 @@ Cola.Parser.prototype.make_unary = function (ctor, op, expr) {
|
||||||
Cola.Parser.prototype.expr_op = function(left, min_prec, no_in) {
|
Cola.Parser.prototype.expr_op = function(left, min_prec, no_in) {
|
||||||
var op = this.is("operator") ? this.S.token.value : null;
|
var op = this.is("operator") ? this.S.token.value : null;
|
||||||
if (op == "in" && no_in) op = null;
|
if (op == "in" && no_in) op = null;
|
||||||
var prec = op != null ? Cola.PRECEDENCE[op] : null;
|
var prec = op != null ? this.PRECEDENCE[op] : null;
|
||||||
if (prec != null && prec > min_prec) {
|
if (prec != null && prec > min_prec) {
|
||||||
this.next();
|
this.next();
|
||||||
var right = this.expr_op(this.maybe_unary(true), prec, no_in);
|
var right = this.expr_op(this.maybe_unary(true), prec, no_in);
|
||||||
|
|
@ -1651,7 +1672,7 @@ Cola.Parser.prototype.is_assignable = function (expr) {
|
||||||
Cola.Parser.prototype.maybe_assign = function(no_in) {
|
Cola.Parser.prototype.maybe_assign = function(no_in) {
|
||||||
var start = this.S.token;
|
var start = this.S.token;
|
||||||
var left = this.maybe_conditional(no_in), val = this.S.token.value;
|
var left = this.maybe_conditional(no_in), val = this.S.token.value;
|
||||||
if (this.is("operator") && Cola.ASSIGNMENT(val)) {
|
if (this.is("operator") && this.ASSIGNMENT(val)) {
|
||||||
if (this.is_assignable(left)) {
|
if (this.is_assignable(left)) {
|
||||||
this.next();
|
this.next();
|
||||||
return new Cola.AST_Assign({
|
return new Cola.AST_Assign({
|
||||||
|
|
|
||||||
|
|
@ -245,7 +245,7 @@ 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 = base54(++this.cname);
|
||||||
if (!Cola.is_identifier(m)) 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
|
||||||
// shadow a name excepted from mangling.
|
// shadow a name excepted from mangling.
|
||||||
|
|
@ -326,7 +326,8 @@ Cola.AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){
|
||||||
eval : false,
|
eval : false,
|
||||||
sort : false,
|
sort : false,
|
||||||
toplevel : false,
|
toplevel : false,
|
||||||
screw_ie8 : false
|
screw_ie8 : false,
|
||||||
|
is_js : false
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -361,7 +362,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));
|
do name = base54(++lname); while (!Cola.is_identifier(name, options.is_js));
|
||||||
node.mangled_name = name;
|
node.mangled_name = name;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user