Skip to content

Commit

Permalink
Merge pull request #486 from aptos-labs/fix/typed_metavariables
Browse files Browse the repository at this point in the history
fix(move-on-aptos): bug fixes and upstream updates
  • Loading branch information
brandonspark authored Jun 26, 2024
2 parents dc31834 + 32903f8 commit ca1e791
Show file tree
Hide file tree
Showing 3 changed files with 361 additions and 59 deletions.
72 changes: 65 additions & 7 deletions lang/semgrep-grammars/src/semgrep-move-on-aptos/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ module.exports = grammar(base_grammar, {
name: 'move_on_aptos',

conflicts: ($, previous) => previous.concat([
[$.quantifier, $._quantifier_directive],
[$.var_name, $._bind],
[$.typed_metavariable, $.name_access_chain]
]),

/*
Expand All @@ -25,13 +24,23 @@ module.exports = grammar(base_grammar, {
// Semgrep components, source: semgrep-rust
ellipsis: $ => '...',
deep_ellipsis: $ => seq('<...', $._expr, '...>'),
typed_metavariable: $ => seq($.identifier, ':', $.type),

// Typed metavariable (an expression, not a parameter)
// This is grammatically indistinguishable from `$.type_hint_expr: $ => seq('(', $._expr, ':', $.type, ')')`.
// This will be handled by the semgrep converter by checking the metavariable name (`$`).
typed_metavariable: $ => seq('(', $.identifier, ':', $.type, ')'),

// Alternate "entry point". Allows parsing a standalone expression.
semgrep_expression: $ => seq('__SEMGREP_EXPRESSION', $._expr),
semgrep_expression: $ => seq('__SEMGREP_EXPRESSION', choice(
$._expr,
$.let_expr,
)),

// Alternate "entry point". Allows parsing a standalone list of sequence items (statements).
semgrep_statement: $ => seq('__SEMGREP_STATEMENT', repeat1($._sequence_item)),
semgrep_statement: $ => seq('__SEMGREP_STATEMENT', repeat1(choice(
$._sequence_item,
$.constant_decl,
))),

// Extend the source_file rule to allow semgrep constructs
source_file: ($, previous) => choice(
Expand All @@ -52,6 +61,12 @@ module.exports = grammar(base_grammar, {
$.ellipsis,
),

// statement (sequence item)
_sequence_item: ($, previous) => choice(
previous,
$.ellipsis,
),

// struct field annotations
field_annot: ($, previous) => choice(
previous,
Expand All @@ -65,6 +80,14 @@ module.exports = grammar(base_grammar, {
$.ellipsis,
),

// struct binding
// (e.g. `let T { field_1, var: ..., } = obj;`)
// (e.g. `let ... = obj;`)
_bind: ($, previous) => choice(
...previous.members,
$.ellipsis,
),

// attribute
// (e.g. `#[..., attr(...)]`)
attribute: ($, previous) => choice(
Expand Down Expand Up @@ -93,15 +116,50 @@ module.exports = grammar(base_grammar, {
prec(UNARY_PREC, $.ellipsis),
prec(UNARY_PREC, $.deep_ellipsis),
prec(UNARY_PREC, $.field_access_ellipsis_expr),
$.typed_metavariable,
),

// type parameter
// (e.g. `T: ..., U: ..., ...`)
// function parameter
// (e.g. `call( ..., arg, ...)`)
parameter: ($, previous) => choice(
previous,
$.ellipsis,
),

// for loop ellipsis
// (e.g. `for (...)`)
for_loop_expr: ($, previous) => choice(
previous,
seq('for', '(', $.ellipsis, ')', field('body', $.block)),
),

// abilities
// (e.g. struct XXX has ..., YYY)
ability: ($, previous) => choice(
previous,
$.ellipsis,
),

// type parameter
// (e.g. `Type<..., T>`)
type_param: ($, previous) => choice(
previous,
$.ellipsis,
),

// type
type: ($, previous) => choice(
...previous.members,
$.ellipsis,
),

// pack field
// (e.g. `Pack { ..., field }`)
expr_field: ($, previous) => choice(
...previous.members,
$.ellipsis,
),

// trailing field access
// (e.g. `foo.bar().baz(). ...`)
field_access_ellipsis_expr: $ => prec.left(FIELD_PREC, seq(
Expand Down
Loading

0 comments on commit ca1e791

Please sign in to comment.