Skip to content

Commit eb0e31f

Browse files
pablukalecthomas
authored andcommitted
Add PromQL lexer
It also includes several test examples
1 parent 552ad24 commit eb0e31f

File tree

3 files changed

+260
-0
lines changed

3 files changed

+260
-0
lines changed

Diff for: lexers/p/promql.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package p
2+
3+
import (
4+
. "github.com/alecthomas/chroma" // nolint
5+
"github.com/alecthomas/chroma/lexers/internal"
6+
)
7+
8+
// Promql lexer.
9+
var Promql = internal.Register(MustNewLexer(
10+
&Config{
11+
Name: "PromQL",
12+
Aliases: []string{"promql"},
13+
Filenames: []string{"*.promql"},
14+
MimeTypes: []string{},
15+
},
16+
Rules{
17+
"root": {
18+
{`\n`, TextWhitespace, nil},
19+
{`\s+`, TextWhitespace, nil},
20+
{`,`, Punctuation, nil},
21+
{Words(``, `\b`, `bool`, `by`, `group_left`, `group_right`, `ignoring`, `offset`, `on`, `without`), Keyword, nil},
22+
{Words(``, `\b`, `sum`, `min`, `max`, `avg`, `group`, `stddev`, `stdvar`, `count`, `count_values`, `bottomk`, `topk`, `quantile`), Keyword, nil},
23+
{Words(``, `\b`, `abs`, `absent`, `absent_over_time`, `avg_over_time`, `ceil`, `changes`, `clamp_max`, `clamp_min`, `count_over_time`, `day_of_month`, `day_of_week`, `days_in_month`, `delta`, `deriv`, `exp`, `floor`, `histogram_quantile`, `holt_winters`, `hour`, `idelta`, `increase`, `irate`, `label_join`, `label_replace`, `ln`, `log10`, `log2`, `max_over_time`, `min_over_time`, `minute`, `month`, `predict_linear`, `quantile_over_time`, `rate`, `resets`, `round`, `scalar`, `sort`, `sort_desc`, `sqrt`, `stddev_over_time`, `stdvar_over_time`, `sum_over_time`, `time`, `timestamp`, `vector`, `year`), KeywordReserved, nil},
24+
{`[1-9][0-9]*[smhdwy]`, LiteralString, nil},
25+
{`-?[0-9]+\.[0-9]+`, LiteralNumberFloat, nil},
26+
{`-?[0-9]+`, LiteralNumberInteger, nil},
27+
{`#.*?$`, CommentSingle, nil},
28+
{`(\+|\-|\*|\/|\%|\^)`, Operator, nil},
29+
{`==|!=|>=|<=|<|>`, Operator, nil},
30+
{`and|or|unless`, OperatorWord, nil},
31+
{`[_a-zA-Z][a-zA-Z0-9_]+`, NameVariable, nil},
32+
{`(["\'])(.*?)(["\'])`, ByGroups(Punctuation, LiteralString, Punctuation), nil},
33+
{`\(`, Operator, Push("function")},
34+
{`\)`, Operator, nil},
35+
{`\{`, Punctuation, Push("labels")},
36+
{`\[`, Punctuation, Push("range")},
37+
},
38+
"labels": {
39+
{`\}`, Punctuation, Pop(1)},
40+
{`\n`, TextWhitespace, nil},
41+
{`\s+`, TextWhitespace, nil},
42+
{`,`, Punctuation, nil},
43+
{`([_a-zA-Z][a-zA-Z0-9_]*?)(\s*?)(=~|!=|=|~!)(\s*?)(")(.*?)(")`, ByGroups(NameLabel, TextWhitespace, Operator, TextWhitespace, Punctuation, LiteralString, Punctuation), nil},
44+
},
45+
"range": {
46+
{`\]`, Punctuation, Pop(1)},
47+
{`[1-9][0-9]*[smhdwy]`, LiteralString, nil},
48+
},
49+
"function": {
50+
{`\)`, Operator, Pop(1)},
51+
{`\(`, Operator, Push()},
52+
Default(Pop(1)),
53+
},
54+
},
55+
))

Diff for: lexers/testdata/promql.actual

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# A metric with label filtering
2+
go_gc_duration_seconds{instance="localhost:9090", job="alertmanager"}
3+
4+
# Aggregation operators
5+
sum by (app, proc) (
6+
instance_memory_limit_bytes - instance_memory_usage_bytes
7+
) / 1024 / 1024
8+
9+
# Metric with multiple lables and whitespaces
10+
go_gc_duration_seconds{ instance="localhost:9090", job="alertmanager" }
11+
12+
# Expression and comment
13+
go_gc_duration_seconds{instance="localhost:9090"} # single comment
14+
15+
# Delta function
16+
delta(cpu_temp_celsius{host="zeus"}[2h])
17+
18+
# Sum with arguments
19+
sum by (app, proc) (instance_memory_usage_bytes)
20+
21+
# Multi-line with offset
22+
label_replace(
23+
avg by(instance)
24+
(irate(node_cpu_seconds_total{mode = "idle"}[5m] offset 3s)
25+
) * 100,
26+
"device",
27+
"cpu",
28+
"instance",
29+
".*"
30+
)

