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.



57
58
59
60
61
# File 'lib/saikuro.rb', line 57

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.



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

def current_file
  @current_file
end

Instance Method Details

#count_token(line_no, token) ⇒ Object

Count the token for the passed line.



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

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.



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

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.



64
65
66
67
68
# File 'lib/saikuro.rb', line 64

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