Class: AssertType::TypeStringParser

Inherits:
Object
  • Object
show all
Defined in:
lib/assert_type/type_string_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_string) ⇒ TypeStringParser

Returns a new instance of TypeStringParser.



12
13
14
# File 'lib/assert_type/type_string_parser.rb', line 12

def initialize type_string
  @type_string = type_string
end

Class Method Details

.parse(type_string) ⇒ Object

Parameters:

  • type_string (String)

    like Array<Fixnum>



8
9
10
# File 'lib/assert_type/type_string_parser.rb', line 8

def self.parse type_string
  new(type_string).parse
end

Instance Method Details

#parseObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/assert_type/type_string_parser.rb', line 16

def parse
  root = TypeNode.root
  previous_word = root
  current_words = [root]
  tokens.each do |token|
    case token.name
      when :open_angle
        current_words.push previous_word
      when :close_angle
        current_words.pop
      when :word
        current_words.last.children << (previous_word = TypeNode.new(token.value))
      when :comma
        # ignore commas
    end
  end
  root
end

#tokensObject



35
36
37
# File 'lib/assert_type/type_string_parser.rb', line 35

def tokens
  @tokens ||= TypeStringTokeniser.tokenise @type_string
end