From 443abb0c5a8394106a0067396a521640dc9c8eb1 Mon Sep 17 00:00:00 2001 From: brandonspark Date: Tue, 2 Jul 2024 16:23:09 -0700 Subject: [PATCH] add in metavar ellipsis for hcl --- .../src/semgrep-hcl/grammar.js | 9 +- .../src/semgrep-hcl/test/corpus/semgrep.txt | 132 +++++++++++++++--- 2 files changed, 121 insertions(+), 20 deletions(-) diff --git a/lang/semgrep-grammars/src/semgrep-hcl/grammar.js b/lang/semgrep-grammars/src/semgrep-hcl/grammar.js index e2bb432d..d8400a2a 100644 --- a/lang/semgrep-grammars/src/semgrep-hcl/grammar.js +++ b/lang/semgrep-grammars/src/semgrep-hcl/grammar.js @@ -45,7 +45,7 @@ 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( @@ -53,13 +53,15 @@ module.exports = grammar(base_grammar, { $.attribute, $.block, $.semgrep_ellipsis, + $.semgrep_ellipsis_metavar ), ), object_elem: ($, previous) => { return choice( previous, - $.semgrep_ellipsis + $.semgrep_ellipsis, + $.semgrep_ellipsis_metavar ); }, @@ -67,7 +69,8 @@ module.exports = grammar(base_grammar, { return choice( previous, $.semgrep_ellipsis, - $.deep_ellipsis + $.deep_ellipsis, + $.semgrep_ellipsis_metavar ); }, diff --git a/lang/semgrep-grammars/src/semgrep-hcl/test/corpus/semgrep.txt b/lang/semgrep-grammars/src/semgrep-hcl/test/corpus/semgrep.txt index 4f70b392..9baa7392 100644 --- a/lang/semgrep-grammars/src/semgrep-hcl/test/corpus/semgrep.txt +++ b/lang/semgrep-grammars/src/semgrep-hcl/test/corpus/semgrep.txt @@ -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 @@ -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)))))