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.



102
103
104
105
106
# File 'lib/saikuro.rb', line 102

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.



100
101
102
# File 'lib/saikuro.rb', line 100

def current_file
  @current_file
end

Instance Method Details

#count_token(line_no, token) ⇒ Object

Count the token for the passed line.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/saikuro.rb', line 129

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.



117
118
119
120
121
122
123
124
125
126
# File 'lib/saikuro.rb', line 117

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.



109
110
111
112
113
# File 'lib/saikuro.rb', line 109

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