Diff for: lexers/testdata/promql.expected

+175
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
[
2+
{"type":"CommentSingle","value":"# A metric with label filtering"},
3+
{"type":"TextWhitespace","value":"\n"},
4+
{"type":"NameVariable","value":"go_gc_duration_seconds"},
5+
{"type":"Punctuation","value":"{"},
6+
{"type":"NameLabel","value":"instance"},
7+
{"type":"Operator","value":"="},
8+
{"type":"Punctuation","value":"\""},
9+
{"type":"LiteralString","value":"localhost:9090"},
10+
{"type":"Punctuation","value":"\","},
11+
{"type":"TextWhitespace","value":" "},
12+
{"type":"NameLabel","value":"job"},
13+
{"type":"Operator","value":"="},
14+
{"type":"Punctuation","value":"\""},
15+
{"type":"LiteralString","value":"alertmanager"},
16+
{"type":"Punctuation","value":"\"}"},
17+
{"type":"TextWhitespace","value":"\n\n"},
18+
19+
{"type":"CommentSingle","value":"# Aggregation operators"},
20+
{"type":"TextWhitespace","value":"\n"},
21+
{"type":"Keyword","value":"sum"},
22+
{"type":"TextWhitespace","value":" "},
23+
{"type":"Keyword","value":"by"},
24+
{"type":"TextWhitespace","value":" "},
25+
{"type":"Operator","value":"("},
26+
{"type":"NameVariable","value":"app"},
27+
{"type":"Punctuation","value":","},
28+
{"type":"TextWhitespace","value":" "},
29+
{"type":"NameVariable","value":"proc"},
30+
{"type":"Operator","value":")"},
31+
{"type":"TextWhitespace","value":" "},
32+
{"type":"Operator","value":"("},
33+
{"type":"TextWhitespace","value":"\n "},
34+
{"type":"NameVariable","value":"instance_memory_limit_bytes"},
35+
{"type":"TextWhitespace","value":" "},
36+
{"type":"Operator","value":"-"},
37+
{"type":"TextWhitespace","value":" "},
38+
{"type":"NameVariable","value":"instance_memory_usage_bytes"},
39+
{"type":"TextWhitespace","value":"\n"},
40+
{"type":"Operator","value":")"},
41+
{"type":"TextWhitespace","value":" "},
42+
{"type":"Operator","value":"/"},
43+
{"type":"TextWhitespace","value":" "},
44+
{"type":"LiteralNumberInteger","value":"1024"},
45+
{"type":"TextWhitespace","value":" "},
46+
{"type":"Operator","value":"/"},
47+
{"type":"TextWhitespace","value":" "},
48+
{"type":"LiteralNumberInteger","value":"1024"},
49+
{"type":"TextWhitespace","value":"\n\n"},
50+
51+
{"type":"CommentSingle","value":"# Metric with multiple lables and whitespaces"},
52+
{"type":"TextWhitespace","value":"\n"},
53+
{"type":"NameVariable","value":"go_gc_duration_seconds"},
54+
{"type":"Punctuation","value":"{"},
55+
{"type":"TextWhitespace","value":" "},
56+
{"type":"NameLabel","value":"instance"},
57+
{"type":"Operator","value":"="},
58+
{"type":"Punctuation","value":"\""},
59+
{"type":"LiteralString","value":"localhost:9090"},
60+
{"type":"Punctuation","value":"\","},
61+
{"type":"TextWhitespace","value":" "},
62+
{"type":"NameLabel","value":"job"},
63+
{"type":"Operator","value":"="},
64+
{"type":"Punctuation","value":"\""},
65+
{"type":"LiteralString","value":"alertmanager"},
66+
{"type":"Punctuation","value":"\""},
67+
{"type":"TextWhitespace","value":" "},
68+
{"type":"Punctuation","value":"}"},
69+
{"type":"TextWhitespace","value":"\n\n"},
70+
71+
{"type":"CommentSingle","value":"# Expression and comment"},
72+
{"type":"TextWhitespace","value":"\n"},
73+
{"type":"NameVariable","value":"go_gc_duration_seconds"},
74+
{"type":"Punctuation","value":"{"},
75+
{"type":"NameLabel","value":"instance"},
76+
{"type":"Operator","value":"="},
77+
{"type":"Punctuation","value":"\""},
78+
{"type":"LiteralString","value":"localhost:9090"},
79+
{"type":"Punctuation","value":"\"}"},
80+
{"type":"TextWhitespace","value":" "},
81+
{"type":"CommentSingle","value":"# single comment"},
82+
{"type":"TextWhitespace","value":"\n\n"},
83+
84+
{"type":"CommentSingle","value":"# Delta function"},
85+
{"type":"TextWhitespace","value":"\n"},
86+
{"type":"KeywordReserved","value":"delta"},
87+
{"type":"Operator","value":"("},
88+
{"type":"NameVariable","value":"cpu_temp_celsius"},
89+
{"type":"Punctuation","value":"{"},
90+
{"type":"NameLabel","value":"host"},
91+
{"type":"Operator","value":"="},
92+
{"type":"Punctuation","value":"\""},
93+
{"type":"LiteralString","value":"zeus"},
94+
{"type":"Punctuation","value":"\"}["},
95+
{"type":"LiteralString","value":"2h"},
96+
{"type":"Punctuation","value":"]"},
97+
{"type":"Operator","value":")"},
98+
{"type":"TextWhitespace","value":"\n\n"},
99+
100+
{"type":"CommentSingle","value":"# Sum with arguments"},
101+
{"type":"TextWhitespace","value":"\n"},
102+
{"type":"Keyword","value":"sum"},
103+
{"type":"TextWhitespace","value":" "},
104+
{"type":"Keyword","value":"by"},
105+
{"type":"TextWhitespace","value":" "},
106+
{"type":"Operator","value":"("},
107+
{"type":"NameVariable","value":"app"},
108+
{"type":"Punctuation","value":","},
109+
{"type":"TextWhitespace","value":" "},
110+
{"type":"NameVariable","value":"proc"},
111+
{"type":"Operator","value":")"},
112+
{"type":"TextWhitespace","value":" "},
113+
{"type":"Operator","value":"("},
114+
{"type":"NameVariable","value":"instance_memory_usage_bytes"},
115+
{"type":"Operator","value":")"},
116+
{"type":"TextWhitespace","value":"\n\n"},
117+
118+
{"type":"CommentSingle","value":"# Multi-line with offset"},
119+
{"type":"TextWhitespace","value":"\n"},
120+
{"type":"KeywordReserved","value":"label_replace"},
121+
{"type":"Operator","value":"("},
122+
{"type":"TextWhitespace","value":"\n "},
123+
{"type":"Keyword","value":"avg"},
124+
{"type":"TextWhitespace","value":" "},
125+
{"type":"Keyword","value":"by"},
126+
{"type":"Operator","value":"("},
127+
{"type":"NameVariable","value":"instance"},
128+
{"type":"Operator","value":")"},
129+
{"type":"TextWhitespace","value":"\n "},
130+
{"type":"Operator","value":"("},
131+
{"type":"KeywordReserved","value":"irate"},
132+
{"type":"Operator","value":"("},
133+
{"type":"NameVariable","value":"node_cpu_seconds_total"},
134+
{"type":"Punctuation","value":"{"},
135+
{"type":"NameLabel","value":"mode"},
136+
{"type":"TextWhitespace","value":" "},
137+
{"type":"Operator","value":"="},
138+
{"type":"TextWhitespace","value":" "},
139+
{"type":"Punctuation","value":"\""},
140+
{"type":"LiteralString","value":"idle"},
141+
{"type":"Punctuation","value":"\"}["},
142+
{"type":"LiteralString","value":"5m"},
143+
{"type":"Punctuation","value":"]"},
144+
{"type":"TextWhitespace","value":" "},
145+
{"type":"Keyword","value":"offset"},
146+
{"type":"TextWhitespace","value":" "},
147+
{"type":"LiteralString","value":"3s"},
148+
{"type":"Operator","value":")"},
149+
{"type":"TextWhitespace","value":"\n "},
150+
{"type":"Operator","value":")"},
151+
{"type":"TextWhitespace","value":" "},
152+
{"type":"Operator","value":"*"},
153+
{"type":"TextWhitespace","value":" "},
154+
{"type":"LiteralNumberInteger","value":"100"},
155+
{"type":"Punctuation","value":","},
156+
{"type":"TextWhitespace","value":"\n "},
157+
{"type":"Punctuation","value":"\""},
158+
{"type":"LiteralString","value":"device"},
159+
{"type":"Punctuation","value":"\","},
160+
{"type":"TextWhitespace","value":"\n "},
161+
{"type":"Punctuation","value":"\""},
162+
{"type":"LiteralString","value":"cpu"},
163+
{"type":"Punctuation","value":"\","},
164+
{"type":"TextWhitespace","value":"\n "},
165+
{"type":"Punctuation","value":"\""},
166+
{"type":"LiteralString","value":"instance"},
167+
{"type":"Punctuation","value":"\","},
168+
{"type":"TextWhitespace","value":"\n "},
169+
{"type":"Punctuation","value":"\""},
170+
{"type":"LiteralString","value":".*"},
171+
{"type":"Punctuation","value":"\""},
172+
{"type":"TextWhitespace","value":"\n"},
173+
{"type":"Operator","value":")"},
174+
{"type":"TextWhitespace","value":"\n"}
175+
]

0 commit comments

Comments
 (0)