Skip to content

Commit

Permalink
elixir allows metavariables as keywords (#467)
Browse files Browse the repository at this point in the history
Previously, keywords (as in keyword/value pairs) didn't allow
metavariables, so cases like

```elixir
  foo($KEYWORD: $VALUE)
  %{$KEYWORD: $VALUE}
```

would fail to parse.

This PR adds support for that.

Tested with make test and added a test.
  • Loading branch information
amchiclet authored Jan 24, 2024
1 parent 326025a commit 1f59784
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
29 changes: 23 additions & 6 deletions lang/semgrep-grammars/src/semgrep-elixir/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ module.exports = grammar(base_grammar, {
);
},

_keyword: ($, previous) => {
return choice(
...previous.members,
$.metavariable_keyword
);
},

metavariable_keyword: $ => seq($._semgrep_metavariable, /:\s/),

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

// Ellipsis
Expand All @@ -40,16 +49,24 @@ module.exports = grammar(base_grammar, {
// keyword/pairs, such as
// foo(some_arg: 0, ...)
// %{some_item: 0, ...}
// Also note that now there is ambiguity whether foo(...) is an
// identity or pair, we set the pair rule to have a lower
// precedence
pair: ($, previous) => {
return prec(-1, choice(
return choice(
previous,
'...',
));
$.semgrep_ellipsis,
);
},

// Note that because of the pair rule above, now there is
// ambiguity whether the ellipsis in foo(...) is an
// ellipsis for positional or keyword arguments. If ... matches
// with the identity rule, it will be considered a positional
// argument. If ... matches with semgrep_ellipsis, it will be
// part of parsing the pair rule, which means its a keyword
// argument. In practice, this doesn't matter because either
// way, they will become ParamEllipsis in the Generic AST
// anyway.
semgrep_ellipsis: $ => prec(-1, '...'),

_expression: ($, previous) => choice(
...previous.members,
$.deep_ellipsis,
Expand Down
20 changes: 18 additions & 2 deletions lang/semgrep-grammars/src/semgrep-elixir/test/corpus/semgrep.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ end
(arguments (identifier))
(do_block (call (identifier) (arguments (integer) (integer))))))

=====================================
Metavariables as key value pairs
=====================================

%{$KEY: $VALUE}

---

(source
(map
(map_content
(keywords
(pair
(metavariable_keyword)
(identifier))))))

=====================================
Ellipsis in calls
=====================================
Expand All @@ -30,9 +46,9 @@ end
(identifier)
(keywords
(pair (keyword) (integer))
(pair)
(pair (semgrep_ellipsis))
(pair (keyword) (integer))
(pair)))))))
(pair (semgrep_ellipsis))))))))

=====================================
Deep Ellipsis
Expand Down

0 comments on commit 1f59784

Please sign in to comment.