5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
CodeMirror.defineMode("q",function(config){
15
var indentUnit=config.indentUnit,
17
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"]),
18
E=/[|/&^!+:\\\-*%$=~#;@><,?_\'\"\[\(\]\)\s{}]/;
19
function buildRE(w){return new RegExp("^("+w.join("|")+")$");}
20
function tokenBase(stream,state){
21
var sol=stream.sol(),c=stream.next();
25
return(state.tokenize=tokenLineComment)(stream,state);
27
if(stream.eol()||/\s/.test(stream.peek()))
28
return stream.skipToEnd(),/^\\\s*$/.test(stream.current())?(state.tokenize=tokenCommentToEOF)(stream):state.tokenize=tokenBase,"comment";
30
return state.tokenize=tokenBase,"builtin";
33
return stream.peek()=="/"?(stream.skipToEnd(),"comment"):"whitespace";
35
return(state.tokenize=tokenString)(stream,state);
37
return stream.eatWhile(/[A-Za-z\d_:\/.]/),"symbol";
38
if(("."==c&&/\d/.test(stream.peek()))||/\d/.test(c)){
41
if(stream.match(/^\d{4}\.\d{2}(m|\.\d{2}([DT](\d{2}(:\d{2}(:\d{2}(\.\d{1,9})?)?)?)?)?)/)
42
|| stream.match(/^\d+D(\d{2}(:\d{2}(:\d{2}(\.\d{1,9})?)?)?)/)
43
|| stream.match(/^\d{2}:\d{2}(:\d{2}(\.\d{1,9})?)?/)
44
|| stream.match(/^\d+[ptuv]{1}/))
46
else if(stream.match(/^0[NwW]{1}/)
47
|| stream.match(/^0x[\da-fA-F]*/)
48
|| stream.match(/^[01]+[b]{1}/)
49
|| stream.match(/^\d+[chijn]{1}/)
50
|| stream.match(/-?\d*(\.\d*)?(e[+\-]?\d+)?(e|f)?/))
52
return(t&&(!(c=stream.peek())||E.test(c)))?t:(stream.next(),"error");
54
if(/[A-Za-z]|\./.test(c))
55
return stream.eatWhile(/[A-Za-z._\d]/),keywords.test(stream.current())?"keyword":"variable";
56
if(/[|/&^!+:\\\-*%$=~#;@><\.,?_\']/.test(c))
58
if(/[{}\(\[\]\)]/.test(c))
62
function tokenLineComment(stream,state){
63
return stream.skipToEnd(),/\/\s*$/.test(stream.current())?(state.tokenize=tokenBlockComment)(stream,state):(state.tokenize=tokenBase),"comment";
65
function tokenBlockComment(stream,state){
66
var f=stream.sol()&&stream.peek()=="\\";
68
if(f&&/^\\\s*$/.test(stream.current()))
69
state.tokenize=tokenBase;
72
function tokenCommentToEOF(stream){return stream.skipToEnd(),"comment";}
73
function tokenString(stream,state){
74
var escaped=false,next,end=false;
75
while((next=stream.next())){
76
if(next=="\""&&!escaped){end=true;break;}
77
escaped=!escaped&&next=="\\";
79
if(end)state.tokenize=tokenBase;
82
function pushContext(state,type,col){state.context={prev:state.context,indent:state.indent,col:col,type:type};}
83
function popContext(state){state.indent=state.context.indent;state.context=state.context.prev;}
85
startState:function(){
86
return{tokenize:tokenBase,
91
token:function(stream,state){
93
if(state.context&&state.context.align==null)
94
state.context.align=false;
95
state.indent=stream.indentation();
98
var style=state.tokenize(stream,state);
99
if(style!="comment"&&state.context&&state.context.align==null&&state.context.type!="pattern"){
100
state.context.align=true;
102
if(curPunc=="(")pushContext(state,")",stream.column());
103
else if(curPunc=="[")pushContext(state,"]",stream.column());
104
else if(curPunc=="{")pushContext(state,"}",stream.column());
105
else if(/[\]\}\)]/.test(curPunc)){
106
while(state.context&&state.context.type=="pattern")popContext(state);
107
if(state.context&&curPunc==state.context.type)popContext(state);
109
else if(curPunc=="."&&state.context&&state.context.type=="pattern")popContext(state);
110
else if(/atom|string|variable/.test(style)&&state.context){
111
if(/[\}\]]/.test(state.context.type))
112
pushContext(state,"pattern",stream.column());
113
else if(state.context.type=="pattern"&&!state.context.align){
114
state.context.align=true;
115
state.context.col=stream.column();
120
indent:function(state,textAfter){
121
var firstChar=textAfter&&textAfter.charAt(0);
122
var context=state.context;
123
if(/[\]\}]/.test(firstChar))
124
while (context&&context.type=="pattern")context=context.prev;
125
var closing=context&&firstChar==context.type;
128
else if(context.type=="pattern")
130
else if(context.align)
131
return context.col+(closing?0:1);
133
return context.indent+(closing?0:indentUnit);
137
CodeMirror.defineMIME("text/x-q","q");