-
Notifications
You must be signed in to change notification settings - Fork 25
/
kdb-lex.min.js
1 lines (1 loc) · 6.69 KB
/
kdb-lex.min.js
1
(function(){"use strict";var KDBLex;KDBLex=function(){function KDBLex(){}KDBLex.prototype.getTokens=function(txt){return this.process(txt)};KDBLex.prototype.filter=function(txt){return this.getHtml(txt)};KDBLex.prototype.getHtml=function(txt,cmap){var i,j,len,o,ref,res,t;if(cmap==null){cmap={}}if(((ref=t=this.getTokens(txt))!=null?ref.length:void 0)===0){return""}res="";for(i=j=0,len=t.length;j<len;i=++j){o=t[i];if(o.x===0){if(o.y!==0){res+="</div>"}res+="<div class='"+(cmap.line||"k-line")+"'>"}res+="<span class='"+(cmap[o.type]||o.type)+"'>"+this.escHtml(o.token)+"</span>"}if(res){return res+"</div>"}else{return res}};KDBLex.prototype.process=function(txt){var ref,st,t;this.toks=[];t=typeof txt==="string"?txt.split("\n"):[].concat(txt);st={state:"q",txt:t,reg:"",line:null,lstart:true,x:0,y:-1};while(st.txt.length>0||((ref=st.line)!=null?ref.length:void 0)>0){st=this.next(st)}st=this.pushTxt(st);return this.toks};KDBLex.prototype.escHtml=function(s){return s.replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">").replace(/ /g," ")};KDBLex.prototype.prevChar=function(st){var t;if(st.reg.length===0){if(this.toks.length===0){return" "}t=this.toks[this.toks.length-1];if(t.y!==st.y){return" "}t=t.token[t.token.length-1]}else{t=st.reg}if(/[a-zA-Z]/.test(t)){return"a"}if(/[0-9]/.test(t)){return"0"}return" "};KDBLex.prototype.next=function(st){var b,cmd,cmt,i,id,ref,ref1,ref2,t;if(st.line===null){if(st.txt.length===0){return}this.pushTxt(st);st.line=st.txt.shift();st.lstart=true;st.x=0;st.y+=1}if(st.state==="sim-com"){this.toks.push({type:"k-simple-comment",token:st.line,x:st.x,y:st.y});if(/^\\\s*$/.test(st.line)){st.state="q"}st.line=null;return st}if(st.state==="eof-com"){this.toks.push({type:"k-eof-comment",token:st.line,x:st.x,y:st.y});st.line=null;return st}if(st.state==="str"){i=0;b=false;while(st.line[i]!=='"'||b){if(st.line.length<=i){this.toks.push({type:"k-string",token:st.reg+st.line,x:st.x,y:st.y});st.line=null;st.reg="";return st}b=b?false:st.line[i]==="\\";i+=1}st.state="q";this.toks.push({type:"k-string",token:st.reg+st.line.slice(0,i+1),x:st.x-1,y:st.y});st.line=st.line.slice(i+1);st.reg="";st.x+=i+1;return st}if(st.lstart){st.lstart=false;if(st.line.length===0){st.line=null;this.toks.push({type:"k-text",token:"",x:st.x,y:st.y});return st}if(cmt=st.line.match(/^\/\s*$/)){st.line=null;st.state="sim-com";this.toks.push({type:"k-simple-comment",token:cmt[0],x:st.x,y:st.y});return st}if(cmt=st.line.match(/^\\\s*$/)){st.line=null;st.state="eof-com";this.toks.push({type:"k-eof-comment",token:cmt[0],x:st.x,y:st.y});return st}if(cmt=st.line.match(/^\/.*/)){st.line=null;this.toks.push({type:"k-comment",token:cmt[0],x:st.x,y:st.y});return st}if(cmd=st.line.match(/^\\.*/)){st.line=null;this.toks.push({type:"k-command",token:cmd[0],x:st.x,y:st.y});return st}if(cmd=st.line.match(/^[a-zA-Z]\)/)){this.toks.push({type:"k-command",token:cmd[0],x:st.x,y:st.y});st.x+=2;st.line=st.line.slice(2);return st}}if(st.line.length===0){st.line=null;return st}if(cmt=st.line.match(/^(\s+)(\/.*)/)){st=this.pushTxt(st);if(cmt[1]){this.toks.push({type:"k-text",token:cmt[1],x:st.x,y:st.y})}st.line=null;this.toks.push({type:"k-comment",token:cmt[2],x:st.x+cmt[1].length,y:st.y});return st}if(st.line[0]==='"'){st=this.pushTxt(st);st.state="str";st.line=st.line.slice(1);st.x+=1;st.reg='"';return st}if(!((ref=this.prevChar(st))==="a"||ref==="0")){if(id=st.line.match(/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}/)){return this.pushTok(st,"k-number-guid",id[0])}if(id=st.line.match(/^[a-zA-Z][a-zA-Z0-9_\.]*/)){if(/^(and|or|except|inter|like|each|cross|vs|sv|within|where|in|asof|bin|binr|cor|cov|cut|ej|fby|div|ij|insert|lj|ljf|mavg|mcount|mdev|mmax|mmin|mmu|mod|msum|over|prior|peach|pj|scan|scov|setenv|ss|sublist|uj|union|upsert|wavg|wsum|xasc|xbar|xcol|xcols|xdesc|xexp|xgroup|xkey|xlog|xprev|xrank)$/.test(id[0])){t="k-keyword-operator"}else if(/^(do|if|while|select|update|delete|exec|from|by)$/.test(id)){t="k-keyword-control"}else if((ref1=id[0])==="x"||ref1==="y"||ref1==="z"){t="k-keyword-language"}else if(/^(first|enlist|value|type|get|set|count|string|key|max|min|sum|prd|last|flip|distinct|raze|neg|til|upper|lower|abs|acos|aj|aj0|not|null|any|asc|asin|attr|avg|avgs|ceiling|cols|cos|csv|all|atan|deltas|desc|differ|dsave|dev|eval|exit|exp|fills|fkeys|floor|getenv|group|gtime|hclose|hcount|hdel|hopen|hsym|iasc|idesc|inv|keys|load|log|lsq|ltime|ltrim|maxs|md5|med|meta|mins|next|parse|plist|prds|prev|rand|rank|ratios|read0|read1|reciprocal|reverse|rload|rotate|rsave|rtrim|save|sdev|show|signum|sin|sqrt|ssr|sums|svar|system|tables|tan|trim|txf|ungroup|var|view|views|wj|wj1|ww)$/.test(id[0])){t="k-keyword-function"}else{t="k-name"}return this.pushTok(st,t,id[0])}if(id=st.line.match(/^\.[a-zA-Z][a-zA-Z0-9_\.]*/)){return this.pushTok(st,id[0][2]==="."&&((ref2=id[0][1])==="q"||ref2==="Q"||ref2==="h"||ref2==="o"||ref2==="z")?"k-keyword-function":"k-variable",id[0])}if(id=st.line.match(/^0[nNwW][hijefcpmdznuvtg]?/)){return this.pushTok(st,"k-const",id[0])}if(id=st.line.match(/^(?:\d+D|\d\d\d\d\.[01]\d\.[0123]\d[DT])(?:[012]\d\:[0-5]\d(?:\:[0-5]\d(?:\.\d+)?)?|([012]\d)?)[zpn]?/)){return this.pushTok(st,"k-number-datetime",id[0])}if(id=st.line.match(/^[012]\d\:[0-5]\d(?:\:[0-5]\d(\.\d+)?)?[uvtpn]?/)){return this.pushTok(st,"k-number-time",id[0])}if(id=st.line.match(/^\d{4}\.[01]\d\.[0-3]\d[dpnzm]?/)){return this.pushTok(st,"k-number-date",id[0])}if(id=st.line.match(/^(?:(?:\d+(?:\.\d*)?|\.\d+)[eE][+-]?\d+|\d+\.\d*|\.\d+)[efpntm]?/)){return this.pushTok(st,"k-number-float",id[0])}if(id=st.line.match(/^-[1-9][0-9]?\s*!/)){return this.pushTok(st,"k-keyword-function",id[0])}if(id=st.line.match(/^(0x[0-9a-fA-F]+|\d+[bhicjefpnuvt]?)/)){return this.pushTok(st,"k-number-int",id[0])}}if(id=st.line.match(/^(`\:[\:a-zA-Z0-9\._\/]*|`(?:[a-zA-Z0-9\.][\:a-zA-Z0-9\._]*)?)/)){return this.pushTok(st,"k-const-sym",id[0])}if(id=st.line.match(/^(\'|\/\:|\\\:|\'\:|\\|\/|0\:|1\:|2\:)/)){return this.pushTok(st,"k-operator",id[0])}if(id=st.line.match(/^(?:<=|>=|<>|::)|^(?:\$|%|&|\@|\.|\_|\#|\*|\^|\-|\+|\+|~|\,|!|>|<|=|\||\?|\:)\:?/)){return this.pushTok(st,"k-operator",id[0])}st.reg=st.reg+st.line[0];st.line=st.line.slice(1);st.x+=1;return st};KDBLex.prototype.pushTok=function(st,t,n){st=this.pushTxt(st);this.toks.push({type:t,token:n,x:st.x,y:st.y});st.x+=n.length;st.line=st.line.slice(n.length);return st};KDBLex.prototype.pushTxt=function(st){if(!st.reg){return st}this.toks.push({type:"k-text",token:st.reg,x:st.x-st.reg.length,y:st.y});st.reg="";return st};return KDBLex}();if(window.KDB==null){window.KDB={}}KDB.KDBLexer=new KDBLex;KDB.KDBLex=KDBLex;KDB.KDBLexer2=function(txt){if(typeof txt==="string"){txt=KDB.KDBLexer.filter(txt)}else if(typeof txt==="object"&&txt[""]!=null){txt[""]=KDB.KDBLexer.filter(txt[""])}return txt}}).call(this);