Class: Accessibility::String::Lexer

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

Overview

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

Examples:


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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Lexer

Returns a new instance of Lexer.

Parameters:

  • (#to_s)


51
52
53
54
# File 'lib/accessibility/string.rb', line 51

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

Instance Attribute Details

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

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

Returns:

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

    Array<String,Array<String,…>]



48
49
50
# File 'lib/accessibility/string.rb', line 48

def tokens
  @tokens
end

Instance Method Details

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

Tokenize the string that the lexer was initialized with and return the sequence of tokens that were lexed.

Returns:

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

    Array<String,Array<String,…>]



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/accessibility/string.rb', line 61

def lex
  length = @chars.length
  @index = 0
  while @index < length
    @tokens << if custom?
                 lex_custom
               else
                 lex_char
               end
    @index += 1
  end
  @tokens
end