Skip to content

Commit

Permalink
Merge pull request #482 from aptos-labs/feat/initial-integration
Browse files Browse the repository at this point in the history
New Language Support: Move on Aptos
  • Loading branch information
aryx authored Jun 8, 2024
2 parents fd9edaf + bc0dfb6 commit 24bf7dc
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,6 @@
[submodule "lang/semgrep-grammars/src/tree-sitter-ql"]
path = lang/semgrep-grammars/src/tree-sitter-ql
url = https://github.com/tree-sitter/tree-sitter-ql
[submodule "lang/semgrep-grammars/src/tree-sitter-move-on-aptos"]
path = lang/semgrep-grammars/src/tree-sitter-move-on-aptos
url = https://github.com/aptos-labs/tree-sitter-move-on-aptos.git
1 change: 1 addition & 0 deletions lang/move-on-aptos/Makefile
7 changes: 7 additions & 0 deletions lang/move-on-aptos/extensions.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# File extensions for the target language, one per line. This is used for
# collecting parsing stats from the repos specified in 'projects.txt'. e.g.:
#
# .h
# .c
#
.move
3 changes: 3 additions & 0 deletions lang/move-on-aptos/fyi.list
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
semgrep-grammars/src/tree-sitter-move-on-aptos/LICENSE
semgrep-grammars/src/tree-sitter-move-on-aptos/grammar.js
semgrep-grammars/src/semgrep-move-on-aptos/grammar.js
8 changes: 8 additions & 0 deletions lang/move-on-aptos/projects.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Git URLs of publicly-accessible projects to be used for parsing stats,
# one per line.
#
https://github.com/aptos-labs/aptos-core
https://github.com/sea-protocol/seaprotocol
https://github.com/wormhole-foundation/wormhole
https://github.com/aptos-labs/aptos-networks
https://github.com/pancakeswap/pancake-contracts-move
1 change: 1 addition & 0 deletions lang/semgrep-grammars/lang/move-on-aptos
1 change: 1 addition & 0 deletions lang/semgrep-grammars/src/semgrep-move-on-aptos/Makefile
112 changes: 112 additions & 0 deletions lang/semgrep-grammars/src/semgrep-move-on-aptos/grammar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
semgrep-move-on-aptos
Extends the standard move-on-aptos grammar with semgrep pattern constructs.
*/

const base_grammar = require('tree-sitter-move-on-aptos/grammar');

const FIELD_PREC = 14;

module.exports = grammar(base_grammar, {
name: 'move_on_aptos',

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

/*
Support for semgrep ellipsis ('...') and metavariables ('$FOO'),
if they're not already part of the base grammar.
*/
rules: {
// Semgrep components, source: semgrep-rust
ellipsis: $ => '...',
deep_ellipsis: $ => seq('<...', $._expr, '...>'),
typed_metavariable: $ => seq($.identifier, ':', $.type),

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

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

// Extend the source_file rule to allow semgrep constructs
source_file: ($, previous) => choice(
previous,
$.semgrep_expression,
$.semgrep_statement,
),

// Module declaration
declaration: ($, previous) => choice(
previous,
$.ellipsis,
),

// Spec block members
_spec_block_member: ($, previous) => choice(
previous,
$.ellipsis,
),

// struct field annotations
field_annot: ($, previous) => choice(
previous,
$.ellipsis,
),

// struct field bindings
// (e.g. `let T { field_1, ... } = 0;`)
bind_field: ($, previous) => choice(
previous,
$.ellipsis,
),

// attribute
// (e.g. `#[..., attr(...)]`)
attribute: ($, previous) => choice(
previous,
$.ellipsis,
),

// use member
// (e.g. `use module_ident::...;`, `use module_ident::{..., item_ident}`)
_use_member: ($, previous) => choice(
previous,
$.ellipsis,
),

// expression
_expr: ($, previous) => choice(
...previous.members,
$.ellipsis,
$.deep_ellipsis,
$.field_access_ellipsis_expr,
),

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

// trailing field access
// (e.g. `foo.bar().baz(). ...`)
field_access_ellipsis_expr: $ => prec.left(FIELD_PREC, seq(
field('element', $._dot_or_index_chain), '.', $.ellipsis,
)),

// identifier, extended to support metavariables
identifier: ($, previous) => token(choice(
previous,
// Metavariables
alias(choice(
/\$[A-Z_][A-Z_0-9]*/,
/\$\.\.\.[A-Z_][A-Z_0-9]*/,
), $.meta_var),
)),
}
});
1 change: 1 addition & 0 deletions lang/semgrep-grammars/src/semgrep-move-on-aptos/prep
Empty file.
1 change: 1 addition & 0 deletions lang/semgrep-grammars/src/tree-sitter-move-on-aptos

0 comments on commit 24bf7dc

Please sign in to comment.