Skip to content

Commit 34ef567

Browse files
authored
flambda-backend: Move code between Printast and Pprintast (#2194)
* Revert "Pprintast now depends on Printast" This reverts commit bdac10c. That commit was part of #1417, but ended up getting squashed in a rebase. Why revert? Because it's hard to upstream this change, due to the new dependency of the tyvar printer on the lexer. This can be disentangled, but doing so would be disruptive for little gain. So I'm just undoing this unforced little cleanup, which isn't so important anyway. * Fix the build
1 parent 0d7cc30 commit 34ef567

File tree

13 files changed

+103
-96
lines changed

13 files changed

+103
-96
lines changed

.depend

Lines changed: 33 additions & 28 deletions
Large diffs are not rendered by default.

boot/ocamlc

5.59 KB
Binary file not shown.

dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,13 @@
262262
(location.mli as compiler-libs/location.mli)
263263
(longident.mli as compiler-libs/longident.mli)
264264
(docstrings.mli as compiler-libs/docstrings.mli)
265-
(printast.mli as compiler-libs/printast.mli)
266265
(syntaxerr.mli as compiler-libs/syntaxerr.mli)
267266
(ast_helper.mli as compiler-libs/ast_helper.mli)
268267
(camlinternalMenhirLib.mli as compiler-libs/camlinternalMenhirLib.mli)
269268
(parser.mli as compiler-libs/parser.mli)
270269
(lexer.mli as compiler-libs/lexer.mli)
271270
(parse.mli as compiler-libs/parse.mli)
271+
(printast.mli as compiler-libs/printast.mli)
272272
(pprintast.mli as compiler-libs/pprintast.mli)
273273
(ast_mapper.mli as compiler-libs/ast_mapper.mli)
274274
(ast_iterator.mli as compiler-libs/ast_iterator.mli)

parsing/parse.ml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,53 @@ 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+
108+
(* Error reporting for Syntaxerr *)
109+
(* The code has been moved here so that one can reuse Pprintast.tyvar *)
110+
111+
let prepare_error err =
112+
let open Syntaxerr in
113+
match err with
114+
| Unclosed(opening_loc, opening, closing_loc, closing) ->
115+
Location.errorf
116+
~loc:closing_loc
117+
~sub:[
118+
Location.msg ~loc:opening_loc
119+
"This '%s' might be unmatched" opening
120+
]
121+
"Syntax error: '%s' expected" closing
122+
123+
| Expecting (loc, nonterm) ->
124+
Location.errorf ~loc "Syntax error: %s expected." nonterm
125+
| Not_expecting (loc, nonterm) ->
126+
Location.errorf ~loc "Syntax error: %s not expected." nonterm
127+
| Applicative_path loc ->
128+
Location.errorf ~loc
129+
"Syntax error: applicative paths of the form F(X).t \
130+
are not supported when the option -no-app-func is set."
131+
| Variable_in_scope (loc, var) ->
132+
Location.errorf ~loc
133+
"In this scoped type, variable %a \
134+
is reserved for the local type %s."
135+
Pprintast.tyvar var var
136+
| Other loc ->
137+
Location.errorf ~loc "Syntax error"
138+
| Ill_formed_ast (loc, s) ->
139+
Location.errorf ~loc
140+
"broken invariant in parsetree: %s" s
141+
| Invalid_package_type (loc, s) ->
142+
Location.errorf ~loc "invalid package type: %s" s
143+
| Removed_string_set loc ->
144+
Location.errorf ~loc
145+
"Syntax error: strings are immutable, there is no assignment \
146+
syntax for them.\n\
147+
@{<hint>Hint@}: Mutable sequences of bytes are available in \
148+
the Bytes module.\n\
149+
@{<hint>Hint@}: Did you mean to use 'Bytes.set'?"
150+
151+
let () =
152+
Location.register_error_of_exn
153+
(function
154+
| Syntaxerr.Error err -> Some (prepare_error err)
155+
| _ -> None
156+
)

parsing/pprintast.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,14 @@ let iter_loc f ctxt {txt; loc = _} = f ctxt txt
288288

289289
let constant_string f s = pp f "%S" s
290290

291-
let tyvar = Printast.tyvar
291+
let tyvar ppf s =
292+
if String.length s >= 2 && s.[1] = '\'' then
293+
(* without the space, this would be parsed as
294+
a character literal *)
295+
Format.fprintf ppf "' %s" s
296+
else
297+
Format.fprintf ppf "'%s" s
298+
292299
let jkind_annotation = Jane_syntax.Layouts.Pprint.jkind_annotation
293300

294301
let tyvar_jkind_loc ~print_quote f (str,jkind) =
@@ -301,6 +308,7 @@ let tyvar_jkind_loc ~print_quote f (str,jkind) =
301308
| None -> pptv f str.txt
302309
| Some lay -> Format.fprintf f "(%a : %a)" pptv str.txt jkind_annotation lay
303310

311+
304312
let tyvar_loc f str = tyvar f str.txt
305313
let string_quot f x = pp f "`%s" x
306314

parsing/printast.ml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,10 @@ let arg_label i ppf = function
130130
| Optional s -> line i ppf "Optional \"%s\"\n" s
131131
| Labelled s -> line i ppf "Labelled \"%s\"\n" s
132132

133-
let tyvar ppf s =
134-
if String.length s >= 2 && s.[1] = '\'' then
135-
(* without the space, this would be parsed as
136-
a character literal *)
137-
Format.fprintf ppf "' %s" s
138-
else
139-
Format.fprintf ppf "'%s" s
140-
141133
let typevars ppf vs =
142-
List.iter (fun x -> fprintf ppf " %a" tyvar x.txt) vs
134+
List.iter (fun x -> fprintf ppf " '%s" x.txt) vs
135+
(* Don't use Pprintast.tyvar, as that causes a dependency cycle with
136+
Jane_syntax, which depends on this module for debugging. *)
143137

144138
let rec core_type i ppf x =
145139
line i ppf "core_type %a\n" fmt_location x.ptyp_loc;
@@ -991,4 +985,3 @@ let implementation ppf x = list 0 structure_item ppf x
991985
let top_phrase ppf x = toplevel_phrase 0 ppf x
992986

993987
let constant = fmt_constant
994-

parsing/printast.mli

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,3 @@ val payload: int -> formatter -> payload -> unit
3535
val core_type: int -> formatter -> core_type -> unit
3636
val extension_constructor: int -> formatter -> extension_constructor -> unit
3737

38-
val tyvar: Format.formatter -> string -> unit
39-
(** Print a type variable name, taking care of the special treatment
40-
required for the single quote character in second position. *)

parsing/syntaxerr.ml

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -43,49 +43,3 @@ let location_of_error = function
4343

4444
let ill_formed_ast loc s =
4545
raise (Error (Ill_formed_ast (loc, s)))
46-
47-
let prepare_error err =
48-
match err with
49-
| Unclosed(opening_loc, opening, closing_loc, closing) ->
50-
Location.errorf
51-
~loc:closing_loc
52-
~sub:[
53-
Location.msg ~loc:opening_loc
54-
"This '%s' might be unmatched" opening
55-
]
56-
"Syntax error: '%s' expected" closing
57-
58-
| Expecting (loc, nonterm) ->
59-
Location.errorf ~loc "Syntax error: %s expected." nonterm
60-
| Not_expecting (loc, nonterm) ->
61-
Location.errorf ~loc "Syntax error: %s not expected." nonterm
62-
| Applicative_path loc ->
63-
Location.errorf ~loc
64-
"Syntax error: applicative paths of the form F(X).t \
65-
are not supported when the option -no-app-func is set."
66-
| Variable_in_scope (loc, var) ->
67-
Location.errorf ~loc
68-
"In this scoped type, variable %a \
69-
is reserved for the local type %s."
70-
Printast.tyvar var var
71-
| Other loc ->
72-
Location.errorf ~loc "Syntax error"
73-
| Ill_formed_ast (loc, s) ->
74-
Location.errorf ~loc
75-
"broken invariant in parsetree: %s" s
76-
| Invalid_package_type (loc, s) ->
77-
Location.errorf ~loc "invalid package type: %s" s
78-
| Removed_string_set loc ->
79-
Location.errorf ~loc
80-
"Syntax error: strings are immutable, there is no assignment \
81-
syntax for them.\n\
82-
@{<hint>Hint@}: Mutable sequences of bytes are available in \
83-
the Bytes module.\n\
84-
@{<hint>Hint@}: Did you mean to use 'Bytes.set'?"
85-
86-
let () =
87-
Location.register_error_of_exn
88-
(function
89-
| Error err -> Some (prepare_error err)
90-
| _ -> None
91-
)

tools/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ ocamldep.opt$(EXE): $(call byte2native, $(OCAMLDEP))
8585
OCAMLPROF=config.cmo build_path_prefix_map.cmo misc.cmo identifiable.cmo \
8686
numbers.cmo arg_helper.cmo clflags.cmo debug.cmo terminfo.cmo \
8787
warnings.cmo location.cmo longident.cmo docstrings.cmo \
88-
printast.cmo syntaxerr.cmo ast_helper.cmo \
88+
syntaxerr.cmo ast_helper.cmo \
8989
language_extension_kernel.cmo language_extension.cmo \
9090
jane_asttypes.cmo jane_syntax_parsing.cmo jane_syntax.cmo \
9191
ast_iterator.cmo builtin_attributes.cmo \

typing/oprint.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ let rec print_list pr sep ppf =
311311
let pr_present =
312312
print_list (fun ppf s -> fprintf ppf "`%s" s) (fun ppf -> fprintf ppf "@ ")
313313

314-
let pr_var = Printast.tyvar
314+
let pr_var = Pprintast.tyvar
315315
let ty_var ~non_gen ppf s =
316316
pr_var ppf (if non_gen then "_" ^ s else s)
317317

typing/printtyped.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ let arg_label i ppf = function
145145
let typevar_jkind ~print_quote ppf (v, l) =
146146
let pptv =
147147
if print_quote
148-
then Printast.tyvar
148+
then Pprintast.tyvar
149149
else fun ppf s -> fprintf ppf "%s" s
150150
in
151151
match l with

typing/typedecl.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2943,7 +2943,7 @@ let report_error ppf = function
29432943
fprintf ppf "an unnamed existential variable"
29442944
| Some str ->
29452945
fprintf ppf "the existential variable %a"
2946-
Printast.tyvar str in
2946+
Pprintast.tyvar str in
29472947
fprintf ppf "@[This type cannot be unboxed because@ \
29482948
it might contain both float and non-float values,@ \
29492949
depending on the instantiation of %a.@ \

typing/typetexp.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ let report_error env ppf = function
13811381
but is here applied to %i argument(s)@]"
13821382
longident lid expected provided
13831383
| Bound_type_variable name ->
1384-
fprintf ppf "Already bound type parameter %a" Printast.tyvar name
1384+
fprintf ppf "Already bound type parameter %a" Pprintast.tyvar name
13851385
| Recursive_type ->
13861386
fprintf ppf "This type is recursive"
13871387
| Unbound_row_variable lid ->
@@ -1437,7 +1437,7 @@ let report_error env ppf = function
14371437
| Cannot_quantify (name, reason) ->
14381438
fprintf ppf
14391439
"@[<hov>The universal type variable %a cannot be generalized:@ "
1440-
Printast.tyvar name;
1440+
Pprintast.tyvar name;
14411441
begin match reason with
14421442
| Unified v ->
14431443
fprintf ppf "it is bound to@ %a" Printtyp.type_expr v
@@ -1451,7 +1451,7 @@ let report_error env ppf = function
14511451
fprintf ppf
14521452
"@[<hov>The universal type variable %a was %s to have@ \
14531453
layout %a, but was inferred to have %t.@]"
1454-
Printast.tyvar name
1454+
Pprintast.tyvar name
14551455
(if jkind_info.defaulted then "defaulted" else "declared")
14561456
Jkind.format jkind_info.original_jkind
14571457
(fun ppf -> match Jkind.get inferred_jkind with

0 commit comments

Comments
 (0)