class Objc2swiftAssistant::ObjC2SwiftFileConverter

Attributes

access_control_state[RW]
categories_by_class_and_name[RW]
classes_by_name[RW]
configuration[RW]
enums[RW]
objc_file_node[RW]
protocol_optional_state[RW]
protocols[RW]
swift_file_node[RW]

Public Class Methods

new( objc_file_node, configuration ) click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 57
def initialize( objc_file_node, configuration )
  @objc_file_node = objc_file_node
  @configuration = configuration
  @enums = {}
  @classes_by_name = {}
  @categories_by_class_and_name = {}
  @protocols = []

  @access_control_state = nil
  @protocol_optional_state = false
end

Public Instance Methods

cannonical_source_file_path() click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 223
def cannonical_source_file_path
  objc_file_node.cannonical_source_file_path
end
category_for_class_and_name( class_name, category_name ) click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 208
def category_for_class_and_name( class_name, category_name )
  the_key = [class_name, category_name]
  category_converter = @categories_by_class_and_name[ the_key ]
  if category_converter.nil?
    category_converter = ObjC2SwiftCategoryConverter.new( self, class_name, category_name, @configuration )
    @categories_by_class_and_name[ [class_name, category_name] ] = category_converter
  end

  category_converter
end
class_for_name( class_name ) click to toggle source

Utility

# File lib/objc2swift_assistant/objc_2_swift.rb, line 198
def class_for_name( class_name )
  class_converter = @classes_by_name[ class_name ]
  if class_converter.nil?
    class_converter = ObjC2SwiftClassConverter.new( self, class_name, @configuration )
    @classes_by_name[ class_name ] = class_converter
  end

  class_converter
end
dump( indent, tab, errors_only ) click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 227
def dump( indent, tab, errors_only )
    puts( "#{indent}Generating Swift file #{@swift_file_node.relative_path} at #{@swift_file_node.full_path}" )

    #objc_file_node
    #enums
    puts( "\n#{indent}Generated Classes:" ) unless @classes_by_name.length == 0
    @classes_by_name.each do  | _, class_converter |
      puts( "#{indent+tab}#{class_converter.class_name}" )
    end

    puts( "\n#{indent}Generated Categories:" ) unless @categories_by_class_and_name.length == 0
    @categories_by_class_and_name.each do | _, category_converter |
      category_str = category_converter.category_name.nil? ? "" : " /*#{category_converter.category_name}*/"
      puts( "#{indent+tab}#{category_converter.class_name}#{category_str}" )
    end

    puts( "\n#{indent}Generated Prototcols:" ) unless @protocols.length == 0
    @protocols.each do  | protocol |
      puts( "#{indent+tab}#{protocol.class_name}" )
    end

  #protocols
    #configuration
end
generate( generator_defs, configuration, dry_run ) click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 176
def generate( generator_defs, configuration, dry_run )
  @configuration.log_verbose( "Generating #{@swift_file_node.relative_path} at #{@swift_file_node.full_path}" )

  @enums.each do |enum_converter|

  end

  @classes_by_name.each do |key, class_converter|
    class_converter.generate( generator_defs, @swift_file_node.relative_path, configuration, dry_run )
  end

  @categories_by_class_and_name.each do |_, category_converter|
    category_converter.generate( generator_defs, @swift_file_node.relative_path, configuration, dry_run )
  end

  @protocols.each do | protcol_converter|
    protcol_converter.generate( generator_defs, @swift_file_node.relative_path, configuration, dry_run )
  end
