Skip to content

Commit 6c4d516

Browse files
authored
Backport dead code in vendored parsers (ocaml-ppx#2424)
* vendored parsers: Backport dead-code as comments Dead code in parser-extended have been removed in the past. I reverted that and commented it out instead. This makes the code more similar with parser-standard and helps recognize new code from previously removed code. * Backport 5.1 error printing code This slightly change some error messages.
1 parent aac4dc6 commit 6c4d516

File tree

13 files changed

+198
-8
lines changed

13 files changed

+198
-8
lines changed

test/failing/tests/unit_lex.ml.broken-ref

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ File "tests/unit_lex.ml", line 18, characters 4-10:
33
^^^^^^
44
Alert deprecated: ISO-Latin1 characters in identifiers
55
ocamlformat: ignoring "tests/unit_lex.ml" (syntax error)
6+
67
File "tests/unit_lex.ml", line 55, characters 2-8:
78
55 | '\999'; (* wrong, but yet... *)
89
^^^^^^

test/failing/tests/unit_values.ml.broken-ref

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ File "tests/unit_values.ml", line 6, characters 10-11:
33
^
44
Alert deprecated: ISO-Latin1 characters in identifiers
55
ocamlformat: ignoring "tests/unit_values.ml" (syntax error)
6+
67
File "tests/unit_values.ml", line 6, characters 11-12:
78
6 | let i32 = −1073741824, 1073741823
89
^

test/passing/tests/error3.ml.err

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ File "tests/error3.ml", line 2, characters 0-13:
33
2 | (** a or b *)
44
^^^^^^^^^^^^^
55
Warning 50 [unexpected-docstring]: ambiguous documentation comment
6+
67
File "tests/error3.ml", line 3, characters 8-16:
78
3 | let b = (** ? *) ()
89
^^^^^^^^

test/passing/tests/error4.ml.err

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ File "tests/error4.ml", line 2, characters 0-13:
22
2 | (** a or b *)
33
^^^^^^^^^^^^^
44
Warning 50 [unexpected-docstring]: ambiguous documentation comment
5+
56
File "tests/error4.ml", line 3, characters 8-16:
67
3 | let b = (** ? *) ()
78
^^^^^^^^

test/passing/tests/option.ml.err

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ File "tests/option.ml", line 63, characters 17-28:
33
^^^^^^^^^^^
44
Warning 47 [attribute-payload]: illegal payload for attribute 'ocamlformat'.
55
margin not allowed here
6+
67
File "tests/option.ml", line 13, characters 3-19:
78
13 | [@@ocamlformat.typo "if-then-else=keyword-first"]
89
^^^^^^^^^^^^^^^^
910
Warning 47 [attribute-payload]: illegal payload for attribute 'ocamlformat.typo'.
1011
Invalid format: Unknown suffix "typo"
12+
1113
File "tests/option.ml", line 21, characters 3-14:
1214
21 | [@@ocamlformat 1, "if-then-else=keyword-first"]
1315
^^^^^^^^^^^
1416
Warning 47 [attribute-payload]: illegal payload for attribute 'ocamlformat'.
1517
Invalid format: String expected
18+
1619
File "tests/option.ml", line 28, characters 3-14:
1720
28 | [@@ocamlformat "if-then-else=bad"]
1821
^^^^^^^^^^^
1922
Warning 47 [attribute-payload]: illegal payload for attribute 'ocamlformat'.
2023
For option "if-then-else": invalid value 'bad', expected one of 'compact', 'fit-or-vertical', 'vertical', 'keyword-first' or 'k-r'
24+
2125
File "tests/option.ml", line 39, characters 14-25:
2226
39 | [@@ocamlformat "if-then-else=bad"]
2327
^^^^^^^^^^^

test/unit/test_translation_unit.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ let test_parse_and_format_module_type =
6565
~expected:
6666
(Error
6767
{|test_unit: ignoring "<test>" (syntax error)
68+
6869
File "<test>", line 1, characters 3-3:
6970
Error: Syntax error: 'end' expected
7071
File "<test>", line 1, characters 0-3:

vendor/ocaml-common/location.ml

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,20 @@ let setup_terminal () =
9595
input in the terminal. This would not be possible without this information,
9696
since printing several warnings/errors adds text between the user input and
9797
the bottom of the terminal.
98+
99+
We also use for {!is_first_report}, see below.
98100
*)
99101
let num_loc_lines = ref 0
100102

103+
(* We use [num_loc_lines] to determine if the report about to be
104+
printed is the first or a follow-up report of the current
105+
"batch" -- contiguous reports without user input in between, for
106+
example for the current toplevel phrase. We use this to print
107+
a blank line between messages of the same batch.
108+
*)
109+
let is_first_message () =
110+
!num_loc_lines = 0
111+
101112
(* This is used by the toplevel to reset [num_loc_lines] before each phrase *)
102113
let reset () =
103114
num_loc_lines := 0
@@ -107,6 +118,13 @@ let echo_eof () =
107118
print_newline ();
108119
incr num_loc_lines
109120

121+
(* This is used by the toplevel and the report printers below. *)
122+
let separate_new_message ppf =
123+
if not (is_first_message ()) then begin
124+
Format.pp_print_newline ppf ();
125+
incr num_loc_lines
126+
end
127+
110128
(* Code printing errors and warnings must be wrapped using this function, in
111129
order to update [num_loc_lines].
112130
@@ -459,20 +477,28 @@ let highlight_quote ppf
459477
(* Single-line error *)
460478
Format.fprintf ppf "%s | %s@," line_nb line;
461479
Format.fprintf ppf "%*s " (String.length line_nb) "";
462-
String.iteri (fun i c ->
480+
(* Iterate up to [rightmost], which can be larger than the length of
481+
the line because we may point to a location after the end of the
482+
last token on the line, for instance:
483+
{[
484+
token
485+
^
486+
Did you forget ...
487+
]} *)
488+
for i = 0 to rightmost.pos_cnum - line_start_cnum - 1 do
463489
let pos = line_start_cnum + i in
464490
if ISet.is_start iset ~pos <> None then
465491
Format.fprintf ppf "@{<%s>" highlight_tag;
466492
if ISet.mem iset ~pos then Format.pp_print_char ppf '^'
467-
else if pos < rightmost.pos_cnum then begin
493+
else if i < String.length line then begin
468494
(* For alignment purposes, align using a tab for each tab in the
469495
source code *)
470-
if c = '\t' then Format.pp_print_char ppf '\t'
496+
if line.[i] = '\t' then Format.pp_print_char ppf '\t'
471497
else Format.pp_print_char ppf ' '
472498
end;
473499
if ISet.is_end iset ~pos <> None then
474500
Format.fprintf ppf "@}"
475-
) line;
501+
done;
476502
Format.fprintf ppf "@}@,"
477503
| _ ->
478504
(* Multi-line error *)
@@ -722,6 +748,7 @@ let batch_mode_printer : report_printer =
722748
let pp_txt ppf txt = Format.fprintf ppf "@[%t@]" txt in
723749
let pp self ppf report =
724750
setup_colors ();
751+
separate_new_message ppf;
725752
(* Make sure we keep [num_loc_lines] updated.
726753
The tabulation box is here to give submessage the option
727754
to be aligned with the main message box
@@ -895,6 +922,32 @@ let alert ?(def = none) ?(use = none) ~kind loc message =
895922
let deprecated ?def ?use loc message =
896923
alert ?def ?use ~kind:"deprecated" loc message
897924

925+
let auto_include_alert lib =
926+
let message = Printf.sprintf "\
927+
OCaml's lib directory layout changed in 5.0. The %s subdirectory has been \
928+
automatically added to the search path, but you should add -I +%s to the \
929+
command-line to silence this alert (e.g. by adding %s to the list of \
930+
libraries in your dune file, or adding use_%s to your _tags file for \
931+
ocamlbuild, or using -package %s for ocamlfind)." lib lib lib lib lib in
932+
let alert =
933+
{Warnings.kind="ocaml_deprecated_auto_include"; use=none; def=none;
934+
message = Format.asprintf "@[@\n%a@]" Format.pp_print_text message}
935+
in
936+
prerr_alert none alert
937+
938+
let deprecated_script_alert program =
939+
let message = Printf.sprintf "\
940+
Running %s where the first argument is an implicit basename with no \
941+
extension (e.g. %s script-file) is deprecated. Either rename the script \
942+
(%s script-file.ml) or qualify the basename (%s ./script-file)"
943+
program program program program
944+
in
945+
let alert =
946+
{Warnings.kind="ocaml_deprecated_cli"; use=none; def=none;
947+
message = Format.asprintf "@[@\n%a@]" Format.pp_print_text message}
948+
in
949+
prerr_alert none alert
950+
898951
(******************************************************************************)
899952
(* Reporting errors on exceptions *)
900953

vendor/ocaml-common/location.mli

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,25 @@ val input_phrase_buffer: Buffer.t option ref
8888
(** {1 Toplevel-specific functions} *)
8989

9090
val echo_eof: unit -> unit
91+
val separate_new_message: formatter -> unit
9192
val reset: unit -> unit
9293

9394

94-
(** {1 Printing locations} *)
95+
(** {1 Rewriting path } *)
9596

9697
val rewrite_absolute_path: string -> string
9798
(** rewrite absolute path to honor the BUILD_PATH_PREFIX_MAP
9899
variable (https://reproducible-builds.org/specs/build-path-prefix-map/)
99100
if it is set. *)
100101

101102
val absolute_path: string -> string
103+
(** [absolute_path path] first makes an absolute path, [s] from [path],
104+
prepending the current working directory if [path] was relative.
105+
Then [s] is rewritten using [rewrite_absolute_path].
106+
Finally the result is normalized by eliminating instances of
107+
['.'] or ['..']. *)
108+
109+
(** {1 Printing locations} *)
102110

103111
val show_filename: string -> string
104112
(** In -absname mode, return the absolute path for this filename.
@@ -243,6 +251,13 @@ val deprecated: ?def:t -> ?use:t -> t -> string -> unit
243251
val alert: ?def:t -> ?use:t -> kind:string -> t -> string -> unit
244252
(** Prints an arbitrary alert. *)
245253

254+
val auto_include_alert: string -> unit
255+
(** Prints an alert that -I +lib has been automatically added to the load
256+
path *)
257+
258+
val deprecated_script_alert: string -> unit
259+
(** [deprecated_script_alert command] prints an alert that [command foo] has
260+
been deprecated in favour of [command ./foo] *)
246261

247262
(** {1 Reporting errors} *)
248263

vendor/parser-extended/parser.mly

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ let mkcf ~loc ?attrs ?docs d =
102102
Cf.mk ~loc:(make_loc loc) ?attrs ?docs d
103103

104104
let mkrhs rhs loc = mkloc rhs (make_loc loc)
105+
(*
106+
let ghrhs rhs loc = mkloc rhs (ghost_loc loc)
107+
*)
105108

106109
let mk_optional lbl loc = Optional (mkrhs lbl loc)
107110
let mk_labelled lbl loc = Labelled (mkrhs lbl loc)
@@ -150,6 +153,9 @@ let mkpatvar ~loc name =
150153
let ghexp ~loc d = Exp.mk ~loc:(ghost_loc loc) d
151154
let ghpat ~loc d = Pat.mk ~loc:(ghost_loc loc) d
152155
let ghtyp ~loc d = Typ.mk ~loc:(ghost_loc loc) d
156+
(*
157+
let ghloc ~loc d = { txt = d; loc = ghost_loc loc }
158+
*)
153159
let ghstr ~loc d = Str.mk ~loc:(ghost_loc loc) d
154160
let ghsig ~loc d = Sig.mk ~loc:(ghost_loc loc) d
155161

@@ -182,6 +188,43 @@ let mkuplus ~oploc name arg =
182188
and locations-as-Location.t; it should be clear when we move from
183189
one world to the other *)
184190

191+
(*
192+
let mkexp_cons_desc consloc args =
193+
Pexp_construct(mkrhs (Lident "::") consloc, Some args)
194+
let mkexp_cons ~loc consloc args =
195+
mkexp ~loc (mkexp_cons_desc consloc args)
196+
197+
let mkpat_cons_desc consloc args =
198+
Ppat_construct(mkrhs (Lident "::") consloc, Some ([], args))
199+
let mkpat_cons ~loc consloc args =
200+
mkpat ~loc (mkpat_cons_desc consloc args)
201+
202+
let ghexp_cons_desc consloc args =
203+
Pexp_construct(ghrhs (Lident "::") consloc, Some args)
204+
let ghpat_cons_desc consloc args =
205+
Ppat_construct(ghrhs (Lident "::") consloc, Some ([], args))
206+
207+
let rec mktailexp nilloc = let open Location in function
208+
[] ->
209+
let nil = ghloc ~loc:nilloc (Lident "[]") in
210+
Pexp_construct (nil, None), nilloc
211+
| e1 :: el ->
212+
let exp_el, el_loc = mktailexp nilloc el in
213+
let loc = (e1.pexp_loc.loc_start, snd el_loc) in
214+
let arg = ghexp ~loc (Pexp_tuple [e1; ghexp ~loc:el_loc exp_el]) in
215+
ghexp_cons_desc loc arg, loc
216+
217+
let rec mktailpat nilloc = let open Location in function
218+
[] ->
219+
let nil = ghloc ~loc:nilloc (Lident "[]") in
220+
Ppat_construct (nil, None), nilloc
221+
| p1 :: pl ->
222+
let pat_pl, el_loc = mktailpat nilloc pl in
223+
let loc = (p1.ppat_loc.loc_start, snd el_loc) in
224+
let arg = ghpat ~loc (Ppat_tuple [p1; ghpat ~loc:el_loc pat_pl]) in
225+
ghpat_cons_desc loc arg, loc
226+
*)
227+
185228
let mkstrexp e attrs =
186229
{ pstr_desc = Pstr_eval (e, attrs); pstr_loc = e.pexp_loc }
187230

@@ -191,6 +234,16 @@ let mkexp_constraint ~loc e (t1, t2) =
191234
| _, Some t -> mkexp ~loc (Pexp_coerce(e, t1, t))
192235
| None, None -> assert false
193236

237+
(*
238+
let mkexp_opt_constraint ~loc e = function
239+
| None -> e
240+
| Some constraint_ -> mkexp_constraint ~loc e constraint_
241+
242+
let mkpat_opt_constraint ~loc p = function
243+
| None -> p
244+
| Some typ -> mkpat ~loc (Ppat_constraint(p, typ))
245+
*)
246+
194247
let syntax_error () =
195248
raise Syntaxerr.Escape_error
196249

@@ -201,6 +254,11 @@ let unclosed opening_name opening_loc closing_name closing_loc =
201254
let expecting loc nonterm =
202255
raise Syntaxerr.(Error(Expecting(make_loc loc, nonterm)))
203256

257+
(* Continues to parse removed syntax
258+
let removed_string_set loc =
259+
raise(Syntaxerr.Error(Syntaxerr.Removed_string_set(make_loc loc)))
260+
*)
261+
204262
(* Using the function [not_expecting] in a semantic action means that this
205263
syntactic form is recognized by the parser but is in fact incorrect. This
206264
idiom is used in a few places to produce ad hoc syntax error messages. *)
@@ -254,9 +312,20 @@ let loc_last (id : Longident.t Location.loc) : string Location.loc =
254312
let loc_lident (id : string Location.loc) : Longident.t Location.loc =
255313
loc_map (fun x -> Lident x) id
256314

315+
(*
316+
let exp_of_longident lid =
317+
let lid = loc_map (fun id -> Lident (Longident.last id)) lid in
318+
Exp.mk ~loc:lid.loc (Pexp_ident lid)
319+
*)
320+
257321
let exp_of_label lbl =
258322
Exp.mk ~loc:lbl.loc (Pexp_ident (loc_lident lbl))
259323

324+
(*
325+
let pat_of_label lbl =
326+
Pat.mk ~loc:lbl.loc (Ppat_var (loc_last lbl))
327+
*)
328+
260329
let mk_newtypes ~loc newtypes exp =
261330
let mkexp = mkexp ~loc in
262331
List.fold_right (fun newtype exp -> mkexp (Pexp_newtype (newtype, exp)))
@@ -299,6 +368,12 @@ let mkpat_attrs ~loc d attrs =
299368

300369
let wrap_class_attrs ~loc:_ body attrs =
301370
{body with pcl_attributes = attrs @ body.pcl_attributes}
371+
(*
372+
let wrap_mod_attrs ~loc:_ attrs body =
373+
{body with pmod_attributes = attrs @ body.pmod_attributes}
374+
let wrap_mty_attrs ~loc:_ attrs body =
375+
{body with pmty_attributes = attrs @ body.pmty_attributes}
376+
*)
302377

303378
let wrap_str_ext ~loc body ext =
304379
match ext with
@@ -1079,6 +1154,8 @@ parse_any_longident:
10791154

10801155
(* Functor arguments appear in module expressions and module types. *)
10811156

1157+
(* Compared to upstream, [functor_args] can be empty and is not in reverse
1158+
order. *)
10821159
%inline functor_args:
10831160
llist(functor_arg)
10841161
{ $1 }
@@ -1292,6 +1369,11 @@ module_binding_body:
12921369
| mkmod(
12931370
COLON mty = module_type EQUAL me = module_expr
12941371
{ Pmod_constraint(me, mty) }
1372+
(*
1373+
| arg_and_pos = functor_arg body = module_binding_body
1374+
{ let (_, arg) = arg_and_pos in
1375+
Pmod_functor(arg, body) }
1376+
*)
12951377
) { $1 }
12961378
;
12971379

@@ -2124,7 +2206,7 @@ expr:
21242206
| expr attribute
21252207
{ Exp.attr $1 $2 }
21262208
/* BEGIN AVOID */
2127-
(*
2209+
(* Allowed in exprs. Commented-out to reduce diffs with upstream.
21282210
| UNDERSCORE
21292211
{ not_expecting $loc($1) "wildcard \"_\"" }
21302212
*)
@@ -3486,6 +3568,7 @@ label_longident:
34863568
;
34873569
type_longident:
34883570
mk_longident(mod_ext_longident, LIDENT) { $1 }
3571+
(* Allow identifiers like [t/42]. *)
34893572
| LIDENT SLASH TYPE_DISAMBIGUATOR { Lident ($1 ^ "/" ^ $3) }
34903573
;
34913574
mod_longident:

vendor/parser-shims/parser_shims.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,14 @@ module Clflags = struct
4949
let color = ref None (* -color *)
5050
let error_style = ref None (* -error-style *)
5151
let unboxed_types = ref false
52+
let no_std_include = ref false
53+
end
54+
55+
module Load_path = struct
56+
type dir
57+
type auto_include_callback =
58+
(dir -> string -> string option) -> string -> string
59+
let init ~auto_include:_ _ = ()
60+
let get_paths () = []
61+
let auto_include_otherlibs _ _ s = s
5262
end

0 commit comments

Comments
 (0)