Module: Autotype::TypeString
- Defined in:
- lib/autotype/type_string.rb
Class Method Summary collapse
Class Method Details
.parse(string) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/autotype/type_string.rb', line 7 def parse(string) string = string.to_s.strip raise ArgumentError, "type string cannot be empty" if string.empty? if string.include?("|") members = string.split("|").map { parse(_1.strip) } return members.first if members.length == 1 return Union.new(members.uniq) end if (match = string.match(/\A([A-Za-z_][\w:]*)\[(.*)\]\z/m)) name = match[1] args = split_args(match[2]).map { parse(_1) } return Generic.new(name, args) end Named.new(string) end |
.split_args(source) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/autotype/type_string.rb', line 27 def split_args(source) args = [] depth = 0 start = 0 source.each_char.with_index do |char, index| case char when "[" depth += 1 when "]" depth -= 1 when "," if depth.zero? args << source[start...index] start = index + 1 end end end args << source[start..] args.map(&:strip).reject(&:empty?) end |