@cloudcare/lezer-metricsql 0.0.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,591 @@
1
+ // Copyright 2021 The Prometheus Authors
2
+ // Licensed under the Apache License, Version 2.0 (the "License");
3
+ // you may not use this file except in compliance with the License.
4
+ // You may obtain a copy of the License at
5
+ //
6
+ // http://www.apache.org/licenses/LICENSE-2.0
7
+ //
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+
14
+ @top MetricsQL { WithExpr | expr }
15
+ @top MetricName { Identifier }
16
+
17
+ @precedence {
18
+ group,
19
+ pow @right,
20
+ mul @left
21
+ add @left,
22
+ eql @left,
23
+ and @left,
24
+ or @left,
25
+ keepMetricNames @left
26
+ }
27
+
28
+ expr[@isGroup=Expr] {
29
+ AggregateExpr |
30
+ BinaryExpr |
31
+ FunctionCall |
32
+ MatrixSelector |
33
+ NumberList |
34
+ NumberDurationLiteral |
35
+ OffsetExpr |
36
+ AnchoredExpr |
37
+ SmoothedExpr |
38
+ KeepMetricNamesExpr |
39
+ ParenExpr |
40
+ StringLiteral |
41
+ SubqueryExpr |
42
+ UnaryExpr |
43
+ VectorSelector |
44
+ StepInvariantExpr
45
+ }
46
+
47
+ WithExpr {
48
+ With "(" (WithBinding ("," WithBinding)* ","?)? ")" expr
49
+ }
50
+
51
+ WithBinding {
52
+ Identifier "=" expr |
53
+ Identifier "(" (Identifier ("," Identifier)* ","?)? ")" "=" expr
54
+ }
55
+
56
+ AnchoredExpr {
57
+ expr Anchored
58
+ }
59
+
60
+ SmoothedExpr {
61
+ expr Smoothed
62
+ }
63
+
64
+ KeepMetricNamesExpr {
65
+ expr !keepMetricNames KeepMetricNames
66
+ }
67
+
68
+ AggregateExpr {
69
+ AggregateOp AggregateModifier FunctionCallBody LimitClause? |
70
+ AggregateOp FunctionCallBody AggregateModifier LimitClause? |
71
+ AggregateOp FunctionCallBody LimitClause?
72
+ }
73
+
74
+ AggregateOp {
75
+ AnyAggr |
76
+ Avg |
77
+ Bottomk |
78
+ BottomkAvg |
79
+ BottomkLast |
80
+ BottomkMax |
81
+ BottomkMedian |
82
+ BottomkMin |
83
+ Count |
84
+ CountValues |
85
+ Distinct |
86
+ Geomean |
87
+ Group |
88
+ Histogram |
89
+ Mad |
90
+ Max |
91
+ Median |
92
+ Min |
93
+ Mode |
94
+ OutliersIQR |
95
+ OutliersMAD |
96
+ OutliersK |
97
+ Quantile |
98
+ Quantiles |
99
+ LimitK |
100
+ LimitRatio |
101
+ Share |
102
+ Stddev |
103
+ Stdvar |
104
+ Sum2 |
105
+ Sum |
106
+ Topk |
107
+ TopkAvg |
108
+ TopkLast |
109
+ TopkMax |
110
+ TopkMedian |
111
+ TopkMin |
112
+ Zscore
113
+ }
114
+
115
+ AggregateModifier {
116
+ By GroupingLabels |
117
+ Without GroupingLabels
118
+ }
119
+
120
+ LimitClause {
121
+ Limit NumberDurationLiteral
122
+ }
123
+
124
+ BinaryExpr {
125
+ expr !pow Pow binModifiers expr |
126
+ expr !mul Mul binModifiers expr |
127
+ expr !mul Div binModifiers expr |
128
+ expr !mul Mod binModifiers expr |
129
+ expr !mul Atan2 binModifiers expr |
130
+ expr !add Add binModifiers expr |
131
+ expr !add Sub binModifiers expr |
132
+ expr !eql Eql binModifiers expr |
133
+ expr !eql Gte binModifiers expr |
134
+ expr !eql Gtr binModifiers expr |
135
+ expr !eql TrimUpper binModifiers expr |
136
+ expr !eql TrimLower binModifiers expr |
137
+ expr !eql Lte binModifiers expr |
138
+ expr !eql Lss binModifiers expr |
139
+ expr !eql Neq binModifiers expr |
140
+ expr !and And binModifiers expr |
141
+ expr !and Unless binModifiers expr |
142
+ expr !or Default binModifiers expr |
143
+ expr !or If binModifiers expr |
144
+ expr !or IfNot binModifiers expr |
145
+ expr !or Or binModifiers expr
146
+ }
147
+
148
+ MatchingModifierClause {
149
+ (Ignoring | On) GroupingLabels
150
+ ((GroupLeft | GroupRight) (!group GroupingLabels)?)?
151
+ }
152
+
153
+ FillClause {
154
+ Fill "(" NumberDurationLiteral ")"
155
+ }
156
+
157
+ FillLeftClause {
158
+ FillLeft "(" NumberDurationLiteral ")"
159
+ }
160
+
161
+ FillRightClause {
162
+ FillRight "(" NumberDurationLiteral ")"
163
+ }
164
+
165
+ FillModifier {
166
+ (FillClause | FillLeftClause | FillRightClause) |
167
+ (FillLeftClause FillRightClause) |
168
+ (FillRightClause FillLeftClause)
169
+ }
170
+
171
+ BoolModifier { Bool }
172
+
173
+ binModifiers {
174
+ BoolModifier?
175
+ MatchingModifierClause?
176
+ FillModifier?
177
+ }
178
+
179
+ GroupingLabels {
180
+ "(" ("*" | ((LabelName | QuotedLabelName) ("," (LabelName | QuotedLabelName))* ","?)?) ")" PrefixClause?
181
+ }
182
+
183
+ PrefixClause {
184
+ Prefix StringLiteral
185
+ }
186
+
187
+ FunctionCall {
188
+ FunctionIdentifier FunctionCallBody |
189
+ Identifier FunctionCallBody
190
+ }
191
+
192
+ FunctionIdentifier {
193
+ AbsentOverTime |
194
+ Absent |
195
+ Abs |
196
+ Acos |
197
+ Acosh |
198
+ Asin |
199
+ Asinh |
200
+ Atan |
201
+ Atanh |
202
+ AvgOverTime |
203
+ Ceil |
204
+ Changes |
205
+ Clamp |
206
+ ClampMax |
207
+ ClampMin |
208
+ Cos |
209
+ Cosh |
210
+ CountOverTime |
211
+ DaysInMonth |
212
+ DayOfMonth |
213
+ DayOfWeek |
214
+ DayOfYear |
215
+ Deg |
216
+ Delta |
217
+ Deriv |
218
+ Exp |
219
+ FirstOverTime |
220
+ Floor |
221
+ HistogramCount |
222
+ HistogramFraction |
223
+ HistogramQuantile |
224
+ HistogramQuantiles |
225
+ HistogramStdDev |
226
+ HistogramStdVar |
227
+ HistogramSum |
228
+ HistogramAvg |
229
+ DoubleExponentialSmoothing |
230
+ Hour |
231
+ Idelta |
232
+ Increase |
233
+ Info |
234
+ Irate |
235
+ LabelReplace |
236
+ LabelJoin |
237
+ LastOverTime |
238
+ Ln |
239
+ Log10 |
240
+ Log2 |
241
+ MadOverTime |
242
+ MaxOverTime |
243
+ MinOverTime |
244
+ TsOfFirstOverTime |
245
+ TsOfMaxOverTime |
246
+ TsOfMinOverTime |
247
+ TsOfLastOverTime |
248
+ Minute |
249
+ Month |
250
+ Pi |
251
+ PredictLinear |
252
+ PresentOverTime |
253
+ QuantileOverTime |
254
+ Rad |
255
+ Rate |
256
+ Resets |
257
+ Round |
258
+ Scalar |
259
+ Sgn |
260
+ Sin |
261
+ Sinh |
262
+ Sort |
263
+ SortDesc |
264
+ SortByLabel |
265
+ SortByLabelDesc |
266
+ StartFn |
267
+ EndFn |
268
+ Range |
269
+ Step |
270
+ Sqrt |
271
+ StddevOverTime |
272
+ StdvarOverTime |
273
+ SumOverTime |
274
+ Tan |
275
+ Tanh |
276
+ Timestamp |
277
+ Time |
278
+ Vector |
279
+ Year
280
+ }
281
+
282
+ FunctionCallBody {
283
+ "(" (expr ("," expr)* ","?)? ")"
284
+ }
285
+
286
+ ParenExpr {
287
+ "(" expr ")"
288
+ }
289
+
290
+ NumberList {
291
+ "(" NumberDurationLiteral ("," NumberDurationLiteral)+ ","? ")"
292
+ }
293
+
294
+ OffsetExpr {
295
+ expr Offset NumberDurationLiteralInDurationContext
296
+ }
297
+
298
+ MatrixSelector {
299
+ expr "[" NumberDurationLiteralInDurationContext? "]"
300
+ }
301
+
302
+ SubqueryExpr {
303
+ expr "[" NumberDurationLiteralInDurationContext ":" ("" | NumberDurationLiteralInDurationContext) "]"
304
+ }
305
+
306
+ UnaryExpr {
307
+ !mul UnaryOp~signed expr
308
+ }
309
+
310
+ UnaryOp {
311
+ "-" |
312
+ "+"
313
+ }
314
+
315
+ VectorSelector {
316
+ Identifier LabelMatchers |
317
+ Identifier |
318
+ LabelMatchers
319
+ }
320
+
321
+ LabelMatchers {
322
+ "{" (LabelMatcherGroup (OrFilter LabelMatcherGroup)*)? "}"
323
+ }
324
+
325
+ LabelMatcherGroup {
326
+ LabelMatcher ("," LabelMatcher)* ","?
327
+ }
328
+
329
+ LabelMatcher {
330
+ UnquotedLabelMatcher | QuotedLabelMatcher | QuotedLabelName
331
+ }
332
+
333
+ MatchOp {
334
+ EqlSingle |
335
+ Neq |
336
+ EqlRegex |
337
+ NeqRegex
338
+ }
339
+
340
+ UnquotedLabelMatcher {
341
+ LabelName MatchOp LabelMatcherValue
342
+ }
343
+
344
+ QuotedLabelMatcher {
345
+ QuotedLabelName MatchOp LabelMatcherValue
346
+ }
347
+
348
+ QuotedLabelName {
349
+ StringLiteral
350
+ }
351
+
352
+ LabelMatcherValue {
353
+ (StringLiteral | Identifier) (Add (Identifier | StringLiteral))*
354
+ }
355
+
356
+ StepInvariantExpr {
357
+ expr At ( NumberDurationLiteral | AtModifierPreprocessors "(" ")" | FunctionCall | ParenExpr )
358
+ }
359
+
360
+ AtModifierPreprocessors {
361
+ AtStart | AtEnd
362
+ }
363
+
364
+ NumberDurationLiteral {
365
+ ("-"|"+")?~signed (number | inf | nan | TemplateVariable)
366
+ }
367
+
368
+ NumberDurationLiteralInDurationContext {
369
+ ("-"|"+")?~signed (number | inf | nan | TemplateVariable)
370
+ }
371
+
372
+ @skip { whitespace | LineComment }
373
+
374
+ @tokens {
375
+ whitespace { std.whitespace+ }
376
+ LineComment { "#" ![\n]* }
377
+
378
+ number {
379
+ (std.digit+ (("_")? std.digit)* ("." std.digit+ (("_")? std.digit)*)? | "." std.digit+ (("_")? std.digit)*) (("e" | "E") ("+" | "-")? std.digit+ (("_")? std.digit)*)? ("K" | "Ki" | "M" | "Mi" | "G" | "Gi" | "T" | "Ti")? |
380
+ "0x" (std.digit | $[a-fA-F])+ | duration
381
+ }
382
+ StringLiteral { // TODO: This is for JS, make this work for MetricsQL.
383
+ '"' (![\\\n"] | "\\" _)* '"'? |
384
+ "'" (![\\\n'] | "\\" _)* "'"? |
385
+ "`" ![`]* "`"
386
+ }
387
+
388
+ duration {
389
+ (std.digit+ ("." std.digit+)? ("ms" | "y" | "w" | "d" | "h" | "m" | "s" | "i"))+
390
+ }
391
+ escapedIdentifierChar { "\\" _ }
392
+ TemplateVariable { "$" (std.asciiLetter | "_") (std.asciiLetter | std.digit | "_")* }
393
+ Identifier { (std.asciiLetter | "_" | ":" | escapedIdentifierChar) (std.asciiLetter | std.digit | "_" | ":" | "." | escapedIdentifierChar)*}
394
+ LabelName { (std.asciiLetter | "_" | escapedIdentifierChar) (std.asciiLetter | std.digit | "_" | escapedIdentifierChar)* }
395
+
396
+ // Operator
397
+ Sub { "-" }
398
+ Add { "+" }
399
+ Mul { "*" }
400
+ Mod { "%" }
401
+ Div { "/" }
402
+ Eql { "==" }
403
+ Neq { "!=" }
404
+ Lte { "<=" }
405
+ Lss { "<" }
406
+ Gte { ">=" }
407
+ Gtr { ">" }
408
+ TrimUpper { "</" }
409
+ TrimLower { ">/" }
410
+ EqlRegex { "=~" }
411
+ EqlSingle { "=" }
412
+ NeqRegex { "!~" }
413
+ Pow { "^" }
414
+
415
+ // Special Modifier
416
+ At { "@" }
417
+ }
418
+
419
+ // Keywords
420
+
421
+ @external specialize {Identifier} specializeIdentifier from "./tokens" {
422
+ inf,
423
+ nan,
424
+ Bool,
425
+ Ignoring,
426
+ On,
427
+ GroupLeft,
428
+ GroupRight,
429
+ Offset
430
+ }
431
+
432
+ @external specialize {LabelName} specializeLabelName from "./tokens" {
433
+ OrFilter
434
+ }
435
+
436
+ // Contextual keywords
437
+
438
+ @external extend {Identifier} extendIdentifier from "./tokens" {
439
+ Atan2,
440
+ AnyAggr,
441
+ Avg,
442
+ Bottomk,
443
+ BottomkAvg,
444
+ BottomkLast,
445
+ BottomkMax,
446
+ BottomkMedian,
447
+ BottomkMin,
448
+ Count,
449
+ CountValues,
450
+ Distinct,
451
+ Geomean,
452
+ Group,
453
+ Histogram,
454
+ Mad,
455
+ Max,
456
+ Median,
457
+ Min,
458
+ Mode,
459
+ OutliersIQR,
460
+ OutliersMAD,
461
+ OutliersK,
462
+ Quantile,
463
+ Quantiles,
464
+ LimitK,
465
+ LimitRatio,
466
+ Share,
467
+ Stddev,
468
+ Stdvar,
469
+ Sum2,
470
+ Sum,
471
+ Topk,
472
+ TopkAvg,
473
+ TopkLast,
474
+ TopkMax,
475
+ TopkMedian,
476
+ TopkMin,
477
+ Zscore,
478
+ By,
479
+ Without,
480
+ And,
481
+ Or,
482
+ Unless,
483
+ Default,
484
+ If,
485
+ IfNot,
486
+ StartFn,
487
+ EndFn,
488
+ AtStart,
489
+ AtEnd,
490
+ Smoothed,
491
+ Anchored,
492
+ Fill,
493
+ FillLeft,
494
+ FillRight,
495
+ Limit,
496
+ Prefix,
497
+ With,
498
+ KeepMetricNames
499
+ }
500
+
501
+ @external propSource metricsQLHighLight from "./highlight"
502
+
503
+ // FunctionIdentifier definitions
504
+ Abs { condFn<"abs"> }
505
+ Absent { condFn<"absent"> }
506
+ AbsentOverTime { condFn<"absent_over_time"> }
507
+ Acos { condFn<"acos"> }
508
+ Acosh { condFn<"acosh"> }
509
+ Asin { condFn<"asin"> }
510
+ Asinh { condFn<"asinh">}
511
+ Atan { condFn<"atan"> }
512
+ Atanh { condFn<"atanh">}
513
+ AvgOverTime { condFn<"avg_over_time"> }
514
+ Ceil { condFn<"ceil"> }
515
+ Changes { condFn<"changes"> }
516
+ Clamp { condFn<"clamp"> }
517
+ ClampMax { condFn<"clamp_max"> }
518
+ ClampMin { condFn<"clamp_min"> }
519
+ Cos { condFn<"cos">}
520
+ Cosh { condFn<"cosh">}
521
+ CountOverTime { condFn<"count_over_time"> }
522
+ DaysInMonth { condFn<"days_in_month"> }
523
+ DayOfMonth { condFn<"day_of_month"> }
524
+ DayOfWeek { condFn<"day_of_week"> }
525
+ DayOfYear { condFn<"day_of_year"> }
526
+ Deg { condFn<"deg"> }
527
+ Delta { condFn<"delta"> }
528
+ Deriv { condFn<"deriv"> }
529
+ Exp { condFn<"exp"> }
530
+ FirstOverTime {condFn<"first_over_time">}
531
+ Floor { condFn<"floor"> }
532
+ HistogramAvg { condFn<"histogram_avg"> }
533
+ HistogramCount { condFn<"histogram_count"> }
534
+ HistogramFraction { condFn<"histogram_fraction"> }
535
+ HistogramQuantile { condFn<"histogram_quantile"> }
536
+ HistogramQuantiles { condFn<"histogram_quantiles"> }
537
+ HistogramStdDev { condFn<"histogram_stddev"> }
538
+ HistogramStdVar { condFn<"histogram_stdvar"> }
539
+ HistogramSum { condFn<"histogram_sum"> }
540
+ DoubleExponentialSmoothing { condFn<"double_exponential_smoothing"> }
541
+ Hour { condFn<"hour"> }
542
+ Idelta { condFn<"idelta"> }
543
+ Increase { condFn<"increase"> }
544
+ Info { condFn<"info"> }
545
+ Irate { condFn<"irate"> }
546
+ LabelReplace { condFn<"label_replace"> }
547
+ LabelJoin { condFn<"label_join"> }
548
+ LastOverTime {condFn<"last_over_time">}
549
+ Ln { condFn<"ln"> }
550
+ Log10 { condFn<"log10"> }
551
+ Log2 { condFn<"log2"> }
552
+ MadOverTime { condFn<"mad_over_time"> }
553
+ MaxOverTime { condFn<"max_over_time"> }
554
+ MinOverTime { condFn<"min_over_time"> }
555
+ TsOfFirstOverTime { condFn<"ts_of_first_over_time"> }
556
+ TsOfMaxOverTime { condFn<"ts_of_max_over_time"> }
557
+ TsOfMinOverTime { condFn<"ts_of_min_over_time"> }
558
+ TsOfLastOverTime { condFn<"ts_of_last_over_time"> }
559
+ Minute { condFn<"minute"> }
560
+ Month { condFn<"month"> }
561
+ Pi { condFn<"pi">}
562
+ PredictLinear { condFn<"predict_linear"> }
563
+ PresentOverTime { condFn<"present_over_time"> }
564
+ QuantileOverTime { condFn<"quantile_over_time"> }
565
+ Rad { condFn<"rad"> }
566
+ Rate { condFn<"rate"> }
567
+ Resets { condFn<"resets"> }
568
+ Round { condFn<"round"> }
569
+ Scalar { condFn<"scalar"> }
570
+ Sgn { condFn<"sgn"> }
571
+ Sin { condFn<"sin">}
572
+ Sinh { condFn<"sinh"> }
573
+ Sort { condFn<"sort"> }
574
+ SortDesc { condFn<"sort_desc"> }
575
+ SortByLabel { condFn<"sort_by_label"> }
576
+ SortByLabelDesc { condFn<"sort_by_label_desc"> }
577
+ Range { condFn<"range"> }
578
+ Step { condFn<"step"> }
579
+ Sqrt { condFn<"sqrt"> }
580
+ StddevOverTime { condFn<"stddev_over_time"> }
581
+ StdvarOverTime { condFn<"stdvar_over_time"> }
582
+ SumOverTime { condFn<"sum_over_time"> }
583
+ Tan { condFn<"tan"> }
584
+ Tanh { condFn<"tanh">}
585
+ Time { condFn<"time"> }
586
+ Timestamp { condFn<"timestamp"> }
587
+ Vector { condFn<"vector"> }
588
+ Year { condFn<"year"> }
589
+
590
+ // Conditional function names (only parsed as function names when used as such).
591
+ condFn<term> { @extend<Identifier, term> }