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.



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

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.



51
52
53
# File 'lib/saikuro.rb', line 51

def current_file
  @current_file
end

Instance Method Details

#count_token(line_no, token) ⇒ Object

Count the token for the passed line.



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

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.



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

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.



60
61
62
63
64
# File 'lib/saikuro.rb', line 60

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