Skip to content

Commit 6aa9d71

Browse files
committed
Dump structure
1 parent c4bdd03 commit 6aa9d71

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

analysis/src/CmtViewer.ml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
let loc_to_string (loc : Warnings.loc) : string =
2-
Format.sprintf "(%03d,%03d--%03d,%03d)" loc.loc_start.pos_lnum
2+
Format.sprintf "(%02d,%02d--%02d,%02d)" loc.loc_start.pos_lnum
33
(loc.loc_start.pos_cnum - loc.loc_start.pos_bol)
44
loc.loc_end.pos_lnum
55
(loc.loc_end.pos_cnum - loc.loc_end.pos_bol)
@@ -47,6 +47,9 @@ let dump ?filter rescript_json cmt_path =
4747
| Some (Cursor (line, col)) ->
4848
Printf.printf "Filtering by cursor %d,%d\n" line col
4949
| Some (Loc loc) -> Printf.printf "Filtering by loc %s\n" (Loc.toString loc));
50+
51+
Printf.printf "file moduleName: %s\n\n" full.file.moduleName;
52+
5053
let stamps =
5154
full.file.stamps |> getEntries
5255
|> List.filter (fun (_, stamp) -> applyFilter (locOfKind stamp))
@@ -78,6 +81,34 @@ let dump ?filter rescript_json cmt_path =
7881
Printf.printf "%d kconstructor %s\n" stamp
7982
(loc_to_string t.extentLoc));
8083

84+
(* dump the structure *)
85+
let rec dump_structure indent (structure : Module.structure) =
86+
if indent > 0 then Printf.printf "%s" (String.make indent ' ');
87+
Printf.printf "Structure %s:\n" structure.name;
88+
structure.items |> List.iter (dump_structure_item (indent + 2))
89+
and dump_structure_item indent item =
90+
if indent > 0 then Printf.printf "%s" (String.make indent ' ');
91+
let open Module in
92+
match item.kind with
93+
| Value _typedExpr ->
94+
Printf.printf "Value %s %s\n" item.name (loc_to_string item.loc)
95+
| Type _ ->
96+
Printf.printf "Type %s %s\n" item.name (loc_to_string item.loc)
97+
| Module {type_ = m} ->
98+
Printf.printf "Module %s %s\n" item.name (loc_to_string item.loc);
99+
dump_module indent m
100+
and dump_module indent (module_ : Module.t) =
101+
match module_ with
102+
| Ident path -> Printf.printf "Module (Ident) %s\n" (Path.to_string path)
103+
| Structure structure -> dump_structure indent structure
104+
| Constraint (m1, m2) ->
105+
dump_module indent m1;
106+
dump_module indent m2
107+
in
108+
109+
print_newline ();
110+
dump_structure 0 full.file.structure;
111+
81112
(* Dump all locItems (typed nodes) *)
82113
let locItems =
83114
match full.extra with

compiler/ml/path.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,8 @@ let is_constructor_typath p =
104104
match constructor_typath p with
105105
| Regular _ -> false
106106
| _ -> true
107+
108+
let rec to_string = function
109+
| Pident id -> Ident.name id
110+
| Pdot (p, s, _) -> to_string p ^ "." ^ s
111+
| Papply (p1, p2) -> to_string p1 ^ "(" ^ to_string p2 ^ ")"

compiler/ml/path.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ type typath =
4242

4343
val constructor_typath : t -> typath
4444
val is_constructor_typath : t -> bool
45+
val to_string : t -> string

0 commit comments

Comments
 (0)