Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update parsetree #95

Merged
merged 6 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
409 changes: 182 additions & 227 deletions lib/Normalize_std_ast.ml

Large diffs are not rendered by default.

131 changes: 65 additions & 66 deletions vendor/ocaml-common/location.ml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type 'a loc = {
let mkloc txt loc = { txt ; loc }
let mknoloc txt = mkloc txt none
let get_txt { txt } = txt
let get_loc { loc } = loc
let map f { txt; loc} = {txt = f txt; loc}
let compare_txt f { txt=t1 } { txt=t2 } = f t1 t2

Expand Down Expand Up @@ -215,7 +216,7 @@ let print_updating_num_loc_lines ppf f arg =
pp_print_flush ppf ();
pp_set_formatter_out_functions ppf out_functions

let setup_colors () =
let setup_tags () =
Misc.Style.setup !Clflags.color

(******************************************************************************)
Expand Down Expand Up @@ -254,7 +255,7 @@ let print_filename ppf file =
location might be invalid; in which case we do not print it.
*)
let print_loc ~capitalize_first ppf loc =
setup_colors ();
setup_tags ();
let file_valid = function
| "_none_" ->
(* This is a dummy placeholder, but we print it anyway to please editors
Expand Down Expand Up @@ -627,7 +628,25 @@ let lines_around
loop ();
List.rev !lines

(* Try to get lines from a lexbuf *)
(* Get lines from a file *)
let lines_around_from_file
~(start_pos: position) ~(end_pos: position)
(filename: string):
input_line list
=
try
let cin = open_in_bin filename in
let read_char () =
try Some (input_char cin) with End_of_file -> None
in
let lines =
lines_around ~start_pos ~end_pos ~seek:(seek_in cin) ~read_char
in
close_in cin;
lines
with Sys_error _ -> []

(* Attempt to get lines from the lexing buffer. *)
let lines_around_from_lexbuf
~(start_pos: position) ~(end_pos: position)
(lb: lexbuf):
Expand Down Expand Up @@ -668,61 +687,26 @@ let lines_around_from_phrasebuf
in
lines_around ~start_pos ~end_pos ~seek ~read_char

(* Get lines from a file *)
let lines_around_from_file
~(start_pos: position) ~(end_pos: position)
(filename: string):
input_line list
=
try
let cin = open_in_bin filename in
let read_char () =
try Some (input_char cin) with End_of_file -> None
in
let lines =
lines_around ~start_pos ~end_pos ~seek:(seek_in cin) ~read_char
in
close_in cin;
lines
with Sys_error _ -> []

(* A [get_lines] function for [highlight_quote] that reads from the current
input.

It first tries to read from [!input_lexbuf], then if that fails (because the
lexbuf no longer contains the input we want), it reads from [!input_name]
directly *)
input. *)
let lines_around_from_current_input ~start_pos ~end_pos =
(* Be a bit defensive, and do not try to open one of the possible
[!input_name] values that we know do not denote valid filenames. *)
let file_valid = function
| "//toplevel//" | "_none_" | "" -> false
| _ -> true
in
let from_file () =
if file_valid !input_name then
lines_around_from_file !input_name ~start_pos ~end_pos
else
[]
in
match !input_lexbuf, !input_phrase_buffer, !input_name with
| _, Some pb, "//toplevel//" ->
begin match lines_around_from_phrasebuf pb ~start_pos ~end_pos with
| [] -> (* Could not read the input from the phrase buffer. This is likely
a sign that we were given a buggy location. *)
[]
| lines ->
lines
end
lines_around_from_phrasebuf pb ~start_pos ~end_pos
| Some lb, _, _ ->
begin match lines_around_from_lexbuf lb ~start_pos ~end_pos with
| [] -> (* The input is likely not in the lexbuf anymore *)
from_file ()
| lines ->
lines
end
| None, _, _ ->
from_file ()
lines_around_from_lexbuf lb ~start_pos ~end_pos
| None, _, filename ->
(* A situation where we have no input buffer and no phrase buffer
is when the compiler is getting the binary AST directly as input. *)
(* Be a bit defensive, and do not try to open one of the possible
[!input_name] values that we know do not denote valid filenames. *)
let file_valid = match filename with
| "//toplevel//" | "_none_" | "" -> false
| _ -> true
in
if file_valid
then lines_around_from_file filename ~start_pos ~end_pos
else []

