Class: TokenCounter

Inherits:
Object
  • Object
show all
Includes:
RubyToken
Defined in:
lib/saikuro/token_counter.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.



7
8
9
10
11
# File 'lib/saikuro/token_counter.rb', line 7

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.



5
6
7
# File 'lib/saikuro/token_counter.rb', line 5

def current_file
  @current_file
end

Instance Method Details

#count_token(line_no, token) ⇒ Object

Count the token for the passed line.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/saikuro/token_counter.rb', line 34

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.



22
23
24
25
26
27
28
29
30
31
# File 'lib/saikuro/token_counter.rb', line 22

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.



14
15
16
17
18
# File 'lib/saikuro/token_counter.rb', line 14

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