package / ocaml-base-compiler.4.10.0 / Makefile.menhir
1 #**************************************************************************
2 #* *
3 #* OCaml *
4 #* *
5 #* Gabriel Scherer, projet Parsifal, INRIA Saclay *
6 #* *
7 #* Copyright 2018 Institut National de Recherche en Informatique et *
8 #* en Automatique. *
9 #* *
10 #* All rights reserved. This file is distributed under the terms of *
11 #* the GNU Lesser General Public License version 2.1, with the *
12 #* special exception on linking described in the file LICENSE. *
13 #* *
14 #**************************************************************************
15
16 # The rules in this Makefile use Menhir to rebuild the OCaml compiler
17 # parser. They are included in the main Makefile, so should be invoked
18 # directly, for example 'make promote-menhir'. They must be called
19 # after any modification to parsing/parser.mly, for the modification
20 # to affect the parser linked in the produced compiler:
21 #
22 # - promote-menhir builds the parser from parser.mly and stores it in
23 # the boot/ directory, so that future builds of the compiler use the
24 # updated result. Use it to make permanent changes to the compiler
25 # parser.
26 #
27 # - demote-menhir undoes the effect of promote-menhir. The files in
28 # the boot/ directory that are affected by promote-menhir and are
29 # under version control are restored to their normal state (HEAD).
30 #
31 # - test-menhir builds the parser from parser.mly without storing it
32 # in the boot/ directory, and only checks that the generated parser
33 # builds correctly. Use it to quickly check if a parser.mly change
34 # breaks the build. If you want to test a compiler produced with
35 # the new parser, you must use promote-menhir instead.
36 # (Using this rule requires a partial compiler build as obtained
37 # by 'make core' or 'make world'.)
38 #
39 # - clean-menhir removes the files generated by Menhir from parsing/,
40 # keeping only the reference sources for the grammar.
41 #
42 # - depend-menhir updates the dependency information for the
43 # Menhir-generated parser, which is versioned in the OCaml repository
44 # like all other .depend files. It should be used when the dependencies
45 # (of the OCaml code in the grammar semantic actions) change.
46
47 MENHIR ?= menhir
48
49 ## Unused tokens
50
51 # tokens COMMENT, DOCSTRING and EOL are produced by special lexer
52 # modes used by other consumers than the parser.
53
54 # GREATERBRACKET ">]" was added by the parser by symmetry with "[<"
55 # (which is used in polymorphic variant), but is not currently used by
56 # the grammar.
57
58 unused_tokens := COMMENT DOCSTRING EOL GREATERRBRACKET
59
60 ## Menhir compilation flags
61
62 MENHIRFLAGS := --explain --dump --ocamlc "$(CAMLC) $(COMPFLAGS)" --infer \
63 --lalr --strict --table -lg 1 -la 1 \
64 $(addprefix --unused-token ,$(unused_tokens)) --fixed-exception
65
66 ## promote-menhir
67
68 .PHONY: promote-menhir
69 promote-menhir: parsing/parser.mly
70 @ $(MAKE) import-menhirLib
71 $(MENHIR) $(MENHIRFLAGS) parsing/parser.mly
72 # The generated parser.ml may contain lexer directives containing
73 # the absolute path to Menhir's standard library on the promoter's machine.
74 # This is benign but will generate pointless churn if another developer
75 # rebuilds the same grammar (from the same Menhir version).
76 @ for f in $(addprefix parser.,ml mli) ; do \
77 sed \
78 's,^#\(.*\)"[^"]*/menhir/standard.mly",#\1"menhir/standard.mly",g' \
79 parsing/$$f \
80 > boot/menhir/$$f; \
81 rm parsing/$$f; \
82 done
83
84 # The import-menhirLib invocation in promote-menhir ensures that each
85 # update of the boot/ parser is paired with an update of the imported
86 # menhirLib; otherwise it would be easy to generate a parser and keep
87 # an incompatible version of menhirLib, which would fail at
88 # compile-time.
89
90 .PHONY: import-menhirLib
91 import-menhirLib:
92 @ mkdir -p boot/menhir
93 @ cp \
94 $(addprefix `$(MENHIR) --suggest-menhirLib`/menhirLib.,ml mli) \
95 boot/menhir
96
97
98 ## demote-menhir
99
100 DEMOTE:=menhirLib.ml menhirLib.mli parser.ml parser.mli
101
102 .PHONY: demote-menhir
103 demote-menhir:
104 git checkout HEAD -- $(addprefix boot/menhir/,$(DEMOTE))
105
106 ## test-menhir
107
108 # This rule assumes that the `parsing/` sources and its dependencies
109 # have already been compiled; 'make core' suffices to be in that
110 # state. We don't make 'core' an explicit dependency, as building
111 # 'test-menhir' repeatedly would rebuild the compiler each time
112 # (parser.ml has changed), without actually taking the changes from
113 # parser.mly into account ('core' uses the parser from boot/).
114
115 # The test-menhir target does not read or write the boot directory,
116 # it directly builds the parser in parsing/. In particular, it must
117 # duplicate the MenhirLib->CamlinternalMenhirlib renaming usually
118 # performed by the parsing/parser.ml import rule in the main
119 # Makefile.
120 .PHONY: test-menhir
121 test-menhir: parsing/parser.mly
122 $(MENHIR) $(MENHIRFLAGS) parsing/parser.mly
123 for f in $(addprefix parsing/parser.,ml mli) ; do \
124 cat $$f | sed "s/MenhirLib/CamlinternalMenhirLib/g" > $$f.tmp && \
125 mv $$f.tmp $$f ; \
126 done
127 $(MAKE) parsing/parser.cmo
128
129
130 ## clean-menhir
131
132 partialclean-menhir::
133 rm -f \
134 $(addprefix parsing/parser.,ml mli) \
135 $(addprefix parsing/camlinternalMenhirLib.,ml mli)
136
137 clean-menhir: partialclean-menhir
138
139
140 ## depend-menhir
141
142 .PHONY: depend-menhir
143 depend-menhir:
144 $(MENHIR) --depend --ocamldep "$(CAMLDEP) $(DEPFLAGS) $(DEPINCLUDES)" \
145 parsing/parser.mly > .depend.menhir
146 # this rule depends on the variables CAMLDEP, DEPFLAGS, DEPINCLUDES
147 # defined in Makefile, so it can only be invoked from the main Makefile
148
149 include .depend.menhir
150
151 ## interpret-menhir
152
153 # This rule runs Menhir in interactive mode.
154 # The user can enter sentences, such as:
155 # implementation: TYPE LIDENT EQUAL LIDENT EOF
156 # and see how Menhir interprets them.
157
158 interpret-menhir:
159 @ echo "Please wait, I am building the LALR automaton..."
160 @ $(MENHIR) $(MENHIRFLAGS) parsing/parser.mly \
161 --interpret \
162 --interpret-show-cst \
163 --trace \
164