Skip to content

Commit

Permalink
Merge pull request #435 from MichaHoffmann/mhoffm-promql-ellipsis
Browse files Browse the repository at this point in the history
feat(promql): more semgrep constructs
  • Loading branch information
mjambon authored Jul 14, 2023
2 parents f9e0d73 + b4f403d commit 4374124
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 20 deletions.
54 changes: 34 additions & 20 deletions lang/semgrep-grammars/src/semgrep-promql/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,43 @@ const base_grammar = require("tree-sitter-promql/grammar");
module.exports = grammar(base_grammar, {
name: "promql",

conflicts: ($, previous) =>
previous.concat([
[
$.instant_vector_selector,
$.range_vector_selector,
$.string_literal,
$.float_literal,
$.metric_name,
],
]),
conflicts: ($, prev) =>
prev.concat([[$.metric_name, $.string_literal, $.float_literal]]),

rules: {
_semgrep_metavariable: (_) => token(/\$[A-Z_][A-Z_0-9]*/),
semgrep_ellipsis: (_) => "...",

metric_name: ($, previous) => choice($._semgrep_metavariable, previous),
function_name: ($, previous) => choice($._semgrep_metavariable, previous),
label_name: ($, previous) => choice($._semgrep_metavariable, previous),
label_value: ($, previous) => choice($._semgrep_metavariable, previous),
string_literal: ($, previous) => choice($._semgrep_metavariable, previous),
float_literal: ($, previous) => choice($._semgrep_metavariable, previous),
instant_vector_selector: ($, previous) =>
choice($._semgrep_metavariable, previous),
range_vector_selector: ($, previous) =>
choice($._semgrep_metavariable, previous),
// Metavariables
metric_name: ($, prev) => choice($._semgrep_metavariable, prev),
function_name: ($, prev) => choice($._semgrep_metavariable, prev),
label_name: ($, prev) => choice($._semgrep_metavariable, prev),
label_value: ($, prev) => choice($._semgrep_metavariable, prev),
string_literal: ($, prev) => choice($._semgrep_metavariable, prev),
float_literal: ($, prev) => choice($._semgrep_metavariable, prev),
_duration: ($, prev) => choice($._semgrep_metavariable, prev),

// Ellipsis
label_selectors: ($, _) =>
seq("{", commaSep(choice($.semgrep_ellipsis, $.label_matcher)), "}"),

grouping: ($) =>
seq(
choice("by", "without"),
"(",
commaSep(choice($.semgrep_ellipsis, $.label_name)),
")",
),

function_args: ($) =>
seq("(", commaSep(choice($.semgrep_ellipsis, $._query)), ")"),
},
});

function commaSep(rule) {
return optional(commaSep1(rule));
}

function commaSep1(rule) {
return seq(rule, repeat(seq(",", rule)), optional(","));
}
66 changes: 66 additions & 0 deletions lang/semgrep-grammars/src/semgrep-promql/test/corpus/semgrep.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,69 @@ $SUM by ($L) ($RATE($SERIES))
(function_name)
(function_args
(float_literal))))))

================================================================================
metavariables - duration
================================================================================

rate($SERIES[$DURATION])

--------------------------------------------------------------------------------

(query
(function_call
(function_name)
(function_args
(range_vector_selector
(metric_name)
(range_selection)))))

================================================================================
ellipsis - function args
================================================================================

rate(...)

--------------------------------------------------------------------------------

(query
(function_call
(function_name)
(function_args
(semgrep_ellipsis))))

================================================================================
ellipsis - aggregation grouping
================================================================================

sum by (..., a, ...) (...)

--------------------------------------------------------------------------------

(query
(function_call
(function_name)
(grouping
(semgrep_ellipsis)
(label_name)
(semgrep_ellipsis))
(function_args
(semgrep_ellipsis))))

================================================================================
ellipsis - label matcher
================================================================================

$METRIC{..., $LABEL=~$VALUE,...}

--------------------------------------------------------------------------------

(query
(instant_vector_selector
(metric_name)
(label_selectors
(semgrep_ellipsis)
(label_matcher
(label_name)
(label_value))
(semgrep_ellipsis))))

0 comments on commit 4374124

Please sign in to comment.