-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment-stats.pl
More file actions
executable file
·82 lines (69 loc) · 2.44 KB
/
comment-stats.pl
File metadata and controls
executable file
·82 lines (69 loc) · 2.44 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
#!/usr/bin/env perl
# Calculates stats on code. (works on perl, bash, sh, R, C/C++, Java)
# Namely, how many comment and code lines are contained in a file
# Understands documentation blocks from:
# POD (Perl), JavaDoc (Java), bvmutils (R), doxygen (C/C++)
our $all; # every line in the file(s)
our $blank; # whitespace lines
our $doc; # parsable documentation
our $comment; # programmer comment
our $nonblank; # non-blank lines, i.e. code or comments or documentation
our $noncomment; # code, the meat
my $nest;
# Single-line comments, beginning with // or # or ;
# my $comment_char = shift || "(\/\/|#|;)";
my $comment_char = "(\/\/|#|;)";
# TODO
# Type of comments checked should depend on file type (check file magic)
# Add fortran comments (line begins with 'c')
while (<>) {
$all++;
if (/^\s*$/) { $blank++; next; }
$nonblank++;
if (/^\s*${comment_char}/) { $comment++; next; }
# PDOC (Perl) doc?
comment_block('^=', '^=cut', \$doc);
# bvmutils (R) doc?
comment_block('^\s*\.doc', '^\"', \$doc);
# JavaDoc?
comment_block('^\s*\/\*\*', '\*\/', \$doc);
# Doxygen?
comment_block('^\s*\/\*\!', '\*\/', \$doc);
# C++/Java block comment? (counted as comment, not documentation)
comment_block('^\s*\/\*', '\*\/', \$comment);
# Otherwise it's meat
$noncomment++;
}
die("No files found\n") unless $all;
printf
"Lines: %d\n" .
" Blank:\t %6d / %6d (%5.2f\%)\n" .
" Non-blank:\t %6d / %6d (%5.2f\%)\n" .
" Docs:\t %6d / %6d (%5.2f\%)\n" .
" Comments:\t %6d / %6d (%5.2f\%)\n" .
" Code:\t %6d / %6d (%5.2f\%)\n",
$all,
$blank, $all, 100 * $blank / $all,
$nonblank, $all, 100 * $nonblank / $all,
$doc, $nonblank, 100 * $doc / $nonblank,
$comment, $nonblank, 100 * $comment / $nonblank,
$noncomment, $nonblank, 100 * $noncomment / $nonblank,
;
sub comment_block {
my ($start, $end, $counter) = @_;
our ($all, $blank, $nonblank);
if (/${start}/) {
${$counter}++; # for the starting line
# Don't continue if the multi-line comment also ends on the current line
return if /${end}/;
# Otherwise read the rest of the comment block
while ($nest = <>) {
$all++;
if ($nest =~ /^\s*$/) { $blank++; next; }
$nonblank++;
if ($nest =~ /${end}/) { ${$counter}++; last; }
${$counter}++;
}
next;
}
} # comment_block