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

Rust: Support top-level statements #2910

Merged
merged 10 commits into from Apr 12, 2021
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,8 @@ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html

### Added

- Rust: Semgrep patterns now support top-level statements (#2910)

### Fixed

### Changed
Expand Down
11 changes: 10 additions & 1 deletion semgrep-core/src/parsing/tree_sitter/Parse_rust_tree_sitter.ml
Expand Up @@ -2866,6 +2866,10 @@ let map_source_file (env : env) (x : CST.source_file): G.any =
let header = token env v1 (* "__SEMGREP_EXPRESSION" *) in
let expr = map_expression env v2 in
G.E expr
| `Semg_stmt (v1, v2) ->
let header = token env v1 (* "__SEMGREP_STATEMENT" *) in
let stmts = List.map (map_statement env) v2 |> List.flatten in
G.Ss stmts
)


Expand All @@ -2891,7 +2895,12 @@ let parse_expression_or_source_file str =
| [] -> res
| _ ->
let expr_str = "__SEMGREP_EXPRESSION " ^ str in
Tree_sitter_rust.Parse.string expr_str
let expr_res = Tree_sitter_rust.Parse.string expr_str in
match expr_res.errors with
| [] -> expr_res
| _ ->
let stmt_str = "__SEMGREP_STATEMENT " ^ str in
Tree_sitter_rust.Parse.string stmt_str

(* todo: special mode to convert Ellipsis in the right construct! *)
let parse_pattern str =
Expand Down
2 changes: 1 addition & 1 deletion semgrep-core/src/tree-sitter-lang/semgrep-rust