Class: Codetree::ParseTree

Inherits:
Object
  • Object
show all
Defined in:
lib/codetree.rb

Constant Summary collapse

NESTING_NODES =
[:module, :class ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files) ⇒ ParseTree

Returns a new instance of ParseTree.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/codetree.rb', line 55

def initialize(files)
  @lines = Array::new
  files.each do |file|
    require "./#{file}"
    @lines << File::readlines(file)        
  end

  @lines.flatten!
  @lines = @lines.collect(&:strip!)
  @lines.delete_if {|line| line =~ /^\s*#/}
  @code = @lines.join("\n")
  @ast = RubyParser.new.process @code, "code"
  @operators = {} # positions of interest
  @operators_index = Hash::new
  @curr_position = []
  map_operators!
  generate_tree!
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



52
53
54
# File 'lib/codetree.rb', line 52

def ast
  @ast
end

#linesObject (readonly)

Returns the value of attribute lines.



53
54
55
# File 'lib/codetree.rb', line 53

def lines
  @lines
end

#operatorsObject (readonly)

Returns the value of attribute operators.



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

def operators
  @operators
end

Instance Method Details

#format_operator(name, _options = {:detail => :full}) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/codetree.rb', line 76

def format_operator(name, _options = {:detail =>  :full})
  res = ""
  _options[:detail] ||= :full
  scope = { 
    :private => {   :full => "Private ",   :medium => '-', :light => ''},
    :public => {    :full => "Public ",    :medium => "+", :light => ''}, 
    :protected => { :full => "Protected ", :medium => "#", :light => ''},  
    :none => {      :full => "",           :medium => " ",  :light => ''} }
  type = { 
    :defn => {    :full => "Instance method ", :medium => '(m):', :light => ''},
    :defs => {    :full => "Class method ",    :medium => "(m) ",  :light => ''}, 
    :class => {   :full => "Class ",           :medium => "(C) ",  :light => ''},  
    :module => {  :full => "Module ",          :medium => "(M) ",  :light => ''} }
  if @operators.include?(name) then
    operator = @operators[name]
    operator.ancestors.each do |ancestor|
      res += @operators[ancestor].render
    end
    res = scope[operator.scope][_options[:detail]] + type[operator.type][_options[:detail]] + res +  operator.render
  end
  return res
end


100
101
102
103
104
105
# File 'lib/codetree.rb', line 100

def print_tree(_options = { :detail => :medium, :flat =>  false, :quiet => false})
  _options[:detail] ||= :medium
  _options[:flat] ||= false
  _options[:quiet] ||= false
 print_subtree(@root,0, _options )
end