Skip to content

Instantly share code, notes, and snippets.

@undu
Forked from yawaramin/sig
Created December 17, 2020 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save undu/904b00fd17d27a74616b7de8693ef0d8 to your computer and use it in GitHub Desktop.
Save undu/904b00fd17d27a74616b7de8693ef0d8 to your computer and use it in GitHub Desktop.
Bourne Shell script to print out Merlin's inferred signature of an OCaml file (module)
#!/usr/bin/env sh
# Works with merlin version 2.5.4. Using protocol described at
# https://github.com/ocaml/merlin/blob/master/doc/dev/OLD-PROTOCOL.md#type-checking
usage ()
{
echo Usage: either of the following will work:
echo
echo ' sig module.ml'
echo ' sig module.mli'
exit 1
}
[ -z "$1" ] && usage
it=~/tmp/it
echo '["tell","start","end","module It = struct' >$it
sed 's/"/\\"/g' ${1%i} >>$it
echo ' end let () = ()"]' >>$it
echo '["type","expression","It","at","end"]' >>$it
ocamlmerlin <$it | sed -e '/^\["return",true\]$/d' -e 's/^\["return","sig *//' -e 's/ *end"\]$//' -e 's/\\"/"/g' -e 's/\\n/\
/g' | sed -e '/^ *$/d' -e 's/^ //'
# Reason syntax tweaks (untested, but should work):
#
# L20: echo '["tell","start","end","module It = {' >$it
# L22: echo '}; let () = ();"]' >>$it
# L25: ocamlmerlin <$it | sed -e '/^\["return",true\]$/d' -e 's/^\["return","{//' -e 's/};"\]$//' -e 's/\\"/"/g' -e 's/\\n/\
# /g' | sed -e '/^ *$/d' -e 's/^ //'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment