Class: Ath::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/ath/scanner.rb

Instance Method Summary collapse

Constructor Details

#initialize(shell:) ⇒ Scanner

Returns a new instance of Scanner.



2
3
4
5
# File 'lib/ath/scanner.rb', line 2

def initialize(shell:)
  @shell = shell
  @buf = ''
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/ath/scanner.rb', line 42

def empty?
  @buf.empty?
end

#scan(line) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ath/scanner.rb', line 7

def scan(line)
  ss = StringScanner.new(line)

  if self.empty? and (tok = ss.scan %r{/\w+(?:\s+.*)?\z})
    cmd, arg = tok.strip.split(/\s+/, 2)
    cmd.slice!(0)
    arg.strip! if arg
    yield(Ath::Command.new(shell: @shell, command: cmd, arg: arg))
  end

  until ss.eos?
    @buf << ' '

    if (tok = ss.scan %r{[^'"`;&]+})
      @buf << tok
    elsif (tok = ss.scan /`(?:``|[^`])*`/)
      @buf << tok
    elsif (tok = ss.scan /'(?:''|[^'])*'/)
      @buf << tok
    elsif (tok = ss.scan /"(?:""|[^"])*"/)
      @buf << tok
    elsif (tok = ss.scan /(?:[;&])/)
      query = @buf.strip
      @buf.clear
      raise Ath::Error, 'No query specified' if query.empty?
      yield(Ath::Query.new(shell: @shell, query: query, detach: (tok == '&')))
    else
      @buf.clear
      raise Ath::Error, 'You have an error in your HiveQL syntax'
    end
  end

  @buf.strip!
end