Debian Codegraph @

tools/rocqwc.mll

raw
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (* rocq wc - counts the lines of spec, proof and comments in Rocq sources
12 * Copyright (C) 2003 Jean-Christophe Filliâtre *)
13
14 (*s {\bf rocq wc.} Counts the lines of spec, proof and comments in a Rocq source.
15 It assumes the files to be lexically well-formed. *)
16
17 (*i*){
18 open Printf
19 open Lexing
20 (*i*)
21
22 (*s Command-line options. *)
23
24 let spec_only = ref false
25 let proof_only = ref false
26 let percentage = ref false
27 let skip_header = ref true
28
29 (*s Counters. [clines] counts the number of code lines of the current
30 file, and [dlines] the number of comment lines. [tclines] and [tdlines]
31 are the corresponding totals. *)
32
33 let slines = ref 0
34 let plines = ref 0
35 let dlines = ref 0
36
37 let tslines = ref 0
38 let tplines = ref 0
39 let tdlines = ref 0
40
41 let update_totals () =
42 tslines := !tslines + !slines;
43 tplines := !tplines + !plines;
44 tdlines := !tdlines + !dlines
45
46 (*s The following booleans indicate whether we have seen spec, proof or
47 comment so far on the current line; when a newline is reached, [newline]
48 is called and updates the counters accordingly. *)
49
50 let seen_spec = ref false
51 let seen_proof = ref false
52 let seen_comment = ref false
53
54 let newline () =
55 if !seen_spec then incr slines;
56 if !seen_proof then incr plines;
57 if !seen_comment then incr dlines;
58 seen_spec := false; seen_proof := false; seen_comment := false
59
60 let reset_counters () =
61 seen_spec := false; seen_proof := false; seen_comment := false;
62 slines := 0; plines := 0; dlines := 0
63
64 (*s Print results. *)
65
66 let print_line sl pl dl fo =
67 if not !proof_only then printf " %8d" sl;
68 if not !spec_only then printf " %8d" pl;
69 if not (!proof_only || !spec_only) then printf " %8d" dl;
70 (match fo with Some f -> printf " %s" f | _ -> ());
71 if !percentage then begin
72 let s = sl + pl + dl in
73 let p = if s > 0 then 100 * dl / s else 0 in
74 printf " (%d%%)" p
75 end;
76 print_newline ()
77
78 let print_file fo = print_line !slines !plines !dlines fo
79
80 let print_totals () = print_line !tslines !tplines !tdlines (Some "total")
81
82 (*i*)}(*i*)
83
84 (*s Shortcuts for regular expressions. The [rcs] regular expression
85 is used to skip the CVS infos possibly contained in some comments,
86 in order not to consider it as documentation. *)
87
88 let space = [' ' '\t' '\r']
89 let character =
90 "'" ([^ '\\' '\''] |
91 '\\' (['\\' '\'' 'n' 't' 'b' 'r'] | ['0'-'9'] ['0'-'9'] ['0'-'9'])) "'"
92 let rcs_keyword =
93 "Author" | "Date" | "Header" | "Id" | "Name" | "Locker" | "Log" |
94 "RCSfile" | "Revision" | "Source" | "State"
95 let rcs = "\036" rcs_keyword [^ '$']* "\036"
96 let stars = "(*" '*'* "*)"
97 let dot = '.' (' ' | '\t' | '\n' | '\r' | eof)
98 let proof_start =
99 "Theorem" | "Lemma" | "Fact" | "Remark" | "Goal" | "Correctness" | "Obligation" space+ (['0' - '9'])+ | "Next" space+ "Obligation"
100 let def_start =
101 "Definition" | "Fixpoint" | "Instance"
102 let proof_end =
103 ("Save" | "Qed" | "Defined" | "Abort" | "Admitted") [^'.']* '.'
104
105 (*s [spec] scans the specification. *)
106
107 rule spec = parse
108 | "(*" { comment lexbuf; spec lexbuf }
109 | '"' { let n = string lexbuf in slines := !slines + n;
110 seen_spec := true; spec lexbuf }
111 | '\n' { newline (); spec lexbuf }
112 | space+ | stars
113 { spec lexbuf }
114 | proof_start
115 { seen_spec := true; spec_to_dot lexbuf; proof lexbuf }
116 | def_start
117 { seen_spec := true; definition lexbuf }
118 | character | _
119 { seen_spec := true; spec lexbuf }
120 | eof { () }
121
122 (*s [spec_to_dot] scans a spec until a dot is reached and returns. *)
123
124 and spec_to_dot = parse
125 | "(*" { comment lexbuf; spec_to_dot lexbuf }
126 | '"' { let n = string lexbuf in slines := !slines + n;
127 seen_spec := true; spec_to_dot lexbuf }
128 | '\n' { newline (); spec_to_dot lexbuf }
129 | dot { () }
130 | space+ | stars
131 { spec_to_dot lexbuf }
132 | character | _
133 { seen_spec := true; spec_to_dot lexbuf }
134 | eof { () }
135
136 (*s [definition] scans a definition; passes to [proof] if the body is
137 absent, and to [spec] otherwise *)
138
139 and definition = parse
140 | "(*" { comment lexbuf; definition lexbuf }
141 | '"' { let n = string lexbuf in slines := !slines + n;
142 seen_spec := true; definition lexbuf }
143 | '\n' { newline (); definition lexbuf }
144 | ":=" { seen_spec := true; spec lexbuf }
145 | dot { proof lexbuf }
146 | space+ | stars
147 { definition lexbuf }
148 | character | _
149 { seen_spec := true; definition lexbuf }
150 | eof { () }
151
152 (*s Scans a proof, then returns to [spec]. *)
153
154 and proof = parse
155 | "(*" { comment lexbuf; proof lexbuf }
156 | '"' { let n = string lexbuf in plines := !plines + n;
157 seen_proof := true; proof lexbuf }
158 | space+ | stars
159 { proof lexbuf }
160 | '\n' { newline (); proof lexbuf }
161 | "Proof" space* '.'
162 | "Proof" space+ "with"
163 | "Proof" space+ "using"
164 { seen_proof := true; proof lexbuf }
165 | "Proof" space
166 { proof_term lexbuf }
167 | proof_end
168 { seen_proof := true; spec lexbuf }
169 | character | _
170 { seen_proof := true; proof lexbuf }
171 | eof { () }
172
173 and proof_term = parse
174 | "(*" { comment lexbuf; proof_term lexbuf }
175 | '"' { let n = string lexbuf in plines := !plines + n;
176 seen_proof := true; proof_term lexbuf }
177 | space+ | stars
178 { proof_term lexbuf }
179 | '\n' { newline (); proof_term lexbuf }
180 | dot { spec lexbuf }
181 | character | _
182 { seen_proof := true; proof_term lexbuf }
183 | eof { () }
184
185 (*s Scans a comment. *)
186
187 and comment = parse
188 | "(*" { comment lexbuf; comment lexbuf }
189 | "*)" { () }
190 | '"' { let n = string lexbuf in dlines := !dlines + n;
191 seen_comment := true; comment lexbuf }
192 | '\n' { newline (); comment lexbuf }
193 | space+ | stars
194 { comment lexbuf }
195 | character | _
196 { seen_comment := true; comment lexbuf }
197 | eof { () }
198
199 (*s The entry [string] reads a string until its end and returns the number
200 of newlines it contains. *)
201
202 and string = parse
203 | '"' { 0 }
204 | '\\' ('\\' | 'n' | '"') { string lexbuf }
205 | '\n' { succ (string lexbuf) }
206 | _ { string lexbuf }
207 | eof { 0 }
208
209 (*s The following entry [read_header] is used to skip the possible header at
210 the beginning of files (unless option \texttt{-e} is specified).
211 It stops whenever it encounters an empty line or any character outside
212 a comment. In this last case, it correctly resets the lexer position
213 on that character (decreasing [lex_curr_pos] by 1). *)
214
215 and read_header = parse
216 | "(*" { skip_comment lexbuf; skip_until_nl lexbuf;
217 read_header lexbuf }
218 | "\n" { () }
219 | space+ { read_header lexbuf }
220 | _ { lexbuf.lex_curr_pos <- lexbuf.lex_curr_pos - 1 }
221 | eof { () }
222
223 and skip_comment = parse
224 | "*)" { () }
225 | "(*" { skip_comment lexbuf; skip_comment lexbuf }
226 | _ { skip_comment lexbuf }
227 | eof { () }
228
229 and skip_until_nl = parse
230 | '\n' { () }
231 | _ { skip_until_nl lexbuf }
232 | eof { () }
233
234 (*i*){(*i*)
235
236 (*s Processing files and channels. *)
237
238 let process_channel ch =
239 let lb = Lexing.from_channel ch in
240 reset_counters ();
241 if !skip_header then read_header lb;
242 spec lb
243
244 let process_file f =
245 try
246 let ch = open_in f in
247 process_channel ch;
248 close_in ch;
249 print_file (Some f);
250 update_totals ()
251 with
252 | Sys_error s ->
253 flush stdout; eprintf "rocq wc: %s: %s\n" f s; flush stderr
254
255 (*s Parsing of the command line. *)
256
257 let usage () =
258 prerr_endline "usage: rocq wc [options] [files]";
259 prerr_endline "Options are:";
260 prerr_endline " -p print percentage of comments";
261 prerr_endline " -s print only the spec size";
262 prerr_endline " -r print only the proof size";
263 prerr_endline " -e (everything) do not skip headers";
264 exit 1
265
266 let rec parse = function
267 | [] -> []
268 | ("-h" | "-?" | "-help" | "--help") :: _ -> usage ()
269 | ("-s" | "--spec-only") :: args ->
270 proof_only := false; spec_only := true; parse args
271 | ("-r" | "--proof-only") :: args ->
272 spec_only := false; proof_only := true; parse args
273 | ("-p" | "--percentage") :: args -> percentage := true; parse args
274 | ("-e" | "--header") :: args -> skip_header := false; parse args
275 | f :: args -> f :: (parse args)
276
277 (*s Main program. *)
278
279 let main args =
280 let files = parse args in
281 if not (!spec_only || !proof_only) then
282 printf " spec proof comments\n";
283 match files with
284 | [] -> process_channel stdin; print_file None
285 | [f] -> process_file f
286 | _ -> List.iter process_file files; print_totals ()
287
288 (*i*)}(*i*)