Skip to content

Commit d841d7f

Browse files
Resolve OCaml 5 conflicts in parsing/ (#173)
* fix parser.mly * Fix conflicts in parse.ml, pprintast.ml, location.ml * Generate parser as part of the dune build, rather than using boot/ Requires two hacks for MenhirLib, copying the hacks used in the Makefile: 1. Copy MenhirLib to camlinternalMenhirLib (keeping a fix for a locals-related horror from Makefile.menhir) 2. Add "module MenhirLib = CamlinternalMenhirLib" to parser.ml (expressed as a custom dune preprocessor) a * promote menhir * CR layouts 1.5 --------- Co-authored-by: Stephen Dolan <[email protected]>
1 parent e81bd7d commit d841d7f

File tree

11 files changed

+32634
-69958
lines changed

11 files changed

+32634
-69958
lines changed

boot/menhir/parser.ml

Lines changed: 32524 additions & 69567 deletions
Large diffs are not rendered by default.

compilerlibs/Makefile.compilerlibs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,9 @@ UTILS = \
3434
utils/local_store.cmo \
3535
utils/load_path.cmo \
3636
utils/clflags.cmo \
37-
<<<<<<< HEAD
3837
utils/debug.cmo \
3938
utils/language_extension_kernel.cmo \
4039
utils/language_extension.cmo \
41-
||||||| merged common ancestors
42-
=======
43-
>>>>>>> ocaml/5.1
4440
utils/profile.cmo \
4541
utils/terminfo.cmo \
4642
utils/ccomp.cmo \
@@ -88,15 +84,8 @@ TYPING = \
8884
typing/path.cmo \
8985
typing/jkind.cmo \
9086
typing/primitive.cmo \
91-
<<<<<<< HEAD
9287
typing/shape.cmo \
9388
typing/mode.cmo \
94-
||||||| merged common ancestors
95-
typing/type_immediacy.cmo \
96-
=======
97-
typing/type_immediacy.cmo \
98-
typing/shape.cmo \
99-
>>>>>>> ocaml/5.1
10089
typing/types.cmo \
10190
typing/btype.cmo \
10291
typing/oprint.cmo \

dune

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
(copy_files# utils/*.ml{,i})
3232
(copy_files# parsing/*.ml{,i})
33+
(copy_files parsing/parser.mly)
3334
(copy_files# typing/*.ml{,i})
3435
(copy_files# bytecomp/*.ml{,i})
3536
(copy_files# driver/*.ml{,i})
@@ -42,6 +43,14 @@
4243
;(copy_files# middle_end/flambda/*.ml{,i})
4344
;(copy_files# middle_end/flambda/base_types/*.ml{,i})
4445

46+
(menhir
47+
(modules parser)
48+
(flags
49+
--lalr --explain --dump --require-aliases --strict
50+
--unused-token COMMENT --unused-token DOCSTRING --unused-token EOL --unused-token GREATERRBRACKET
51+
--fixed-exception --table
52+
--strategy simplified))
53+
4554
(library
4655
(name ocamlcommon)
4756
(wrapped false)
@@ -52,6 +61,13 @@
5261
; remove -w -67 by adding the camlinternalMenhirLib hack like the Makefile
5362
))
5463
(ocamlopt_flags (:include %{project_root}/ocamlopt_flags.sexp))
64+
(preprocess
65+
(per_module
66+
((action
67+
(progn
68+
(echo "module MenhirLib = CamlinternalMenhirLib")
69+
(run cat %{input-file})))
70+
parser)))
5571
(library_flags -linkall)
5672
(modules_without_implementation
5773
annot asttypes jane_asttypes cmo_format outcometree parsetree debug_event)

dune-project.jst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(lang dune 2.8)
22
(wrapped_executables false)
33
(using experimental_building_ocaml_compiler_with_dune 0.1)
4+
(using menhir 2.1)
45

56
(use_standard_c_and_cxx_flags true)
67
(cram enable)

parsing/dune

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,20 @@
1212
;* *
1313
;**************************************************************************
1414

15-
;; We're just reusing the stuff from boot/ here.
16-
;; One could add a dune file in boot/menhir/ with the appropriate rules if we
17-
;; want to regenerate the parser while building with dune, but it doesn't seem
18-
;; essential right now.
19-
2015
(rule
2116
(targets camlinternalMenhirLib.ml)
22-
(mode fallback)
23-
(action (copy# ../boot/menhir/menhirLib.ml %{targets})))
24-
25-
(rule
26-
(targets camlinternalMenhirLib.mli)
27-
(mode fallback)
28-
(action (copy# ../boot/menhir/menhirLib.mli %{targets})))
29-
30-
(rule
31-
(targets parser.ml)
32-
(mode fallback)
33-
(deps (:dep ../boot/menhir/parser.ml))
3417
(action
35-
(with-stdout-to %{targets}
36-
(bash "cat %{dep} | sed 's/MenhirLib/CamlinternalMenhirLib/g'"))))
18+
(with-stdout-to %{targets}
19+
;; Partial applications of the form Obj.magic f x in menhirLib cause
20+
;; an issue with locals, so rewrite these to Obj.magic (f x)
21+
(bash "cat $(menhir --suggest-menhirLib)/menhirLib.ml |
22+
sed 's/\\b\\(in\\|then\\|with\\|else\\)\\b/@@@\\1/g; s/Obj.magic \\([a-z0-9_]\\+\\( [a-z0-9_]\\+\\)\\+\\)/Obj.magic (\\1)/g; s/@@@//g'"))))
3723

3824
(rule
39-
(targets parser.mli)
40-
(mode fallback)
41-
(deps (:dep ../boot/menhir/parser.mli))
25+
(targets camlinternalMenhirLib.mli)
4226
(action
43-
(with-stdout-to %{targets}
44-
(bash "cat %{dep} | sed 's/MenhirLib/CamlinternalMenhirLib/g'"))))
27+
(with-stdout-to %{targets}
28+
(bash "cat $(menhir --suggest-menhirLib)/menhirLib.mli"))))
4529

4630
(ocamllex
4731
(modules lexer)

parsing/location.ml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ open Lexing
1818
type t = Warnings.loc =
1919
{ loc_start: position; loc_end: position; loc_ghost: bool }
2020

21-
<<<<<<< HEAD
2221
let compare_position : position -> position -> int =
2322
fun
2423
{ pos_fname = pos_fname_1
@@ -59,18 +58,7 @@ let compare
5958
| i -> i
6059
;;
6160

62-
let in_file name =
63-
let loc = { dummy_pos with pos_fname = name } in
64-
{ loc_start = loc; loc_end = loc; loc_ghost = true }
65-
;;
66-
||||||| merged common ancestors
67-
let in_file name =
68-
let loc = { dummy_pos with pos_fname = name } in
69-
{ loc_start = loc; loc_end = loc; loc_ghost = true }
70-
;;
71-
=======
7261
let in_file = Warnings.ghost_loc_in_file
73-
>>>>>>> ocaml/5.1
7462

7563
let none = in_file "_none_"
7664
let is_none l = (l = none)

parsing/parse.ml

Lines changed: 0 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -104,99 +104,3 @@ let constr_ident= wrap Parser.parse_constr_longident
104104
let extended_module_path = wrap Parser.parse_mod_ext_longident
105105
let simple_module_path = wrap Parser.parse_mod_longident
106106
let type_ident = wrap Parser.parse_mty_longident
107-
<<<<<<< HEAD
108-
||||||| merged common ancestors
109-
110-
(* Error reporting for Syntaxerr *)
111-
(* The code has been moved here so that one can reuse Pprintast.tyvar *)
112-
113-
let prepare_error err =
114-
let open Syntaxerr in
115-
match err with
116-
| Unclosed(opening_loc, opening, closing_loc, closing) ->
117-
Location.errorf
118-
~loc:closing_loc
119-
~sub:[
120-
Location.msg ~loc:opening_loc
121-
"This '%s' might be unmatched" opening
122-
]
123-
"Syntax error: '%s' expected" closing
124-
125-
| Expecting (loc, nonterm) ->
126-
Location.errorf ~loc "Syntax error: %s expected." nonterm
127-
| Not_expecting (loc, nonterm) ->
128-
Location.errorf ~loc "Syntax error: %s not expected." nonterm
129-
| Applicative_path loc ->
130-
Location.errorf ~loc
131-
"Syntax error: applicative paths of the form F(X).t \
132-
are not supported when the option -no-app-func is set."
133-
| Variable_in_scope (loc, var) ->
134-
Location.errorf ~loc
135-
"In this scoped type, variable %a \
136-
is reserved for the local type %s."
137-
Pprintast.tyvar var var
138-
| Other loc ->
139-
Location.errorf ~loc "Syntax error"
140-
| Ill_formed_ast (loc, s) ->
141-
Location.errorf ~loc
142-
"broken invariant in parsetree: %s" s
143-
| Invalid_package_type (loc, s) ->
144-
Location.errorf ~loc "invalid package type: %s" s
145-
146-
let () =
147-
Location.register_error_of_exn
148-
(function
149-
| Syntaxerr.Error err -> Some (prepare_error err)
150-
| _ -> None
151-
)
152-
=======
153-
154-
(* Error reporting for Syntaxerr *)
155-
(* The code has been moved here so that one can reuse Pprintast.tyvar *)
156-
157-
let prepare_error err =
158-
let open Syntaxerr in
159-
match err with
160-
| Unclosed(opening_loc, opening, closing_loc, closing) ->
161-
Location.errorf
162-
~loc:closing_loc
163-
~sub:[
164-
Location.msg ~loc:opening_loc
165-
"This '%s' might be unmatched" opening
166-
]
167-
"Syntax error: '%s' expected" closing
168-
169-
| Expecting (loc, nonterm) ->
170-
Location.errorf ~loc "Syntax error: %s expected." nonterm
171-
| Not_expecting (loc, nonterm) ->
172-
Location.errorf ~loc "Syntax error: %s not expected." nonterm
173-
| Applicative_path loc ->
174-
Location.errorf ~loc
175-
"Syntax error: applicative paths of the form F(X).t \
176-
are not supported when the option -no-app-func is set."
177-
| Variable_in_scope (loc, var) ->
178-
Location.errorf ~loc
179-
"In this scoped type, variable %a \
180-
is reserved for the local type %s."
181-
Pprintast.tyvar var var
182-
| Other loc ->
183-
Location.errorf ~loc "Syntax error"
184-
| Ill_formed_ast (loc, s) ->
185-
Location.errorf ~loc
186-
"broken invariant in parsetree: %s" s
187-
| Invalid_package_type (loc, s) ->
188-
Location.errorf ~loc "invalid package type: %s" s
189-
| Removed_string_set loc ->
190-
Location.errorf ~loc
191-
"Syntax error: strings are immutable, there is no assignment \
192-
syntax for them.\n\
193-
@{<hint>Hint@}: Mutable sequences of bytes are available in \
194-
the Bytes module.\n\
195-
@{<hint>Hint@}: Did you mean to use 'Bytes.set'?"
196-
let () =
197-
Location.register_error_of_exn
198-
(function
199-
| Syntaxerr.Error err -> Some (prepare_error err)
200-
| _ -> None
201-
)
202-
>>>>>>> ocaml/5.1

parsing/parser.mly

Lines changed: 28 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -3125,113 +3125,53 @@ labeled_simple_expr:
31253125
;
31263126
let_binding_body_no_punning:
31273127
let_ident strict_binding
3128-
<<<<<<< HEAD
3129-
{ ($1, $2) }
3128+
{ ($1, $2, None) }
31303129
| mode_flags let_ident type_constraint EQUAL seq_expr
31313130
{ let v = $2 in (* PR#7344 *)
3132-
||||||| merged common ancestors
3133-
{ ($1, $2) }
3134-
| let_ident type_constraint EQUAL seq_expr
3135-
{ let v = $1 in (* PR#7344 *)
3136-
=======
3137-
{ ($1, $2, None) }
3138-
| let_ident type_constraint EQUAL seq_expr
3139-
{ let v = $1 in (* PR#7344 *)
3140-
>>>>>>> ocaml/5.1
31413131
let t =
3142-
<<<<<<< HEAD
31433132
match $3 with
3144-
| N_ary.Pconstraint t -> t
3145-
| N_ary.Pcoerce (_, t) -> t
3146-
||||||| merged common ancestors
3147-
match $2 with
3148-
Some t, None -> t
3149-
| _, Some t -> t
3150-
| _ -> assert false
3151-
=======
3152-
match $2 with
3153-
Some t, None ->
3133+
| N_ary.Pconstraint t ->
31543134
Pvc_constraint { locally_abstract_univars = []; typ=t }
3155-
| ground, Some coercion -> Pvc_coercion { ground; coercion}
3156-
| _ -> assert false
3157-
>>>>>>> ocaml/5.1
3135+
| N_ary.Pcoerce (ground, coercion) -> Pvc_coercion { ground; coercion}
31583136
in
3159-
<<<<<<< HEAD
3160-
let loc = Location.(t.ptyp_loc.loc_start, t.ptyp_loc.loc_end) in
3161-
let typ = ghtyp ~loc (Ptyp_poly([],t)) in
3162-
let patloc = ($startpos($2), $endpos($3)) in
3163-
let pat =
3164-
mkpat_with_modes $1 (ghpat ~loc:patloc (Ppat_constraint(v, typ)))
3165-
in
3166-
let exp =
3167-
ghexp_with_modes $sloc $1
3168-
(wrap_exp_with_modes $1 (mkexp_constraint ~loc:$sloc $5 $3))
3169-
in
3170-
(pat, exp) }
3137+
let pat = mkpat_with_modes $1 v in
3138+
let exp = ghexp_with_modes $sloc $1 (wrap_exp_with_modes $1 $5) in
3139+
(pat, exp, Some t)
3140+
}
31713141
| mode_flags let_ident COLON poly(core_type) EQUAL seq_expr
3172-
{ let patloc = ($startpos($2), $endpos($4)) in
3173-
let bound_vars, inner_type = $4 in
3142+
{ let bound_vars, inner_type = $4 in
31743143
let ltyp = Jane_syntax.Layouts.Ltyp_poly { bound_vars; inner_type } in
31753144
let typ_loc = Location.ghostify (make_loc $loc($4)) in
31763145
let typ =
31773146
Jane_syntax.Layouts.type_of ~loc:typ_loc ltyp
31783147
in
3179-
let pat =
3180-
mkpat_with_modes $1
3181-
(ghpat ~loc:patloc
3182-
(Ppat_constraint($2, typ)))
3183-
in
3148+
let pat = mkpat_with_modes $1 $2 in
31843149
let exp = ghexp_with_modes $sloc $1 $6 in
3185-
(pat, exp) }
3150+
(pat, exp, Some (Pvc_constraint { locally_abstract_univars = []; typ }))
3151+
}
31863152
| let_ident COLON TYPE newtypes DOT core_type EQUAL seq_expr
3187-
{ let exp, poly =
3188-
wrap_type_annotation ~loc:$sloc $4 $6 $8 in
3189-
let loc = ($startpos($1), $endpos($6)) in
3190-
(ghpat ~loc (Ppat_constraint($1, poly)), exp) }
3191-
||||||| merged common ancestors
3192-
let loc = Location.(t.ptyp_loc.loc_start, t.ptyp_loc.loc_end) in
3193-
let typ = ghtyp ~loc (Ptyp_poly([],t)) in
3194-
let patloc = ($startpos($1), $endpos($2)) in
3195-
(ghpat ~loc:patloc (Ppat_constraint(v, typ)),
3196-
mkexp_constraint ~loc:$sloc $4 $2) }
3197-
| let_ident COLON poly(core_type) EQUAL seq_expr
3198-
{ let patloc = ($startpos($1), $endpos($3)) in
3199-
(ghpat ~loc:patloc
3200-
(Ppat_constraint($1, ghtyp ~loc:($loc($3)) $3)),
3201-
$5) }
3202-
| let_ident COLON TYPE lident_list DOT core_type EQUAL seq_expr
3203-
{ let exp, poly =
3204-
wrap_type_annotation ~loc:$sloc $4 $6 $8 in
3153+
(* The code upstream looks like:
3154+
{[
3155+
let constraint' =
3156+
Pvc_constraint { locally_abstract_univars=$4; typ = $6}
3157+
in
3158+
($1, $8, Some constraint')
3159+
]}
3160+
3161+
But this would require encoding [newtypes] (which, internally, may
3162+
associate a layout with a newtype) in Jane Syntax, which will require
3163+
a small amount of work.
3164+
*)
3165+
{ let exp, poly = wrap_type_annotation ~loc:$sloc $4 $6 $8 in
32053166
let loc = ($startpos($1), $endpos($6)) in
3206-
(ghpat ~loc (Ppat_constraint($1, poly)), exp) }
3207-
=======
3208-
(v, $4, Some t)
3209-
}
3210-
| let_ident COLON poly(core_type) EQUAL seq_expr
3211-
{
3212-
let t = ghtyp ~loc:($loc($3)) $3 in
3213-
($1, $5, Some (Pvc_constraint { locally_abstract_univars = []; typ=t }))
3214-
}
3215-
| let_ident COLON TYPE lident_list DOT core_type EQUAL seq_expr
3216-
{ let constraint' =
3217-
Pvc_constraint { locally_abstract_univars=$4; typ = $6}
3218-
in
3219-
($1, $8, Some constraint') }
3220-
>>>>>>> ocaml/5.1
3167+
(ghpat ~loc (Ppat_constraint($1, poly)), exp, None)
3168+
}
32213169
| pattern_no_exn EQUAL seq_expr
32223170
{ ($1, $3, None) }
32233171
| simple_pattern_not_ident COLON core_type EQUAL seq_expr
3224-
<<<<<<< HEAD
3225-
{ let loc = ($startpos($1), $endpos($3)) in
3226-
(ghpat ~loc (Ppat_constraint($1, $3)), $5) }
3227-
| mode_flag+ let_ident strict_binding_modes
3228-
{ ($2, ghexp_with_modes $sloc $1 ($3 $1)) }
3229-
||||||| merged common ancestors
3230-
{ let loc = ($startpos($1), $endpos($3)) in
3231-
(ghpat ~loc (Ppat_constraint($1, $3)), $5) }
3232-
=======
32333172
{ ($1, $5, Some(Pvc_constraint { locally_abstract_univars=[]; typ=$3 })) }
3234-
>>>>>>> ocaml/5.1
3173+
| mode_flag+ let_ident strict_binding_modes
3174+
{ ($2, ghexp_with_modes $sloc $1 ($3 $1), None) }
32353175
;
32363176
let_binding_body:
32373177
| let_binding_body_no_punning

0 commit comments

Comments
 (0)