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

8 lines
322 KiB
JavaScript
Raw Normal View History

/**
2010-08-11 04:40:15 +00:00
* CoffeeScript Compiler v0.9.1
* http://coffeescript.org
*
* Copyright 2010, Jeremy Ashkenas
* Released under the MIT License
*/
2010-08-11 04:40:15 +00:00
(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];if(item){_a.push(item)}}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];if(item instanceof Array){memo=memo.concat(item)}else{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.closeOpenCalls();this.closeOpenIndexes();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],i);i+=move}return true};Rewriter.prototype.detectEnd=function(i,condition,action){var levels,token;levels=0;while(true){token=this.tokens[i];if(levels===0&&condition.call(this,token,i)){return action.call(this,token,i)}if(!token||levels<0){return action.call(this,token,i-1)}if(include(EXPRESSION_START,token[0])){levels+=1}if(include(EXPRESSION_END,token[0])){levels-=1}i+=1}return i-1};Rewriter.prototype.adjustComments=function(){return this.scanTokens(__bind(function(token,i){var _c,_d,after,before,post,prev;if(!(token[0]==="HERECOMMENT")){return 1}_c=[this.tokens[i-2],this.tokens[i-1],this.tokens[i+1],this.tokens[i+2]];before=_c[0];prev=_c[1];post=_c[2];after=_c[3];if(after&&after[0]==="INDENT"){this.tokens.splice(i+2,1);if(before&&before[0]==="OUTDENT"&&post&&(prev[0]===post[0])&&(post[0]==="TERMINATOR")){this.tokens.splice(i-2,1)}else{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)));if(this.tokens[i+2][0]!=="TERMINATOR"){this.tokens.spli