Class: GembirdBackend::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/gembird-backend/parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(result) ⇒ Array

Returns stack of parsed result.

Parameters:

  • result (String)

Returns:

  • (Array)

    stack of parsed result



5
6
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
# File 'lib/gembird-backend/parser.rb', line 5

def parse result
  tokens = GembirdBackend::Scanner.new.scan result
  stack = []
  tokens.each do |token, *args|
    case token
      when :begin_device
        stack << {}
        stack.last[:number] = args.first.first
      when :device_type
        stack.last[:type] = args.first.first
      when :device_serial
        stack.last[:serial] = args.first.first
      when :usb_info
        stack.last[:usb_device] = args.first
        stack.last[:usb_bus] = args.last
      when :accessing_device
        stack << {}
        stack.last[:number] = args.first
        stack.last[:usb_device] = args.last
      when :socket_status
        stack.last[:sockets] ||= {}
        stack.last[:sockets][args.first] = args.last
      when :error
        raise GembirdBackend::ExecutionError, args.first
    end
  end
  stack
end