Structural search of OCaml code

ocamlgrep is a command-line tool written at LexiFi to perform structural search of OCaml code.

The code is released as-is in case it is of interest to the community. The present code is extracted from a version used internally at LexiFi and we do not pretend that it is ready for public consumption, but we are making the code available to publicize the approach.

Compilation

dune build

The tool requires OCaml 5.2 (since it depends on compiler-libs, compilation aginst other versions of OCaml will require some adjustments).

This produces an executable _build/install/bin/ocamlgrep.exe which can be run from the command line.

Usage

ocamlgrep PATTERN

The tool assumes that it is executed within a Dune workspace, as it has a number of heuristics that will not work otherwise. The tool will search the files under the current directory subtree.

You will need to run dune build @check in your project to ensure all .cmt files are up-to-date before using ocamlgrep.

PATTERN: The pattern to search for. This should be a valid OCaml expression.

Examples

ocamlgrep 'List.filter'
ocamlgrep '(__ (__ : floatarray): float array)'
ocamlgrep  'List.rev __ @ __'
ocamlgrep 'match __ with None -> __ | Some __1 -> Some __1'
ocamlgrep 'List.fold_left __ __ (List.map __ __)'
ocamlgrep 'Stdlib.max (__:float) __'