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