Skip to content

Commit 3bd0288

Browse files
authored
Allow building FLambda parser in Dune (#495)
Unfortunately, I couldn't find a nice way to regenerate `flambda_parser.ml`, etc. conditionally, which is important because it requires not just Menhir but precisely the correct version of Menhir (the same one the library was built with). So one has to change the mode from `fallback` to `promote` manually. But after that, you get all the benefits of `dune build -w` when editing the parser or lexer.
1 parent 960583e commit 3bd0288

File tree

4 files changed

+79
-11
lines changed

4 files changed

+79
-11
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ _build
9494
/manual/manual/cmds/warnings-help.etex
9595
/manual/manual/warnings-help.etex
9696

97-
middle_end/flambda/parser/flambda_parser.ml.in
98-
middle_end/flambda/parser/flambda_parser.mli.in
97+
middle_end/flambda/parser/flambda_parser_in.ml
98+
middle_end/flambda/parser/flambda_parser_in.mli
99+
middle_end/flambda/parser/flambda_parser.new-messages
99100
middle_end/flambda/simplify/simplify.ml
100101
middle_end/flambda/terms/flambda.ml
101102
middle_end/flambda/types/flambda_type.ml

middle_end/flambda/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
(wrapped false)
66
(flags (:standard -principal -nostdlib))
77
(ocamlopt_flags (:standard -O3))
8+
(modules (:standard \ flambda_parser_in)) ; ignore inputs to sed
89
(modules_without_implementation flambda_backend_intf)
910
(libraries stdlib ocamlcommon ocamlbytecomp flambdadeps)
1011
)

middle_end/flambda/parser/Makefile

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,13 @@ OCAMLLEX=ocamllex
44
flambda_lex.ml: flambda_lex.mll
55
${OCAMLLEX} flambda_lex.mll
66

7-
# Avoid creating intermediate files with .ml or .mli extensions so Dune
8-
# doesn't complain
9-
flambda_parser.ml.in flambda_parser.mli.in: flambda_parser.mly
10-
${MENHIR} --table flambda_parser.mly --base flambda_parser-in
11-
mv -f flambda_parser-in.ml flambda_parser.ml.in
12-
mv -f flambda_parser-in.mli flambda_parser.mli.in
13-
14-
flambda_parser.ml: flambda_parser.ml.in
7+
flambda_parser_in.ml flambda_parser_in.mli: flambda_parser.mly
8+
${MENHIR} --table flambda_parser.mly --base flambda_parser_in
9+
10+
flambda_parser.ml: flambda_parser_in.ml
1511
sed "s/MenhirLib/CamlinternalMenhirLib/g" $< > $@
1612

17-
flambda_parser.mli: flambda_parser.mli.in
13+
flambda_parser.mli: flambda_parser_in.mli
1814
sed "s/MenhirLib/CamlinternalMenhirLib/g" $< > $@
1915

2016
flambda_parser_messages.ml: flambda_parser.mly flambda_parser.messages

middle_end/flambda/parser/dune

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
; Using user actions rather than (ocamllex) and (menhir) because the Makefile
2+
; runs ocamllex and menhir from this directory rather than the workspace root,
3+
; meaning the generated source is annotated with the bare filename.
4+
5+
; These rules are disabled by default because you need precisely the correct
6+
; version of Menhir installed. Comment out (mode fallback) and uncomment (mode
7+
; promote) in each stanza if you make changes to the Flambda
8+
; parser.
9+
10+
(rule
11+
(mode fallback)
12+
;(mode promote)
13+
(target flambda_lex.ml)
14+
(deps flambda_lex.mll)
15+
(action (run ocamllex -q %{deps})))
16+
17+
(rule
18+
(mode fallback)
19+
;(mode promote)
20+
(target flambda_parser.ml)
21+
(deps flambda_parser_in.ml)
22+
(action (with-stdin-from %{deps}
23+
(with-stdout-to %{target}
24+
(run sed "s/MenhirLib/CamlinternalMenhirLib/g")))))
25+
26+
(rule
27+
(mode fallback)
28+
;(mode promote)
29+
(target flambda_parser.mli)
30+
(deps flambda_parser_in.mli)
31+
(action (with-stdin-from %{deps}
32+
(with-stdout-to %{target}
33+
(run sed "s/MenhirLib/CamlinternalMenhirLib/g")))))
34+
35+
(rule
36+
(targets flambda_parser_in.ml
37+
flambda_parser_in.mli)
38+
(deps flambda_parser.mly)
39+
(action (run menhir --table %{deps} --base flambda_parser_in)))
40+
41+
; NOTE: flambda_parser.messages is massively out of date. No need to activate
42+
; this rule unless you've fixed it (thanks!).
43+
(rule
44+
(mode fallback)
45+
;(mode promote)
46+
(target flambda_parser_messages.ml)
47+
(deps flambda_parser.messages flambda_parser.mly)
48+
(action (with-stdout-to %{target}
49+
(run menhir --compile-errors %{deps}))))
50+
51+
(alias (name list-errors))
52+
53+
(rule
54+
(alias list-errors)
55+
(target flambda_parser.new-messages)
56+
(deps flambda_parser.mly)
57+
(mode promote)
58+
(action (with-stdout-to %{target}
59+
(run menhir --list-errors %{deps}))))
60+
61+
(alias (name check-errors))
62+
63+
(rule
64+
(alias check-errors)
65+
(deps flambda_parser.mly
66+
flambda_parser.messages
67+
flambda_parser.new-messages)
68+
(action (run menhir flambda_parser.mly
69+
--compare-errors flambda_parser.messages
70+
--compare-errors flambda_parser.new-messages)))

0 commit comments

Comments
 (0)