-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgrammar.js
170 lines (144 loc) · 4.56 KB
/
grammar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
module.exports = grammar({
name: "promql",
extras: ($) => [$.comment, /\s/],
conflicts: ($) => [
[$.instant_vector_selector, $.range_vector_selector],
],
rules: {
query: ($) => $._query,
_query: ($) =>
choice($._query_expression, seq("(", $._query_expression, ")")),
_query_expression: ($) =>
choice(
$._literal_expression,
$._selector_expression,
$._call_expression,
$._operator_expression,
$._subquery_expression,
),
// literals
_literal_expression: ($) => choice($.float_literal, $.string_literal),
float_literal: (_) =>
/[-+]?([0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?|0[xX][0-9a-fA-F]+|[nN][aA][nN]|[iI][nN][fF])/,
string_literal: ($) => $._quoted_string,
// selectors
_selector_expression: ($) =>
choice($.instant_vector_selector, $.range_vector_selector),
instant_vector_selector: ($) =>
seq($._series_matcher, optional($.modifier)),
range_vector_selector: ($) =>
seq($._series_matcher, $.range_selection, optional($.modifier)),
range_selection: ($) => seq("[", $._duration, "]"),
modifier: ($) =>
choice(seq($.offset, optional($.at)), seq($.at, optional($.offset))),
offset: ($) => seq("offset", optional("-"), $._duration),
at: (_) => seq("@", choice("start()", "end()", /[1-9][0-9]*/)),
_series_matcher: ($) =>
choice(
$.metric_name,
$.label_selectors,
seq($.metric_name, $.label_selectors),
),
label_selectors: ($) => seq("{", commaSep($.label_matcher), "}"),
label_matcher: ($) =>
seq($.label_name, choice("=", "!=", "=~", "!~"), $.label_value),
metric_name: ($) => $._identifier,
label_name: ($) => $._identifier,
label_value: ($) => $._quoted_string,
// functions
// NOTE: we group also aggregations here; otherwise there will be ambiguities for
// code like `sum(X)` in integrations like semgrep
_call_expression: ($) => $.function_call,
function_call: ($) =>
choice(
seq($.function_name, $.function_args),
seq($.function_name, $.grouping, $.function_args),
seq($.function_name, $.function_args, $.grouping),
),
function_name: ($) => $._identifier,
function_args: ($) => seq("(", commaSep($._query), ")"),
grouping: ($) =>
seq(
choice(/by/i, /without/i),
"(",
commaSep($.label_name),
")",
),
// operators
_operator_expression: ($) => $.binary_expression,
binary_expression: ($) => {
const table = [
[6, choice("^")],
[5, choice("*", "/", "%")],
[4, choice("+", "-")],
[
3,
seq(
choice("==", "!=", ">", ">=", "<", "<="),
optional(/bool/i),
),
],
[
2,
choice(
/and/i,
/or/i,
/unless/i,
),
],
[1, /atan2/i],
];
return choice(
...table.map(([precedence, operator]) =>
prec.left(
precedence,
seq($._query, operator, optional($.binary_grouping), $._query),
),
),
);
},
binary_grouping: ($) =>
prec.right(
seq(
choice(/on/i, /ignoring/i),
seq("(", commaSep($.label_name), ")"),
optional(
seq(
choice(
/group_left/i,
/group_right/i,
),
optional(seq("(", commaSep($.label_name), ")")),
),
),
),
),
// subqueries
_subquery_expression: ($) => $.subquery,
subquery: ($) =>
seq($._query, $.subquery_range_selection, optional($.modifier)),
subquery_range_selection: ($) =>
seq("[", $._duration, ":", optional($._duration), "]"),
// misc helpers
_quoted_string: ($) =>
choice(
$._single_quoted_string,
$._double_quoted_string,
$._backtick_quoted_string,
),
_double_quoted_string: (_) => token(seq('"', /(\\"|[^"])*/, '"')),
_single_quoted_string: (_) => token(seq("'", /(\\'|[^'])*/, "'")),
_backtick_quoted_string: (_) => token(seq("`", /(\\`|[^`])*/, "`")),
_duration: (_) =>
repeat1(seq(/[0-9]+/, choice("ms", "s", "m", "h", "d", "w", "y"))),
// label_name, metric_name, function_name
_identifier: (_) => /[a-zA-Z_:][a-zA-Z0-9_:]*/,
comment: (_) => token(choice(seq("#", /.*/))),
},
});
function commaSep(rule) {
return optional(commaSep1(rule));
}
function commaSep1(rule) {
return seq(rule, repeat(seq(",", rule)), optional(","));
}