promql.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. """
  2. pygments.lexers.promql
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. Lexer for Prometheus Query Language.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. from pygments.lexer import RegexLexer, bygroups, default, words
  9. from pygments.token import (
  10. Comment,
  11. Keyword,
  12. Name,
  13. Number,
  14. Operator,
  15. Punctuation,
  16. String,
  17. Whitespace,
  18. )
  19. __all__ = ["PromQLLexer"]
  20. class PromQLLexer(RegexLexer):
  21. """
  22. For `PromQL <https://prometheus.io/docs/prometheus/latest/querying/basics/>`_ queries.
  23. For details about the grammar see:
  24. https://github.com/prometheus/prometheus/tree/master/promql/parser
  25. .. versionadded: 2.7
  26. """
  27. name = "PromQL"
  28. aliases = ["promql"]
  29. filenames = ["*.promql"]
  30. base_keywords = (
  31. words(
  32. (
  33. "bool",
  34. "by",
  35. "group_left",
  36. "group_right",
  37. "ignoring",
  38. "offset",
  39. "on",
  40. "without",
  41. ),
  42. suffix=r"\b",
  43. ),
  44. Keyword,
  45. )
  46. aggregator_keywords = (
  47. words(
  48. (
  49. "sum",
  50. "min",
  51. "max",
  52. "avg",
  53. "group",
  54. "stddev",
  55. "stdvar",
  56. "count",
  57. "count_values",
  58. "bottomk",
  59. "topk",
  60. "quantile",
  61. ),
  62. suffix=r"\b",
  63. ),
  64. Keyword,
  65. )
  66. function_keywords = (
  67. words(
  68. (
  69. "abs",
  70. "absent",
  71. "absent_over_time",
  72. "avg_over_time",
  73. "ceil",
  74. "changes",
  75. "clamp_max",
  76. "clamp_min",
  77. "count_over_time",
  78. "day_of_month",
  79. "day_of_week",
  80. "days_in_month",
  81. "delta",
  82. "deriv",
  83. "exp",
  84. "floor",
  85. "histogram_quantile",
  86. "holt_winters",
  87. "hour",
  88. "idelta",
  89. "increase",
  90. "irate",
  91. "label_join",
  92. "label_replace",
  93. "ln",
  94. "log10",
  95. "log2",
  96. "max_over_time",
  97. "min_over_time",
  98. "minute",
  99. "month",
  100. "predict_linear",
  101. "quantile_over_time",
  102. "rate",
  103. "resets",
  104. "round",
  105. "scalar",
  106. "sort",
  107. "sort_desc",
  108. "sqrt",
  109. "stddev_over_time",
  110. "stdvar_over_time",
  111. "sum_over_time",
  112. "time",
  113. "timestamp",
  114. "vector",
  115. "year",
  116. ),
  117. suffix=r"\b",
  118. ),
  119. Keyword.Reserved,
  120. )
  121. tokens = {
  122. "root": [
  123. (r"\n", Whitespace),
  124. (r"\s+", Whitespace),
  125. (r",", Punctuation),
  126. # Keywords
  127. base_keywords,
  128. aggregator_keywords,
  129. function_keywords,
  130. # Offsets
  131. (r"[1-9][0-9]*[smhdwy]", String),
  132. # Numbers
  133. (r"-?[0-9]+\.[0-9]+", Number.Float),
  134. (r"-?[0-9]+", Number.Integer),
  135. # Comments
  136. (r"#.*?$", Comment.Single),
  137. # Operators
  138. (r"(\+|\-|\*|\/|\%|\^)", Operator),
  139. (r"==|!=|>=|<=|<|>", Operator),
  140. (r"and|or|unless", Operator.Word),
  141. # Metrics
  142. (r"[_a-zA-Z][a-zA-Z0-9_]+", Name.Variable),
  143. # Params
  144. (r'(["\'])(.*?)(["\'])', bygroups(Punctuation, String, Punctuation)),
  145. # Other states
  146. (r"\(", Operator, "function"),
  147. (r"\)", Operator),
  148. (r"\{", Punctuation, "labels"),
  149. (r"\[", Punctuation, "range"),
  150. ],
  151. "labels": [
  152. (r"\}", Punctuation, "#pop"),
  153. (r"\n", Whitespace),
  154. (r"\s+", Whitespace),
  155. (r",", Punctuation),
  156. (r'([_a-zA-Z][a-zA-Z0-9_]*?)(\s*?)(=~|!=|=|!~)(\s*?)("|\')(.*?)("|\')',
  157. bygroups(Name.Label, Whitespace, Operator, Whitespace,
  158. Punctuation, String, Punctuation)),
  159. ],
  160. "range": [
  161. (r"\]", Punctuation, "#pop"),
  162. (r"[1-9][0-9]*[smhdwy]", String),
  163. ],
  164. "function": [
  165. (r"\)", Operator, "#pop"),
  166. (r"\(", Operator, "#push"),
  167. default("#pop"),
  168. ],
  169. }