| 1 |
var curPunc, |
| 2 |
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"]), |
| 3 |
E=/[|/&^!+:\\\-*%$=~#;@><,?_\'\"\[\(\]\)\s{}]/; |
| 4 |
function buildRE(w){return new RegExp("^("+w.join("|")+")$");} |
| 5 |
function tokenBase(stream,state){ |
| 6 |
var sol=stream.sol(),c=stream.next(); |
| 7 |
curPunc=null; |
| 8 |
if(sol) |
| 9 |
if(c=="/") |
| 10 |
return(state.tokenize=tokenLineComment)(stream,state); |
| 11 |
else if(c=="\\"){ |
| 12 |
if(stream.eol()||/\s/.test(stream.peek())) |
| 13 |
return stream.skipToEnd(),/^\\\s*$/.test(stream.current())?(state.tokenize=tokenCommentToEOF)(stream):state.tokenize=tokenBase,"comment"; |
| 14 |
else |
| 15 |
return state.tokenize=tokenBase,"builtin"; |
| 16 |
} |
| 17 |
if(/\s/.test(c)) |
| 18 |
return stream.peek()=="/"?(stream.skipToEnd(),"comment"):"null"; |
| 19 |
if(c=='"') |
| 20 |
return(state.tokenize=tokenString)(stream,state); |
| 21 |
if(c=='`') |
| 22 |
return stream.eatWhile(/[A-Za-z\d_:\/.]/),"macroName"; |
| 23 |
if(("."==c&&/\d/.test(stream.peek()))||/\d/.test(c)){ |
| 24 |
var t=null; |
| 25 |
stream.backUp(1); |
| 26 |
if(stream.match(/^\d{4}\.\d{2}(m|\.\d{2}([DT](\d{2}(:\d{2}(:\d{2}(\.\d{1,9})?)?)?)?)?)/) |
| 27 |
|| stream.match(/^\d+D(\d{2}(:\d{2}(:\d{2}(\.\d{1,9})?)?)?)/) |
| 28 |
|| stream.match(/^\d{2}:\d{2}(:\d{2}(\.\d{1,9})?)?/) |
| 29 |
|| stream.match(/^\d+[ptuv]{1}/)) |
| 30 |
t="temporal"; |
| 31 |
else if(stream.match(/^0[NwW]{1}/) |
| 32 |
|| stream.match(/^0x[\da-fA-F]*/) |
| 33 |
|| stream.match(/^[01]+[b]{1}/) |
| 34 |
|| stream.match(/^\d+[chijn]{1}/) |
| 35 |
|| stream.match(/-?\d*(\.\d*)?(e[+\-]?\d+)?(e|f)?/)) |
| 36 |
t="number"; |
| 37 |
return(t&&(!(c=stream.peek())||E.test(c)))?t:(stream.next(),"error"); |
| 38 |
} |
| 39 |
if(/[A-Za-z]|\./.test(c)) |
| 40 |
return stream.eatWhile(/[A-Za-z._\d]/),keywords.test(stream.current())?"keyword":"variable"; |
| 41 |
if(/[|/&^!+:\\\-*%$=~#;@><\.,?_\']/.test(c)) |
| 42 |
return null; |
| 43 |
if(/[{}\(\[\]\)]/.test(c)) |
| 44 |
return null; |
| 45 |
return"error"; |
| 46 |
} |
| 47 |
function tokenLineComment(stream,state){ |
| 48 |
return stream.skipToEnd(),/\/\s*$/.test(stream.current())?(state.tokenize=tokenBlockComment)(stream,state):(state.tokenize=tokenBase),"comment"; |
| 49 |
} |
| 50 |
function tokenBlockComment(stream,state){ |
| 51 |
var f=stream.sol()&&stream.peek()=="\\"; |
| 52 |
stream.skipToEnd(); |
| 53 |
if(f&&/^\\\s*$/.test(stream.current())) |
| 54 |
state.tokenize=tokenBase; |
| 55 |
return"comment"; |
| 56 |
} |
| 57 |
function tokenCommentToEOF(stream){return stream.skipToEnd(),"comment";} |
| 58 |
function tokenString(stream,state){ |
| 59 |
var escaped=false,next,end=false; |
| 60 |
while((next=stream.next())){ |
| 61 |
if(next=="\""&&!escaped){end=true;break;} |
| 62 |
escaped=!escaped&&next=="\\"; |
| 63 |
} |
| 64 |
if(end)state.tokenize=tokenBase; |
| 65 |
return"string"; |
| 66 |
} |
| 67 |
function pushContext(state,type,col){state.context={prev:state.context,indent:state.indent,col:col,type:type};} |
| 68 |
function popContext(state){state.indent=state.context.indent;state.context=state.context.prev;} |
| 69 |
export const q = { |
| 70 |
name: "q", |
| 71 |
startState:function(){ |
| 72 |
return{tokenize:tokenBase, |
| 73 |
context:null, |
| 74 |
indent:0, |
| 75 |
col:0}; |
| 76 |
}, |
| 77 |
token:function(stream,state){ |
| 78 |
if(stream.sol()){ |
| 79 |
if(state.context&&state.context.align==null) |
| 80 |
state.context.align=false; |
| 81 |
state.indent=stream.indentation(); |
| 82 |
} |
| 83 |
//if (stream.eatSpace()) return null; |
| 84 |
var style=state.tokenize(stream,state); |
| 85 |
if(style!="comment"&&state.context&&state.context.align==null&&state.context.type!="pattern"){ |
| 86 |
state.context.align=true; |
| 87 |
} |
| 88 |
if(curPunc=="(")pushContext(state,")",stream.column()); |
| 89 |
else if(curPunc=="[")pushContext(state,"]",stream.column()); |
| 90 |
else if(curPunc=="{")pushContext(state,"}",stream.column()); |
| 91 |
else if(/[\]\}\)]/.test(curPunc)){ |
| 92 |
while(state.context&&state.context.type=="pattern")popContext(state); |
| 93 |
if(state.context&&curPunc==state.context.type)popContext(state); |
| 94 |
} |
| 95 |
else if(curPunc=="."&&state.context&&state.context.type=="pattern")popContext(state); |
| 96 |
else if(/atom|string|variable/.test(style)&&state.context){ |
| 97 |
if(/[\}\]]/.test(state.context.type)) |
| 98 |
pushContext(state,"pattern",stream.column()); |
| 99 |
else if(state.context.type=="pattern"&&!state.context.align){ |
| 100 |
state.context.align=true; |
| 101 |
state.context.col=stream.column(); |
| 102 |
} |
| 103 |
} |
| 104 |
return style; |
| 105 |
}, |
| 106 |
indent:function(state,textAfter,cx){ |
| 107 |
var firstChar=textAfter&&textAfter.charAt(0); |
| 108 |
var context=state.context; |
| 109 |
if(/[\]\}]/.test(firstChar)) |
| 110 |
while (context&&context.type=="pattern")context=context.prev; |
| 111 |
var closing=context&&firstChar==context.type; |
| 112 |
if(!context) |
| 113 |
return 0; |
| 114 |
else if(context.type=="pattern") |
| 115 |
return context.col; |
| 116 |
else if(context.align) |
| 117 |
return context.col+(closing?0:1); |
| 118 |
else |
| 119 |
return context.indent+(closing?0:cx.unit); |
| 120 |
} |
| 121 |
}; |