C++ Line Counting in Ruby

#
# $Id: cpplinecount.html,v 1.2 2002/11/19 00:49:37 erngui Exp $
#
# CppLineCount by ErnestoGuisado (erngui@acm.org)
# Count Lines of code in C++ sources.
# Doesn't handle complicated stuff like keywords appearing inside string literals, etc.
#
keywords = %w(case do else enum for if public private protected
  class struct union switch while try catch)

# Humphrey prefers to count '};' too.
#punct = /(;|,|\{|\};*)/

# this regexp catches statements and declarations. 
# The comma catches 'int x,y;' type declarations.
punct = /(;|,)/

lines = 0
empty = 0
comments = 0
prepro = 0
kwd = 0
statements = 0
while line = gets()
  lines += 1
  if line =~ /^$/
    empty += 1
    next
  end
  if line =~ /^\s*\/\//
    comments += 1
    next
  end
  if line =~ /^#/
    prepro += 1
    next
  end
  line.scan(/\w+/) do |word|
    kwd += 1 if keywords.include?(word)
  end
  line.scan(punct) do |p|
    statements += 1
  end
end 

puts "Lines:               #{lines}"
puts "Blank lines:         #{empty}"
puts "Comments(C++ style): #{comments}"
puts "Preprocessor:        #{prepro}"
puts "Keywords:            #{kwd}"
puts "Statements:          #{statements}"
sloc = statements + kwd + prepro # omit blanks and comments
puts "SLOC:                #{sloc}"