Class: ParseState
- Inherits:
-
Object
- Object
- ParseState
- Includes:
- RubyToken
- Defined in:
- lib/saikuro/parse_state.rb
Overview
Main class and structure used to compute the cyclomatic complexity of Ruby programs.
Direct Known Subclasses
Constant Summary collapse
- @@top_state =
nil- @@token_counter =
TokenCounter.new
Instance Attribute Summary collapse
-
#children ⇒ Object
Returns the value of attribute children.
-
#complexity ⇒ Object
Returns the value of attribute complexity.
-
#lines ⇒ Object
Returns the value of attribute lines.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
Class Method Summary collapse
Instance Method Summary collapse
- #calc_complexity ⇒ Object
- #calc_lines ⇒ Object
- #compute_state(formater) ⇒ Object
- #compute_state_for_global(formater) ⇒ Object
-
#count_tokens? ⇒ Boolean
Count the tokens parsed if true else ignore them.
- #do_begin_token(token) ⇒ Object
- #do_block_token(token) ⇒ Object
- #do_case_token(token) ⇒ Object
- #do_class_token(token) ⇒ Object
- #do_comment_token(token) ⇒ Object
- #do_conditional_do_control_token(token) ⇒ Object
- #do_conditional_token(token) ⇒ Object
- #do_constant_token(token) ⇒ Object
- #do_def_token(token) ⇒ Object
- #do_else_token(token) ⇒ Object
- #do_end_token(token) ⇒ Object
- #do_identifier_token(token) ⇒ Object
- #do_module_token(token) ⇒ Object
- #do_one_line_conditional_token(token) ⇒ Object
- #do_right_brace_token(token) ⇒ Object
- #do_symbol_token(token) ⇒ Object
- #end_debug ⇒ Object
-
#initialize(lexer, parent = nil) ⇒ ParseState
constructor
A new instance of ParseState.
- #lexer=(lexer) ⇒ Object
-
#lexer_loop?(token) ⇒ Boolean
Ruby-Lexer can go into a loop if the file does not end with a newline.
- #make_state(type, parent = nil) ⇒ Object
- #parse ⇒ Object
- #parse_token(token) ⇒ Object
- #top_state? ⇒ Boolean
Constructor Details
#initialize(lexer, parent = nil) ⇒ ParseState
Returns a new instance of ParseState.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/saikuro/parse_state.rb', line 22 def initialize(lexer,parent=nil) @name = "" @children = Array.new @complexity = 0 @parent = parent @lexer = lexer @run = true # To catch one line def statements, We always have one line. @lines = 0 @last_token_line_and_char = Array.new end |
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
5 6 7 |
# File 'lib/saikuro/parse_state.rb', line 5 def children @children end |
#complexity ⇒ Object
Returns the value of attribute complexity.
5 6 7 |
# File 'lib/saikuro/parse_state.rb', line 5 def complexity @complexity end |
#lines ⇒ Object
Returns the value of attribute lines.
5 6 7 |
# File 'lib/saikuro/parse_state.rb', line 5 def lines @lines end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/saikuro/parse_state.rb', line 5 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
5 6 7 |
# File 'lib/saikuro/parse_state.rb', line 5 def parent @parent end |
Class Method Details
.get_token_counter ⇒ Object
18 19 20 |
# File 'lib/saikuro/parse_state.rb', line 18 def ParseState.get_token_counter @@token_counter end |
.make_top_state ⇒ Object
8 9 10 11 12 |
# File 'lib/saikuro/parse_state.rb', line 8 def ParseState.make_top_state() @@top_state = ParseState.new(nil) @@top_state.name = "__top__" @@top_state end |
.set_token_counter(counter) ⇒ Object
15 16 17 |
# File 'lib/saikuro/parse_state.rb', line 15 def ParseState.set_token_counter(counter) @@token_counter = counter end |
Instance Method Details
#calc_complexity ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/saikuro/parse_state.rb', line 49 def calc_complexity complexity = @complexity children.each do |child| complexity += child.calc_complexity end complexity end |
#calc_lines ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/saikuro/parse_state.rb', line 57 def calc_lines lines = @lines children.each do |child| lines += child.calc_lines end lines end |
#compute_state(formater) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/saikuro/parse_state.rb', line 65 def compute_state(formater) if top_state? compute_state_for_global(formater) end @children.each do |s| s.compute_state(formater) end end |
#compute_state_for_global(formater) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/saikuro/parse_state.rb', line 75 def compute_state_for_global(formater) global_def, @children = @children.partition do |s| !s.kind_of?(ParseClass) end return if global_def.empty? gx = global_def.inject(0) { |c,s| s.calc_complexity } gl = global_def.inject(0) { |c,s| s.calc_lines } formater.start_class_compute_state("Global", "", gx, gl) global_def.each do |s| s.compute_state(formater) end formater.end_class_compute_state("") end |
#count_tokens? ⇒ Boolean
Count the tokens parsed if true else ignore them.
90 91 92 |
# File 'lib/saikuro/parse_state.rb', line 90 def count_tokens? true end |
#do_begin_token(token) ⇒ Object
135 136 137 |
# File 'lib/saikuro/parse_state.rb', line 135 def do_begin_token(token) make_state(EndableParseState, self) end |
#do_block_token(token) ⇒ Object
172 173 174 |
# File 'lib/saikuro/parse_state.rb', line 172 def do_block_token(token) make_state(ParseBlock,self) end |
#do_case_token(token) ⇒ Object
184 185 186 |
# File 'lib/saikuro/parse_state.rb', line 184 def do_case_token(token) make_state(EndableParseState, self) end |
#do_class_token(token) ⇒ Object
139 140 141 |
# File 'lib/saikuro/parse_state.rb', line 139 def do_class_token(token) make_state(ParseClass,self) end |
#do_comment_token(token) ⇒ Object
204 205 206 |
# File 'lib/saikuro/parse_state.rb', line 204 def do_comment_token(token) make_state(ParseComment, self) end |
#do_conditional_do_control_token(token) ⇒ Object
180 181 182 |
# File 'lib/saikuro/parse_state.rb', line 180 def do_conditional_do_control_token(token) make_state(ParseDoCond,self) end |
#do_conditional_token(token) ⇒ Object
176 177 178 |
# File 'lib/saikuro/parse_state.rb', line 176 def do_conditional_token(token) make_state(ParseCond,self) end |
#do_constant_token(token) ⇒ Object
151 152 153 |
# File 'lib/saikuro/parse_state.rb', line 151 def do_constant_token(token) nil end |
#do_def_token(token) ⇒ Object
147 148 149 |
# File 'lib/saikuro/parse_state.rb', line 147 def do_def_token(token) make_state(ParseDef,self) end |
#do_else_token(token) ⇒ Object
199 200 201 202 |
# File 'lib/saikuro/parse_state.rb', line 199 def do_else_token(token) STDOUT.puts "Ignored/Unknown Token:#{token.class}" if $VERBOSE nil end |
#do_end_token(token) ⇒ Object
167 168 169 170 |
# File 'lib/saikuro/parse_state.rb', line 167 def do_end_token(token) end_debug nil end |
#do_identifier_token(token) ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/saikuro/parse_state.rb', line 155 def do_identifier_token(token) if (token.name == "__END__" && token.char_no.to_i == 0) # The Ruby code has stopped and the rest is data so cease parsing. @run = false end nil end |
#do_module_token(token) ⇒ Object
143 144 145 |
# File 'lib/saikuro/parse_state.rb', line 143 def do_module_token(token) make_state(ParseModule,self) end |
#do_one_line_conditional_token(token) ⇒ Object
188 189 190 191 192 193 194 195 196 197 |
# File 'lib/saikuro/parse_state.rb', line 188 def do_one_line_conditional_token(token) # This is an if with no end @complexity += 1 #STDOUT.puts "got IF_MOD: #{self.to_yaml}" if $VERBOSE #if state.type != "class" && state.type != "def" && state.type != "cond" #STDOUT.puts "Changing IF_MOD Parent" if $VERBOSE #state = state.parent #@run = false nil end |
#do_right_brace_token(token) ⇒ Object
163 164 165 |
# File 'lib/saikuro/parse_state.rb', line 163 def do_right_brace_token(token) nil end |
#do_symbol_token(token) ⇒ Object
208 209 210 |
# File 'lib/saikuro/parse_state.rb', line 208 def do_symbol_token(token) make_state(ParseSymbol, self) end |
#end_debug ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/saikuro/parse_state.rb', line 272 def end_debug STDOUT.puts "got an end: #{@name} in #{self.class.name}" if $VERBOSE if @parent.nil? STDOUT.puts "DEBUG: Line #{@lexer.line_no}" STDOUT.puts "DEBUG: #{@name}; #{self.class}" # to_yaml can cause an infinite loop? #STDOUT.puts "TOP: #{@@top_state.to_yaml}" #STDOUT.puts "TOP: #{@@top_state.inspect}" # This may not be an error? #exit 1 end end |
#lexer=(lexer) ⇒ Object
38 39 40 41 |
# File 'lib/saikuro/parse_state.rb', line 38 def lexer=(lexer) @run = true @lexer = lexer end |
#lexer_loop?(token) ⇒ Boolean
Ruby-Lexer can go into a loop if the file does not end with a newline.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/saikuro/parse_state.rb', line 112 def lexer_loop?(token) return false if @last_token_line_and_char.empty? loop_flag = false last = @last_token_line_and_char.last line = last[0] char = last[1] ltok = last[2] if ( (line == @lexer.line_no.to_i) && (char == @lexer.char_no.to_i) && (ltok.class == token.class) ) # We are potentially in a loop if @last_token_line_and_char.size >= 3 loop_flag = true end else # Not in a loop so clear stack @last_token_line_and_char = Array.new end loop_flag end |
#make_state(type, parent = nil) ⇒ Object
43 44 45 46 47 |
# File 'lib/saikuro/parse_state.rb', line 43 def make_state(type,parent = nil) cstate = type.new(@lexer,self) parent.children<< cstate cstate end |
#parse ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/saikuro/parse_state.rb', line 94 def parse while @run do tok = @lexer.token @run = false if tok.nil? if lexer_loop?(tok) STDERR.puts "Lexer loop at line : #{@lexer.line_no} char #{@lexer.char_no}." @run = false end @last_token_line_and_char<< [@lexer.line_no.to_i, @lexer.char_no.to_i, tok] if $VERBOSE puts "DEBUG: #{@lexer.line_no} #{tok.class}:#{tok.name if tok.respond_to?(:name)}" end @@token_counter.count_token(@lexer.line_no, tok) if count_tokens? parse_token(tok) end end |
#parse_token(token) ⇒ Object
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/saikuro/parse_state.rb', line 212 def parse_token(token) state = nil case token when TkCLASS state = do_class_token(token) when TkMODULE state = do_module_token(token) when TkDEF state = do_def_token(token) when TkCONSTANT # Nothing to do with a constant at top level? state = do_constant_token(token) when TkIDENTIFIER,TkFID # Nothing to do at top level? state = do_identifier_token(token) when TkRBRACE # Nothing to do at top level state = do_right_brace_token(token) when TkEND state = do_end_token(token) # At top level this might be an error... when TkDO,TkfLBRACE state = do_block_token(token) when TkIF,TkUNLESS state = do_conditional_token(token) when TkWHILE,TkUNTIL,TkFOR state = do_conditional_do_control_token(token) when TkELSIF #,TkELSE @complexity += 1 when TkELSE # Else does not increase complexity when TkCASE state = do_case_token(token) when TkWHEN @complexity += 1 when TkBEGIN state = do_begin_token(token) when TkRESCUE # Maybe this should add complexity and not begin @complexity += 1 when TkIF_MOD, TkUNLESS_MOD, TkUNTIL_MOD, TkWHILE_MOD, TkQUESTION state = do_one_line_conditional_token(token) when TkNL # @lines += 1 when TkRETURN # Early returns do not increase complexity as the condition that # calls the return is the one that increases it. when TkCOMMENT state = do_comment_token(token) when TkSYMBEG state = do_symbol_token(token) when TkError STDOUT.puts "Lexer received an error for line #{@lexer.line_no} char #{@lexer.char_no}" else state = do_else_token(token) end state.parse if state end |
#top_state? ⇒ Boolean
34 35 36 |
# File 'lib/saikuro/parse_state.rb', line 34 def top_state? self == @@top_state end |