q.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. """
  2. pygments.lexers.q
  3. ~~~~~~~~~~~~~~~~~
  4. Lexer for the Q programming language.
  5. :copyright: Copyright 2006-2024 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, words, include, bygroups, inherit
  9. from pygments.token import Comment, Name, Number, Operator, Punctuation, \
  10. String, Whitespace, Literal, Generic
  11. __all__ = ["KLexer", "QLexer"]
  12. class KLexer(RegexLexer):
  13. """
  14. For K source code.
  15. """
  16. name = "K"
  17. aliases = ["k"]
  18. filenames = ["*.k"]
  19. url = "https://code.kx.com"
  20. version_added = '2.12'
  21. tokens = {
  22. "whitespace": [
  23. # hashbang script
  24. (r"^#!.*", Comment.Hashbang),
  25. # Comments
  26. (r"^/\s*\n", Comment.Multiline, "comments"),
  27. (r"(?<!\S)/.*", Comment.Single),
  28. # Whitespace
  29. (r"\s+", Whitespace),
  30. # Strings
  31. (r"\"", String.Double, "strings"),
  32. ],
  33. "root": [
  34. include("whitespace"),
  35. include("keywords"),
  36. include("declarations"),
  37. ],
  38. "keywords": [
  39. (words(("abs", "acos", "asin", "atan", "avg", "bin",
  40. "binr", "by", "cor", "cos", "cov", "dev",
  41. "delete", "div", "do", "enlist", "exec", "exit",
  42. "exp", "from", "getenv", "hopen", "if", "in",
  43. "insert", "last", "like", "log", "max", "min",
  44. "prd", "select", "setenv", "sin", "sqrt", "ss",
  45. "sum", "tan", "update", "var", "wavg", "while",
  46. "within", "wsum", "xexp"),
  47. suffix=r"\b"), Operator.Word),
  48. ],
  49. "declarations": [
  50. # Timing
  51. (r"^\\ts?", Comment.Preproc),
  52. (r"^(\\\w\s+[^/\n]*?)(/.*)",
  53. bygroups(Comment.Preproc, Comment.Single)),
  54. # Generic System Commands
  55. (r"^\\\w.*", Comment.Preproc),
  56. # Prompt
  57. (r"^[a-zA-Z]\)", Generic.Prompt),
  58. # Function Names
  59. (r"([.]?[a-zA-Z][\w.]*)(\s*)([-.~=!@#$%^&*_+|,<>?/\\:']?:)(\s*)(\{)",
  60. bygroups(Name.Function, Whitespace, Operator, Whitespace, Punctuation),
  61. "functions"),
  62. # Variable Names
  63. (r"([.]?[a-zA-Z][\w.]*)(\s*)([-.~=!@#$%^&*_+|,<>?/\\:']?:)",
  64. bygroups(Name.Variable, Whitespace, Operator)),
  65. # Functions
  66. (r"\{", Punctuation, "functions"),
  67. # Parentheses
  68. (r"\(", Punctuation, "parentheses"),
  69. # Brackets
  70. (r"\[", Punctuation, "brackets"),
  71. # Errors
  72. (r"'`([a-zA-Z][\w.]*)?", Name.Exception),
  73. # File Symbols
  74. (r"`:([a-zA-Z/][\w./]*)?", String.Symbol),
  75. # Symbols
  76. (r"`([a-zA-Z][\w.]*)?", String.Symbol),
  77. # Numbers
  78. include("numbers"),
  79. # Variable Names
  80. (r"[a-zA-Z][\w.]*", Name),
  81. # Operators
  82. (r"[-=+*#$%@!~^&:.,<>'\\|/?_]", Operator),
  83. # Punctuation
  84. (r";", Punctuation),
  85. ],
  86. "functions": [
  87. include("root"),
  88. (r"\}", Punctuation, "#pop"),
  89. ],
  90. "parentheses": [
  91. include("root"),
  92. (r"\)", Punctuation, "#pop"),
  93. ],
  94. "brackets": [
  95. include("root"),
  96. (r"\]", Punctuation, "#pop"),
  97. ],
  98. "numbers": [
  99. # Binary Values
  100. (r"[01]+b", Number.Bin),
  101. # Nulls/Infinities
  102. (r"0[nNwW][cefghijmndzuvtp]?", Number),
  103. # Timestamps
  104. ((r"(?:[0-9]{4}[.][0-9]{2}[.][0-9]{2}|[0-9]+)"
  105. "D(?:[0-9](?:[0-9](?::[0-9]{2}"
  106. "(?::[0-9]{2}(?:[.][0-9]*)?)?)?)?)?"), Literal.Date),
  107. # Datetimes
  108. ((r"[0-9]{4}[.][0-9]{2}"
  109. "(?:m|[.][0-9]{2}(?:T(?:[0-9]{2}:[0-9]{2}"
  110. "(?::[0-9]{2}(?:[.][0-9]*)?)?)?)?)"), Literal.Date),
  111. # Times
  112. (r"[0-9]{2}:[0-9]{2}(?::[0-9]{2}(?:[.][0-9]{1,3})?)?",
  113. Literal.Date),
  114. # GUIDs
  115. (r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
  116. Number.Hex),
  117. # Byte Vectors
  118. (r"0x[0-9a-fA-F]+", Number.Hex),
  119. # Floats
  120. (r"([0-9]*[.]?[0-9]+|[0-9]+[.]?[0-9]*)[eE][+-]?[0-9]+[ef]?",
  121. Number.Float),
  122. (r"([0-9]*[.][0-9]+|[0-9]+[.][0-9]*)[ef]?", Number.Float),
  123. (r"[0-9]+[ef]", Number.Float),
  124. # Characters
  125. (r"[0-9]+c", Number),
  126. # Integers
  127. (r"[0-9]+[ihtuv]", Number.Integer),
  128. # Long Integers
  129. (r"[0-9]+[jnp]?", Number.Integer.Long),
  130. ],
  131. "comments": [
  132. (r"[^\\]+", Comment.Multiline),
  133. (r"^\\", Comment.Multiline, "#pop"),
  134. (r"\\", Comment.Multiline),
  135. ],
  136. "strings": [
  137. (r'[^"\\]+', String.Double),
  138. (r"\\.", String.Escape),
  139. (r'"', String.Double, "#pop"),
  140. ],
  141. }
  142. class QLexer(KLexer):
  143. """
  144. For `Q <https://code.kx.com/>`_ source code.
  145. """
  146. name = "Q"
  147. aliases = ["q"]
  148. filenames = ["*.q"]
  149. version_added = '2.12'
  150. tokens = {
  151. "root": [
  152. (words(("aj", "aj0", "ajf", "ajf0", "all", "and", "any", "asc",
  153. "asof", "attr", "avgs", "ceiling", "cols", "count", "cross",
  154. "csv", "cut", "deltas", "desc", "differ", "distinct", "dsave",
  155. "each", "ej", "ema", "eval", "except", "fby", "fills", "first",
  156. "fkeys", "flip", "floor", "get", "group", "gtime", "hclose",
  157. "hcount", "hdel", "hsym", "iasc", "idesc", "ij", "ijf",
  158. "inter", "inv", "key", "keys", "lj", "ljf", "load", "lower",
  159. "lsq", "ltime", "ltrim", "mavg", "maxs", "mcount", "md5",
  160. "mdev", "med", "meta", "mins", "mmax", "mmin", "mmu", "mod",
  161. "msum", "neg", "next", "not", "null", "or", "over", "parse",
  162. "peach", "pj", "prds", "prior", "prev", "rand", "rank", "ratios",
  163. "raze", "read0", "read1", "reciprocal", "reval", "reverse",
  164. "rload", "rotate", "rsave", "rtrim", "save", "scan", "scov",
  165. "sdev", "set", "show", "signum", "ssr", "string", "sublist",
  166. "sums", "sv", "svar", "system", "tables", "til", "trim", "txf",
  167. "type", "uj", "ujf", "ungroup", "union", "upper", "upsert",
  168. "value", "view", "views", "vs", "where", "wj", "wj1", "ww",
  169. "xasc", "xbar", "xcol", "xcols", "xdesc", "xgroup", "xkey",
  170. "xlog", "xprev", "xrank"),
  171. suffix=r"\b"), Name.Builtin,
  172. ),
  173. inherit,
  174. ],
  175. }