Class: ScripTTY::Term::DG410::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/scriptty/term/dg410/parser.rb

Constant Summary collapse

SERVER_PARSER_DEFINITION =
File.read(File.join(File.dirname(__FILE__), "dg410-escapes.txt"))
CLIENT_PARSER_DEFINITION =
File.read(File.join(File.dirname(__FILE__), "dg410-client-escapes.txt"))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.



31
32
33
34
35
36
37
38
# File 'lib/scriptty/term/dg410/parser.rb', line 31

def initialize(options={})
  @fsm = Util::FSM.new(
    :definition => options[:client] ? CLIENT_PARSER_DEFINITION : SERVER_PARSER_DEFINITION,
    :callback => self, :callback_method => :handle_event)
  @callback = options[:callback]
  @callback_method = options[:callback_method] || :call
  on_unknown_sequence :error
end

Instance Attribute Details

#fsmObject (readonly)

ScripTTY::Util::FSM object used by this parser. (Used for debugging.)



29
30
31
# File 'lib/scriptty/term/dg410/parser.rb', line 29

def fsm
  @fsm
end

Instance Method Details

#feed_byte(byte) ⇒ Object

Feed the specified byte to the terminal. Returns a string of bytes that should be transmitted (e.g. for TELNET negotiation).

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/scriptty/term/dg410/parser.rb', line 70

def feed_byte(byte)
  raise ArgumentError.new("input should be single byte") unless byte.is_a?(String) and byte.length == 1
  begin
    @fsm.process(byte)
  rescue Util::FSM::NoMatch => e
    @fsm.reset!
    if @on_unknown_sequence == :error
      raise
    elsif @on_unknown_sequence == :ignore
      # do nothing
    elsif !@on_unknown_sequence.is_a?(Symbol)   # @on_unknown_sequence is a Proc
      @on_unknown_sequence.call(e.input_sequence.join)
    else
      raise "BUG"
    end
  end
  ""
end

#feed_bytes(bytes) ⇒ Object

Convenience method: Feeds several bytes to the terminal. Returns a string of bytes that should be transmitted (e.g. for TELNET negotiation).



92
93
94
95
96
97
98
# File 'lib/scriptty/term/dg410/parser.rb', line 92

def feed_bytes(bytes)
  retvals = []
  bytes.split(//n).each do |byte|
    retvals << feed_byte(byte)
  end
  retvals.join
end

#on_unknown_sequence(mode = nil, &block) ⇒ Object

Set the behaviour of the terminal when an unknown escape sequence is found.

This method takes either a symbol or a block.

When a block is given, it is executed whenever an unknown escape sequence is received. The block is passed the escape sequence as a single string.

When a symbol is given, it may be one of the following:

:error

(default) Raise a ScripTTY::Util::FSM::NoMatch exception.

:ignore

Ignore the unknown escape sequence.



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/scriptty/term/dg410/parser.rb', line 54

def on_unknown_sequence(mode=nil, &block)
  if !block and !mode
    raise ArgumentError.new("No mode specified and no block given")
  elsif block and mode
    raise ArgumentError.new("Block and mode are mutually exclusive, but both were given")
  elsif block
    @on_unknown_sequence = block
  elsif [:error, :ignore].include?(mode)
    @on_unknown_sequence = mode
  else
    raise ArgumentError.new("Invalid mode #{mode.inspect}")
  end
end