Class: Landescape::Converter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/landescape/converter.rb

Direct Known Subclasses

VT100

Constant Summary collapse

CODES =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens, options = {}) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
# File 'lib/landescape/converter.rb', line 15

def initialize(tokens, options = {})
  @options = {non_block: false}.merge(options)
  @result  = Queue.new
  @tokens  = tokens
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



13
14
15
# File 'lib/landescape/converter.rb', line 13

def result
  @result
end

#tokensObject (readonly)

Returns the value of attribute tokens.



13
14
15
# File 'lib/landescape/converter.rb', line 13

def tokens
  @tokens
end

Class Method Details

.start(tokens, options = {}) ⇒ Object



8
9
10
# File 'lib/landescape/converter.rb', line 8

def start(tokens, options = {})
  new(tokens, options).tap(&:start)
end

Instance Method Details

#startObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/landescape/converter.rb', line 21

def start
  @convert_thread = Thread.fork {
    loop do
      token =
        begin
          tokens.shift(@options[:non_block])
        rescue ThreadError
          Thread.exit
        end

      _, detected = self.class::CODES.detect {|pattern, _| pattern === token }
      match       = Regexp.last_match
      statement   =
        if detected
          name, args_proc = detected.values_at(:name, :args)
          args            = args_proc ? args_proc.call(match.captures) : nil

          Landescape.logger.debug %([statement] #{name}(#{Array(args).join(', ')}))
          [name, *args]
        else
          Landescape.logger.debug %([statement] print(#{token.inspect}))
          [:print, *token]
        end

      result.push statement
    end
  }
end

#stopObject



50
51
52
# File 'lib/landescape/converter.rb', line 50

def stop
  @convert_thread.exit if @convert_thread.alive?
end