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