q.py 6.8 KB

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