File: Makefile.preprocess

package info (click to toggle)
ocaml-visitors 20251114-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,944 kB
  • sloc: ml: 4,035; makefile: 42; sh: 18
file content (51 lines) | stat: -rw-r--r-- 2,274 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# This Makefile allows running visitors as a preprocessor,
# so as to inspect the generated code and possibly include
# it in a TeX document.

# This assumes the visitors package is installed.

# The name of the "driver" executable.
DRIVER := visitors_preprocess

# The rewriting command.
REWRITE := $(shell \
   if command -v $(DRIVER) >/dev/null ; \
   then echo $(DRIVER) ; \
   else echo "dune exec $(DRIVER) --" ; \
   fi)

# Use GNU sed to extract the generated code.
# This requires GNU sed 3.95 or above, I am told.
SED          := $(shell if command -v gsed >/dev/null ; then echo gsed ; else echo sed ; fi)
EXTRACT      := $(SED) -e '/VISITORS.BEGIN/,/VISITORS.END/!d;//d'

# Fix some deficiencies of OCaml's code printer.
# -- Force a space after a comma.
# -- Force a space after an ordinary letter and before [=].
# -- Replace multiple consecutive spaces with a single space.
#    This destroys indentation; we restore it afterwards.
# -- Remove a space before a comma or closing parenthesis.
# -- Force a line break after [in], unless there is one already.
# -- Force a line break after [| ... ->] on a line by itself, unless there is one already.
# -- Force a line break after [method ... =], unless there is one already.
# -- Remove the line break between [=] and [object].
# -- Replace [fun x y -> fun ] with [fun x y ], so multiple-argument functions are prettier.
#    Do this twice, so we can handle functions of arity up to 3. (Yes, this is very ad hoc.)
BEAUTIFY     := \
              | $(SED) -e 's/,/, /g' \
              | $(SED) -e 's/\([a-zA-Z_)]\)=/\1 =/g' \
              | $(SED) -e 's/  / /g' \
              | $(SED) -e 's/ \([,)]\)/\1/g' \
              | $(SED) -e 's/ in / in\n/g' \
              | $(SED) -e 's/^\( *|.* ->\) /\1\n/g' \
              | $(SED) -e 's/\(method[^=]*=\) /\1\n/g' \
              | perl -0777 -pe 's/=\n *object/= object/gs' \
              | perl -0777 -pe "s/fun ([a-zA-Z0-9_' ]+) ->\n *fun /fun \1 /gs" \
              | perl -0777 -pe "s/fun ([a-zA-Z0-9_' ]+) ->\n *fun /fun \1 /gs" \

# Use ocp-indent to beautify the generated code.
INDENT       := ocp-indent --config=JaneStreet,match_clause=4

%.processed.ml: %.ml
	@ echo Preprocessing $<...
	@ $(REWRITE) $< | $(EXTRACT) $(BEAUTIFY) | $(INDENT) > $@