module SyntaxTree::Reflection::Type
def parse(comment)
def parse(comment) comment = comment.gsub(/\n/, " ") unless comment.start_with?("[") raise "Comment does not start with a bracket: #{comment.inspect}" end count = 1 found = comment.chars[1..] .find .with_index(1) do |char, index| count += { "[" => 1, "]" => -1 }.fetch(char, 0) break index if count == 0 end # If we weren't able to find the end of the balanced brackets, then # the comment is malformed. if found.nil? raise "Comment does not have balanced brackets: #{comment.inspect}" end parse_type(comment[1...found].strip) end
def parse_type(value)
def parse_type(value) case value when "Integer" Integer when "String" String when "Symbol" Symbol when "boolean" UnionType.new([TrueClass, FalseClass]) when "nil" NilClass when ":\"::\"" :"::" when ":call" :call when ":nil" :nil when /\AArray\[(.+)\]\z/ ArrayType.new(parse_type($1.strip)) when /\A\[(.+)\]\z/ TupleType.new($1.strip.split(/\s*,\s*/).map { parse_type(_1) }) else if value.include?("|") UnionType.new(value.split(/\s*\|\s*/).map { parse_type(_1) }) else CONSTANTS.fetch(value.to_sym) end end end