Class: Steep::AnnotationParser

Inherits:
Object
  • Object
show all
Defined in:
lib/steep/annotation_parser.rb

Defined Under Namespace

Classes: SyntaxError

Constant Summary collapse

VAR_NAME =
/[a-z][A-Za-z0-9_]*/
METHOD_NAME =
Regexp.union(
  /[^.:\s]+/
)
CONST_NAME =
Regexp.union(
  /(::)?([A-Z][A-Za-z0-9_]*::)*[A-Z][A-Za-z0-9_]*/
)
DYNAMIC_NAME =
/(self\??\.)?#{METHOD_NAME}/
IVAR_NAME =
/@[^:\s]+/
TYPE =
/(?<type>.*)/
COLON =
/\s*:\s*/
PARAM =
/[A-Z][A-Za-z0-9_]*/
TYPE_PARAMS =
/(\[(?<params>#{PARAM}(,\s*#{PARAM})*)\])?/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory:) ⇒ AnnotationParser

Returns a new instance of AnnotationParser.



15
16
17
# File 'lib/steep/annotation_parser.rb', line 15

def initialize(factory:)
  @factory = factory
end

Instance Attribute Details

#factoryObject (readonly)

Returns the value of attribute factory.



13
14
15
# File 'lib/steep/annotation_parser.rb', line 13

def factory
  @factory
end

Instance Method Details

#keyword_and_type(keyword) ⇒ Object



76
77
78
# File 'lib/steep/annotation_parser.rb', line 76

def keyword_and_type(keyword)
  /@type\s+#{keyword}#{COLON}#{TYPE}/
end

#keyword_subject_type(keyword, name) ⇒ Object



72
73
74
# File 'lib/steep/annotation_parser.rb', line 72

def keyword_subject_type(keyword, name)
  /@type\s+#{keyword}\s+(?<name>#{name})#{COLON}#{TYPE}/
end

#parse(src, location:) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/steep/annotation_parser.rb', line 80

def parse(src, location:)
  case src
  when keyword_subject_type("var", VAR_NAME)
    Regexp.last_match.yield_self do |match|
      match or raise
      name = match[:name] or raise

      AST::Annotation::VarType.new(name: name.to_sym,
                                   type: parse_type(match, location: location),
                                   location: location)
    end

  when keyword_subject_type("method", METHOD_NAME)
    Regexp.last_match.yield_self do |match|
      match or raise
      name = match[:name] or raise
      type = match[:type] or raise

      method_type = factory.method_type(RBS::Parser.parse_method_type(type) || raise)

      AST::Annotation::MethodType.new(name: name.to_sym,
                                      type: method_type,
                                      location: location)
    end

  when keyword_subject_type("const", CONST_NAME)
    Regexp.last_match.yield_self do |match|
      match or raise
      name = match[:name] or raise
      type = parse_type(match, location: location)

      AST::Annotation::ConstType.new(name: RBS::TypeName.parse(name), type: type, location: location)
    end

  when keyword_subject_type("ivar", IVAR_NAME)
    Regexp.last_match.yield_self do |match|
      match or raise
      name = match[:name] or raise
      type = parse_type(match, location: location)

      AST::Annotation::IvarType.new(name: name.to_sym,
                                     type: type,
                                     location: location)
    end

  when keyword_and_type("return")
    Regexp.last_match.yield_self do |match|
      match or raise
      type = parse_type(match, location: location)
      AST::Annotation::ReturnType.new(type: type, location: location)
    end

  when keyword_and_type("block")
    Regexp.last_match.yield_self do |match|
      match or raise
      type = parse_type(match, location: location)
      AST::Annotation::BlockType.new(type: type, location: location)
    end

  when keyword_and_type("self")
    Regexp.last_match.yield_self do |match|
      match or raise
      type = parse_type(match, location: location)
      AST::Annotation::SelfType.new(type: type, location: location)
    end

  when keyword_and_type("instance")
    Regexp.last_match.yield_self do |match|
      match or raise
      type = parse_type(match, location: location)
      AST::Annotation::InstanceType.new(type: type, location: location)
    end

  when keyword_and_type("module")
    Regexp.last_match.yield_self do |match|
      match or raise
      type = parse_type(match, location: location)
      AST::Annotation::ModuleType.new(type: type, location: location)
    end

  when keyword_and_type("break")
    Regexp.last_match.yield_self do |match|
      match or raise
      type = parse_type(match, location: location)

      AST::Annotation::BreakType.new(type: type, location: location)
    end

  when /@dynamic\s+(?<names>(#{DYNAMIC_NAME}\s*,\s*)*#{DYNAMIC_NAME})/
    Regexp.last_match.yield_self do |match|
      match or raise
      names = (match[:names] || raise).split(/\s*,\s*/)

      AST::Annotation::Dynamic.new(
        names: names.map {|name|
          case
          when name.delete_prefix!("self.")
            AST::Annotation::Dynamic::Name.new(name: name.to_sym, kind: :module)
          when name.delete_prefix!("self?.")
            AST::Annotation::Dynamic::Name.new(name: name.to_sym, kind: :module_instance)
          else
            AST::Annotation::Dynamic::Name.new(name: name.to_sym, kind: :instance)
          end
        },
        location: location
      )
    end

  when /@implements\s+(?<name>#{CONST_NAME})#{TYPE_PARAMS}$/
    Regexp.last_match.yield_self do |match|
      match or raise
      type_name = RBS::TypeName.parse(match[:name] || raise)
      params = match[:params]&.yield_self {|params| params.split(/,/).map {|param| param.strip.to_sym } } || []

      name = AST::Annotation::Implements::Module.new(name: type_name, args: params)
      AST::Annotation::Implements.new(name: name, location: location)
    end
  end

rescue RBS::ParsingError => exn
  raise SyntaxError.new(source: src, location: location, exn: exn)
end

#parse_type(match, name = :type, location:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/steep/annotation_parser.rb', line 47

def parse_type(match, name = :type, location:)
  string = match[name] or raise
  st, en = match.offset(name)
  st or raise
  en or raise
  loc = RBS::Location.new(location.buffer, location.start_pos + st, location.start_pos + en)

  type =
    begin
      RBS::Parser.parse_type(string)
    rescue RBS::ParsingError => exn
      raise SyntaxError.new(source: string, location: loc, exn: exn)
    end

  unless type
    raise SyntaxError.new(source: string, location: loc, message: "Failed to parse a type in annotation")
  end
  
  unless (type.location || raise).source == string.strip
    raise SyntaxError.new(source: string, location: loc, message: "Failed to parse a type in annotation")
  end

  factory.type(type)
end