Mercurial > projects > dil
comparison trunk/src/cmd/Statistics.d @ 506:1b897a4536a4
Refactored code and fixed a few things in cmd.Statistics.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Wed, 12 Dec 2007 02:28:37 +0100 |
parents | ea8c7459f1c4 |
children | 581b3cf3fe8f |
comparison
equal
deleted
inserted
replaced
505:3bb94ba21490 | 506:1b897a4536a4 |
---|---|
85 | 85 |
86 Statistics getStatistics(string filePath) | 86 Statistics getStatistics(string filePath) |
87 { | 87 { |
88 auto sourceText = loadFile(filePath); | 88 auto sourceText = loadFile(filePath); |
89 auto lx = new Lexer(sourceText, filePath); | 89 auto lx = new Lexer(sourceText, filePath); |
90 | 90 lx.scanAll(); |
91 auto token = lx.getTokens(); | 91 auto token = lx.firstToken(); |
92 | 92 |
93 Statistics stats; | 93 Statistics stats; |
94 | 94 |
95 stats.linesOfCode = lx.lineNum; | 95 stats.linesOfCode = lx.lineNum; |
96 // Traverse linked list. | 96 // Traverse linked list. |
98 { | 98 { |
99 token = token.next; | 99 token = token.next; |
100 | 100 |
101 // Count whitespace characters | 101 // Count whitespace characters |
102 if (token.ws !is null) | 102 if (token.ws !is null) |
103 stats.whitespaceCount += countWhitespaceCharacters(token.ws, token.start); | 103 stats.whitespaceCount += token.start - token.ws; |
104 | 104 |
105 switch (token.type) | 105 switch (token.type) |
106 { | 106 { |
107 case TOK.Identifier: | 107 case TOK.Identifier: |
108 stats.identCount++; | 108 stats.identCount++; |
113 case TOK.Int32, TOK.Int64, TOK.Uint32, TOK.Uint64, | 113 case TOK.Int32, TOK.Int64, TOK.Uint32, TOK.Uint64, |
114 TOK.Float32, TOK.Float64, TOK.Float80, | 114 TOK.Float32, TOK.Float64, TOK.Float80, |
115 TOK.Imaginary32, TOK.Imaginary64, TOK.Imaginary80: | 115 TOK.Imaginary32, TOK.Imaginary64, TOK.Imaginary80: |
116 stats.numberCount++; | 116 stats.numberCount++; |
117 break; | 117 break; |
118 case TOK.Newline: | |
119 break; | |
118 default: | 120 default: |
119 if (token.isKeyword) | 121 if (token.isKeyword) |
120 stats.keywordCount++; | 122 stats.keywordCount++; |
123 else if (token.isWhitespace) | |
124 stats.wsTokenCount++; | |
121 } | 125 } |
122 | |
123 if (token.isWhitespace) | |
124 stats.wsTokenCount++; | |
125 } | 126 } |
126 return stats; | 127 return stats; |
127 } | 128 } |
128 | |
129 /// Counts newlines, \t, \v, \f and ' ' as whitespace characters. | |
130 uint countWhitespaceCharacters(char* p, char* end) | |
131 { | |
132 uint count; | |
133 Loop: | |
134 while (1) | |
135 { | |
136 switch (*p) | |
137 { | |
138 case '\r': | |
139 if (p[1] == '\n') | |
140 ++p; | |
141 case '\n': | |
142 ++p; | |
143 ++count; | |
144 break; | |
145 case LS[0]: | |
146 if (p[1] == LS[1] && (p[2] == LS[2] || p[2] == PS[2])) | |
147 { | |
148 p += 3; | |
149 ++count; | |
150 break; | |
151 } | |
152 default: | |
153 if (!dil.Lexer.isspace(*p)) | |
154 break Loop; // Exit loop. | |
155 ++p; | |
156 ++count; | |
157 } | |
158 } | |
159 assert(p is end); | |
160 return count; | |
161 } |