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