(******************************************************************************)
(* Reporting errors and warnings *)
Expand Down Expand Up @@ -823,7 +807,7 @@ let batch_mode_printer : report_printer =
in
let pp_txt ppf txt = Format.fprintf ppf "@[%t@]" txt in
let pp self ppf report =
setup_colors ();
setup_tags ();
separate_new_message ppf;
(* Make sure we keep [num_loc_lines] updated.
The tabulation box is here to give submessage the option
Expand Down Expand Up @@ -877,7 +861,7 @@ let batch_mode_printer : report_printer =

let terminfo_toplevel_printer (lb: lexbuf): report_printer =
let pp self ppf err =
setup_colors ();
setup_tags ();
(* Highlight all toplevel locations of the report, instead of displaying
the main location. Do it now instead of in [pp_main_loc], to avoid
messing with Format boxes. *)
Expand Down Expand Up @@ -998,25 +982,36 @@ let alert ?(def = none) ?(use = none) ~kind loc message =
let deprecated ?def ?use loc message =
alert ?def ?use ~kind:"deprecated" loc message

module Style = Misc.Style

let auto_include_alert lib =
let message = Printf.sprintf "\
OCaml's lib directory layout changed in 5.0. The %s subdirectory has been \
automatically added to the search path, but you should add -I +%s to the \
command-line to silence this alert (e.g. by adding %s to the list of \
libraries in your dune file, or adding use_%s to your _tags file for \
ocamlbuild, or using -package %s for ocamlfind)." lib lib lib lib lib in
let message = Format.asprintf "\
OCaml's lib directory layout changed in 5.0. The %a subdirectory has been \
automatically added to the search path, but you should add %a to the \
command-line to silence this alert (e.g. by adding %a to the list of \
libraries in your dune file, or adding %a to your %a file for \
ocamlbuild, or using %a for ocamlfind)."
Style.inline_code lib
Style.inline_code ("-I +" ^lib)
Style.inline_code lib
Style.inline_code ("use_"^lib)
Style.inline_code "_tags"
Style.inline_code ("-package " ^ lib) in
let alert =
{Warnings.kind="ocaml_deprecated_auto_include"; use=none; def=none;
message = Format.asprintf "@[@\n%a@]" Format.pp_print_text message}
in
prerr_alert none alert

let deprecated_script_alert program =
let message = Printf.sprintf "\
Running %s where the first argument is an implicit basename with no \
extension (e.g. %s script-file) is deprecated. Either rename the script \
(%s script-file.ml) or qualify the basename (%s ./script-file)"
program program program program
let message = Format.asprintf "\
Running %a where the first argument is an implicit basename with no \
extension (e.g. %a) is deprecated. Either rename the script \
(%a) or qualify the basename (%a)"
Style.inline_code program
Style.inline_code (program ^ " script-file")
Style.inline_code (program ^ " script-file.ml")
Style.inline_code (program ^ " ./script-file")
in
let alert =
{Warnings.kind="ocaml_deprecated_cli"; use=none; def=none;
Expand Down Expand Up @@ -1077,3 +1072,7 @@ let () =

let raise_errorf ?(loc = none) ?(sub = []) =
Format.kdprintf (fun txt -> raise (Error (mkerror loc sub txt)))

let todo_overwrite_not_implemented ?(kind = "") t =
alert ~kind t "Overwrite not implemented.";
assert false
4 changes: 4 additions & 0 deletions vendor/ocaml-common/location.mli
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ type 'a loc = {
val mknoloc : 'a -> 'a loc
val mkloc : 'a -> t -> 'a loc
val get_txt : 'a loc -> 'a
val get_loc : 'a loc -> t
val map : ('a -> 'b) -> 'a loc -> 'b loc
val compare_txt : ('a -> 'b -> 'c) -> 'a loc -> 'b loc -> 'c

Expand Down Expand Up @@ -347,3 +348,6 @@ val raise_errorf: ?loc:t -> ?sub:msg list ->

val report_exception: formatter -> exn -> unit
(** Reraise the exception if it is unknown. *)

(** CR uniqueness: remove this once overwriting is fully implemented *)
val todo_overwrite_not_implemented : ?kind:string -> t -> 'a
11 changes: 10 additions & 1 deletion vendor/ocaml-common/syntaxerr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@

(* Auxiliary type for reporting syntax errors *)

type invalid_package_type =
| Parameterized_types
| Constrained_types
| Private_types
| Not_with_type
| Neither_identifier_nor_with_type

type error =
Unclosed of Location.t * string * Location.t * string
| Expecting of Location.t * string
Expand All @@ -23,9 +30,10 @@ type error =
| Variable_in_scope of Location.t * string
| Other of Location.t
| Ill_formed_ast of Location.t * string
| Invalid_package_type of Location.t * string
| Invalid_package_type of Location.t * invalid_package_type
| Removed_string_set of Location.t
| Missing_unboxed_literal_suffix of Location.t
| Malformed_instance_identifier of Location.t

exception Error of error
exception Escape_error
Expand All @@ -41,6 +49,7 @@ let location_of_error = function
| Expecting (l, _)
| Removed_string_set l -> l
| Missing_unboxed_literal_suffix l -> l
| Malformed_instance_identifier l -> l


let ill_formed_ast loc s =
Expand Down
10 changes: 9 additions & 1 deletion vendor/ocaml-common/syntaxerr.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@

*)

type invalid_package_type =
| Parameterized_types
| Constrained_types
| Private_types
| Not_with_type
| Neither_identifier_nor_with_type

type error =
Unclosed of Location.t * string * Location.t * string
| Expecting of Location.t * string
Expand All @@ -28,9 +35,10 @@ type error =
| Variable_in_scope of Location.t * string
| Other of Location.t
| Ill_formed_ast of Location.t * string
| Invalid_package_type of Location.t * string
| Invalid_package_type of Location.t * invalid_package_type
| Removed_string_set of Location.t
| Missing_unboxed_literal_suffix of Location.t
| Malformed_instance_identifier of Location.t

exception Error of error
exception Escape_error
Expand Down
Loading
Loading