Module: TypedRb::TypeSignature::TypeProcessor

Included in:
Parser
Defined in:
lib/typed/type_signature/parser.rb

Instance Method Summary collapse

Instance Method Details

#block_arg?(function_tokens) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/typed/type_signature/parser.rb', line 84

def block_arg?(function_tokens)
  function_tokens[-2] == '&'
end

#parse_bindingObject



95
96
97
98
99
# File 'lib/typed/type_signature/parser.rb', line 95

def parse_binding
  new_type = parse_new_type
  @current_function << new_type unless @current_type.empty?
  @current_type = []
end

#parse_block_arg(function_tokens) ⇒ Object



88
89
90
91
92
93
# File 'lib/typed/type_signature/parser.rb', line 88

def parse_block_arg(function_tokens)
  block = { :block => function_tokens.pop,
            :kind  => :block_arg }
  function_tokens.pop
  function_tokens << block
end

#parse_end_of_bindingObject



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/typed/type_signature/parser.rb', line 101

def parse_end_of_binding
  bound = parse_variable_binding
  # method variables
  if bound[:kind] == :type_var && method_info[bound[:type]]
    bound = method_info[bound[:type]].dup
    bound[:sub_kind] = :method_type_var
  end
  @binding = nil
  parent_function = @stack.pop
  parent_function << bound
  @current_function = parent_function
  @current_type = []
end

#parse_end_of_functionObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/typed/type_signature/parser.rb', line 73

def parse_end_of_function
  new_type = parse_new_type
  @current_function << new_type unless @current_type.empty?
  parent_function = @stack.pop
  next_function_elem = transform_function_tokens(@current_function)
  parent_function << next_function_elem
  parse_block_arg(parent_function) if block_arg?(parent_function)
  @current_function = parent_function
  @current_type = []
end

#parse_new_typeObject



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/typed/type_signature/parser.rb', line 145

def parse_new_type
  new_type = @current_type.join
  new_type = :unit if new_type == 'unit'
  if new_type.to_s.end_with?('...')
    new_type = new_type.split('...').first
    new_type = @current_function.pop if new_type.nil?
    new_type =  { :type       => 'Array',
                  :kind       => :rest,
                  :parameters => [new_type] }
  end
  new_type
end

#parse_next_elemObject



136
137
138
139
140
141
142
143
# File 'lib/typed/type_signature/parser.rb', line 136

def parse_next_elem
  return parse_binding if @in_binding

  new_type = parse_new_type
  @current_function << new_type unless @current_type.empty?
  @current_function << :<
  @current_type = []
end

#parse_start_of_typeObject



65
66
67
68
69
70
71
# File 'lib/typed/type_signature/parser.rb', line 65

def parse_start_of_type
  new_type = parse_new_type
  @current_function << new_type unless @current_type.empty?
  @stack << @current_function
  @current_type = []
  @current_function = []
end

#parse_variable_bindingObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/typed/type_signature/parser.rb', line 115

def parse_variable_binding
  new_type = parse_new_type
  @current_function << new_type unless @current_type.empty?
  if @current_function.size == 1
    { :type => @current_function.first,
      :kind => :type_var }
  else
    if @binding.nil?
      # This is the case for nested generic types
      { :type       => @current_function.first,
        :parameters => @current_function.drop(1),
        :kind       => :generic_type }
    else
      { :type    => @current_function.first,
        :bound   => @current_function.last,
        :binding => @binding,
        :kind    => :type_var }
    end
  end
end