Repository navigation
Expand file tree
/
Copy pathshape.ml
More file actions
125 lines (114 loc) · 5.25 KB
/
Copy pathshape.ml
File metadata and controls
125 lines (114 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
(* shape.ml -- report where the forms in a .sl file open and close.
A dev tool, not part of the language. It diagnoses nothing: it states facts
and leaves the reading to you, so it has no notion of a "correct" file and
can never be wrong about one. It reads Reader.files's tokens, so
comments and string literals are skipped the way the checker skips them,
and a file the lexer cannot finish is reported as far as it got. Forms
are identified by line:col so that two opening on the same line stay
distinct, and each file a `;! file` pragma starts is reported on its
own, its positions prefixed with its name.
Usage: shape [--closers] <file.sl>
(default) every form spanning more than one line
--closers what each run of ) actually closes *)
open Sleight
open Reader
let tok_text = function LParen -> "(" | RParen -> ")" | Dollar -> "$" | Item r -> Reader.show r | Invalid _ -> "?" | Pragma _ -> ";!"
type form = { at : loc; depth : int; text : string; mutable closes : loc }
let place path ?(mark = "") ?(col = true) = function
| At { file; line; col = c } -> (if file = path then "" else file ^ ":") ^ mark ^ Z.to_string line ^ if col then ":" ^ Z.to_string c else ""
| NoLoc | Unknown | Framed _ -> ""
let () =
let closers = ref false and path = ref "" in
Array.iteri
(fun i a ->
if i > 0 then
match a with
| "--closers" -> closers := true
| _ -> path := a)
Sys.argv;
if !path = "" then (prerr_endline "usage: shape [--closers] <file.sl>"; exit 2);
let src = match Program.read_file !path with
| s -> s
| exception Sys_error m -> prerr_endline m; exit 2
in
let report (f : file) =
let place = place !path in
let toks = Array.of_list f.toks in
(* the first two tokens after a `(`, which is enough to recognise a form:
"let mklets", "fun inner", "raw-cons (" *)
let label k =
let at j = if j < Array.length toks then (let _, _, t = toks.(j) in tok_text t) else "" in
let a = at (k + 1) and b = at (k + 2) in
if b = "" then a else a ^ " " ^ b
in
(* one pass: every ( is pushed with where it opened; every ) pops and
records the line that closed it. A form left open keeps closes = 0. *)
let forms = ref [] and stack = ref [] and depth = ref 0 in
Array.iteri
(fun k ((l : Reader.loc), _, t) ->
match t with
| LParen ->
let f = { at = l; depth = !depth; text = label k; closes = nowhere } in
forms := f :: !forms;
stack := f :: !stack;
incr depth
| RParen ->
decr depth;
(match !stack with
| f :: rest -> f.closes <- l; stack := rest
| [] -> ())
| _ -> ())
toks;
let forms = List.rev !forms in
if !closers then (
(* Group consecutive )s: what a ))))) run actually closes, in order. A
second scan rather than a reuse of `forms`, because the first pass
keeps each form's closing LINE -- all the default report needs -- and
that discards exactly what a run is: which )s were ADJACENT. That
adjacency lives only in the byte offsets, so it is read back from them. *)
let runs = ref [] and pending = ref [] and last = ref (-1) in
let run_line = ref nowhere and stack = ref [] in
Array.iteri
(fun k ((l : Reader.loc), off, t) ->
match t with
| LParen -> stack := (l, label k) :: !stack
| RParen ->
(match !stack with
| o :: rest ->
if !last <> off - 1 && !pending <> [] then (
runs := (!run_line, List.rev !pending) :: !runs; pending := []);
if !pending = [] then run_line := l;
pending := o :: !pending;
last := off;
stack := rest
| [] -> ())
| _ -> ())
toks;
if !pending <> [] then runs := (!run_line, List.rev !pending) :: !runs;
print_endline "closers";
List.iter
(fun (ln, os) ->
if List.length os > 1 then
Printf.printf " %-5s %2d closers -> %s\n" (place ~mark:"L" ~col:false ln) (List.length os)
(String.concat " " (List.map (fun (l, t) -> Printf.sprintf "%s@%s" t (place l)) os)))
(List.rev !runs))
else (
print_endline "depth opens form closes";
List.iter
(fun f ->
if f.closes = nowhere || fst (coords f.closes) <> fst (coords f.at) then
Printf.printf "%5d %-9s %-25s %s\n" f.depth (place f.at) f.text
(if f.closes = nowhere then "NEVER" else place ~mark:"L" ~col:false f.closes))
forms);
Printf.printf "%sfinal depth %d%s\n" (if f.name = !path then "" else f.name ^ ": ") !depth
(if !depth > 0 then " (unclosed forms: append that many )"
else if !depth < 0 then " (surplus ))"
else "");
in
let files = files !path src in
List.iter report (match List.filter (fun (f : file) -> f.toks <> []) files with [] -> [ List.hd files ] | fs -> fs);
match (List.nth files (List.length files - 1)).stopped with
| None -> ()
| Some (l, m) ->
Printf.eprintf "%s: %s -- everything above is what was read before that\n" (Reader.where !path l) m;
exit 2