Class: RubySimpleParser::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/brain_damage/lib/ruby_simple_parser/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code = '') ⇒ Parser

Returns a new instance of Parser.



14
15
16
17
18
# File 'lib/generators/brain_damage/lib/ruby_simple_parser/parser.rb', line 14

def initialize(code = '')
  @code = code
  @line_classifier = LineClassifier.new
  parse if code
end

Instance Attribute Details

#class_definitionObject (readonly)

Returns the value of attribute class_definition.



12
13
14
# File 'lib/generators/brain_damage/lib/ruby_simple_parser/parser.rb', line 12

def class_definition
  @class_definition
end

#class_method_callsObject

Returns the value of attribute class_method_calls.



11
12
13
# File 'lib/generators/brain_damage/lib/ruby_simple_parser/parser.rb', line 11

def class_method_calls
  @class_method_calls
end

Instance Method Details

#class_method_calls_after(method) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/generators/brain_damage/lib/ruby_simple_parser/parser.rb', line 82

def class_method_calls_after(method)
  if @class_method_calls.has_key? method
    @class_method_calls[method]
  else
    []
  end
end

#parseObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/generators/brain_damage/lib/ruby_simple_parser/parser.rb', line 28

def parse
  prepare_to_parse

  @code_lines.each_index do |line_number|
    line_code = @code_lines[line_number]
    line_class = @line_classifier.classify line_code
    new_block = nil

    if line_class == CLASS_START
      new_block = RubySimpleParser::ClassDefinition.new line_code, @context
      @context = new_block
      @class_definition = new_block
      # puts "Entered context: #{@context.name}"

    elsif line_class == CODE_WITH_BLOCK
      new_block = RubySimpleParser::Block.new line_code, @context
      @context = new_block

    elsif line_class == METHOD_START
      new_block = RubySimpleParser::Method.new line_code, @method_scope, @context
      @context = new_block
      @methods[@method_scope][new_block.name] = new_block
      @last_method = new_block.name
      # puts "Entered context: #{@context.name}"

    elsif line_class == PRIVATE_BLOCK
      @method_scope = :private
      RubySimpleParser::CodeLine.new line_code, @context

    else
      new_block = RubySimpleParser::CodeLine.new line_code, @context

      if line_class == BLOCK_END
        if @context.parent
          # puts "Left context: #{@context.name} (line_class: #{line_class})"
          @context = @context.parent
        end
      end
    end

    if new_block and (![EMPTY, BLOCK_END].include? line_class) and !new_block.is_a? ClassDefinition and
      (( line_class == CODE_WITH_BLOCK and @context.parent.is_a? ClassDefinition ) or
       @context.is_a? ClassDefinition )

      if @last_method
        @class_method_calls[@last_method] ||= []
        @class_method_calls[@last_method] << new_block
      else
        @class_method_calls[:after_class_definition] << new_block
      end
    end
  end
end


90
91
92
# File 'lib/generators/brain_damage/lib/ruby_simple_parser/parser.rb', line 90

def print
  @global_context.print
end

#private_methodsObject



24
25
26
# File 'lib/generators/brain_damage/lib/ruby_simple_parser/parser.rb', line 24

def private_methods
  @methods[:private]
end

#public_methodsObject



20
21
22
# File 'lib/generators/brain_damage/lib/ruby_simple_parser/parser.rb', line 20

def public_methods
  @methods[:public]
end