ocaml/ocaml — pull request #118 by def-lkb (2014-11-14 13:59:29+00:00)
Title: ocamldep: allow approximate dependency computation

This change adds a fallback to ocamldep when the file can't be parsed.
It's done by trying to detect the first component of paths at the lexical level.

It is useful during development to enable use of tools such as merlin: if dependencies can't be computed, build systems will stop too early (see https://github.com/the-lambda-church/merlin/issues/302).

### How to use

The approximation works by looking at tokens, skipping "unlexable" parts if needed.
It is used only if parsing or preprocessing failed and the OCAMLDEP_APPROX environment variable is set.
Suggestions to improve the integration are welcome, especially to make it easier to use from build systems (ocamlbuild, make).

Errors which were recovered by this lexical approximation are still printed on stderr but are ignored after that: dependency computation will continue, return code will be 0, the rest of the build should continue.

### The heuristic

It tries to identify the first component of a path:
- an uppercase identifier alone will be considered a dependency (as such, `None` will be a dependency if none.ml exists but no real dependency should be missed),
- identifiers following a dot (B in `A.B`) are ignored, except if the dot is preceded by a lowercase identifier (Lexing will be considered in `buf.Lexing.lex_curr_p`)
- all identifiers following a backquote (`A) are ignored.

### Motivations ("best-effort")

While not really clean, such feature can greatly improve the workflow during development.
- dependency computation is already an approximation, so an over-approximation shouldn't
break expectations of the toolchain.
- this fallback triggers only when the normal path failed, it shouldn't break existing workflow. If this is used, we are already dealing with garbage input.
- the heuristic is really simple and can be improved later, but I am not sure it's worth exploring more complex workflow (like handling partial ASTs as done by merlin).