Class: RBS::Inline::Writer
- Inherits:
-
Object
- Object
- RBS::Inline::Writer
- Defined in:
- lib/rbs/inline/writer.rb
Instance Attribute Summary collapse
-
#output ⇒ Object
readonly
@rbs! interface _Content def <<: (RBS::AST::Declarations::t | RBS::AST::Members::t) -> void.
-
#writer ⇒ Object
readonly
: RBS::Writer.
Class Method Summary collapse
Instance Method Summary collapse
- #header(*lines) ⇒ Object
-
#initialize(buffer = +"")) ⇒ Writer
constructor
A new instance of Writer.
- #translate_class_decl(decl, rbs) ⇒ Object
- #translate_constant_decl(decl, rbs) ⇒ Object
- #translate_data_assign_decl(decl, rbs) ⇒ Object
- #translate_decl(decl, rbs) ⇒ Object
- #translate_member(member, decl, rbs) ⇒ Object
- #translate_members(members, decl, rbs) ⇒ Object
- #translate_module_decl(decl, rbs) ⇒ Object
- #translate_singleton_decl(decl, rbs) ⇒ Object
- #translate_struct_assign_decl(decl, rbs) ⇒ Object
- #write(uses, decls, rbs_decls) ⇒ Object
Constructor Details
#initialize(buffer = +"")) ⇒ Writer
Returns a new instance of Writer.
17 18 19 20 |
# File 'lib/rbs/inline/writer.rb', line 17 def initialize(buffer = +"") #: void @output = buffer @writer = RBS::Writer.new(out: StringIO.new(buffer)) end |
Instance Attribute Details
#output ⇒ Object (readonly)
@rbs!
interface _Content
def <<: (RBS::AST::Declarations::t | RBS::AST::Members::t) -> void
def concat: (Array[RBS::AST::Declarations::t | RBS::AST::Members::t]) -> void
end
13 14 15 |
# File 'lib/rbs/inline/writer.rb', line 13 def output @output end |
#writer ⇒ Object (readonly)
: RBS::Writer
14 15 16 |
# File 'lib/rbs/inline/writer.rb', line 14 def writer @writer end |
Class Method Details
.write(uses, decls, rbs_decls) ⇒ Object
25 26 27 28 29 |
# File 'lib/rbs/inline/writer.rb', line 25 def self.write(uses, decls, rbs_decls) #: void writer = Writer.new() writer.write(uses, decls, rbs_decls) writer.output end |
Instance Method Details
#header(*lines) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/rbs/inline/writer.rb', line 33 def header(*lines) lines.each do |line| writer.out.puts("# " + line) end writer.out.puts end |
#translate_class_decl(decl, rbs) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rbs/inline/writer.rb', line 99 def translate_class_decl(decl, rbs) return unless decl.class_name if decl.comments comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil) end members = [] #: Array[RBS::AST::Members::t | RBS::AST::Declarations::t] translate_members(decl.members, nil, members) rbs << RBS::AST::Declarations::Class.new( name: decl.class_name, type_params: decl.type_params, members: members, super_class: decl.super_class, annotations: [], location: nil, comment: comment ) end |
#translate_constant_decl(decl, rbs) ⇒ Object
174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/rbs/inline/writer.rb', line 174 def translate_constant_decl(decl, rbs) return unless decl.constant_name if decl.comments comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil) end rbs << RBS::AST::Declarations::Constant.new( name: decl.constant_name, type: decl.type, comment: comment, location: nil ) end |
#translate_data_assign_decl(decl, rbs) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/rbs/inline/writer.rb', line 191 def translate_data_assign_decl(decl, rbs) #: void return unless decl.constant_name if decl.comments comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil) end attributes = decl.each_attribute.map do |name, type| RBS::AST::Members::AttrReader.new( name: name, type: type&.type || Types::Bases::Any.new(location: nil), ivar_name: false, comment: nil, kind: :instance, annotations: [], visibility: nil, location: nil ) end new = RBS::AST::Members::MethodDefinition.new( name: :new, kind: :singleton, overloads: [ RBS::AST::Members::MethodDefinition::Overload.new( method_type: RBS::MethodType.new( type_params: [], type: Types::Function.empty(Types::Bases::Instance.new(location: nil)).update( required_positionals: decl.each_attribute.map do |name, attr| RBS::Types::Function::Param.new( type: attr&.type || Types::Bases::Any.new(location: nil), name: name, location: nil ) end ), block: nil, location: nil ), annotations: [] ), RBS::AST::Members::MethodDefinition::Overload.new( method_type: RBS::MethodType.new( type_params: [], type: Types::Function.empty(Types::Bases::Instance.new(location: nil)).update( required_keywords: decl.each_attribute.map do |name, attr| [ name, RBS::Types::Function::Param.new( type: attr&.type || Types::Bases::Any.new(location: nil), name: nil, location: nil ) ] end.to_h ), block: nil, location: nil ), annotations: [] ) ], annotations: [], location: nil, comment: nil, overloading: false, visibility: nil ) rbs << RBS::AST::Declarations::Class.new( name: decl.constant_name, type_params: [], members: [*attributes, new], super_class: RBS::AST::Declarations::Class::Super.new( name: RBS::TypeName.new(name: :Data, namespace: RBS::Namespace.empty), args: [], location: nil ), annotations: decl.class_annotations, location: nil, comment: comment ) end |
#translate_decl(decl, rbs) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rbs/inline/writer.rb', line 72 def translate_decl(decl, rbs) case decl when AST::Declarations::ClassDecl translate_class_decl(decl, rbs) when AST::Declarations::ModuleDecl translate_module_decl(decl, rbs) when AST::Declarations::ConstantDecl translate_constant_decl(decl, rbs) when AST::Declarations::DataAssignDecl translate_data_assign_decl(decl, rbs) when AST::Declarations::StructAssignDecl translate_struct_assign_decl(decl, rbs) when AST::Declarations::BlockDecl if decl.module_class_annotation case decl.module_class_annotation when AST::Annotations::ModuleDecl translate_module_block_decl(decl, rbs) when AST::Annotations::ClassDecl translate_class_block_decl(decl, rbs) end end end end |
#translate_member(member, decl, rbs) ⇒ Object
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/rbs/inline/writer.rb', line 401 def translate_member(member, decl, rbs) case member when AST::Members::RubyDef if member.comments comment = RBS::AST::Comment.new(string: member.comments.content(trim: true), location: nil) end kind = method_kind(member, decl) if member.override_annotation rbs << RBS::AST::Members::MethodDefinition.new( name: member.method_name, kind: kind, overloads: [], annotations: [], location: nil, comment: comment, overloading: true, visibility: member.visibility ) return end rbs << RBS::AST::Members::MethodDefinition.new( name: member.method_name, kind: kind, overloads: member.method_overloads, annotations: member.method_annotations, location: nil, comment: comment, overloading: member.overloading?, visibility: member.visibility ) when AST::Members::RubyAlias if member.comments comment = RBS::AST::Comment.new(string: member.comments.content(trim: true), location: nil) end rbs << RBS::AST::Members::Alias.new( new_name: member.new_name, old_name: member.old_name, kind: :instance, annotations: [], location: nil, comment: comment ) when AST::Members::RubyMixin if m = member.rbs rbs << m end when AST::Members::RubyAttr if m = member.rbs rbs.concat m end when AST::Members::RubyPrivate rbs << RBS::AST::Members::Private.new(location: nil) when AST::Members::RubyPublic rbs << RBS::AST::Members::Public.new(location: nil) when AST::Members::RBSIvar if m = member.rbs rbs << m end when AST::Members::RBSEmbedded case members = member.members when Array rbs.concat members end end end |
#translate_members(members, decl, rbs) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rbs/inline/writer.rb', line 125 def translate_members(members, decl, rbs) members.each do |member| case member when AST::Members::Base translate_member(member, decl, rbs) when AST::Declarations::SingletonClassDecl translate_singleton_decl(member, rbs) when AST::Declarations::BlockDecl if member.module_class_annotation translate_decl(member, rbs) else translate_members(member.members, decl, rbs) end when AST::Declarations::ClassDecl, AST::Declarations::ModuleDecl, AST::Declarations::ConstantDecl, AST::Declarations::DataAssignDecl, AST::Declarations::StructAssignDecl translate_decl(member, rbs) end end end |
#translate_module_decl(decl, rbs) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rbs/inline/writer.rb', line 147 def translate_module_decl(decl, rbs) return unless decl.module_name if decl.comments comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil) end members = [] #: Array[RBS::AST::Members::t | RBS::AST::Declarations::t] translate_members(decl.members, nil, members) self_types = decl.module_selfs.map { _1.constraint }.compact rbs << RBS::AST::Declarations::Module.new( name: decl.module_name, type_params: decl.type_params, members: members, self_types: self_types, annotations: [], location: nil, comment: comment ) end |
#translate_singleton_decl(decl, rbs) ⇒ Object
388 389 390 391 392 393 394 |
# File 'lib/rbs/inline/writer.rb', line 388 def translate_singleton_decl(decl, rbs) decl.members.each do |member| if member.is_a?(AST::Members::Base) translate_member(member, decl, rbs) end end end |
#translate_struct_assign_decl(decl, rbs) ⇒ Object
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/rbs/inline/writer.rb', line 277 def translate_struct_assign_decl(decl, rbs) #: void return unless decl.constant_name if decl.comments comment = RBS::AST::Comment.new(string: decl.comments.content(trim: true), location: nil) end attributes = decl.each_attribute.map do |name, type| if decl.readonly_attributes? RBS::AST::Members::AttrReader.new( name: name, type: type&.type || Types::Bases::Any.new(location: nil), ivar_name: false, comment: nil, kind: :instance, annotations: [], visibility: nil, location: nil ) else RBS::AST::Members::AttrAccessor.new( name: name, type: type&.type || Types::Bases::Any.new(location: nil), ivar_name: false, comment: nil, kind: :instance, annotations: [], visibility: nil, location: nil ) end end new = RBS::AST::Members::MethodDefinition.new( name: :new, kind: :singleton, overloads: [], annotations: [], location: nil, comment: nil, overloading: false, visibility: nil ) if decl.positional_init? attr_params = decl.each_attribute.map do |name, attr| RBS::Types::Function::Param.new( type: attr&.type || Types::Bases::Any.new(location: nil), name: name, location: nil ) end method_type = Types::Function.empty(Types::Bases::Instance.new(location: nil)) if decl.required_new_args? method_type = method_type.update(required_positionals: attr_params) else method_type = method_type.update(optional_positionals: attr_params) end new.overloads << RBS::AST::Members::MethodDefinition::Overload.new( method_type: RBS::MethodType.new(type_params: [], type: method_type, block: nil, location: nil), annotations: [] ) end if decl.keyword_init? attr_keywords = decl.each_attribute.map do |name, attr| [ name, RBS::Types::Function::Param.new( type: attr&.type || Types::Bases::Any.new(location: nil), name: nil, location: nil ) ] end.to_h #: Hash[Symbol, RBS::Types::Function::Param] method_type = Types::Function.empty(Types::Bases::Instance.new(location: nil)) if decl.required_new_args? method_type = method_type.update(required_keywords: attr_keywords) else method_type = method_type.update(optional_keywords: attr_keywords) end new.overloads << RBS::AST::Members::MethodDefinition::Overload.new( method_type: RBS::MethodType.new(type_params: [], type: method_type, block: nil, location: nil), annotations: [] ) end rbs << RBS::AST::Declarations::Class.new( name: decl.constant_name, type_params: [], members: [*attributes, new], super_class: RBS::AST::Declarations::Class::Super.new( name: RBS::TypeName.new(name: :Struct, namespace: RBS::Namespace.empty), args: [RBS::Types::Bases::Any.new(location: nil)], location: nil ), annotations: decl.class_annotations, location: nil, comment: comment ) end |
#write(uses, decls, rbs_decls) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rbs/inline/writer.rb', line 45 def write(uses, decls, rbs_decls) use_dirs = uses.map do |use| RBS::AST::Directives::Use.new( clauses: use.clauses, location: nil ) end rbs = [] #: Array[RBS::AST::Declarations::t] decls.each do |decl| translate_decl( decl, rbs #: Array[RBS::AST::Declarations::t | RBS::AST::Members::t] ) end rbs.concat(rbs_decls) writer.write( use_dirs + rbs ) end |