UglifyJS/lib/browser-cola.js

9 lines
278 KiB
JavaScript
Raw Normal View History

2015-01-02 11:32:04 +00:00
!this.Cola&&(this.Cola={});if(typeof window!="undefined"){Cola.getSource=function(url){var xhr=new XMLHttpRequest;xhr.open("GET",url,false);xhr.send();return xhr.status==200?xhr.responseText:""};Cola.dirname=function(dir){return dir.replace(/\/[^\/]*$/,"")};Cola.getPackages=function(){var packages=document.querySelector('script[type="text/packages-json"]');if(packages){return JSON.parse(packages.innerHTML)}packages=document.querySelector('script[type="text/packages-json"][src]');if(packages){return JSON.parse(Cola.getSource(packages.src))}if(packages=Cola.getSource("/packages.json")){return JSON.parse(packages)}return{}}}else{var path=require("path");var fs=require("fs");Cola.getSource=function(file){try{return fs.readFileSync(Cola.notRoot(file)?path.join(process.cwd(),file):file,"utf8")}catch(e){return""}};Cola.getPackages=function(path){if(!path)path="packages.json";return JSON.parse(Cola.getSource(path))};Cola.dirname=path.dirname}Cola.tryGetRequiredSource=function(src){var source;if(source=Cola.getSource(src))return[src,source];if(source=Cola.getSource(src+".cola"))return[src,source];if(source=Cola.getSource(src+".js"))return[src,source];return false};Cola.notRoot=function(path){return path[0]!="/"&&!/^http/.test(path)};Cola.translate=function(source,opts){var stream=new Cola.OutputStream({beautify:true}),ast;try{ast=Cola.parse(source).toJavaScript(opts);ast.print(stream);return stream.toString()}catch(e){throw e}};Cola.eval=function(source,opts){return eval.call(this,Cola.translate(source,opts))};Cola.bootstraped=false;Cola.bootstrap=function(){if(Cola.bootstraped)return;Cola.bootstraped=true;Array.prototype.forEach.call(document.querySelectorAll('script[type="text/colascript"][src]'),function(script){if(/\.js$/.test(script.src))eval.call(window,Cola.getSource(script.src));else Cola.eval.call(window,Cola.getSource(script.src),{path:Cola.dirname(script.src)})});var event=document.createEvent("HTMLEvents");event.initEvent("DOMContentLoaded",true,true);event.eventName="DOMContentLoaded";window.dispatchEvent(event);event=document.createEvent("HTMLEvents");event.initEvent("load",true,true);event.eventName="load";window.dispatchEvent(event)};Cola.modsVerifi=function(mods,allowedMods){mods=mods.slice();allowedMods.forEach(function(mod){Cola.remove(mods,mod)});return!mods.length};Cola.modsContains=function(mods,allowedMods){for(var i in allowedMods)if(allowedMods.hasOwnProperty(i)&&mods.indexOf(allowedMods[i])!=-1)return true;return false};"use strict";Cola.array_to_hash=function(a){var ret=Object.create(null);for(var i=0;i<a.length;++i)ret[a[i]]=true;return ret};Cola.slice=function(a,start){return Array.prototype.slice.call(a,start||0)};Cola.characters=function(str){return str.split("")};Cola.member=function(name,array){for(var i=array.length;--i>=0;)if(array[i]==name)return true;return false};Cola.find_if=function(func,array){for(var i=0,n=array.length;i<n;++i){if(func(array[i]))return array[i]}};Cola.repeat_string=function(str,i){if(i<=0)return"";if(i==1)return str;var d=Cola.repeat_string(str,i>>1);d+=d;if(i&1)d+=str;return d};Cola.DefaultsError=function(msg,defs){Error.call(this,msg);this.msg=msg;this.defs=defs};Cola.DefaultsError.prototype=Object.create(Error.prototype);Cola.DefaultsError.prototype.constructor=Cola.DefaultsError;Cola.DefaultsError.croak=function(msg,defs){throw new Cola.DefaultsError(msg,defs)};Cola.defaults=function(args,defs,croak){if(args===true)args={};var ret=args||{};if(croak)for(var i in ret)if(ret.hasOwnProperty(i)&&!defs.hasOwnProperty(i))Cola.DefaultsError.croak("`"+i+"` is not a supported option",defs);for(var i in defs)if(defs.hasOwnProperty(i)){ret[i]=args&&args.hasOwnProperty(i)?args[i]:defs[i]}return ret};Cola.merge=function(obj,ext){for(var i in ext)if(ext.hasOwnProperty(i)){obj[i]=ext[i]}return obj};Cola.noop=function(){};Cola.MAP=function(){function MAP(a,f,backwards){var ret=[],top=[],i;function doit(){var val=f(a[i],i);var is_last=val instanceof Last;if(is_last)val=val.v;if(val instanceof Continue)return false;if(val instanceof AtTop){val=val.v;if(val instanceof Splice){top.push.a
Cola.AST_SymbolAccessor=Cola.DEFNODE("SymbolAccessor",null,{$documentation:"The name of a property accessor (setter/getter function)"},Cola.AST_Symbol);Cola.AST_SymbolDeclaration=Cola.DEFNODE("SymbolDeclaration","init",{$documentation:"A declaration symbol (symbol in var/const, function name or argument, symbol in catch)",$propdoc:{init:"[AST_Node*/S] array of initializers for this declaration."}},Cola.AST_Symbol);Cola.AST_SymbolVar=Cola.DEFNODE("SymbolVar",null,{$documentation:"Symbol defining a variable"},Cola.AST_SymbolDeclaration);Cola.AST_SymbolConst=Cola.DEFNODE("SymbolConst",null,{$documentation:"A constant declaration"},Cola.AST_SymbolDeclaration);Cola.AST_SymbolClass=Cola.DEFNODE("SymbolClass",null,{$iscola:true,$documentation:"Symbol defining a class"},Cola.AST_SymbolDeclaration);Cola.AST_SymbolSingleton=Cola.DEFNODE("SymbolSingleton",null,{$iscola:true,$documentation:"Symbol defining a singleton"},Cola.AST_SymbolDeclaration);Cola.AST_SymbolFunarg=Cola.DEFNODE("SymbolFunarg","type",{$documentation:"Symbol naming a function argument"},Cola.AST_SymbolVar);Cola.AST_SymbolDefun=Cola.DEFNODE("SymbolDefun",null,{$documentation:"Symbol defining a function"},Cola.AST_SymbolDeclaration);Cola.AST_SymbolLambda=Cola.DEFNODE("SymbolLambda",null,{$documentation:"Symbol naming a function expression"},Cola.AST_SymbolDeclaration);Cola.AST_SymbolCatch=Cola.DEFNODE("SymbolCatch",null,{$documentation:"Symbol naming the exception in catch"},Cola.AST_SymbolDeclaration);Cola.AST_Label=Cola.DEFNODE("Label","references",{$documentation:"Symbol naming a label (declaration)",$propdoc:{references:"[AST_LoopControl*] a list of nodes referring to this label"},initialize:function(){this.references=[];this.thedef=this}},Cola.AST_Symbol);Cola.AST_SymbolRef=Cola.DEFNODE("SymbolRef",null,{$documentation:"Reference to some symbol (not definition/declaration)"},Cola.AST_Symbol);Cola.AST_LabelRef=Cola.DEFNODE("LabelRef",null,{$documentation:"Reference to a label symbol"},Cola.AST_Symbol);Cola.AST_This=Cola.DEFNODE("This",null,{$documentation:"The `this` symbol"},Cola.AST_Symbol);Cola.AST_Constant=Cola.DEFNODE("Constant",null,{$documentation:"Base class for all constants",getValue:function(){return this.value}});Cola.AST_String=Cola.DEFNODE("String","value",{$documentation:"A string literal",$propdoc:{value:"[string] the contents of this string"}},Cola.AST_Constant);Cola.AST_StringTemplate=Cola.DEFNODE("StringTemplate","body",{$iscola:true,$documentation:"A string template",$propdoc:{body:"[AST_Statement*] the contents of this string template"},_walk:function(visitor){return visitor._visit(this,function(){Cola.walk_body(this,visitor)})}},Cola.AST_Statement);Cola.AST_Number=Cola.DEFNODE("Number","value",{$documentation:"A number literal",$propdoc:{value:"[number] the numeric value"}},Cola.AST_Constant);Cola.AST_RegExp=Cola.DEFNODE("RegExp","value",{$documentation:"A regexp literal",$propdoc:{value:"[RegExp] the actual regexp"}},Cola.AST_Constant);Cola.AST_Atom=Cola.DEFNODE("Atom",null,{$documentation:"Base class for atoms"},Cola.AST_Constant);Cola.AST_Null=Cola.DEFNODE("Null",null,{$documentation:"The `null` atom",value:null},Cola.AST_Atom);Cola.AST_NaN=Cola.DEFNODE("NaN",null,{$documentation:"The impossible value",value:0/0},Cola.AST_Atom);Cola.AST_Undefined=Cola.DEFNODE("Undefined",null,{$documentation:"The `undefined` value",value:function(){}()},Cola.AST_Atom);Cola.AST_Hole=Cola.DEFNODE("Hole",null,{$documentation:"A hole in an array",value:function(){}()},Cola.AST_Atom);Cola.AST_Infinity=Cola.DEFNODE("Infinity",null,{$documentation:"The `Infinity` value",value:1/0},Cola.AST_Atom);Cola.AST_Boolean=Cola.DEFNODE("Boolean",null,{$documentation:"Base class for booleans"},Cola.AST_Atom);Cola.AST_False=Cola.DEFNODE("False",null,{$documentation:"The `false` atom",value:false},Cola.AST_Boolean);Cola.AST_True=Cola.DEFNODE("True",null,{$documentation:"The `true` atom",value:true},Cola.AST_Boolean);Cola.TreeWalker=function(callback){this.visit=callback;this.stack=[]};Cola.TreeWalker.prototype={_visit:function(node,descend){this.stack.push(node);var ret=thi
Cola.cPRECEDENCE=Cola.mergeTokens([["||"],["&&"],["|"],["^"],["&"],["==","===","!=","!=="],["<",">","<=",">=","in","of","instanceof","is","isnt"],[">>","<<",">>>"],["+","-"],["*","/","%","**","%%"]],{});Cola.STATEMENTS_WITH_LABELS=Cola.array_to_hash(["for","do","while","switch"]);Cola.ATOMIC_START_TOKEN=Cola.array_to_hash(["atom","num","string","regexp","name"]);Cola.parse=function($TEXT,options){return new Cola.Parser($TEXT,options).parse()};Cola.Parser=function($TEXT,options){var _this=this;this.options=Cola.defaults(options,{strict:false,filename:null,toplevel:null,expression:false,html5_comments:true,is_js:false,braces_free:true});this.is_js=!!this.options.is_js;this.tokenizer=typeof $TEXT=="string"?new Cola.Tokenizer($TEXT,this.options.filename,this.is_js,this.options.html5_comments):$TEXT;this.S={input:function(){return _this.tokenizer.next_token()},token:null,prev:null,peeked:null,in_function:0,in_async_function:0,in_directives:true,in_loop:0,labels:[]};this.S.input.context=function(){return _this.tokenizer.context()};this.S.token=this.next();this.dumps=[];this.dumpi=-1;this.requiredModules=[];if(this.is_js){this.UNARY_PREFIX=Cola.UNARY_PREFIX;this.PRECEDENCE=Cola.PRECEDENCE;this.ASSIGNMENT=Cola.ASSIGNMENT;this.UNARY_POSTFIX=Cola.UNARY_POSTFIX}else{this.UNARY_PREFIX=Cola.cUNARY_PREFIX;this.PRECEDENCE=Cola.cPRECEDENCE;this.ASSIGNMENT=Cola.cASSIGNMENT;this.UNARY_POSTFIX=Cola.cUNARY_POSTFIX}};Cola.Parser.prototype.parse=function(){var _this=this;if(this.options.expression){return this.expression(true)}return function(){var start=_this.S.token;var body=[];while(!_this.is("eof"))body.push(_this.statement());var end=_this.prev();var toplevel=_this.options.toplevel;if(toplevel){toplevel.body=toplevel.body.concat(body);toplevel.end=end}else{toplevel=new Cola.AST_Toplevel({start:start,body:body,end:end})}toplevel.language=_this.is_js?"js":"cola";toplevel.requiredModules=_this.requiredModules;return toplevel}()};Cola.Parser.prototype.dumpS=function(){this.dumps[++this.dumpi]=Cola.clone(this.S);this.tokenizer.dumpS()};Cola.Parser.prototype.restoreS=function(onlyDown){if(this.dumpi==-1)return;this.tokenizer.restoreS(onlyDown);if(onlyDown)return this.dumpi--;this.S=this.dumps[this.dumpi];delete this.dumps[this.dumpi--]};Cola.Parser.prototype.next_until=function(until){while(true){if(until(this.S.token))break;this.next()}return this.S.token};Cola.Parser.prototype.is=function(type,value){return Cola.is_token(this.S.token,type,value)};Cola.Parser.prototype.peek=function(){return this.S.peeked||(this.S.peeked=this.S.input())};Cola.Parser.prototype.next_is=function(type,value){return Cola.is_token(this.peek(),type,value)};Cola.Parser.prototype.next=function(){this.S.prev=this.S.token;if(this.S.peeked){this.S.token=this.S.peeked;this.S.peeked=null}else{this.S.token=this.S.input()}this.S.in_directives=this.S.in_directives&&(this.S.token.type=="string"||this.is("punc",";"));return this.S.token};Cola.Parser.prototype.prev=function(){return this.S.prev};Cola.Parser.prototype.croak=function(msg,line,col,pos){var ctx=this.S?this.S.input.context():{};Cola.js_error(msg,ctx.filename,line!=null?line:ctx.tokline,col!=null?col:ctx.tokcol,pos!=null?pos:ctx.tokpos)};Cola.Parser.prototype.token_error=function(token,msg){this.croak(msg,token.line,token.col,token.pos)};Cola.Parser.prototype.unexpected=function(token){if(token==null)token=this.S.token;this.token_error(token,"Unexpected token: "+token.type+" `"+token.value+"`")};Cola.Parser.prototype.expect_token=function(type,val,stay){if(this.is(type,val)){return stay?undefined:this.next()}this.token_error(this.S.token,"Unexpected token "+this.S.token.type+" `"+this.S.token.value+"`"+", expected "+type+" `"+val+"`")};Cola.Parser.prototype.expect=function(punc,stay){return this.expect_token("punc",punc,stay)};Cola.Parser.prototype.can_insert_semicolon=function(){if(!this.is_js)return false;return!this.options.strict&&(this.S.token.nlb||this.is("eof")||this.is("punc","}"))};Cola.Parser.prototype.semicolon=function(){if(this.is("punc",";"))this.next();else if(!this.can_insert_semicolon())this.unexpect
if(this.is("punc",",")&&allow_empty){a.push(new Cola.AST_Hole({start:this.S.token,end:this.S.token}))}else if(this.is("name")&&allow_named_args){name=this.S.token.clone();this.dumpS();this.next();if(this.is("punc",":")){this.restoreS(true);this.next();a.push(new Cola.AST_Namedarg({name:name.value,value:this.expression(false),start:name,end:this.prev()}))}else{this.restoreS();a.push(this.expression(false))}}else a.push(this.expression(false))}this.next();return a};Cola.Parser.prototype.array_=Cola.Parser.embed_tokens(function(is_template,is_var,with_vardef){this.expect("[");if(!this.is_js&&!this.is("punc","]")&&!this.is("punc",",")&&!(this.is("name")&&this.next_is("name"))){this.dumpS();var tmp,from=this.expression(false,false,true),triple;if((this.is("punc","..")||this.is("punc","..."))&&!this.next_is("punc",",")&&!this.next_is("punc","]")){triple=this.is("punc","...");this.next();return new Cola.AST_ArrayRange({from:from,to:(tmp=this.expression(true,false,true),this.expect("]"),tmp),triple:triple,start:from.start,end:this.prev()})}this.restoreS()}if(this.is_js)return new Cola.AST_Array({elements:this.expr_list("]",!this.options.strict,true)});var is_array=is_template?false:null,vardef=false,first=true,a=[],val,skiped=false;while(!this.is("punc","]")){if(first)first=false;else this.expect(",");if(this.is("punc",",")||this.is("punc","]")){a.push(new Cola.AST_Hole({start:this.S.token,end:this.S.token}));if(!this.options.strict&&this.is("punc","]"))break}else if(this.is("punc","...")&&is_array!==true){if(skiped)this.unexpected();this.next();skiped=true;is_array=false;a.push(new Cola.AST_Noop)}else if(!is_var&&with_vardef&&this.is("name")&&this.next_is("name")&&(!Cola.cKEYWORDS(this.peek().value)||this.peek().value=="var")){if(is_array===true)this.unexpected();is_array=false;vardef=true;a.push(new Cola.AST_VarDef({start:this.S.token,type:this.S.token.value=="var"?"dynamic":this.S.token.value,name:function(_this){_this.next();val=_this.as_symbol(Cola.AST_SymbolVar);if(_this.is("punc","...")&&is_array!==true){if(skiped)_this.unexpected();_this.next();skiped=true;is_array=false;val.splated=true}return val}(this),value:null,end:this.prev()}))}else{if(is_array===false&&this.is("punc","["))val=this.array_(true,is_var);else if(is_array===false&&this.is("punc","{"))val=this.object_(true,is_var);else val=this.expression(false);if(val.vardef)vardef=true;if(this.is("punc","...")&&is_array!==true){if(skiped)this.unexpected();this.next();skiped=true;is_array=false;val.splated=true}if(val instanceof Cola.AST_ObjectTemplate||val instanceof Cola.AST_ArrayTemplate){if(is_array===true)this.unexpected();is_array=false}if(!(val instanceof Cola.AST_SymbolRef||val instanceof Cola.AST_ObjectTemplate||val instanceof Cola.AST_ArrayTemplate||!is_var&&val instanceof Cola.AST_PropAccess||val instanceof Cola.AST_Object&&val.template==true||val instanceof Cola.AST_Array&&val.template==true)){if(is_array===false)this.unexpected();is_array=true}a.push(val)}}this.next();return is_array===true||is_array===null?new Cola.AST_Array({elements:a,template:is_array===null,vardef:vardef}):new Cola.AST_ArrayTemplate({elements:a,vardef:vardef})});Cola.Parser.prototype.object_=Cola.Parser.embed_tokens(function(is_template,is_var,with_vardef){this.expect("{");var first=true,a=[],ptype,is_object=is_template?false:null,vardef=false,val;while(!this.is("punc","}")){if(first)first=false;else this.expect(",");if(!this.options.strict&&this.is("punc","}"))break;var start=this.S.token;var type=start.type;var name=this.as_property_name();if(!this.is_js&&!is_var&&with_vardef&&this.is("name")&&!this.next_is("punc","(")&&(!Cola.cKEYWORDS(name)||name=="var")){vardef=true;ptype=name=="var"?"dynamic":name;name=this.as_name()}else ptype=false;if((type=="name"||!this.is_js&&type=="keyword")&&!this.is("punc",":")){if(name=="get"){if(!this.is_js&&is_object===false)this.unexpected();is_object=true;a.push(new Cola.AST_ObjectGetter({start:start,type:ptype,key:this.as_atom_node(),value:this.function_(Cola.AST_Accessor),end:this.prev()}));continue}if(name=="set"){if(!this.is_js&&is_object===fa
}var a=str.split(/\r?\n/),n=a.length-1;this.current_line+=n;if(n==0){this.current_col+=a[n].length}else{this.current_col=a[n].length}this.current_pos+=str.length;this.last=str;this.OUTPUT+=str};Cola.OutputStream.prototype.space=function(){this.options.beautify?this.print(" "):this.might_need_space=true};Cola.OutputStream.prototype.indent=function(half){if(this.options.beautify){this.print(this.make_indent(half?.5:0))}};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();this.indentation=save_indentation;return ret};Cola.OutputStream.prototype.newline=function(){if(this.options.beautify)this.print("\n")};Cola.OutputStream.prototype.semicolon=function(){this.options.beautify?this.print(";"):this.might_need_semicolon=true};Cola.OutputStream.prototype.force_semicolon=function(){this.might_need_semicolon=false;this.print(";")};Cola.OutputStream.prototype.next_indent=function(){return this.indentation+this.options.indent_level};Cola.OutputStream.prototype.with_block=function(cont){var ret;this.print("{");this.newline();this.with_indent(this.next_indent(),function(){ret=cont()});this.indent();this.print("}");return ret};Cola.OutputStream.prototype.with_parens=function(cont){this.print("(");var ret=cont();this.print(")");return ret};Cola.OutputStream.prototype.with_square=function(cont){this.print("[");var ret=cont();this.print("]");return ret};Cola.OutputStream.prototype.comma=function(){this.print(",");this.space()};Cola.OutputStream.prototype.colon=function(){this.print(":");if(this.options.space_colon)this.space()};Cola.OutputStream.prototype.add_mapping=function(token,name){if(!this.options.source_map)return;try{if(token&&!token.file)console.log(token);if(token)this.options.source_map.add(token.file||"?",this.current_line,this.current_col,token.line,token.col,!name&&token.type=="name"?token.value:name)}catch(ex){Cola.AST_Node.warn("Couldn't figure out mapping for {file}:{line},{col} → {cline},{ccol} [{name}]",{file:token.file,line:token.line,col:token.col,cline:this.current_line,ccol:this.current_col,name:name||""})}};Cola.OutputStream.prototype.get=function(){return this.OUTPUT};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)]};(function(){function DEFPRINT(nodetype,generator){nodetype.DEFMETHOD("_codegen",generator)}Cola.AST_Node.DEFMETHOD("print",function(stream,force_parens){var self=this,generator=self._codegen;function doit(){self.add_comments(stream);self.add_source_map(stream);generator(self,stream)}stream.push_node(self);if(force_parens||self.needs_parens(stream)){stream.with_parens(doit)}else{doit()}stream.pop_node()});Cola.AST_Node.DEFMETHOD("print_to_string",function(options){var s=new Cola.OutputStream(options);this.print(s);return s.get()});Cola.AST_Node.DEFMETHOD("add_comments",function(output){var c=output.option("comments"),self=this;if(c){var start=self.start;if(start&&!start._comments_dumped){start._comments_dumped=true;var comments=start.comments_before||[];if(self instanceof Cola.AST_Exit&&self.value){self.value.walk(new Cola.TreeWalker(function(node){if(node.start&&node.start.comments_before){comments=comments.concat(node.start.comments_before);node.start.comments_before=[]}if(node instanceof Cola.AST_Function||node instanceof
statements.forEach(function(stat){if(prev){if(stat instanceof Cola.AST_For){var opera={};try{prev.body.walk(new Cola.TreeWalker(function(node){if(node instanceof Cola.AST_Binary&&node.operator=="in")throw opera}));if(stat.init&&!(stat.init instanceof Cola.AST_Definitions)){stat.init=cons_seq(stat.init)}else if(!stat.init){stat.init=prev.body;ret.pop()}}catch(ex){if(ex!==opera)throw ex}}else if(stat instanceof Cola.AST_If){stat.condition=cons_seq(stat.condition)}else if(stat instanceof Cola.AST_With){stat.expression=cons_seq(stat.expression)}else if(stat instanceof Cola.AST_Exit&&stat.value){stat.value=cons_seq(stat.value)}else if(stat instanceof Cola.AST_Exit){stat.value=cons_seq(make_node(Cola.AST_Undefined,stat))}else if(stat instanceof Cola.AST_Switch){stat.expression=cons_seq(stat.expression)}}ret.push(stat);prev=stat instanceof Cola.AST_SimpleStatement?stat:null});return ret}function join_consecutive_vars(statements,compressor){var prev=null;return statements.reduce(function(a,stat){if(stat instanceof Cola.AST_Definitions&&prev&&prev.TYPE==stat.TYPE){prev.definitions=prev.definitions.concat(stat.definitions);CHANGED=true}else if(stat instanceof Cola.AST_For&&prev instanceof Cola.AST_Definitions&&(!stat.init||stat.init.TYPE==prev.TYPE)){CHANGED=true;a.pop();if(stat.init){stat.init.definitions=prev.definitions.concat(stat.init.definitions)}else{stat.init=prev}a.push(stat);prev=stat}else{prev=stat;a.push(stat)}return a},[])}function negate_iifes(statements,compressor){statements.forEach(function(stat){if(stat instanceof Cola.AST_SimpleStatement){stat.body=function transform(thing){return thing.transform(new Cola.TreeTransformer(function(node){if(node instanceof Cola.AST_Call&&node.expression instanceof Cola.AST_Function){return make_node(Cola.AST_UnaryPrefix,node,{operator:"!",expression:node})}else if(node instanceof Cola.AST_Call){node.expression=transform(node.expression)}else if(node instanceof Cola.AST_Seq){node.car=transform(node.car)}else if(node instanceof Cola.AST_Conditional){var expr=transform(node.condition);if(expr!==node.condition){node.condition=expr;var tmp=node.consequent;node.consequent=node.alternative;node.alternative=tmp}}return node}))}(stat.body)}})}}function extract_declarations_from_unreachable_code(compressor,stat,target){compressor.warn("Dropping unreachable code [{file}:{line},{col}]",stat.start);stat.walk(new Cola.TreeWalker(function(node){if(node instanceof Cola.AST_Definitions){compressor.warn("Declarations in unreachable code! [{file}:{line},{col}]",node.start);node.remove_initializers();target.push(node);return true}if(node instanceof Cola.AST_Defun){target.push(node);return true}if(node instanceof Cola.AST_Scope){return true}}))}(function(def){var unary_bool=["!","delete"];var binary_bool=["in","instanceof","==","!=","===","!==","<","<=",">=",">"];def(Cola.AST_Node,function(){return false});def(Cola.AST_UnaryPrefix,function(){return Cola.member(this.operator,unary_bool)});def(Cola.AST_Binary,function(){return Cola.member(this.operator,binary_bool)||(this.operator=="&&"||this.operator=="||")&&this.left.is_boolean()&&this.right.is_boolean()});def(Cola.AST_Conditional,function(){return this.consequent.is_boolean()&&this.alternative.is_boolean()});def(Cola.AST_Assign,function(){return this.operator=="="&&this.right.is_boolean()});def(Cola.AST_Seq,function(){return this.cdr.is_boolean()});def(Cola.AST_True,function(){return true});def(Cola.AST_False,function(){return true})})(function(node,func){node.DEFMETHOD("is_boolean",func)});(function(def){def(Cola.AST_Node,function(){return false});def(Cola.AST_String,function(){return true});def(Cola.AST_UnaryPrefix,function(){return this.operator=="typeof"});def(Cola.AST_Binary,function(compressor){return this.operator=="+"&&(this.left.is_string(compressor)||this.right.is_string(compressor))});def(Cola.AST_Assign,function(compressor){return(this.operator=="="||this.operator=="+=")&&this.right.is_string(compressor)});def(Cola.AST_Seq,function(compressor){return this.cdr.is_string(compressor)});def(Cola.AST_Conditional,function(compressor){return th
a[a.length-1]=[make_node_from_constant(compressor,val,last[0]),val]}else{a.push(el)}}return a},[]);if(elements.length==0)return make_node(Cola.AST_String,self,{value:""});if(elements.length==1)return elements[0][0];if(separator==""){var first;if(elements[0][0]instanceof Cola.AST_String||elements[1][0]instanceof Cola.AST_String){first=elements.shift()[0]}else{first=make_node(Cola.AST_String,self,{value:""})}return elements.reduce(function(prev,el){return make_node(Cola.AST_Binary,el[0],{operator:"+",left:prev,right:el[0]})},first).transform(compressor)}var node=self.clone();node.expression=node.expression.clone();node.expression.expression=node.expression.expression.clone();node.expression.expression.elements=elements.map(function(el){return el[0]});return best_of(self,node)}}if(compressor.option("side_effects")){if(self.expression instanceof Cola.AST_Function&&self.args.length==0&&!Cola.AST_Block.prototype.has_side_effects.call(self.expression,compressor)){return make_node(Cola.AST_Undefined,self).transform(compressor)}}if(compressor.option("drop_console")){if(self.expression instanceof Cola.AST_PropAccess&&self.expression.expression instanceof Cola.AST_SymbolRef&&self.expression.expression.name=="console"&&self.expression.expression.undeclared()){return make_node(Cola.AST_Undefined,self).transform(compressor)}}return self.evaluate(compressor)[0]});OPT(Cola.AST_New,function(self,compressor){if(compressor.option("unsafe")){var exp=self.expression;if(exp instanceof Cola.AST_SymbolRef&&exp.undeclared()){switch(exp.name){case"Object":case"RegExp":case"Function":case"Error":case"Array":return make_node(Cola.AST_Call,self,self).transform(compressor)}}}return self});OPT(Cola.AST_Seq,function(self,compressor){if(!compressor.option("side_effects"))return self;if(!self.car.has_side_effects(compressor)){var p;if(!(self.cdr instanceof Cola.AST_SymbolRef&&self.cdr.name=="eval"&&self.cdr.undeclared()&&(p=compressor.parent())instanceof Cola.AST_Call&&p.expression===self)){return self.cdr}}if(compressor.option("cascade")){if(self.car instanceof Cola.AST_Assign&&!self.car.left.has_side_effects(compressor)){if(self.car.left.equivalent_to(self.cdr)){return self.car}if(self.cdr instanceof Cola.AST_Call&&self.cdr.expression.equivalent_to(self.car.left)){self.cdr.expression=self.car;return self.cdr}}if(!self.car.has_side_effects(compressor)&&!self.cdr.has_side_effects(compressor)&&self.car.equivalent_to(self.cdr)){return self.car}}if(self.cdr instanceof Cola.AST_UnaryPrefix&&self.cdr.operator=="void"&&!self.cdr.expression.has_side_effects(compressor)){self.cdr.operator=self.car;return self.cdr}if(self.cdr instanceof Cola.AST_Undefined){return make_node(Cola.AST_UnaryPrefix,self,{operator:"void",expression:self.car})}return self});Cola.AST_Unary.DEFMETHOD("lift_sequences",function(compressor){if(compressor.option("sequences")){if(this.expression instanceof Cola.AST_Seq){var seq=this.expression;var x=seq.to_array();this.expression=x.pop();x.push(this);seq=Cola.AST_Seq.from_array(x).transform(compressor);return seq}}return this});OPT(Cola.AST_UnaryPostfix,function(self,compressor){return self.lift_sequences(compressor)});OPT(Cola.AST_UnaryPrefix,function(self,compressor){self=self.lift_sequences(compressor);var e=self.expression;if(compressor.option("booleans")&&compressor.in_boolean_context()){switch(self.operator){case"!":if(e instanceof Cola.AST_UnaryPrefix&&e.operator=="!"){return e.expression}break;case"typeof":compressor.warn("Boolean expression always true [{file}:{line},{col}]",self.start);return make_node(Cola.AST_True,self)}if(e instanceof Cola.AST_Binary&&self.operator=="!"){self=best_of(self,e.negate(compressor))}}return self.evaluate(compressor)[0]});function has_side_effects_or_prop_access(node,compressor){var save_pure_getters=compressor.option("pure_getters");compressor.options.pure_getters=false;var ret=node.has_side_effects(compressor);compressor.options.pure_getters=save_pure_getters;return ret}Cola.AST_Binary.DEFMETHOD("lift_sequences",function(compressor){if(compressor.option("sequences")){if(this.left instanceof Cola.AST_S
};Cola.FuncAsync=function(func){if(!func.body.length)return func;var newBody=[];if(func.argnames.length)newBody.push(new Cola.AST_Var({mods:[],type:"dynamic",definitions:[new Cola.AST_VarDef({type:"dynamic",name:new Cola.AST_SymbolVar({name:"_ColaRuntime$$arguments"}),value:new Cola.AST_SymbolRef({name:"arguments"})})]}));newBody.push(new Cola.AST_Return({value:new Cola.AST_New({args:[],expression:new Cola.AST_SymbolRef({name:"Promise"})})}));newBody[newBody.length-1].value.args.push(new Cola.AST_Call({args:[new Cola.AST_This],expression:new Cola.AST_Dot({expression:false,property:"bind"})}));func.body.unshift(new Cola.AST_SimpleStatement({body:new Cola.AST_Assign({left:new Cola.AST_SymbolRef({name:"arguments"}),operator:"=",right:func.argnames.length?new Cola.AST_SymbolRef({name:"_ColaRuntime$$arguments"}):new Cola.AST_Object})}));newBody[newBody.length-1].value.args[0].expression.expression=new Cola.AST_Function({body:func.body,argnames:[new Cola.AST_SymbolFunarg({name:"_ColaRuntime$$resolve"}),new Cola.AST_SymbolFunarg({name:"_ColaRuntime$$reject"})]});func.body=newBody;return func};Cola.PushModuleToCommonWrapper=function(module,commonWrapper,name,id){commonWrapper.body.args[0].elements.push(new Cola.AST_Function({body:module.body,argnames:[new Cola.AST_SymbolFunarg({name:"require"}),new Cola.AST_SymbolFunarg({name:"module"}),new Cola.AST_SymbolFunarg({name:"exports"})]}));if(!name||!id)return;commonWrapper.body.args[2].properties.push(new Cola.AST_ObjectKeyVal({key:name,value:new Cola.AST_Number({value:id})}))};Cola.AST_Toplevel.prototype.toJavaScript=function(options){if(this.language=="js")return this;this.language="js";options=Cola.defaults(options,{main_binding:true,main_event:"DOMContentLoaded",parser:{},is_node:false,std_hash:{},path:"",modules:{},root:true});var _ColaRuntime$$ast=Cola.parse(Cola._ColaRuntime,{is_js:true}),_ColaRuntime$$hash=options.std_hash,_this=this,deep=0,required=[],required_hash={},exports=[],awaitData={"with":false,level:null,indent:-1,expression:[],origExpression:[],body:[]},tt=new Cola.TreeTransformer(function(node,descend,in_list){var newNode,props={},parent=this.parent();node=node.clone();if(node instanceof Cola.AST_Defun&&node.mods.indexOf("async")!=-1){node=Cola.FuncAsync(node)}if(options.main_binding&&parent instanceof Cola.AST_Toplevel&&node instanceof Cola.AST_Defun&&node.name instanceof Cola.AST_SymbolDefun&&node.name.name=="main"){node.name=new Cola.AST_SymbolLambda(node.name);node=new Cola.AST_Function(node);props=!options.is_node?{args:[new Cola.AST_String({value:options.main_event}),node,new Cola.AST_False],expression:new Cola.AST_Dot({property:"addEventListener",expression:new Cola.AST_SymbolRef({name:"window"})})}:{args:[],expression:node};node=new Cola.AST_SimpleStatement({start:node.start,end:node.left,body:new Cola.AST_Call(props)})}else if(node instanceof Cola.AST_Binary&&node.operator=="**"){props={start:node.start,end:node.left,args:[node.left,node.right],expression:new Cola.AST_Dot({property:"pow",expression:new Cola.AST_SymbolRef({name:"Math"})})};node=new Cola.AST_Call(props)}else if(node instanceof Cola.AST_Binary&&node.operator=="%%"){_ColaRuntime$$hash[Cola._ColaRuntime$$modulo.i]=true;props={start:node.start,end:node.end,args:[node.left,node.right],expression:new Cola.AST_SymbolRef({name:"_ColaRuntime$$modulo"})};node=new Cola.AST_Call(props)}else if(node instanceof Cola.AST_SimpleStatement&&(props=Cola.ContainCondAccess(node.body))){if(node.body instanceof Cola.AST_CondAccess)node.body=new Cola.AST_Dot(node.body);else{var expr=node.body;while(expr.expression instanceof Cola.AST_Call||expr.expression instanceof Cola.AST_PropAccess){if(expr.expression instanceof Cola.AST_CondAccess){expr.expression=new Cola.AST_Dot(expr.expression);break}expr=expr.expression}}node=new Cola.AST_If({condition:Cola.Constructions.IsSet(props),body:node})}else if(props=Cola.ContainCondAccess(node)){if(node instanceof Cola.AST_CondAccess)node=new Cola.AST_Dot(node);else{var expr=node;while(expr.expression instanceof Cola.AST_Call||expr.expression instanceof Cola.AST_PropAccess){if(
}}else if(node instanceof Cola.AST_Cascade){_ColaRuntime$$hash[Cola._ColaRuntime$$arguments_def.i]=true;props={type:"dynamic",body:[],argnames:[new Cola.AST_ArgDef({argtype:"positional",type:"dynamic",defval:new Cola.AST_Noop,name:new Cola.AST_SymbolFunarg({name:"_ColaRuntime$$expr",start:node.expression.start,end:node.expression.end})}),new Cola.AST_ArgDef({argtype:"positional",type:"dynamic",defval:new Cola.AST_Noop,name:new Cola.AST_SymbolFunarg({name:"arguments",start:new Cola.AST_Token,end:new Cola.AST_Token})})]};var Expr,Parent=false;node.subexpressions.forEach(function(expr){Expr=expr,Parent=false;while(true)if(expr instanceof Cola.AST_Call||expr instanceof Cola.AST_PropAccess){Parent=expr;expr=expr.expression}else if(expr instanceof Cola.AST_Binary){Parent=expr;expr=expr.left}else if(expr instanceof Cola.AST_Array||expr instanceof Cola.AST_ArrayRange||expr instanceof Cola.AST_SymbolRef)break;if(!Parent){if(expr instanceof Cola.AST_Array||expr instanceof Cola.AST_ArrayRange)Expr=new Cola.AST_Sub({start:Expr.start,end:Expr.end,expression:new Cola.AST_SymbolRef({name:"_ColaRuntime$$expr"}),property:expr instanceof Cola.AST_ArrayRange?expr:expr.elements.length==0?new Cola.AST_Noop:expr.elements[0]});else if(expr instanceof Cola.AST_SymbolRef)Expr=new Cola.AST_Dot({start:Expr.start,end:Expr.end,expression:new Cola.AST_SymbolRef({name:"_ColaRuntime$$expr"}),property:Expr.name})}else{if(expr instanceof Cola.AST_Array||expr instanceof Cola.AST_ArrayRange)expr=new Cola.AST_Sub({start:expr.start,end:expr.end,expression:new Cola.AST_SymbolRef({name:"_ColaRuntime$$expr"}),property:expr instanceof Cola.AST_ArrayRange?expr:expr.elements.length==0?new Cola.AST_Noop:expr.elements[0]});else expr=new Cola.AST_Dot({start:expr.start,end:expr.end,expression:new Cola.AST_SymbolRef({name:"_ColaRuntime$$expr"}),property:expr.name});if(Parent instanceof Cola.AST_Call||Parent instanceof Cola.AST_PropAccess)Parent.expression=expr;else if(Parent instanceof Cola.AST_Binary)Parent.left=expr}props.body.push(new Cola.AST_SimpleStatement({start:Expr.start,end:Expr.end,body:Expr}))});props.body.push(new Cola.AST_Return({value:new Cola.AST_SymbolRef({name:"_ColaRuntime$$expr"})}));props={expression:new Cola.AST_Function(props),property:"call"};node=new Cola.AST_Call({start:node.start,end:node.end,args:[new Cola.AST_SymbolRef({name:"this"}),node.expression,new Cola.AST_SymbolRef({name:"arguments"})],expression:new Cola.AST_Dot(props)})}else if(node.body instanceof Cola.AST_Assign&&(node.body.left instanceof Cola.AST_ArrayTemplate||node.body.left instanceof Cola.AST_ObjectTemplate||(node.body.left instanceof Cola.AST_Array||node.body.left instanceof Cola.AST_Object)&&node.body.left.template)&&node instanceof Cola.AST_SimpleStatement){node=node.body;var defs=[];var Symbol=node.right instanceof Cola.AST_SymbolRef?node.right:new Cola.AST_SymbolRef({name:"_ColaRuntime$$tmp"});if(!(node.right instanceof Cola.AST_SymbolRef))defs.push(new Cola.AST_VarDef({type:"dynamic",name:new Cola.AST_SymbolVar(Symbol),value:node.right}));(function _rec(def,symbol,uid){var skiped=false,k=0,is_arrayt=def instanceof Cola.AST_Array||def instanceof Cola.AST_ArrayTemplate,_=is_arrayt?def.elements:def.properties;_.forEach(is_arrayt?function(el,j){if(el instanceof Cola.AST_VarDef&&el.name.splated){skiped=true;defs.push(new Cola.AST_VarDef({type:"int",name:new Cola.AST_SymbolVar({name:"_ColaRuntime$$"+uid+"i"}),value:null}));el.name.splated=undefined;el.value=Cola.Constructions.SplatedConditional(symbol,uid,j,_.length-j-1,_.length);defs.push(el)}else if((el instanceof Cola.AST_SymbolRef||el instanceof Cola.AST_PropAccess)&&el.splated){skiped=true;defs.push(new Cola.AST_VarDef({type:"int",name:new Cola.AST_SymbolVar({name:"_ColaRuntime$$"+uid+"i"}),value:null}));el.splated=undefined;defs.push(new Cola.AST_Assign({operator:node.operator,left:el,right:Cola.Constructions.SplatedConditional(symbol,uid,j,_.length-j-1,_.length)}))}else if(el instanceof Cola.AST_VarDef){el.value=Cola.Constructions.ValueWithOffset(symbol,uid,!skiped,j,k++);defs.push(el)}else if(el instanceof Cola.AST_