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

fix(move-on-aptos): bug fixes and upstream updates #486

Merged
merged 9 commits into from
Jun 26, 2024
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
Loading