# frozen_string_literal: truemoduleRougemoduleLexersclassQ<RegexLexertitle'Q'desc'The Q programming language (kx.com)'tag'q'aliases'kdb+'filenames'*.q'mimetypes'text/x-q','application/x-q'identifier=/\.?[a-z][a-z0-9_.]*/idefself.keywords@keywords||=%w[do if while select update delete exec from by]enddefself.word_operators@word_operators||=%w[
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
]enddefself.builtins@builtins||=%w[
first enlist value type get set count string key max min sum prd last flip distinct raze neg
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 til trim txf ungroup var view views wj wj1 ww
]endstate:rootdo# q allows a file to start with a shebangrule%r/#!(.*?)$/,Comment::Preproc,:toprule%r//,Text,:topendstate:topdo# indented lines at the top of the file are ignored by qrule%r/^[ \t\r]+.*$/,Comment::Specialrule%r/\n+/,Textrule%r//,Text,:baseendstate:basedorule%r/\n+/m,Textrule(/^.\)/,Keyword::Declaration)# Identifiers, word operators, etc.rule%r/#{identifier}/do|m|ifself.class.keywords.include?m[0]tokenKeywordelsifself.class.word_operators.include?m[0]tokenOperator::Wordelsifself.class.builtins.include?m[0]tokenName::Builtinelsif/^\.[zQqho]\./=~m[0]tokenName::ConstantelsetokenNameendend# White space and commentsrule(%r{[ \t\r]\/.*$},Comment::Single)rule(/[ \t\r]+/,Text::Whitespace)rule(%r{^/$.*?^\\$}m,Comment::Multiline)rule(%r{^\/[^\n]*$(\n[^\S\n]+.*$)*},Comment::Multiline)# til EOF commentrule(/^\\$/,Comment,:bottom)rule(/^\\\\\s+/,Keyword,:bottom)# Literals## stringsrule(/"/,Str,:string)## timespan/stamp constantsrule(/(?:\d+D|\d{4}\.[01]\d\.[0123]\d[DT])(?:[012]\d:[0-5]\d(?::[0-5]\d(?:\.\d+)?)?|([012]\d)?)[zpn]?\b/,Literal::Date)## time/minute/second constantsrule(/[012]\d:[0-5]\d(?::[0-5]\d(\.\d+)?)?[uvtpn]?\b/,Literal::Date)## date constantsrule(/\d{4}\.[01]\d\.[0-3]\d[dpnzm]?\b/,Literal::Date)## special valuesrule(/0[nNwW][hijefcpmdznuvt]?/,Keyword::Constant)# operators to match before numbersrule(%r{'|\/:|\\:|':|\\|\/|0:|1:|2:},Operator)## numbersrule(/(\d+[.]\d*|[.]\d+)(e[+-]?\d+)?[ef]?/,Num::Float)rule(/\d+e[+-]?\d+[ef]?/,Num::Float)rule(/\d+[ef]/,Num::Float)rule(/0x[0-9a-f]+/i,Num::Hex)rule(/[01]+b/,Num::Bin)rule(/[0-9]+[hij]?/,Num::Integer)## symbols and pathsrule(%r{(`:[:a-z0-9._\/]*|`(?:[a-z0-9.][:a-z0-9._]*)?)}i,Str::Symbol)rule(/(?:<=|>=|<>|::)|[?:$%&|@._#*^\-+~,!><=]:?/,Operator)rule%r/[{}\[\]();]/,Punctuation# commandsrule(/\\.*\n/,Text)endstate:stringdorule(/"/,Str,:pop!)rule%r/\\([\\nr]|[01][0-7]{2})/,Str::Escaperule%r/[^\\"\n]+/,Strrule%r/\\/,Str# stray backslashendstate:bottomdorule%r/.*\z/m,Comment::Multilineendendendend