2
0

q.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. CodeMirror.defineMode("q",function(config){
  2. var indentUnit=config.indentUnit,
  3. curPunc,
  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();
  9. curPunc=null;
  10. if(sol)
  11. if(c=="/")
  12. return(state.tokenize=tokenLineComment)(stream,state);
  13. else if(c=="\\"){
  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";
  16. else
  17. return state.tokenize=tokenBase,"builtin";
  18. }
  19. if(/\s/.test(c))
  20. return stream.peek()=="/"?(stream.skipToEnd(),"comment"):"whitespace";
  21. if(c=='"')
  22. return(state.tokenize=tokenString)(stream,state);
  23. if(c=='`')
  24. return stream.eatWhile(/[A-Z|a-z|\d|_|:|\/|\.]/),"symbol";
  25. if(("."==c&&/\d/.test(stream.peek()))||/\d/.test(c)){
  26. var t=null;
  27. stream.backUp(1);
  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}/))
  32. t="temporal";
  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)?/))
  38. t="number";
  39. return(t&&(!(c=stream.peek())||E.test(c)))?t:(stream.next(),"error");
  40. }
  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))
  44. return null;
  45. if(/[{}\(\[\]\)]/.test(c))
  46. return null;
  47. return"error";
  48. }
  49. function tokenLineComment(stream,state){
  50. return stream.skipToEnd(),/\/\s*$/.test(stream.current())?(state.tokenize=tokenBlockComment)(stream,state):(state.tokenize=tokenBase),"comment";
  51. }
  52. function tokenBlockComment(stream,state){
  53. var f=stream.sol()&&stream.peek()=="\\";
  54. stream.skipToEnd();
  55. if(f&&/^\\\s*$/.test(stream.current()))
  56. state.tokenize=tokenBase;
  57. return"comment";
  58. }
  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=="\\";
  65. }
  66. if(end)state.tokenize=tokenBase;
  67. return"string";
  68. }
  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;}
  71. return{
  72. startState:function(){
  73. return{tokenize:tokenBase,
  74. context:null,
  75. indent:0,
  76. col:0};
  77. },
  78. token:function(stream,state){
  79. if(stream.sol()){
  80. if(state.context&&state.context.align==null)
  81. state.context.align=false;
  82. state.indent=stream.indentation();
  83. }
  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;
  88. }
  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);
  95. }
  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();
  103. }
  104. }
  105. return style;
  106. },
  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;
  113. if(!context)
  114. return 0;
  115. else if(context.type=="pattern")
  116. return context.col;
  117. else if(context.align)
  118. return context.col+(closing?0:1);
  119. else
  120. return context.indent+(closing?0:indentUnit);
  121. }
  122. };
  123. });
  124. CodeMirror.defineMIME("text/x-q","q");