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("\\CONTROL").lex    # => [["\\CONTROL"]]
Lexer.new("\\COMMAND+a").lex  # => [["\\COMMAND", ['a']]]
Lexer.new("One\nTwo").lex     # => ['O','n','e',"\n",'T','w','o']

Constant Summary

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Lexer) initialize(string)

A new instance of Lexer

Parameters:

  • string (#to_s)


59
60
61
62
# File 'lib/accessibility/string.rb', line 59

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

Instance Attribute Details

- (Array<String,Array<String,...>]) tokens

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

Returns:



56
57
58
# File 'lib/accessibility/string.rb', line 56

def tokens
  @tokens
end

Instance Method Details

- (Array<String,Array<String,...>]) lex

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

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/accessibility/string.rb', line 69

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