all repos — NoPaste @ 29b774f090102303e43cf939b38ac2083e62d9f1

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/q/q.js (view raw)

  1// CodeMirror, copyright (c) by Marijn Haverbeke and others
  2// Distributed under an MIT license: https://codemirror.net/LICENSE
  3
  4(function(mod) {
  5  if (typeof exports == "object" && typeof module == "object") // CommonJS
  6    mod(require("../../lib/codemirror"));
  7  else if (typeof define == "function" && define.amd) // AMD
  8    define(["../../lib/codemirror"], mod);
  9  else // Plain browser env
 10    mod(CodeMirror);
 11})(function(CodeMirror) {
 12"use strict";
 13
 14CodeMirror.defineMode("q",function(config){
 15  var indentUnit=config.indentUnit,
 16      curPunc,
 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();
 22    curPunc=null;
 23    if(sol)
 24      if(c=="/")
 25        return(state.tokenize=tokenLineComment)(stream,state);
 26      else if(c=="\\"){
 27        if(stream.eol()||/\s/.test(stream.peek()))
 28          return stream.skipToEnd(),/^\\\s*$/.test(stream.current())?(state.tokenize=tokenCommentToEOF)(stream):state.tokenize=tokenBase,"comment";
 29        else
 30          return state.tokenize=tokenBase,"builtin";
 31      }
 32    if(/\s/.test(c))
 33      return stream.peek()=="/"?(stream.skipToEnd(),"comment"):"whitespace";
 34    if(c=='"')
 35      return(state.tokenize=tokenString)(stream,state);
 36    if(c=='`')
 37      return stream.eatWhile(/[A-Za-z\d_:\/.]/),"symbol";
 38    if(("."==c&&/\d/.test(stream.peek()))||/\d/.test(c)){
 39      var t=null;
 40      stream.backUp(1);
 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}/))
 45        t="temporal";
 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)?/))
 51        t="number";
 52      return(t&&(!(c=stream.peek())||E.test(c)))?t:(stream.next(),"error");
 53    }
 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))
 57      return null;
 58    if(/[{}\(\[\]\)]/.test(c))
 59      return null;
 60    return"error";
 61  }
 62  function tokenLineComment(stream,state){
 63    return stream.skipToEnd(),/\/\s*$/.test(stream.current())?(state.tokenize=tokenBlockComment)(stream,state):(state.tokenize=tokenBase),"comment";
 64  }
 65  function tokenBlockComment(stream,state){
 66    var f=stream.sol()&&stream.peek()=="\\";
 67    stream.skipToEnd();
 68    if(f&&/^\\\s*$/.test(stream.current()))
 69      state.tokenize=tokenBase;
 70    return"comment";
 71  }
 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=="\\";
 78    }
 79    if(end)state.tokenize=tokenBase;
 80    return"string";
 81  }
 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;}
 84  return{
 85    startState:function(){
 86      return{tokenize:tokenBase,
 87             context:null,
 88             indent:0,
 89             col:0};
 90    },
 91    token:function(stream,state){
 92      if(stream.sol()){
 93        if(state.context&&state.context.align==null)
 94          state.context.align=false;
 95        state.indent=stream.indentation();
 96      }
 97      //if (stream.eatSpace()) return null;
 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;
101      }
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);
108      }
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();
116        }
117      }
118      return style;
119    },
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;
126      if(!context)
127        return 0;
128      else if(context.type=="pattern")
129        return context.col;
130      else if(context.align)
131        return context.col+(closing?0:1);
132      else
133        return context.indent+(closing?0:indentUnit);
134    }
135  };
136});
137CodeMirror.defineMIME("text/x-q","q");
138
139});