cloudcmd/lib/client/editor/codemirror/pack/javascript.pack.js
2012-08-02 08:33:19 -04:00

1 line
No EOL
8.8 KiB
JavaScript

CodeMirror.defineMode("javascript",function(config,parserConfig){function chain(stream,state,f){return state.tokenize=f,f(stream,state)}function nextUntilUnescaped(stream,end){var escaped=!1,next;while((next=stream.next())!=null){if(next==end&&!escaped)return!1;escaped=!escaped&&next=="\\"}return escaped}function ret(tp,style,cont){return type=tp,content=cont,style}function jsTokenBase(stream,state){var ch=stream.next();if(ch=='"'||ch=="'")return chain(stream,state,jsTokenString(ch));if(/[\[\]{}\(\),;\:\.]/.test(ch))return ret(ch);if(ch=="0"&&stream.eat(/x/i))return stream.eatWhile(/[\da-f]/i),ret("number","number");if(/\d/.test(ch)||ch=="-"&&stream.eat(/\d/))return stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/),ret("number","number");if(ch=="/")return stream.eat("*")?chain(stream,state,jsTokenComment):stream.eat("/")?(stream.skipToEnd(),ret("comment","comment")):state.reAllowed?(nextUntilUnescaped(stream,"/"),stream.eatWhile(/[gimy]/),ret("regexp","string-2")):(stream.eatWhile(isOperatorChar),ret("operator",null,stream.current()));if(ch=="#")return stream.skipToEnd(),ret("error","error");if(isOperatorChar.test(ch))return stream.eatWhile(isOperatorChar),ret("operator",null,stream.current());stream.eatWhile(/[\w\$_]/);var word=stream.current(),known=keywords.propertyIsEnumerable(word)&&keywords[word];return known&&state.kwAllowed?ret(known.type,known.style,word):ret("variable","variable",word)}function jsTokenString(quote){return function(stream,state){return nextUntilUnescaped(stream,quote)||(state.tokenize=jsTokenBase),ret("string","string")}}function jsTokenComment(stream,state){var maybeEnd=!1,ch;while(ch=stream.next()){if(ch=="/"&&maybeEnd){state.tokenize=jsTokenBase;break}maybeEnd=ch=="*"}return ret("comment","comment")}function JSLexical(indented,column,type,align,prev,info){this.indented=indented,this.column=column,this.type=type,this.prev=prev,this.info=info,align!=null&&(this.align=align)}function inScope(state,varname){for(var v=state.localVars;v;v=v.next)if(v.name==varname)return!0}function parseJS(state,style,type,content,stream){var cc=state.cc;cx.state=state,cx.stream=stream,cx.marked=null,cx.cc=cc,state.lexical.hasOwnProperty("align")||(state.lexical.align=!0);for(;;){var combinator=cc.length?cc.pop():jsonMode?expression:statement;if(combinator(type,content)){while(cc.length&&cc[cc.length-1].lex)cc.pop()();return cx.marked?cx.marked:type=="variable"&&inScope(state,content)?"variable-2":style}}}function pass(){for(var i=arguments.length-1;i>=0;i--)cx.cc.push(arguments[i])}function cont(){return pass.apply(null,arguments),!0}function register(varname){var state=cx.state;if(state.context){cx.marked="def";for(var v=state.localVars;v;v=v.next)if(v.name==varname)return;state.localVars={name:varname,next:state.localVars}}}function pushcontext(){cx.state.context||(cx.state.localVars=defaultVars),cx.state.context={prev:cx.state.context,vars:cx.state.localVars}}function popcontext(){cx.state.localVars=cx.state.context.vars,cx.state.context=cx.state.context.prev}function pushlex(type,info){var result=function(){var state=cx.state;state.lexical=new JSLexical(state.indented,cx.stream.column(),type,null,state.lexical,info)};return result.lex=!0,result}function poplex(){var state=cx.state;state.lexical.prev&&(state.lexical.type==")"&&(state.indented=state.lexical.indented),state.lexical=state.lexical.prev)}function expect(wanted){return function(type){return type==wanted?cont():wanted==";"?pass():cont(arguments.callee)}}function statement(type){return type=="var"?cont(pushlex("vardef"),vardef1,expect(";"),poplex):type=="keyword a"?cont(pushlex("form"),expression,statement,poplex):type=="keyword b"?cont(pushlex("form"),statement,poplex):type=="{"?cont(pushlex("}"),block,poplex):type==";"?cont():type=="function"?cont(functiondef):type=="for"?cont(pushlex("form"),expect("("),pushlex(")"),forspec1,expect(")"),poplex,statement,poplex):type=="variable"?cont(pushlex("stat"),maybelabel):type=="switch"?cont(pushlex("form"),expression,pushlex("}","switch"),expect("{"),block,poplex,poplex):type=="case"?cont(expression,expect(":")):type=="default"?cont(expect(":")):type=="catch"?cont(pushlex("form"),pushcontext,expect("("),funarg,expect(")"),statement,poplex,popcontext):pass(pushlex("stat"),expression,expect(";"),poplex)}function expression(type){return atomicTypes.hasOwnProperty(type)?cont(maybeoperator):type=="function"?cont(functiondef):type=="keyword c"?cont(maybeexpression):type=="("?cont(pushlex(")"),maybeexpression,expect(")"),poplex,maybeoperator):type=="operator"?cont(expression):type=="["?cont(pushlex("]"),commasep(expression,"]"),poplex,maybeoperator):type=="{"?cont(pushlex("}"),commasep(objprop,"}"),poplex,maybeoperator):cont()}function maybeexpression(type){return type.match(/[;\}\)\],]/)?pass():pass(expression)}function maybeoperator(type,value){if(type=="operator"&&/\+\+|--/.test(value))return cont(maybeoperator);if(type=="operator"||type==":")return cont(expression);if(type==";")return;if(type=="(")return cont(pushlex(")"),commasep(expression,")"),poplex,maybeoperator);if(type==".")return cont(property,maybeoperator);if(type=="[")return cont(pushlex("]"),expression,expect("]"),poplex,maybeoperator)}function maybelabel(type){return type==":"?cont(poplex,statement):pass(maybeoperator,expect(";"),poplex)}function property(type){if(type=="variable")return cx.marked="property",cont()}function objprop(type){type=="variable"&&(cx.marked="property");if(atomicTypes.hasOwnProperty(type))return cont(expect(":"),expression)}function commasep(what,end){function proceed(type){return type==","?cont(what,proceed):type==end?cont():cont(expect(end))}return function(type){return type==end?cont():pass(what,proceed)}}function block(type){return type=="}"?cont():pass(statement,block)}function vardef1(type,value){return type=="variable"?(register(value),cont(vardef2)):cont()}function vardef2(type,value){if(value=="=")return cont(expression,vardef2);if(type==",")return cont(vardef1)}function forspec1(type){return type=="var"?cont(vardef1,forspec2):type==";"?pass(forspec2):type=="variable"?cont(formaybein):pass(forspec2)}function formaybein(type,value){return value=="in"?cont(expression):cont(maybeoperator,forspec2)}function forspec2(type,value){return type==";"?cont(forspec3):value=="in"?cont(expression):cont(expression,expect(";"),forspec3)}function forspec3(type){type!=")"&&cont(expression)}function functiondef(type,value){if(type=="variable")return register(value),cont(functiondef);if(type=="(")return cont(pushlex(")"),pushcontext,commasep(funarg,")"),poplex,statement,popcontext)}function funarg(type,value){if(type=="variable")return register(value),cont()}var indentUnit=config.indentUnit,jsonMode=parserConfig.json,keywords=function(){function kw(type){return{type:type,style:"keyword"}}var A=kw("keyword a"),B=kw("keyword b"),C=kw("keyword c"),operator=kw("operator"),atom={type:"atom",style:"atom"};return{"if":A,"while":A,"with":A,"else":B,"do":B,"try":B,"finally":B,"return":C,"break":C,"continue":C,"new":C,"delete":C,"throw":C,"var":kw("var"),"const":kw("var"),let:kw("var"),"function":kw("function"),"catch":kw("catch"),"for":kw("for"),"switch":kw("switch"),"case":kw("case"),"default":kw("default"),"in":operator,"typeof":operator,"instanceof":operator,"true":atom,"false":atom,"null":atom,"undefined":atom,NaN:atom,Infinity:atom}}(),isOperatorChar=/[+\-*&%=<>!?|]/,type,content,atomicTypes={atom:!0,number:!0,variable:!0,string:!0,regexp:!0},cx={state:null,column:null,marked:null,cc:null},defaultVars={name:"this",next:{name:"arguments"}};return poplex.lex=!0,{startState:function(basecolumn){return{tokenize:jsTokenBase,reAllowed:!0,kwAllowed:!0,cc:[],lexical:new JSLexical((basecolumn||0)-indentUnit,0,"block",!1),localVars:parserConfig.localVars,context:parserConfig.localVars&&{vars:parserConfig.localVars},indented:0}},token:function(stream,state){stream.sol()&&(state.lexical.hasOwnProperty("align")||(state.lexical.align=!1),state.indented=stream.indentation());if(stream.eatSpace())return null;var style=state.tokenize(stream,state);return type=="comment"?style:(state.reAllowed=type=="operator"||type=="keyword c"||!!type.match(/^[\[{}\(,;:]$/),state.kwAllowed=type!=".",parseJS(state,style,type,content,stream))},indent:function(state,textAfter){if(state.tokenize!=jsTokenBase)return 0;var firstChar=textAfter&&textAfter.charAt(0),lexical=state.lexical;lexical.type=="stat"&&firstChar=="}"&&(lexical=lexical.prev);var type=lexical.type,closing=firstChar==type;return type=="vardef"?lexical.indented+4:type=="form"&&firstChar=="{"?lexical.indented:type=="stat"||type=="form"?lexical.indented+indentUnit:lexical.info=="switch"&&!closing?lexical.indented+(/^(?:case|default)\b/.test(textAfter)?indentUnit:2*indentUnit):lexical.align?lexical.column+(closing?0:1):lexical.indented+(closing?0:indentUnit)},electricChars:":{}"}}),CodeMirror.defineMIME("text/javascript","javascript"),CodeMirror.defineMIME("application/json",{name:"javascript",json:!0})