class Groonga::Command::Parser::CommandLineSplitter

Public Class Methods

new(command_line) click to toggle source
# File lib/groonga/command/parser/command-line-splitter.rb, line 24
def initialize(command_line)
  @command_line = command_line
end

Public Instance Methods

split() click to toggle source
# File lib/groonga/command/parser/command-line-splitter.rb, line 28
def split
  tokens = []
  scanner = StringScanner.new(@command_line)
  skip_spaces(scanner)
  start_quote = nil
  until scanner.eos?
    if start_quote
      token = ""
      loop do
        chunk = scanner.scan_until(/#{Regexp.escape(start_quote)}/)
        if chunk.nil?
          token = start_quote + token + scanner.rest
          scanner.terminate
          break
        end
        if chunk[-2] == "\\"
          token << chunk
        else
          token << chunk.chomp(start_quote)
          break
        end
      end
      tokens << unescape(token)
      start_quote = nil
      skip_spaces(scanner)
    else
      start_quote = scanner.scan(/['"]/)
      if start_quote.nil?
        tokens << scanner.scan_until(/\S+/)
        skip_spaces(scanner)
      end
    end
  end
  tokens
end

Private Instance Methods

skip_spaces(scanner) click to toggle source
# File lib/groonga/command/parser/command-line-splitter.rb, line 65
def skip_spaces(scanner)
  scanner.scan(/\s+/)
end
unescape(token) click to toggle source
# File lib/groonga/command/parser/command-line-splitter.rb, line 69
def unescape(token)
  token.gsub(/\\(.)/) do
    character = $1
    case character
    when "b"
      "\b"
    when "f"
      "\f"
    when "n"
      "\n"
    when "r"
      "\r"
    when "t"
      "\t"
    else
      character
    end
  end
end