Mercurial > projects > dil
annotate src/cmd/Statistics.d @ 806:bcb74c9b895c
Moved out files in the trunk folder to the root.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Sun, 09 Mar 2008 00:12:19 +0100 |
parents | trunk/src/cmd/Statistics.d@cf2ad5df025c |
children | 5c29f1ebec9f |
rev | line source |
---|---|
363 | 1 /++ |
2 Author: Aziz Köksal | |
3 License: GPL3 | |
4 +/ | |
5 module cmd.Statistics; | |
643
3bd947eb72c2
Added module cmd.ASTStats.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
596
diff
changeset
|
6 |
764
4579e8505d5e
Fixed unittests and removed dil.File.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
755
diff
changeset
|
7 import cmd.ASTStats; |
576
0df647660e76
Moved Lexer.d to new package 'lexer'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
554
diff
changeset
|
8 import dil.lexer.Lexer; |
596
39fac5531b85
Moved dil.Token to dil.lexer.Token.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
576
diff
changeset
|
9 import dil.lexer.Token; |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
10 import dil.parser.Parser; |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
11 import dil.ast.NodesEnum; |
755
90668b83ae5e
Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
679
diff
changeset
|
12 import dil.SourceText; |
391
33b566df6af4
Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
364
diff
changeset
|
13 import common; |
363 | 14 |
786
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
764
diff
changeset
|
15 /// A group of statistics variables. |
363 | 16 struct Statistics |
17 { | |
534
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
18 uint whitespaceCount; /// Counter for whitespace characters. |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
19 uint wsTokenCount; /// Counter for all whitespace tokens. |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
20 uint keywordCount; /// Counter for keywords. |
786
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
764
diff
changeset
|
21 uint identCount; /// Counter for identifiers. |
534
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
22 uint numberCount; /// Counter for number literals. |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
23 uint commentCount; /// Counter for comments. |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
24 uint tokenCount; /// Counter for all tokens produced by the Lexer. |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
25 uint linesOfCode; /// Number of lines. |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
26 uint[] tokensTable; /// Table of counters for all token kinds. |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
27 uint[] nodesTable; /// Table of counters for all node kinds. |
545
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
28 |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
29 static Statistics opCall(bool allocateTokensTable, bool allocateNodesTable = false) |
545
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
30 { |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
31 Statistics s; |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
32 if (allocateTokensTable) |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
33 s.tokensTable = new uint[TOK.MAX]; |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
34 if (allocateNodesTable) |
797
cf2ad5df025c
Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
786
diff
changeset
|
35 s.nodesTable = new uint[g_classNames.length]; |
545
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
36 return s; |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
37 } |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
38 |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
39 void opAddAssign(Statistics s) |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
40 { |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
41 this.whitespaceCount += s.whitespaceCount; |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
42 this.wsTokenCount += s.wsTokenCount; |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
43 this.keywordCount += s.keywordCount; |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
44 this.identCount += s.identCount; |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
45 this.numberCount += s.numberCount; |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
46 this.commentCount += s.commentCount; |
534
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
47 this.tokenCount += s.tokenCount; |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
48 this.linesOfCode += s.linesOfCode; |
545
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
49 foreach (i, count; s.tokensTable) |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
50 this.tokensTable[i] += count; |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
51 foreach (i, count; s.nodesTable) |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
52 this.nodesTable[i] += count; |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
53 } |
363 | 54 } |
55 | |
786
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
764
diff
changeset
|
56 /// Executes the statistics command. |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
57 void execute(string[] filePaths, bool printTokensTable, bool printNodesTable) |
363 | 58 { |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
59 Statistics[] stats; |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
60 foreach (filePath; filePaths) |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
61 stats ~= getStatistics(filePath, printTokensTable, printNodesTable); |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
62 |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
63 auto total = Statistics(printTokensTable, printNodesTable); |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
64 |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
65 foreach (i, ref stat; stats) |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
66 { |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
67 total += stat; |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
68 Stdout.formatln( |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
69 "----\n" |
534
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
70 "File: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
71 "Whitespace character count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
72 "Whitespace token count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
73 "Keyword count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
74 "Identifier count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
75 "Number count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
76 "Comment count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
77 "All tokens count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
78 "Lines of code: {}", |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
79 filePaths[i], |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
80 stat.whitespaceCount, |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
81 stat.wsTokenCount, |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
82 stat.keywordCount, |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
83 stat.identCount, |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
84 stat.numberCount, |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
85 stat.commentCount, |
534
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
86 stat.tokenCount, |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
87 stat.linesOfCode |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
88 ); |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
89 } |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
90 |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
91 if (filePaths.length > 1) |
545
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
92 { |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
93 Stdout.formatln( |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
94 "--------------------------------------------------------------------------------\n" |
534
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
95 "Total of {} files:\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
96 "Whitespace character count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
97 "Whitespace token count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
98 "Keyword count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
99 "Identifier count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
100 "Number count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
101 "Comment count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
102 "All tokens count: {}\n" |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
103 "Lines of code: {}", |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
104 filePaths.length, |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
105 total.whitespaceCount, |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
106 total.wsTokenCount, |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
107 total.keywordCount, |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
108 total.identCount, |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
109 total.numberCount, |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
110 total.commentCount, |
534
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
111 total.tokenCount, |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
112 total.linesOfCode |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
113 ); |
545
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
114 } |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
115 |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
116 if (printTokensTable) |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
117 { |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
118 Stdout("Table of tokens:").newline; |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
119 Stdout.formatln(" {,10} | {}", "Count", "Token kind"); |
545
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
120 Stdout("-----------------------------").newline; |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
121 foreach (i, count; total.tokensTable) |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
122 Stdout.formatln(" {,10} | {}", count, Token.toString(cast(TOK)i)); |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
123 Stdout("// End of tokens table.").newline; |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
124 } |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
125 |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
126 if(printNodesTable) |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
127 { |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
128 Stdout("Table of nodes:").newline; |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
129 Stdout.formatln(" {,10} | {}", "Count", "Node kind"); |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
130 Stdout("-----------------------------").newline; |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
131 foreach (i, count; total.nodesTable) |
797
cf2ad5df025c
Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
786
diff
changeset
|
132 Stdout.formatln(" {,10} | {}", count, g_classNames[i]); |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
133 Stdout("// End of nodes table.").newline; |
545
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
134 } |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
135 } |
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
136 |
786
3b34f6a95a27
Added and revised documenation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
764
diff
changeset
|
137 /// Returns the statistics for a D source file. |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
138 Statistics getStatistics(string filePath, bool printTokensTable, bool printNodesTable) |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
139 { |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
140 // Create a new record. |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
141 auto stats = Statistics(printTokensTable); |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
142 |
755
90668b83ae5e
Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
679
diff
changeset
|
143 auto sourceText = new SourceText(filePath, true); |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
144 Parser parser; |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
145 Lexer lx; |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
146 if (printNodesTable) |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
147 { |
755
90668b83ae5e
Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
679
diff
changeset
|
148 parser = new Parser(sourceText); |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
149 auto rootNode = parser.start(); |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
150 // Count nodes. |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
151 stats.nodesTable = (new ASTStats).count(rootNode); |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
152 lx = parser.lexer; |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
153 } |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
154 else |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
155 { |
755
90668b83ae5e
Introduced new module dil.SourceText and class SourceText.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
679
diff
changeset
|
156 lx = new Lexer(sourceText); |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
157 lx.scanAll(); |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
158 } |
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
159 |
506
1b897a4536a4
Refactored code and fixed a few things in cmd.Statistics.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
485
diff
changeset
|
160 auto token = lx.firstToken(); |
363 | 161 |
672
d422e5f2f3ea
Added '--asttable' option to command 'statistics'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
643
diff
changeset
|
162 // Count tokens. |
534
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
163 // Lexer creates HEAD + Newline, which are not in the source text. |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
164 // No token left behind! |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
165 stats.tokenCount = 2; |
485
ea8c7459f1c4
Changed a lot of things in the Lexer.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
401
diff
changeset
|
166 stats.linesOfCode = lx.lineNum; |
545
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
167 if (printTokensTable) |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
168 { |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
169 stats.tokensTable[TOK.HEAD] = 1; |
554
d6212e3b9f36
Fixed code in cmd.Statistics.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
545
diff
changeset
|
170 stats.tokensTable[TOK.Newline] = 1; |
545
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
171 } |
363 | 172 // Traverse linked list. |
534
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
173 while (1) |
363 | 174 { |
534
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
175 stats.tokenCount += 1; |
545
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
176 |
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
177 if (printTokensTable) |
679
ff6971637f88
Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
672
diff
changeset
|
178 stats.tokensTable[token.kind] += 1; |
545
0bac0bb506ca
Added '--table' option to 'stats' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
534
diff
changeset
|
179 |
363 | 180 // Count whitespace characters |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
181 if (token.ws !is null) |
506
1b897a4536a4
Refactored code and fixed a few things in cmd.Statistics.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
485
diff
changeset
|
182 stats.whitespaceCount += token.start - token.ws; |
363 | 183 |
679
ff6971637f88
Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
672
diff
changeset
|
184 switch (token.kind) |
363 | 185 { |
186 case TOK.Identifier: | |
187 stats.identCount++; | |
188 break; | |
189 case TOK.Comment: | |
190 stats.commentCount++; | |
191 break; | |
192 case TOK.Int32, TOK.Int64, TOK.Uint32, TOK.Uint64, | |
193 TOK.Float32, TOK.Float64, TOK.Float80, | |
194 TOK.Imaginary32, TOK.Imaginary64, TOK.Imaginary80: | |
195 stats.numberCount++; | |
196 break; | |
506
1b897a4536a4
Refactored code and fixed a few things in cmd.Statistics.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
485
diff
changeset
|
197 case TOK.Newline: |
1b897a4536a4
Refactored code and fixed a few things in cmd.Statistics.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
485
diff
changeset
|
198 break; |
363 | 199 default: |
200 if (token.isKeyword) | |
201 stats.keywordCount++; | |
506
1b897a4536a4
Refactored code and fixed a few things in cmd.Statistics.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
485
diff
changeset
|
202 else if (token.isWhitespace) |
1b897a4536a4
Refactored code and fixed a few things in cmd.Statistics.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
485
diff
changeset
|
203 stats.wsTokenCount++; |
363 | 204 } |
534
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
205 |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
206 if (token.next is null) |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
207 break; |
581b3cf3fe8f
Added a new field to statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
506
diff
changeset
|
208 token = token.next; |
363 | 209 } |
679
ff6971637f88
Renamed Token member type to kind.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
672
diff
changeset
|
210 assert(token.kind == TOK.EOF); |
401
fcdf7ac5ad27
Improved statistics generation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
391
diff
changeset
|
211 return stats; |
363 | 212 } |