class ActiveLdap::Ldif::Scanner

def [](*args)

def [](*args)
  @sub_scanner[*args]
end

def check(regexp)

def check(regexp)
  @sub_scanner = next_segment if @sub_scanner.eos?
  @sub_scanner.check(regexp)
end

def check_separator

def check_separator
  return @scanner.check(SEPARATOR) if @sub_scanner.eos?
  check(SEPARATOR)
end

def column

def column
  _consumed_source = consumed_source
  return 1 if _consumed_source.empty?
  position - (_consumed_source.rindex("\n") || -1)
end

def consumed_source

def consumed_source
  @source[0, position]
end

def eos?

def eos?
  @sub_scanner = next_segment if @sub_scanner.eos?
  @sub_scanner.eos? and @scanner.eos?
end

def initialize(source)

def initialize(source)
  @source = source
  @scanner = StringScanner.new(@source)
  @sub_scanner = nil
  @sub_scanner = next_segment || StringScanner.new("")
end

def line

def line
  _consumed_source = consumed_source
  return 1 if _consumed_source.empty?
  n = _consumed_source.lines.size
  n += 1 if _consumed_source[-1, 1] == "\n"
  n
end

def next_segment

def next_segment
  loop do
    segment = @scanner.scan(/.+(?:#{SEPARATOR} .*)*#{SEPARATOR}?/)
    return @sub_scanner if segment.nil?
    next if segment[0, 1] == "#"
    return StringScanner.new(segment.gsub(/\r?\n /, ''))
  end
end

def position

def position
  sub_scanner_string = @sub_scanner.string
  if sub_scanner_string.respond_to?(:bytesize)
    sub_scanner_string_size = sub_scanner_string.bytesize
  else
    sub_scanner_string_size = sub_scanner_string.size
  end
  @scanner.pos - (sub_scanner_string_size - @sub_scanner.pos)
end

def scan(regexp)

def scan(regexp)
  @sub_scanner = next_segment if @sub_scanner.eos?
  @sub_scanner.scan(regexp)
end

def scan_separator

def scan_separator
  return @scanner.scan(SEPARATOR) if @sub_scanner.eos?
  scan(SEPARATOR)
end

def scan_separators

def scan_separators
  return @scanner.scan(SEPARATORS) if @sub_scanner.eos?
  sub_result = scan(SEPARATORS)
  return nil if sub_result.nil?
  return sub_result unless @sub_scanner.eos?
  result = @scanner.scan(SEPARATORS)
  return sub_result if result.nil?
  sub_result + result
end