From d461f9d9270db7ddc8873f041fcbcf9a25415530 Mon Sep 17 00:00:00 2001 From: Onoshko Dan Date: Thu, 17 Apr 2014 17:10:18 +0700 Subject: [PATCH] parse.js refectory is done --- lib/compress.js | 8 ++++--- lib/index.html | 6 ++--- lib/output.js | 7 +++--- lib/parse.js | 59 +++++++++++++++++++++++++++++++++---------------- lib/scope.js | 7 +++--- 5 files changed, 56 insertions(+), 31 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index f4b47053..c94d2436 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -75,7 +75,9 @@ function Compressor(options, false_by_default) { angular : false, warnings : true, - global_defs : {} + global_defs : {}, + + is_js : false }, true); }; @@ -1996,7 +1998,7 @@ Cola.merge(Compressor.prototype, { // result. hence, force switch. 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); } } @@ -2345,7 +2347,7 @@ Cola.merge(Compressor.prototype, { var prop = self.property; if (prop instanceof Cola.AST_String && compressor.option("properties")) { 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, { expression : self.expression, property : prop diff --git a/lib/index.html b/lib/index.html index 248a6e2e..896a4b8e 100644 --- a/lib/index.html +++ b/lib/index.html @@ -63,8 +63,8 @@ localStorage.source = source; localStorage.isjs = isjs.checked ? "t" : "f"; - stream = OutputStream({ beautify : true }); - compressor = Compressor(); + stream = OutputStream({ beautify : true, is_js : isjs.checked }); + compressor = Compressor({ is_js : isjs.checked }); try { // 1. compile @@ -80,7 +80,7 @@ // 3. mangle ast.figure_out_scope(); ast.compute_char_frequency(); - ast.mangle_names({ sort : true, toplevel : true }); + ast.mangle_names({ is_js : isjs.checked, sort : true, toplevel : true }); stream = OutputStream(); ast.print(stream); diff --git a/lib/output.js b/lib/output.js index 990ce993..7e2213b9 100644 --- a/lib/output.js +++ b/lib/output.js @@ -63,6 +63,7 @@ function OutputStream(options) { preserve_line : false, screw_ie8 : false, preamble : null, + is_js : false }, true); var indentation = 0; @@ -485,8 +486,8 @@ function OutputStream(options) { return true; // this deals with precedence: 3 * (2 + 1) if (p instanceof Cola.AST_Binary) { - var po = p.operator, pp = Cola.PRECEDENCE[po]; - var so = this.operator, sp = Cola.PRECEDENCE[so]; + var po = p.operator, pp = (output.option("is_js") ? Cola.PRECEDENCE : Cola.cPRECEDENCE)[po]; + var so = this.operator, sp = (output.option("is_js") ? Cola.PRECEDENCE : Cola.cPRECEDENCE)[so]; if (pp > sp || (pp == sp && this === p.right)) { @@ -1073,7 +1074,7 @@ function OutputStream(options) { && +key + "" == key) && parseFloat(key) >= 0) { 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); } else { output.print_string(key); diff --git a/lib/parse.js b/lib/parse.js index d6240529..340c1f7d 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -177,8 +177,8 @@ Cola.is_unicode_connector_punctuation = function (ch) { return Cola.UNICODE.connector_punctuation.test(ch); }; -Cola.is_identifier = function (name) { - return !Cola.RESERVED_WORDS(name) && /^[a-z_$][a-z0-9_$]*$/i.test(name); +Cola.is_identifier = function (name, is_js) { + 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) { @@ -259,6 +259,16 @@ Cola.Tokenizer = function ($TEXT, filename, is_js, html5_comments) { 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.html5_comments = html5_comments; this.prev_was_dot = false; @@ -558,7 +568,7 @@ Cola.Tokenizer.prototype.read_name = function () { backslash = false; } } - if (Cola.KEYWORDS(name) && escaped) { + if (this.KEYWORDS(name) && escaped) { hex = name.charCodeAt(0).toString(16).toUpperCase(); name = "\\u" + "0000".substr(hex.length) + hex + name.slice(1); } @@ -593,7 +603,7 @@ Cola.Tokenizer.prototype.read_operator = function (prefix) { function grow(op) { if (!_this.peek()) return op; var bigger = op + _this.peek(); - if (Cola.OPERATORS(bigger)) { + if (_this.OPERATORS(bigger)) { _this.next(); return grow(bigger); } else { @@ -626,9 +636,9 @@ Cola.Tokenizer.prototype.handle_dot = function () { Cola.Tokenizer.prototype.read_word = function () { var word = this.read_name(); if (this.prev_was_dot) return this.token("name", word); - return Cola.KEYWORDS_ATOM(word) ? this.token("atom", word) - : !Cola.KEYWORDS(word) ? this.token("name", word) - : Cola.OPERATORS(word) ? this.token("operator", word) + return this.KEYWORDS_ATOM(word) ? this.token("atom", word) + : !this.KEYWORDS(word) ? this.token("name", word) + : this.OPERATORS(word) ? this.token("operator", word) : this.token("keyword", word); }; @@ -659,8 +669,8 @@ Cola.Tokenizer.prototype.next_token = function (force_regexp) { ch = this.peek(); if (!ch) return this.token("eof"); var code = ch.charCodeAt(0); - if(!this.is_js && code == 96) this.read_string(); - else switch (code) { + if(!this.is_js && code == 96) return this.read_string(); + switch (code) { case 34: case 39: return this.read_string(); case 46: return this.handle_dot(); case 47: return this.handle_slash(); @@ -800,8 +810,17 @@ Cola.Parser = function ($TEXT, options, is_js) { labels : [] }; this.S.input.context = function(){ return _this.tokenizer.context() }; - 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 () { @@ -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.can_insert_semicolon = function () { - return false; // ColaScript + if(!this.is_js) return false; // ColaScript return !this.options.strict && ( 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() { - var tmp; + var tmp, _this = this; this.handle_regexp(); switch (this.S.token.type) { case "string": @@ -970,14 +989,14 @@ Cola.Parser.prototype.statement = Cola.Parser.embed_tokens(function() { case "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) }); case "while": return new Cola.AST_While({ condition : this.parenthesised(), - body : this.in_loop(statement) + body : this.in_loop(function(){ return _this.statement() }) }); case "for": @@ -1108,11 +1127,12 @@ Cola.Parser.prototype.regular_for = function (init) { this.expect(";"); var step = this.is("punc", ")") ? null : this.expression(true); this.expect(")"); + var _this = this; return new Cola.AST_For({ init : init, condition : test, 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 obj = this.expression(true); this.expect(")"); + var _this = this; return new Cola.AST_ForIn({ init : init, name : lhs, 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) { 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.handle_regexp(); 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) { var op = this.is("operator") ? this.S.token.value : 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) { this.next(); 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) { var start = this.S.token; 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)) { this.next(); return new Cola.AST_Assign({ diff --git a/lib/scope.js b/lib/scope.js index 6d09fb0d..cce35134 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -245,7 +245,7 @@ Cola.AST_Scope.DEFMETHOD("next_mangled", function(options){ var ext = this.enclosed; out: while (true) { 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 // shadow a name excepted from mangling. @@ -326,7 +326,8 @@ Cola.AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){ eval : false, sort : 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) { 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; return true; }