coffeescript.js
3.75 KB
CodeMirror.defineMode("coffeescript",function(e){function t(e){return new RegExp("^(("+e.join(")|(")+"))\\b")}function n(e,t){if(e.sol()){var n=t.scopes[0].offset;if(e.eatSpace()){var i=e.indentation();return i>n?"indent":i<n?"dedent":null}n>0&&f(e,t)}if(e.eatSpace())return null;var c=e.peek();if(e.match("####"))return e.skipToEnd(),"comment";if(e.match("###"))return t.tokenize=o,t.tokenize(e,t);if("#"===c)return e.skipToEnd(),"comment";if(e.match(/^-?[0-9\.]/,!1)){var k=!1;if(e.match(/^-?\d*\.\d+(e[\+\-]?\d+)?/i)&&(k=!0),e.match(/^-?\d+\.\d*/)&&(k=!0),e.match(/^-?\.\d+/)&&(k=!0),k)return"."==e.peek()&&e.backUp(1),"number";var g=!1;if(e.match(/^-?0x[0-9a-f]+/i)&&(g=!0),e.match(/^-?[1-9]\d*(e[\+\-]?\d+)?/)&&(g=!0),e.match(/^-?0(?![\dx])/i)&&(g=!0),g)return"number"}if(e.match(x))return t.tokenize=r(e.current(),"string"),t.tokenize(e,t);if(e.match(v)){if("/"!=e.current()||e.match(/^.*\//,!1))return t.tokenize=r(e.current(),"string-2"),t.tokenize(e,t);e.backUp(1)}return e.match(l)||e.match(d)?"punctuation":e.match(p)||e.match(s)||e.match(m)?"operator":e.match(u)?"punctuation":e.match(y)?"atom":e.match(b)?"keyword":e.match(h)?"variable":(e.next(),a)}function r(t,r){var o=1==t.length;return function(i,f){for(;!i.eol();)if(i.eatWhile(/[^'"\/\\]/),i.eat("\\")){if(i.next(),o&&i.eol())return r}else{if(i.match(t))return f.tokenize=n,r;i.eat(/['"\/]/)}return o&&(e.mode.singleLineStringErrors?r=a:f.tokenize=n),r}}function o(e,t){for(;!e.eol();){if(e.eatWhile(/[^#]/),e.match("###")){t.tokenize=n;break}e.eatWhile("#")}return"comment"}function i(t,n,r){r=r||"coffee";var o=0;if("coffee"===r){for(var i=0;i<n.scopes.length;i++)if("coffee"===n.scopes[i].type){o=n.scopes[i].offset+e.indentUnit;break}}else o=t.column()+t.current().length;n.scopes.unshift({offset:o,type:r})}function f(e,t){if(1!=t.scopes.length){if("coffee"===t.scopes[0].type){for(var n=e.indentation(),r=-1,o=0;o<t.scopes.length;++o)if(n===t.scopes[o].offset){r=o;break}if(r===-1)return!0;for(;t.scopes[0].offset!==n;)t.scopes.shift();return!1}return t.scopes.shift(),!1}}function c(e,t){var n=t.tokenize(e,t),r=e.current();if("."===r)return n=t.tokenize(e,t),r=e.current(),"variable"===n?"variable":a;if("@"===r)return e.eat("@"),"keyword";"return"===r&&(t.dedent+=1),("->"!==r&&"=>"!==r||t.lambda||"coffee"!=t.scopes[0].type||""!==e.peek())&&"indent"!==n||i(e,t);var o="[({".indexOf(r);return o!==-1&&i(e,t,"])}".slice(o,o+1)),k.exec(r)&&i(e,t),"then"==r&&f(e,t),"dedent"===n&&f(e,t)?a:(o="])}".indexOf(r),o!==-1&&f(e,t)?a:(t.dedent>0&&e.eol()&&"coffee"==t.scopes[0].type&&(t.scopes.length>1&&t.scopes.shift(),t.dedent-=1),n))}var a="error",s=new RegExp("^[\\+\\-\\*/%&|\\^~<>!?]"),u=new RegExp("^[\\(\\)\\[\\]\\{\\}@,:`=;\\.]"),p=new RegExp("^((->)|(=>)|(\\+\\+)|(\\+\\=)|(\\-\\-)|(\\-\\=)|(\\*\\*)|(\\*\\=)|(\\/\\/)|(\\/\\=)|(==)|(!=)|(<=)|(>=)|(<>)|(<<)|(>>)|(//))"),d=new RegExp("^((\\.\\.)|(\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&=)|(\\|=)|(\\^=))"),l=new RegExp("^((\\.\\.\\.)|(//=)|(>>=)|(<<=)|(\\*\\*=))"),h=new RegExp("^[_A-Za-z$][_A-Za-z$0-9]*"),m=t(["and","or","not","is","isnt","in","instanceof","typeof"]),k=["for","while","loop","if","unless","else","switch","try","catch","finally","class"],g=["break","by","continue","debugger","delete","do","in","of","new","return","then","this","throw","when","until"],b=t(k.concat(g));k=t(k);var x=new RegExp("^('{3}|\"{3}|['\"])"),v=new RegExp("^(/{3}|/)"),w=["Infinity","NaN","undefined","null","true","false","on","off","yes","no"],y=t(w),z={startState:function(e){return{tokenize:n,scopes:[{offset:e||0,type:"coffee"}],lastToken:null,lambda:!1,dedent:0}},token:function(e,t){var n=c(e,t);return t.lastToken={style:n,content:e.current()},e.eol()&&e.lambda&&(t.lambda=!1),n},indent:function(e,t){return e.tokenize!=n?0:e.scopes[0].offset}};return z}),CodeMirror.defineMIME("text/x-coffeescript","coffeescript");