Class: TokenCounter

Inherits:
Object
  • Object
show all
Includes:
RubyToken
Defined in:
lib/saikuro.rb

Overview

Counts the number of tokens in each line.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTokenCounter

Returns a new instance of TokenCounter.



55
56
57
58
59
# File 'lib/saikuro.rb', line 55

def initialize
  @files = Hash.new
  @tokens_per_line = Hash.new(0)
  @current_file = ""
end

Instance Attribute Details

#current_fileObject (readonly)

Returns the value of attribute current_file.



53
54
55
# File 'lib/saikuro.rb', line 53

def current_file
  @current_file
end

Instance Method Details

#count_token(line_no, token) ⇒ Object

Count the token for the passed line.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/saikuro.rb', line 82

def count_token(line_no,token)
  case token
  when TkSPACE, TkNL, TkRD_COMMENT
    # Do not count these as tokens
  when TkCOMMENT
    # Ignore this only for comments in a statement?
    # Ignore TkCOLON,TkCOLON2  and operators? like "." etc..
  when TkRBRACK, TkRPAREN, TkRBRACE
    # Ignore the closing of an array/index/hash/paren
    # The opening is counted, but no more.
    # Thus [], () {} is counted as 1 token not 2.
  else
    # may want to filter out comments...
    @tokens_per_line[line_no] += 1
  end
end

#list_tokens_per_line(formater) ⇒ Object

Iterate through all tracked files, passing the the provided formater the token counts.



70
71
72
73
74
75
76
77
78
79
# File 'lib/saikuro.rb', line 70

def list_tokens_per_line(formater)
  formater.start_count(@files.size)
  @files.each do |fname, tok_per_line|
    formater.start_file(fname)
    tok_per_line.sort.each do |line,num|
	formater.line_token_count(line,num)
    end
    formater.end_file
  end
end

#set_current_file(file) ⇒ Object

Mark file to associate with the token count.



62
63
64
65
66
# File 'lib/saikuro.rb', line 62

def set_current_file(file)
  @current_file = file
  @tokens_per_line = Hash.new(0)
  @files[@current_file] = @tokens_per_line
end