q.js 6.4 KB

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