1 CodeMirror.defineMode("q",function(config){
2 var indentUnit=config.indentUnit,
4 keywords=buildRE(["abs","acos","aj","aj0","all","and","any","asc","asin","asof","atan","attr","avg","avgs","bin","by","ceiling","cols","cor","cos","count","cov","cross","csv","cut","delete","deltas","desc","dev","differ","distinct","div","do","each","ej","enlist","eval","except","exec","exit","exp","fby","fills","first","fkeys","flip","floor","from","get","getenv","group","gtime","hclose","hcount","hdel","hopen","hsym","iasc","idesc","if","ij","in","insert","inter","inv","key","keys","last","like","list","lj","load","log","lower","lsq","ltime","ltrim","mavg","max","maxs","mcount","md5","mdev","med","meta","min","mins","mmax","mmin","mmu","mod","msum","neg","next","not","null","or","over","parse","peach","pj","plist","prd","prds","prev","prior","rand","rank","ratios","raze","read0","read1","reciprocal","reverse","rload","rotate","rsave","rtrim","save","scan","select","set","setenv","show","signum","sin","sqrt","ss","ssr","string","sublist","sum","sums","sv","system","tables","tan","til","trim","txf","type","uj","ungroup","union","update","upper","upsert","value","var","view","views","vs","wavg","where","where","while","within","wj","wj1","wsum","xasc","xbar","xcol","xcols","xdesc","xexp","xgroup","xkey","xlog","xprev","xrank"]),
5 E=/[|/&^!+:\\\-*%$=~#;@><,?_\'\"\[\(\]\)\s{}]/;
6 function buildRE(w){return new RegExp("^("+w.join("|")+")$");}
7 function tokenBase(stream,state){
8 var sol=stream.sol(),c=stream.next();
12 return(state.tokenize=tokenLineComment)(stream,state);
14 if(stream.eol()||/\s/.test(stream.peek()))
15 return stream.skipToEnd(),/^\\\s*$/.test(stream.current())?(state.tokenize=tokenCommentToEOF)(stream, state):state.tokenize=tokenBase,"comment";
17 return state.tokenize=tokenBase,"builtin";
20 return stream.peek()=="/"?(stream.skipToEnd(),"comment"):"whitespace";
22 return(state.tokenize=tokenString)(stream,state);
24 return stream.eatWhile(/[A-Z|a-z|\d|_|:|\/|\.]/),"symbol";
25 if(("."==c&&/\d/.test(stream.peek()))||/\d/.test(c)){
28 if(stream.match(/^\d{4}\.\d{2}(m|\.\d{2}([D|T](\d{2}(:\d{2}(:\d{2}(\.\d{1,9})?)?)?)?)?)/)
29 || stream.match(/^\d+D(\d{2}(:\d{2}(:\d{2}(\.\d{1,9})?)?)?)/)
30 || stream.match(/^\d{2}:\d{2}(:\d{2}(\.\d{1,9})?)?/)
31 || stream.match(/^\d+[ptuv]{1}/))
33 else if(stream.match(/^0[NwW]{1}/)
34 || stream.match(/^0x[\d|a-f|A-F]*/)
35 || stream.match(/^[0|1]+[b]{1}/)
36 || stream.match(/^\d+[chijn]{1}/)
37 || stream.match(/-?\d*(\.\d*)?(e[+\-]?\d+)?(e|f)?/))
39 return(t&&(!(c=stream.peek())||E.test(c)))?t:(stream.next(),"error");
41 if(/[A-Z|a-z]|\./.test(c))
42 return stream.eatWhile(/[A-Z|a-z|\.|_|\d]/),keywords.test(stream.current())?"keyword":"variable";
43 if(/[|/&^!+:\\\-*%$=~#;@><\.,?_\']/.test(c))
45 if(/[{}\(\[\]\)]/.test(c))
49 function tokenLineComment(stream,state){
50 return stream.skipToEnd(),/\/\s*$/.test(stream.current())?(state.tokenize=tokenBlockComment)(stream,state):(state.tokenize=tokenBase),"comment";
52 function tokenBlockComment(stream,state){
53 var f=stream.sol()&&stream.peek()=="\\";
55 if(f&&/^\\\s*$/.test(stream.current()))
56 state.tokenize=tokenBase;
59 function tokenCommentToEOF(stream){return stream.skipToEnd(),"comment";}
60 function tokenString(stream,state){
61 var escaped=false,next,end=false;
62 while((next=stream.next())){
63 if(next=="\""&&!escaped){end=true;break;}
64 escaped=!escaped&&next=="\\";
66 if(end)state.tokenize=tokenBase;
69 function pushContext(state,type,col){state.context={prev:state.context,indent:state.indent,col:col,type:type};}
70 function popContext(state){state.indent=state.context.indent;state.context=state.context.prev;}
72 startState:function(){
73 return{tokenize:tokenBase,
78 token:function(stream,state){
80 if(state.context&&state.context.align==null)
81 state.context.align=false;
82 state.indent=stream.indentation();
84 //if (stream.eatSpace()) return null;
85 var style=state.tokenize(stream,state);
86 if(style!="comment"&&state.context&&state.context.align==null&&state.context.type!="pattern"){
87 state.context.align=true;
89 if(curPunc=="(")pushContext(state,")",stream.column());
90 else if(curPunc=="[")pushContext(state,"]",stream.column());
91 else if(curPunc=="{")pushContext(state,"}",stream.column());
92 else if(/[\]\}\)]/.test(curPunc)){
93 while(state.context&&state.context.type=="pattern")popContext(state);
94 if(state.context&&curPunc==state.context.type)popContext(state);
96 else if(curPunc=="."&&state.context&&state.context.type=="pattern")popContext(state);
97 else if(/atom|string|variable/.test(style)&&state.context){
98 if(/[\}\]]/.test(state.context.type))
99 pushContext(state,"pattern",stream.column());
100 else if(state.context.type=="pattern"&&!state.context.align){
101 state.context.align=true;
102 state.context.col=stream.column();
107 indent:function(state,textAfter){
108 var firstChar=textAfter&&textAfter.charAt(0);
109 var context=state.context;
110 if(/[\]\}]/.test(firstChar))
111 while (context&&context.type=="pattern")context=context.prev;
112 var closing=context&&firstChar==context.type;
115 else if(context.type=="pattern")
117 else if(context.align)
118 return context.col+(closing?0:1);
120 return context.indent+(closing?0:indentUnit);
124 CodeMirror.defineMIME("text/x-q","q");