Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pygments/lexers/q.py: 100%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

17 statements  

1""" 

2 pygments.lexers.q 

3 ~~~~~~~~~~~~~~~~~ 

4 

5 Lexer for the Q programming language. 

6 

7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS. 

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11from pygments.lexer import RegexLexer, words, include, bygroups, inherit 

12from pygments.token import Comment, Name, Number, Operator, Punctuation, \ 

13 String, Whitespace, Literal, Generic 

14 

15__all__ = ["KLexer", "QLexer"] 

16 

17 

18class KLexer(RegexLexer): 

19 """ 

20 For K source code. 

21 """ 

22 

23 name = "K" 

24 aliases = ["k"] 

25 filenames = ["*.k"] 

26 url = "https://code.kx.com" 

27 version_added = '2.12' 

28 

29 tokens = { 

30 "whitespace": [ 

31 # hashbang script 

32 (r"^#!.*", Comment.Hashbang), 

33 # Comments 

34 (r"^/\s*\n", Comment.Multiline, "comments"), 

35 (r"(?<!\S)/.*", Comment.Single), 

36 # Whitespace 

37 (r"\s+", Whitespace), 

38 # Strings 

39 (r"\"", String.Double, "strings"), 

40 ], 

41 "root": [ 

42 include("whitespace"), 

43 include("keywords"), 

44 include("declarations"), 

45 ], 

46 "keywords": [ 

47 (words(("abs", "acos", "asin", "atan", "avg", "bin", 

48 "binr", "by", "cor", "cos", "cov", "dev", 

49 "delete", "div", "do", "enlist", "exec", "exit", 

50 "exp", "from", "getenv", "hopen", "if", "in", 

51 "insert", "last", "like", "log", "max", "min", 

52 "prd", "select", "setenv", "sin", "sqrt", "ss", 

53 "sum", "tan", "update", "var", "wavg", "while", 

54 "within", "wsum", "xexp"), 

55 suffix=r"\b"), Operator.Word), 

56 ], 

57 "declarations": [ 

58 # Timing 

59 (r"^\\ts?", Comment.Preproc), 

60 (r"^(\\\w\s+[^/\n]*?)(/.*)", 

61 bygroups(Comment.Preproc, Comment.Single)), 

62 # Generic System Commands 

63 (r"^\\\w.*", Comment.Preproc), 

64 # Prompt 

65 (r"^[a-zA-Z]\)", Generic.Prompt), 

66 # Function Names 

67 (r"([.]?[a-zA-Z][\w.]*)(\s*)([-.~=!@#$%^&*_+|,<>?/\\:']?:)(\s*)(\{)", 

68 bygroups(Name.Function, Whitespace, Operator, Whitespace, Punctuation), 

69 "functions"), 

70 # Variable Names 

71 (r"([.]?[a-zA-Z][\w.]*)(\s*)([-.~=!@#$%^&*_+|,<>?/\\:']?:)", 

72 bygroups(Name.Variable, Whitespace, Operator)), 

73 # Functions 

74 (r"\{", Punctuation, "functions"), 

75 # Parentheses 

76 (r"\(", Punctuation, "parentheses"), 

77 # Brackets 

78 (r"\[", Punctuation, "brackets"), 

79 # Errors 

80 (r"'`([a-zA-Z][\w.]*)?", Name.Exception), 

81 # File Symbols 

82 (r"`:([a-zA-Z/][\w./]*)?", String.Symbol), 

83 # Symbols 

84 (r"`([a-zA-Z][\w.]*)?", String.Symbol), 

85 # Numbers 

86 include("numbers"), 

87 # Variable Names 

88 (r"[a-zA-Z][\w.]*", Name), 

89 # Operators 

90 (r"[-=+*#$%@!~^&:.,<>'\\|/?_]", Operator), 

91 # Punctuation 

92 (r";", Punctuation), 

93 ], 

94 "functions": [ 

95 include("root"), 

96 (r"\}", Punctuation, "#pop"), 

97 ], 

98 "parentheses": [ 

99 include("root"), 

100 (r"\)", Punctuation, "#pop"), 

101 ], 

102 "brackets": [ 

103 include("root"), 

104 (r"\]", Punctuation, "#pop"), 

105 ], 

106 "numbers": [ 

107 # Binary Values 

108 (r"[01]+b", Number.Bin), 

109 # Nulls/Infinities 

110 (r"0[nNwW][cefghijmndzuvtp]?", Number), 

111 # Timestamps 

112 ((r"(?:[0-9]{4}[.][0-9]{2}[.][0-9]{2}|[0-9]+)" 

113 "D(?:[0-9](?:[0-9](?::[0-9]{2}" 

114 "(?::[0-9]{2}(?:[.][0-9]*)?)?)?)?)?"), Literal.Date), 

115 # Datetimes 

116 ((r"[0-9]{4}[.][0-9]{2}" 

117 "(?:m|[.][0-9]{2}(?:T(?:[0-9]{2}:[0-9]{2}" 

118 "(?::[0-9]{2}(?:[.][0-9]*)?)?)?)?)"), Literal.Date), 

119 # Times 

120 (r"[0-9]{2}:[0-9]{2}(?::[0-9]{2}(?:[.][0-9]{1,3})?)?", 

121 Literal.Date), 

122 # GUIDs 

123 (r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", 

124 Number.Hex), 

125 # Byte Vectors 

126 (r"0x[0-9a-fA-F]+", Number.Hex), 

127 # Floats 

128 (r"([0-9]*[.]?[0-9]+|[0-9]+[.]?[0-9]*)[eE][+-]?[0-9]+[ef]?", 

129 Number.Float), 

130 (r"([0-9]*[.][0-9]+|[0-9]+[.][0-9]*)[ef]?", Number.Float), 

131 (r"[0-9]+[ef]", Number.Float), 

132 # Characters 

133 (r"[0-9]+c", Number), 

134 # Integers 

135 (r"[0-9]+[ihtuv]", Number.Integer), 

136 # Long Integers 

137 (r"[0-9]+[jnp]?", Number.Integer.Long), 

138 ], 

139 "comments": [ 

140 (r"[^\\]+", Comment.Multiline), 

141 (r"^\\", Comment.Multiline, "#pop"), 

142 (r"\\", Comment.Multiline), 

143 ], 

144 "strings": [ 

145 (r'[^"\\]+', String.Double), 

146 (r"\\.", String.Escape), 

147 (r'"', String.Double, "#pop"), 

148 ], 

149 } 

150 

151 

152class QLexer(KLexer): 

153 """ 

