Skip to content

Commit

Permalink
Allow building FLambda parser in Dune (#495)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lukemaurer authored Jun 24, 2021
1 parent 960583e commit 3bd0288
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 11 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ _build
/manual/manual/cmds/warnings-help.etex
/manual/manual/warnings-help.etex

middle_end/flambda/parser/flambda_parser.ml.in
middle_end/flambda/parser/flambda_parser.mli.in
middle_end/flambda/parser/flambda_parser_in.ml
middle_end/flambda/parser/flambda_parser_in.mli
middle_end/flambda/parser/flambda_parser.new-messages
middle_end/flambda/simplify/simplify.ml
middle_end/flambda/terms/flambda.ml
middle_end/flambda/types/flambda_type.ml
Expand Down
1 change: 1 addition & 0 deletions middle_end/flambda/dune
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
(wrapped false)
(flags (:standard -principal -nostdlib))
(ocamlopt_flags (:standard -O3))
(modules (:standard \ flambda_parser_in)) ; ignore inputs to sed
(modules_without_implementation flambda_backend_intf)
(libraries stdlib ocamlcommon ocamlbytecomp flambdadeps)
)
14 changes: 5 additions & 9 deletions middle_end/flambda/parser/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ OCAMLLEX=ocamllex
flambda_lex.ml: flambda_lex.mll
${OCAMLLEX} flambda_lex.mll

# Avoid creating intermediate files with .ml or .mli extensions so Dune
# doesn't complain
flambda_parser.ml.in flambda_parser.mli.in: flambda_parser.mly
${MENHIR} --table flambda_parser.mly --base flambda_parser-in
mv -f flambda_parser-in.ml flambda_parser.ml.in
mv -f flambda_parser-in.mli flambda_parser.mli.in

flambda_parser.ml: flambda_parser.ml.in
flambda_parser_in.ml flambda_parser_in.mli: flambda_parser.mly
${MENHIR} --table flambda_parser.mly --base flambda_parser_in

flambda_parser.ml: flambda_parser_in.ml
sed "s/MenhirLib/CamlinternalMenhirLib/g" $< > $@

flambda_parser.mli: flambda_parser.mli.in
flambda_parser.mli: flambda_parser_in.mli
sed "s/MenhirLib/CamlinternalMenhirLib/g" $< > $@

flambda_parser_messages.ml: flambda_parser.mly flambda_parser.messages
Expand Down
70 changes: 70 additions & 0 deletions middle_end/flambda/parser/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
; Using user actions rather than (ocamllex) and (menhir) because the Makefile
; runs ocamllex and menhir from this directory rather than the workspace root,
; meaning the generated source is annotated with the bare filename.

; These rules are disabled by default because you need precisely the correct
; version of Menhir installed. Comment out (mode fallback) and uncomment (mode
; promote) in each stanza if you make changes to the Flambda
; parser.

(rule
(mode fallback)
;(mode promote)
(target flambda_lex.ml)
(deps flambda_lex.mll)
(action (run ocamllex -q %{deps})))

(rule
(mode fallback)
;(mode promote)
(target flambda_parser.ml)
(deps flambda_parser_in.ml)
(action (with-stdin-from %{deps}
(with-stdout-to %{target}
(run sed "s/MenhirLib/CamlinternalMenhirLib/g")))))

(rule
(mode fallback)
;(mode promote)
(target flambda_parser.mli)
(deps flambda_parser_in.mli)
(action (with-stdin-from %{deps}
(with-stdout-to %{target}
(run sed "s/MenhirLib/CamlinternalMenhirLib/g")))))

(rule
(targets flambda_parser_in.ml
flambda_parser_in.mli)
(deps flambda_parser.mly)
(action (run menhir --table %{deps} --base flambda_parser_in)))

; NOTE: flambda_parser.messages is massively out of date. No need to activate
; this rule unless you've fixed it (thanks!).
(rule
(mode fallback)
;(mode promote)
(target flambda_parser_messages.ml)
(deps flambda_parser.messages flambda_parser.mly)
(action (with-stdout-to %{target}
(run menhir --compile-errors %{deps}))))

(alias (name list-errors))

(rule
(alias list-errors)
(target flambda_parser.new-messages)
(deps flambda_parser.mly)
(mode promote)
(action (with-stdout-to %{target}
(run menhir --list-errors %{deps}))))

(alias (name check-errors))

(rule
(alias check-errors)
(deps flambda_parser.mly
flambda_parser.messages
flambda_parser.new-messages)
(action (run menhir flambda_parser.mly
--compare-errors flambda_parser.messages
--compare-errors flambda_parser.new-messages)))

0 comments on commit 3bd0288

Please sign in to comment.