package ocamlgrep
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=4a60b9e05c922d4c44414879266ec6bbc4f0bf70c0cbf7a673dc0d776950b4b8
sha512=501b6250bed524980c3bd4aef091eb81e9435357a406a7a9be4ee8a2a2b49fa610f677cacc318b42712343ba2de99b22068b7608f2c6b0c834082a62602c36d0
Description
Added to opam-repository:
README
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 buildThe 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 PATTERNThe 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.
- The wildcard
__matches any expression or any record field. - A numbered wildcard
__1,__2, ... matches any expression or record field and enforce strict equality of all the matching occurrences for the same number. - An identifier (value or class) is matched as a suffix of the fully qualified path in the typed expression.
- Labels and constructors identifiers are matched as a suffix of the identifier in the typed expression.
For function applications, it is allowed to omit in the pattern any argument of the actual function call; special forms are recognized to enforce that a given optional argument present or missing:
foo ?arg:PRESENT foo ?arg:MISSING- Applications written with the operators
|>and@@of the standard library, or with explicit parentheses, are matched as plain applications:x |> f a,f a @@ xand(f a) xin the code are all matched asf a x. In the pattern,x |> f aandf a @@ xmatch either an application of an operator with the same name (which can be defined locally in the code) orf a x. - Anonymous functions are matched one parameter at a time (labels included), so that
fun p1 p2 -> eandfun p1 -> fun p2 -> eare equivalent, and an unlabeled parameterfun p -> eis equivalent to the single clausefunction p -> e(as for otherfunctionexpressions, a single clause of the pattern can match several clauses of the code). For instance,fun __ -> __matchesfun x y -> eandfunction Some x -> e1 | None -> e2. An optional parameter without a default value in the pattern also matches one with a default value in the code. - An expression
(... : typexpr)matches any expression matching...and whose type is equal totypexpr. - For
try..with/match..with/functionsexpressions, the order of clauses doesn't matter. A single clause of the searched expression can match several clauses of the code. Same set-semantics for record expressions. - An expression
e1.lid1matchese2.lid2 <- e3ife1matchese2and the labellid1matcheslid2. - The expression
__.idmatches any pattern of the form{...; P.id; ...}for any prefixP. This rule was added so that grepping for__.foowill return every "get" and "set" of the record fieldfoo, including reads in patterns.
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) __'
Dependencies (4)
-
ocamlgrep-lib
= version -
cmdliner
>= "2.0.0" - ocaml
-
dune
>= "3.18"
Dev Dependencies (4)
-
odoc
with-doc -
testo
with-test -
ppx_deriving
with-test -
menhir
with-test
Used by
None
Conflicts
None