jashkenas--coffeescript/extras/coffee-script.js

8 lines
322 KiB
JavaScript
Raw Normal View History

/**
2010-08-03 04:06:34 +00:00
* CoffeeScript Compiler v0.9.0
* http://coffeescript.org
*
* Copyright 2010, Jeremy Ashkenas
* Released under the MIT License
*/
(function(){var compact,count,del,ends,extend,flatten,helpers,include,indexOf,merge,starts;if(!(typeof process!=="undefined"&&process!==null)){this.exports=this}helpers=(exports.helpers={});helpers.indexOf=(indexOf=function(array,item,from){var _a,_b,index,other;if(array.indexOf){return array.indexOf(item,from)}_a=array;for(index=0,_b=_a.length;index<_b;index++){other=_a[index];if(other===item&&(!from||(from<=index))){return index}}return -1});helpers.include=(include=function(list,value){return indexOf(list,value)>=0});helpers.starts=(starts=function(string,literal,start){return string.substring(start,(start||0)+literal.length)===literal});helpers.ends=(ends=function(string,literal,back){var start;start=string.length-literal.length-((typeof back!=="undefined"&&back!==null)?back:0);return string.substring(start,start+literal.length)===literal});helpers.compact=(compact=function(array){var _a,_b,_c,_d,item;_a=[];_c=array;for(_b=0,_d=_c.length;_b<_d;_b++){item=_c[_b];item?_a.push(item):null}return _a});helpers.count=(count=function(string,letter){var num,pos;num=0;pos=indexOf(string,letter);while(pos!==-1){num+=1;pos=indexOf(string,letter,pos+1)}return num});helpers.merge=(merge=function(options,overrides){var _a,_b,fresh,key,val;fresh={};_a=options;for(key in _a){val=_a[key];(fresh[key]=val)}if(overrides){_b=overrides;for(key in _b){val=_b[key];(fresh[key]=val)}}return fresh});helpers.extend=(extend=function(object,properties){var _a,_b,key,val;_a=[];_b=properties;for(key in _b){val=_b[key];_a.push(object[key]=val)}return _a});helpers.flatten=(flatten=function(array){var _a,_b,_c,item,memo;memo=[];_b=array;for(_a=0,_c=_b.length;_a<_c;_a++){item=_b[_a];item instanceof Array?(memo=memo.concat(item)):memo.push(item)}return memo});helpers.del=(del=function(obj,key){var val;val=obj[key];delete obj[key];return val})})();(function(){var BALANCED_PAIRS,EXPRESSION_CLOSE,EXPRESSION_END,EXPRESSION_START,IMPLICIT_BLOCK,IMPLICIT_CALL,IMPLICIT_END,IMPLICIT_FUNC,INVERSES,Rewriter,SINGLE_CLOSERS,SINGLE_LINERS,_a,_b,_c,_d,_e,_f,_g,_h,_i,_j,_k,_l,_m,helpers,include,pair;var __bind=function(func,context){return function(){return func.apply(context,arguments)}},__hasProp=Object.prototype.hasOwnProperty;if(typeof process!=="undefined"&&process!==null){_a=require("./helpers");helpers=_a.helpers}else{this.exports=this;helpers=this.helpers}_b=helpers;include=_b.include;exports.Rewriter=(function(){Rewriter=function(){};Rewriter.prototype.rewrite=function(tokens){this.tokens=tokens;this.adjustComments();this.removeLeadingNewlines();this.removeMidExpressionNewlines();this.closeOpenCallsAndIndexes();this.addImplicitIndentation();this.addImplicitBraces();this.addImplicitParentheses();this.ensureBalance(BALANCED_PAIRS);this.rewriteClosingParens();return this.tokens};Rewriter.prototype.scanTokens=function(block){var i,move;i=0;while(true){if(!(this.tokens[i])){break}move=block(this.tokens[i-1],this.tokens[i],this.tokens[i+1],i);i+=move}return true};Rewriter.prototype.adjustComments=function(){return this.scanTokens(__bind(function(prev,token,post,i){var _c,_d,after,before;if(!(token[0]==="HERECOMMENT")){return 1}_c=[this.tokens[i-2],this.tokens[i+2]];before=_c[0];after=_c[1];if(after&&after[0]==="INDENT"){this.tokens.splice(i+2,1);before&&before[0]==="OUTDENT"&&post&&(prev[0]===post[0])&&(post[0]==="TERMINATOR")?this.tokens.splice(i-2,1):this.tokens.splice(i,0,after)}else{if(prev&&!("TERMINATOR"===(_d=prev[0])||"INDENT"===_d||"OUTDENT"===_d)){if(post&&post[0]==="TERMINATOR"&&after&&after[0]==="OUTDENT"){this.tokens.splice.apply(this.tokens,[i+2,0].concat(this.tokens.splice(i,2)));this.tokens[i+2][0]!=="TERMINATOR"?this.tokens.splice(i+2,0,["TERMINATOR","\n",prev[2]]):null}else{this.tokens.splice(i,0,["TERMINATOR","\n",prev[2]])}return 2}}return 1},this))};Rewriter.prototype.removeLeadingNewlines=function(){var _c;_c=[];while(this.tokens[0]&&this.tokens[0][0]==="TERMINATOR"){_c.push(this.tokens.shift())}return _c};Rewriter.prototype.removeMidExpressionNewlines=function(){return this.scanTokens(__bind(function(prev,token,post,i){if(!(post&&include(EXPR