promql.py 4.6 KB

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