Mercurial > projects > dil
comparison trunk/src/cmd/Statistics.d @ 534:581b3cf3fe8f
Added a new field to statistics generation.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Mon, 17 Dec 2007 22:16:13 +0100 |
parents | 1b897a4536a4 |
children | 0bac0bb506ca |
comparison
equal
deleted
inserted
replaced
533:2a8d0ed0d71e | 534:581b3cf3fe8f |
---|---|
8 import dil.Lexer; | 8 import dil.Lexer; |
9 import common; | 9 import common; |
10 | 10 |
11 struct Statistics | 11 struct Statistics |
12 { | 12 { |
13 uint whitespaceCount; | 13 uint whitespaceCount; /// Counter for whitespace characters. |
14 uint wsTokenCount; | 14 uint wsTokenCount; /// Counter for all whitespace tokens. |
15 uint keywordCount; | 15 uint keywordCount; /// Counter for keywords. |
16 uint identCount; | 16 uint identCount; /// Counter for identifier. |
17 uint numberCount; | 17 uint numberCount; /// Counter for number literals. |
18 uint commentCount; | 18 uint commentCount; /// Counter for comments. |
19 uint linesOfCode; | 19 uint tokenCount; /// Counter for all tokens produced by the Lexer. |
20 uint linesOfCode; /// Number of lines. | |
20 | 21 |
21 void opAddAssign(Statistics s) | 22 void opAddAssign(Statistics s) |
22 { | 23 { |
23 this.whitespaceCount += s.whitespaceCount; | 24 this.whitespaceCount += s.whitespaceCount; |
24 this.wsTokenCount += s.wsTokenCount; | 25 this.wsTokenCount += s.wsTokenCount; |
25 this.keywordCount += s.keywordCount; | 26 this.keywordCount += s.keywordCount; |
26 this.identCount += s.identCount; | 27 this.identCount += s.identCount; |
27 this.numberCount += s.numberCount; | 28 this.numberCount += s.numberCount; |
28 this.commentCount += s.commentCount; | 29 this.commentCount += s.commentCount; |
30 this.tokenCount += s.tokenCount; | |
29 this.linesOfCode += s.linesOfCode; | 31 this.linesOfCode += s.linesOfCode; |
30 } | 32 } |
31 } | 33 } |
32 | 34 |
33 void execute(string[] filePaths) | 35 void execute(string[] filePaths) |
41 foreach (i, ref stat; stats) | 43 foreach (i, ref stat; stats) |
42 { | 44 { |
43 total += stat; | 45 total += stat; |
44 Stdout.formatln( | 46 Stdout.formatln( |
45 "----\n" | 47 "----\n" |
46 "File: {0}\n" | 48 "File: {}\n" |
47 "Whitespace character count: {1}\n" | 49 "Whitespace character count: {}\n" |
48 "Whitespace token count: {2}\n" | 50 "Whitespace token count: {}\n" |
49 "Keyword count: {3}\n" | 51 "Keyword count: {}\n" |
50 "Identifier count: {4}\n" | 52 "Identifier count: {}\n" |
51 "Number count: {5}\n" | 53 "Number count: {}\n" |
52 "Comment count: {6}\n" | 54 "Comment count: {}\n" |
53 "Lines of code: {7}", | 55 "All tokens count: {}\n" |
56 "Lines of code: {}", | |
54 filePaths[i], | 57 filePaths[i], |
55 stat.whitespaceCount, | 58 stat.whitespaceCount, |
56 stat.wsTokenCount, | 59 stat.wsTokenCount, |
57 stat.keywordCount, | 60 stat.keywordCount, |
58 stat.identCount, | 61 stat.identCount, |
59 stat.numberCount, | 62 stat.numberCount, |
60 stat.commentCount, | 63 stat.commentCount, |
64 stat.tokenCount, | |
61 stat.linesOfCode | 65 stat.linesOfCode |
62 ); | 66 ); |
63 } | 67 } |
64 | 68 |
65 if (filePaths.length > 1) | 69 if (filePaths.length > 1) |
66 Stdout.formatln( | 70 Stdout.formatln( |
67 "--------------------------------------------------------------------------------\n" | 71 "--------------------------------------------------------------------------------\n" |
68 "Total:\n" | 72 "Total of {} files:\n" |
69 "Whitespace character count: {0}\n" | 73 "Whitespace character count: {}\n" |
70 "Whitespace token count: {1}\n" | 74 "Whitespace token count: {}\n" |
71 "Keyword count: {2}\n" | 75 "Keyword count: {}\n" |
72 "Identifier count: {3}\n" | 76 "Identifier count: {}\n" |
73 "Number count: {4}\n" | 77 "Number count: {}\n" |
74 "Comment count: {5}\n" | 78 "Comment count: {}\n" |
75 "Lines of code: {6}", | 79 "All tokens count: {}\n" |
80 "Lines of code: {}", | |
81 filePaths.length, | |
76 total.whitespaceCount, | 82 total.whitespaceCount, |
77 total.wsTokenCount, | 83 total.wsTokenCount, |
78 total.keywordCount, | 84 total.keywordCount, |
79 total.identCount, | 85 total.identCount, |
80 total.numberCount, | 86 total.numberCount, |
81 total.commentCount, | 87 total.commentCount, |
88 total.tokenCount, | |
82 total.linesOfCode | 89 total.linesOfCode |
83 ); | 90 ); |
84 } | 91 } |
85 | 92 |
86 Statistics getStatistics(string filePath) | 93 Statistics getStatistics(string filePath) |
89 auto lx = new Lexer(sourceText, filePath); | 96 auto lx = new Lexer(sourceText, filePath); |
90 lx.scanAll(); | 97 lx.scanAll(); |
91 auto token = lx.firstToken(); | 98 auto token = lx.firstToken(); |
92 | 99 |
93 Statistics stats; | 100 Statistics stats; |
94 | 101 // Lexer creates HEAD + Newline, which are not in the source text. |
102 // No token left behind! | |
103 stats.tokenCount = 2; | |
95 stats.linesOfCode = lx.lineNum; | 104 stats.linesOfCode = lx.lineNum; |
96 // Traverse linked list. | 105 // Traverse linked list. |
97 while (token.type != TOK.EOF) | 106 while (1) |
98 { | 107 { |
99 token = token.next; | 108 stats.tokenCount += 1; |
100 | |
101 // Count whitespace characters | 109 // Count whitespace characters |
102 if (token.ws !is null) | 110 if (token.ws !is null) |
103 stats.whitespaceCount += token.start - token.ws; | 111 stats.whitespaceCount += token.start - token.ws; |
104 | 112 |
105 switch (token.type) | 113 switch (token.type) |
121 if (token.isKeyword) | 129 if (token.isKeyword) |
122 stats.keywordCount++; | 130 stats.keywordCount++; |
123 else if (token.isWhitespace) | 131 else if (token.isWhitespace) |
124 stats.wsTokenCount++; | 132 stats.wsTokenCount++; |
125 } | 133 } |
134 | |
135 if (token.next is null) | |
136 break; | |
137 token = token.next; | |
126 } | 138 } |
139 assert(token.type == TOK.EOF); | |
127 return stats; | 140 return stats; |
128 } | 141 } |