1 // Copyright Brian Schott (Hackerpilot) 2012. 2 // Distributed under the Boost Software License, Version 1.0. 3 // (See accompanying file LICENSE_1_0.txt or copy at 4 // http://www.boost.org/LICENSE_1_0.txt) 5 6 module stats; 7 8 import std.stdio; 9 import std.algorithm; 10 import dparse.lexer; 11 12 pure nothrow bool isLineOfCode(IdType t) 13 { 14 switch (t) 15 { 16 case tok!";": 17 case tok!"while": 18 case tok!"if": 19 case tok!"do": 20 case tok!"else": 21 case tok!"switch": 22 case tok!"for": 23 case tok!"foreach": 24 case tok!"foreach_reverse": 25 case tok!"default": 26 case tok!"case": 27 return true; 28 default: 29 return false; 30 } 31 } 32 33 ulong printTokenCount(Tokens)(File output, string fileName, ref Tokens tokens) 34 { 35 ulong c; 36 foreach (ref t; tokens) 37 { 38 c++; 39 } 40 output.writefln("%s:\t%d", fileName, c); 41 return c; 42 } 43 44 ulong printLineCount(Tokens)(File output, string fileName, ref Tokens tokens) 45 { 46 ulong count; 47 foreach (ref t; tokens) 48 { 49 if (isLineOfCode(t.type)) 50 ++count; 51 } 52 output.writefln("%s:\t%d", fileName, count); 53 return count; 54 } 55