q.js 6.3 KB

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