class SetBuilderAdapter::Ag::Parser

Attributes

current_item[RW]
input[RW]
parsed_items[RW]

Public Class Methods

new(input) click to toggle source
# File lib/set_builder_adapter/ag.rb, line 11
def initialize(input)
  self.input = input
  self.parsed_items = []
end

Public Instance Methods

add(list_key, value) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
parse() click to toggle source
# 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) click to toggle source
# 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_item() click to toggle source
# File lib/set_builder_adapter/ag.rb, line 33
def record_existing_item
  if current_item
    parsed_items << current_item
  end
end