promql.py 4.6 KB

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