154 For `Q <https://code.kx.com/>`_ source code. 

155 """ 

156 

157 name = "Q" 

158 aliases = ["q"] 

159 filenames = ["*.q"] 

160 version_added = '2.12' 

161 

162 tokens = { 

163 "root": [ 

164 (words(("aj", "aj0", "ajf", "ajf0", "all", "and", "any", "asc", 

165 "asof", "attr", "avgs", "ceiling", "cols", "count", "cross", 

166 "csv", "cut", "deltas", "desc", "differ", "distinct", "dsave", 

167 "each", "ej", "ema", "eval", "except", "fby", "fills", "first", 

168 "fkeys", "flip", "floor", "get", "group", "gtime", "hclose", 

169 "hcount", "hdel", "hsym", "iasc", "idesc", "ij", "ijf", 

170 "inter", "inv", "key", "keys", "lj", "ljf", "load", "lower", 

171 "lsq", "ltime", "ltrim", "mavg", "maxs", "mcount", "md5", 

172 "mdev", "med", "meta", "mins", "mmax", "mmin", "mmu", "mod", 

173 "msum", "neg", "next", "not", "null", "or", "over", "parse", 

174 "peach", "pj", "prds", "prior", "prev", "rand", "rank", "ratios", 

175 "raze", "read0", "read1", "reciprocal", "reval", "reverse", 

176 "rload", "rotate", "rsave", "rtrim", "save", "scan", "scov", 

177 "sdev", "set", "show", "signum", "ssr", "string", "sublist", 

178 "sums", "sv", "svar", "system", "tables", "til", "trim", "txf", 

179 "type", "uj", "ujf", "ungroup", "union", "upper", "upsert", 

180 "value", "view", "views", "vs", "where", "wj", "wj1", "ww", 

181 "xasc", "xbar", "xcol", "xcols", "xdesc", "xgroup", "xkey", 

182 "xlog", "xprev", "xrank"), 

183 suffix=r"\b"), Name.Builtin, 

184 ), 

185 inherit, 

186 ], 

187 }