Class: Steep::AST::Node::TypeApplication

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/ast/node/type_application.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location) ⇒ TypeApplication

Returns a new instance of TypeApplication.



7
8
9
# File 'lib/steep/ast/node/type_application.rb', line 7

def initialize(location)
  @location = location
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



5
6
7
# File 'lib/steep/ast/node/type_application.rb', line 5

def location
  @location
end

Class Method Details

.parse(location) ⇒ Object



86
87
88
89
90
# File 'lib/steep/ast/node/type_application.rb', line 86

def self.parse(location)
  if location.source =~/\A\$\s*(.+)/
    TypeApplication.new(location)
  end
end

Instance Method Details

#each_rbs_type(context, subtyping, type_vars) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/steep/ast/node/type_application.rb', line 59

def each_rbs_type(context, subtyping, type_vars)
  resolver = RBS::Resolver::TypeNameResolver.build(subtyping.factory.env)

  loc = type_location

  while true
    type = RBS::Parser.parse_type(loc.buffer, range: loc.range, variables: type_vars) or break
    type_loc = type.location or raise
    type = type.map_type_name {|name| resolver.resolve(name, context: context) || name.absolute! }

    yield type

    match = RBS::Location.new(loc.buffer, type_loc.end_pos, type_location.end_pos).source.match(/\A\s*,\s*/) or break
    offset = match.length
    loc = RBS::Location.new(loc.buffer, type_loc.end_pos + offset, type_location.end_pos)
  end
end

#lineObject



19
20
21
# File 'lib/steep/ast/node/type_application.rb', line 19

def line
  location.start_line
end

#nodeObject



11
12
13
# File 'lib/steep/ast/node/type_application.rb', line 11

def node
  @node || raise
end

#set_node(node) ⇒ Object



15
16
17
# File 'lib/steep/ast/node/type_application.rb', line 15

def set_node(node)
  @node = node
end

#sourceObject



23
24
25
# File 'lib/steep/ast/node/type_application.rb', line 23

def source
  location.source
end

#type_locationObject



81
82
83
84
# File 'lib/steep/ast/node/type_application.rb', line 81

def type_location
  offset = source.size - type_str.size
  RBS::Location.new(location.buffer, location.start_pos + offset, location.end_pos)
end

#type_strObject



77
78
79
# File 'lib/steep/ast/node/type_application.rb', line 77

def type_str
  @type_str ||= source.delete_prefix("$").lstrip
end

#types(context, subtyping, type_vars) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/steep/ast/node/type_application.rb', line 27

def types(context, subtyping, type_vars)
  # @type var types: Array[LocatedValue[Types::t]]
  types = []

  each_rbs_type(context, subtyping, type_vars) do |rbs_ty|
    validator = Signature::Validator.new(checker: subtyping)
    validator.rescue_validation_errors do
      validator.validate_type(rbs_ty)
    end

    if validator.has_error?
      return validator.each_error
    end

    ty = subtyping.factory.type(rbs_ty)
    types << LocatedValue.new(value: ty, location: rbs_ty.location || raise)
  end

  types
rescue ::RBS::ParsingError => exn
  exn
end

#types?(context, subtyping, type_vars) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
57
# File 'lib/steep/ast/node/type_application.rb', line 50

def types?(context, subtyping, type_vars)
  case types = types(context, subtyping, type_vars)
  when RBS::ParsingError, Enumerator
    nil
  else
    types
  end
end