Class: Accessibility::Keyboard::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/accessibility/keyboard/parser.rb

Overview

Parse arbitrary strings into a stream of keyboard events

This class will take a string and break it up into chunks for the keyboard event generator. The structure generated here is an array that contains strings and recursively other arrays of strings and arrays of strings.

Examples:


Parser.new("Hai").parse          # => ['H','a','i']
Parser.new("\\CONTROL").parse    # => [["\\CONTROL"]]
Parser.new("\\COMMAND+a").parse  # => [["\\COMMAND", ['a']]]
Parser.new("One\nTwo").parse     # => ['O','n','e',"\n",'T','w','o']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Parser

Returns a new instance of Parser.

Parameters:

  • string (String, #to_s)


26
27
28
29
# File 'lib/accessibility/keyboard/parser.rb', line 26

def initialize string
  @chars  = string.to_s
  @tokens = []
end

Instance Attribute Details

#tokensArray<String,Array<String,...>]

Once a string is parsed, this contains the tokenized structure.

Returns:

  • (Array<String,Array<String,...>])

    Array]



23
24
25
# File 'lib/accessibility/keyboard/parser.rb', line 23

def tokens
  @tokens
end

Instance Method Details

#parseArray<String,Array<String,...>]

Tokenize the string that the parser was initialized with and return the sequence of tokens that were parsed.

Returns:

  • (Array<String,Array<String,...>])

    Array]



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/accessibility/keyboard/parser.rb', line 36

def parse
  length = @chars.length
  @index = 0
  while @index < length
    @tokens << if custom?
                 parse_custom
               else
                 parse_char
               end
    @index += 1
  end
  @tokens
end