class Groonga::Command::Parser::Error

Attributes

location[R]
reason[R]

Public Class Methods

new(reason, before, after) click to toggle source
Calls superclass method
# File lib/groonga/command/parser/error.rb, line 25
def initialize(reason, before, after)
  @reason = reason
  @location = compute_location(before, after)
  super("#{@reason}:\n#{@location}")
end

Private Instance Methods

compute_location(before, after) click to toggle source
# File lib/groonga/command/parser/error.rb, line 32
def compute_location(before, after)
  location = ""
  if before[-1] == ?\n
    location << before
    location << after
    location << "^"
  elsif after[0] == ?\n
    location << before
    location << "\n"
    location << " " * before.bytesize + "^"
    location << after
  else
    before_lines = before.lines.to_a
    last_before_line = before_lines.last
    if last_before_line
      error_offset = last_before_line.bytesize
    else
      error_offset = 0
    end
    before_lines.each do |before_line|
      location << before_line
    end

    unless after.empty?
      after_lines = after.lines.to_a
      location << after_lines.first
      location << " " * error_offset + "^\n"
      after_lines[1..-1].each do |after_line|
        location << after_line
      end
    end
  end
  location
end