Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add in metavar ellipsis for hcl #493

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lang/semgrep-grammars/src/semgrep-hcl/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,32 @@ module.exports = grammar(base_grammar, {
},

_semgrep_metavariable: $ => token(/\$[A-Z_][A-Z_0-9]*/),

semgrep_ellipsis_metavar : $ => /\$\.\.\.[a-zA-Z_][a-zA-Z_0-9]*/,

// Ellipsis
body: ($, previous) => repeat1(
choice(
$.attribute,
$.block,
$.semgrep_ellipsis,
$.semgrep_ellipsis_metavar
),
),

object_elem: ($, previous) => {
return choice(
previous,
$.semgrep_ellipsis
$.semgrep_ellipsis,
$.semgrep_ellipsis_metavar
);
},

_expr_term: ($, previous) => {
return choice(
previous,
$.semgrep_ellipsis,
$.deep_ellipsis
$.deep_ellipsis,
$.semgrep_ellipsis_metavar
);
},

Expand Down
132 changes: 115 additions & 17 deletions lang/semgrep-grammars/src/semgrep-hcl/test/corpus/semgrep.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,52 @@ Simple Ellipsis
resource "..." "..." {
...
}


---

(config_file
(body
(block
(identifier)
(string_lit
(quoted_template_start)
(template_literal)
(quoted_template_end))
(string_lit
(quoted_template_start)
(template_literal)
(quoted_template_end))
(block_start)
(body
(semgrep_ellipsis))
(block_end))))

=====================================
Simple Ellipsis Metavariable
=====================================

resource "..." "..." {
$...BODY
}

---

(config_file
(body
(block (identifier)
(string_lit (quoted_template_start) (template_literal) (quoted_template_end))
(string_lit (quoted_template_start) (template_literal) (quoted_template_end))
(block_start)
(body (semgrep_ellipsis))
(block_end))))
(body
(block
(identifier)
(string_lit
(quoted_template_start)
(template_literal)
(quoted_template_end))
(string_lit
(quoted_template_start)
(template_literal)
(quoted_template_end))
(block_start)
(body
(semgrep_ellipsis_metavar))
(block_end))))

=====================================
Object Ellipsis
Expand All @@ -25,15 +60,78 @@ policy = jsonencode({
...
})



---

(config_file
(body
(attribute
(identifier)
(expression
(function_call
(identifier)
(function_arguments
(expression
(collection_value
(object
(object_start)
(object_elem
(semgrep_ellipsis))
(object_end))))))))))

=====================================
Object Ellipsis Metavariable
=====================================

policy = jsonencode({
$...BODY
})


---

(config_file
(body
(attribute
(identifier)
(expression
(function_call
(identifier)
(function_arguments
(expression
(collection_value
(object
(object_start)
(object_elem
(semgrep_ellipsis_metavar))
(object_end))))))))))

=====================================
Ellipsis Expression
=====================================

x = ...

---

(config_file
(body
(attribute
(identifier)
(expression
(semgrep_ellipsis)))))

=====================================
Metavariable Ellipsis Expression
=====================================

x = $...EXPR

---

(config_file
(body (attribute (identifier)
(expression (function_call (identifier)
(function_arguments
(expression (collection_value
(object
(object_start)
(object_elem (semgrep_ellipsis))
(object_end))))))))))
(body
(attribute
(identifier)
(expression
(semgrep_ellipsis_metavar)))))
Loading