Class: SetBuilderAdapter::Ag::Parser

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Parser

Returns a new instance of Parser.



11
12
13
14
# File 'lib/set_builder_adapter/ag.rb', line 11

def initialize(input)
  self.input = input
  self.parsed_items = []
end

Instance Attribute Details

#current_itemObject

Returns the value of attribute current_item.



9
10
11
# File 'lib/set_builder_adapter/ag.rb', line 9

def current_item
  @current_item
end

#inputObject

Returns the value of attribute input.



9
10
11
# File 'lib/set_builder_adapter/ag.rb', line 9

def input
  @input
end

#parsed_itemsObject

Returns the value of attribute parsed_items.



9
10
11
# File 'lib/set_builder_adapter/ag.rb', line 9

def parsed_items
  @parsed_items
end

Instance Method Details

#add(list_key, value) ⇒ Object



28
29
30
31
# File 'lib/set_builder_adapter/ag.rb', line 28

def add(list_key, value)
  self.current_item[list_key] ||= []
  self.current_item[list_key].push value
end

#learn(key, value) ⇒ Object



24
25
26
# File 'lib/set_builder_adapter/ag.rb', line 24

def learn(key, value)
  self.current_item[key] = value
end

#new_item_with_file_path(file_path) ⇒ Object



39
40
41
42
43
# File 'lib/set_builder_adapter/ag.rb', line 39

def new_item_with_file_path(file_path)
  record_existing_item
  self.current_item = { }
  learn :file_path, file_path
end

#parseObject



16
17
18
19
20
21
22
# File 'lib/set_builder_adapter/ag.rb', line 16

def parse
  input.split("\n").each do |line|
    parse_line line
  end
  record_existing_item
  parsed_items
end

#parse_line(line) ⇒ Object



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
81
82
83
84
# File 'lib/set_builder_adapter/ag.rb', line 45

def parse_line(line)
  # A new item is started when the current item has no path, or if the
  # path for the current result line doesn't match the current item's file
  # path.
  if current_item == nil or (line != "--" and line != "" and not line.start_with?(current_item[:file_path]))
    line =~ /^(.*?):\d/
    new_item_with_file_path $1
  end

  # The line can be either a pre or post match for the current item
  if line =~ /^(.*?):(\d+)-(.*)/
    if current_item[:match_line]
      add :post_match_lines, $3
    else
      add :pre_match_lines, $3
    end

  # The line can be the actual match itself
  elsif line =~ /^(.*?):(\d+):(\d+):(.*)/ # match line
    if current_item[:match_line]
      new_item_with_file_path current_item[:file_path]
    end
    learn :row, $2
    learn :column, $3
    learn :match_line, $4

  # Finally, the item can be the inter-file match separator
  elsif line =~ /--/
    new_item_with_file_path current_item[:file_path]

  # Weird exception: a blank line will be ignored.
  elsif line == ""

  # Otherwise big fat fail.
  else
    debug_message "Parse failed for:"
    debug_message line.inspect
    raise ParserError.new("parse_line failed for: #{line.inspect}")
  end
end

#record_existing_itemObject



33
34
35
36
37
# File 'lib/set_builder_adapter/ag.rb', line 33

def record_existing_item
  if current_item
    parsed_items << current_item
  end
end