python.js 4.47 KB
CodeMirror.defineMode("python",function(e,t){function n(e){return new RegExp("^(("+e.join(")|(")+"))\\b")}function r(e,t){if(e.sol()){var n=t.scopes[0].offset;if(e.eatSpace()){var r=e.indentation();return r>n?E="indent":r<n&&(E="dedent"),null}n>0&&o(e,t)}if(e.eatSpace())return null;var a=e.peek();if("#"===a)return e.skipToEnd(),"comment";if(e.match(/^[0-9\.]/,!1)){var s=!1;if(e.match(/^\d*\.\d+(e[\+\-]?\d+)?/i)&&(s=!0),e.match(/^\d+\.\d*/)&&(s=!0),e.match(/^\.\d+/)&&(s=!0),s)return e.eat(/J/i),"number";var b=!1;if(e.match(/^0x[0-9a-f]+/i)&&(b=!0),e.match(/^0b[01]+/i)&&(b=!0),e.match(/^0o[0-7]+/i)&&(b=!0),e.match(/^[1-9]\d*(e[\+\-]?\d+)?/)&&(e.eat(/J/i),b=!0),e.match(/^0(?![\dx])/i)&&(b=!0),b)return e.eat(/L/i),"number"}return e.match(v)?(t.tokenize=i(e.current()),t.tokenize(e,t)):e.match(d)||e.match(u)?null:e.match(f)||e.match(c)||e.match(h)?"operator":e.match(p)?null:e.match(k)?"keyword":e.match(w)?"builtin":e.match(m)?"variable":(e.next(),l)}function i(e){for(;"rub".indexOf(e.charAt(0).toLowerCase())>=0;)e=e.substr(1);var n=1==e.length,i="string";return function(a,o){for(;!a.eol();)if(a.eatWhile(/[^'"\\]/),a.eat("\\")){if(a.next(),n&&a.eol())return i}else{if(a.match(e))return o.tokenize=r,i;a.eat(/['"]/)}if(n){if(t.singleLineStringErrors)return l;o.tokenize=r}return i}}function a(t,n,r){r=r||"py";var i=0;if("py"===r){if("py"!==n.scopes[0].type)return void(n.scopes[0].offset=t.indentation());for(var a=0;a<n.scopes.length;++a)if("py"===n.scopes[a].type){i=n.scopes[a].offset+e.indentUnit;break}}else i=t.column()+t.current().length;n.scopes.unshift({offset:i,type:r})}function o(e,t,n){if(n=n||"py",1!=t.scopes.length){if("py"===t.scopes[0].type){for(var r=e.indentation(),i=-1,a=0;a<t.scopes.length;++a)if(r===t.scopes[a].offset){i=a;break}if(i===-1)return!0;for(;t.scopes[0].offset!==r;)t.scopes.shift();return!1}return"py"===n?(t.scopes[0].offset=e.indentation(),!1):t.scopes[0].type!=n||(t.scopes.shift(),!1)}}function s(e,t){E=null;var n=t.tokenize(e,t),r=e.current();if("."===r)return n=e.match(m,!1)?null:l,null===n&&"meta"===t.lastToken&&(n="meta"),n;if("@"===r)return e.match(m,!1)?"meta":l;"variable"!==n&&"builtin"!==n||"meta"!==t.lastToken||(n="meta"),"pass"!==r&&"return"!==r||(t.dedent+=1),"lambda"===r&&(t.lambda=!0),(":"===r&&!t.lambda&&"py"==t.scopes[0].type||"indent"===E)&&a(e,t);var i="[({".indexOf(r);return i!==-1&&a(e,t,"])}".slice(i,i+1)),"dedent"===E&&o(e,t)?l:(i="])}".indexOf(r),i!==-1&&o(e,t,r)?l:(t.dedent>0&&e.eol()&&"py"==t.scopes[0].type&&(t.scopes.length>1&&t.scopes.shift(),t.dedent-=1),n))}var l="error",c=new RegExp("^[\\+\\-\\*/%&|\\^~<>!]"),p=new RegExp("^[\\(\\)\\[\\]\\{\\}@,:`=;\\.]"),f=new RegExp("^((==)|(!=)|(<=)|(>=)|(<>)|(<<)|(>>)|(//)|(\\*\\*))"),u=new RegExp("^((\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&=)|(\\|=)|(\\^=))"),d=new RegExp("^((//=)|(>>=)|(<<=)|(\\*\\*=))"),m=new RegExp("^[_A-Za-z][_A-Za-z0-9]*"),h=n(["and","or","not","is","in"]),b=["as","assert","break","class","continue","def","del","elif","else","except","finally","for","from","global","if","import","lambda","pass","raise","return","try","while","with","yield"],y=["abs","all","any","bin","bool","bytearray","callable","chr","classmethod","compile","complex","delattr","dict","dir","divmod","enumerate","eval","filter","float","format","frozenset","getattr","globals","hasattr","hash","help","hex","id","input","int","isinstance","issubclass","iter","len","list","locals","map","max","memoryview","min","next","object","oct","open","ord","pow","property","range","repr","reversed","round","set","setattr","slice","sorted","staticmethod","str","sum","super","tuple","type","vars","zip","__import__","NotImplemented","Ellipsis","__debug__"],x={builtins:["apply","basestring","buffer","cmp","coerce","execfile","file","intern","long","raw_input","reduce","reload","unichr","unicode","xrange","False","True","None"],keywords:["exec","print"]},g={builtins:["ascii","bytes","exec","print"],keywords:["nonlocal","False","True","None"]};if(t.version&&3===parseInt(t.version,10)){b=b.concat(g.keywords),y=y.concat(g.builtins);var v=new RegExp("^(([rb]|(br))?('{3}|\"{3}|['\"]))","i")}else{b=b.concat(x.keywords),y=y.concat(x.builtins);var v=new RegExp("^(([rub]|(ur)|(br))?('{3}|\"{3}|['\"]))","i")}var k=n(b),w=n(y),E=null,z={startState:function(e){return{tokenize:r,scopes:[{offset:e||0,type:"py"}],lastToken:null,lambda:!1,dedent:0}},token:function(e,t){var n=s(e,t);return t.lastToken=n,e.eol()&&e.lambda&&(t.lambda=!1),n},indent:function(e,t){return e.tokenize!=r?0:e.scopes[0].offset}};return z}),CodeMirror.defineMIME("text/x-python","python");