end
prepare( ) click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 69
def prepare( )
  @objc_file_node.root_matches.each do |region|

    if region.region_type == CLASS_INTERFACE_KEY
      class_converter = class_for_name( region.class_name )
      class_converter.interface_region = region
      process_interface_sub_regions( region, class_converter )
    elsif region.region_type == CLASS_IMPLEMENTATION_KEY
      class_converter = class_for_name( region.class_name )
      class_converter.implementation_region = region
      process_impl_sub_regions( region, class_converter )
    elsif region.region_type == EXTENSION_DECLARARATION_KEY
      class_converter = class_for_name( region.class_name )
      extension_converter = ObjC2SwiftCategoryConverter.new( self, region.class_name, nil, @configuration   )
      extension_converter.interface_region = region
      class_converter.extension_converter = extension_converter
    elsif region.region_type == CATEGORY_DECLARARATION_KEY
      category_converter = category_for_class_and_name( region.class_name, region.category_name  )
      category_converter.interface_region = region
      process_interface_sub_regions( region, category_converter )
    elsif region.region_type == CATEGORY_IMPLEMENTATION_KEY
      category_converter = category_for_class_and_name( region.class_name, region.category_name  )
      category_converter.implementation_region = region
      process_impl_sub_regions( region, category_converter )
    elsif region.region_type == PROTOCOL_DECLARATION_KEY
      protocol_converter = ObjC2SwiftProtocolConverter.new( self, region.protocol_name, @configuration )
      protocol_converter.interface_region = region
      @protocols << protocol_converter
      process_interface_sub_regions( region, protocol_converter )

      # category_converter.implementation_region = region
      # process_impl_sub_regions( region, category_converter )
      @configuration.log_warning( "protocols NYI")
    elsif region.region_type == ENUM_DECLARATION_KEY
      @configuration.log_warning( "Not Yet Implemented Region Type: #{region.region_type.to_s}")
    elsif region.region_type == PRAGMA_MARK_KEY
      @configuration.log_warning( "Not Yet Implemented Region Type: #{region.region_type.to_s}")
    else
      @configuration.log_warning( "Unexpected Root Region Type: #{region.region_type.to_s}")
    end
  end # Iterating over root matches

  @classes_by_name.each_value do |class_converter|
    class_converter.prepare
  end

  @categories_by_class_and_name.each do |_, category_converter|
    category_converter.prepare
  end

  @protocols.each do | protcol_converter|
    protcol_converter.prepare
  end
end
process_at_directive_region( region ) click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 162
def process_at_directive_region( region )
  directive = region.directive_symbol

  if( directive == :required )
    @protocol_optional_state = directive
  elsif ( directive == :optional )
    @protocol_optional_state = directive
  elsif ( [:public, :private, :protected].include? directive )
    @access_control_state = directive
  else
    add_error( "unknown '@' directive: #{directive}")
  end
end
process_impl_sub_regions( impl_region, class_converter ) click to toggle source

common logic for class-like entities (classes, categories)

# File lib/objc2swift_assistant/objc_2_swift.rb, line 151
def process_impl_sub_regions( impl_region, class_converter )
  impl_region.sub_regions.each do |sub_region|
    if sub_region.region_type == METHOD_IMPLEMENTATION_KEY
      method = class_converter.method_for_signature( sub_region.objc_signature )
      method.implementation_region = sub_region
    else
      unexpected_sub_region( sub_region, impl_region )
    end
  end
end
process_interface_sub_regions( interface_region, class_converter ) click to toggle source

common logic for class-like entities (classes, categories, extensions)

# File lib/objc2swift_assistant/objc_2_swift.rb, line 125
def process_interface_sub_regions( interface_region, class_converter )

  @configuration.log_verbose( "\nprocess_interface_sub_regions") unless interface_region.sub_regions.length == 0

  interface_region.sub_regions.each do |sub_region|
    @configuration.log_verbose( "#{sub_region.region_type} #{sub_region.starting_line_number} #{sub_region.detection_line}" )

    if sub_region.region_type == PROPERTY_DECLARATION_KEY
      property = class_converter.property_for_name( sub_region.name )
      property.declaration_region = sub_region
      property.access_control_state = @access_control_state
      property.protocol_optional_state = @protocol_optional_state
    elsif sub_region.region_type == METHOD_DECLARATION_KEY
      method = class_converter.method_for_signature( sub_region.objc_signature )
      method.declaration_region = sub_region
      method.access_control_state = @access_control_state
      method.protocol_optional_state = @protocol_optional_state
    elsif sub_region.region_type == AT_DIRECTIVE_KEY
      process_at_directive_region( sub_region )
    else
      unexpected_sub_region( sub_region, interface_region )
    end
  end
end
unexpected_sub_region( sub_region, region ) click to toggle source
# File lib/objc2swift_assistant/objc_2_swift.rb, line 219
def unexpected_sub_region( sub_region, region )
  @configuration.log_warning( "Unexpected Sub Region Type: #{sub_region.region_type.to_s} of #{region.region_type.to_s}")
end