Class: BitClust::MethodSignature

Inherits:
Object
  • Object
show all
Includes:
NameUtils
Defined in:
lib/bitclust/methodsignature.rb

Overview

Represents detailed signature of a method in a class/module. Includes parameters, block specification and return type.

Constant Summary collapse

METHOD_SIGNATURE_RE =
/\A
    --- \s*
      (?: (?:#{CLASS_PATH_RE} #{TYPEMARK_RE})? (#{METHOD_NAME_RE})
      | (#{GVAR_RE})
      )                 \s*         # method name ($1) or gvar name ($2)
    (?: \( (.*?) \)     \s* )?      # parameters (optional); $3=parameter_list
    (?: (\{ .* \})      \s* )?      # block (optional); $4=block
    (?: -> \s* (\S.*)   \s* )?      # type declaration (optional); $5=return_type
\z/x

Constants included from NameUtils

NameUtils::CHAR_TO_MARK, NameUtils::CHAR_TO_NAME, NameUtils::CLASS_NAME_RE, NameUtils::CLASS_PATH_RE, NameUtils::CONST_PATH_RE, NameUtils::CONST_RE, NameUtils::GVAR_RE, NameUtils::LIBNAME_RE, NameUtils::MARK_TO_CHAR, NameUtils::MARK_TO_NAME, NameUtils::METHOD_NAME_RE, NameUtils::METHOD_SPEC_RE, NameUtils::MID, NameUtils::NAME_TO_CHAR, NameUtils::NAME_TO_MARK, NameUtils::TYPEMARK_RE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NameUtils

build_method_id, classid2name, classname2id, classname?, decodeid, decodename_fs, decodename_url, encodeid, encodename_fs, encodename_rdocurl, encodename_url, functionname?, gvarname?, html_filename, libid2name, libname2id, libname?, method_spec?, methodid2classid, methodid2libid, methodid2mname, methodid2specparts, methodid2specstring, methodid2typechar, methodid2typemark, methodid2typename, methodname?, split_method_id, split_method_spec, typechar2mark, typechar2name, typechar?, typemark2char, typemark2name, typemark?, typename2char, typename2mark, typename?

Constructor Details

#initialize(name, params, block, type) ⇒ MethodSignature

Returns a new instance of MethodSignature.


38
39
40
41
42
43
# File 'lib/bitclust/methodsignature.rb', line 38

def initialize(name, params, block, type)
  @name = name
  @params = params
  @block = block
  @type = type
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block


47
48
49
# File 'lib/bitclust/methodsignature.rb', line 47

def block
  @block
end

#nameObject (readonly)

Returns the value of attribute name


45
46
47
# File 'lib/bitclust/methodsignature.rb', line 45

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params


46
47
48
# File 'lib/bitclust/methodsignature.rb', line 46

def params
  @params
end

#typeObject (readonly)

Returns the value of attribute type


48
49
50
# File 'lib/bitclust/methodsignature.rb', line 48

def type
  @type
end

Class Method Details

.parse(line) ⇒ Object


31
32
33
34
35
36
# File 'lib/bitclust/methodsignature.rb', line 31

def MethodSignature.parse(line)
  m = METHOD_SIGNATURE_RE.match(line) or
      raise ParseError, %Q(unknown signature format: "#{line.strip}")
  method, gvar, params, block, type = m.captures
  new(method || gvar, params && params.strip, block && block.strip, type && type.strip)
end

Instance Method Details

#friendly_stringObject


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bitclust/methodsignature.rb', line 57

def friendly_string
  case @name
  when /\A\$/   # gvar
    @name + (@type ? " -> #{@type}" : "")
  when "[email protected]", "[email protected]", "~", "!", "[email protected]"  # unary operator
    "#{@name.sub(/@/, '')} self" + (@type ? " -> #{@type}" : "")
  when "[]"     # aref
    "self[#{@params}]" + (@type ? " -> #{@type}" : "")
  when "[]="    # aset
    params = @params.split(',')
    val = params.pop
    "self[#{params.join(',').strip}] = #{val.strip}"
  when "`"  # `command`
    "`#{@params}`" + (@type ? " -> #{@type}" : "")
  when /\A\W/   # binary operator
    "self #{@name} #{@params}" + (@type ? " -> #{@type}" : "")
  else
    to_s()
  end
end

#inspectObject


78
79
80
# File 'lib/bitclust/methodsignature.rb', line 78

def inspect
  "\#<#{self.class} name=#{@name.inspect} params=#{@params.inspect} block=#{@block.inspect} type=#{@type.inspect}>"
end

#to_sObject


50
51
52
53
54
55
# File 'lib/bitclust/methodsignature.rb', line 50

def to_s
  @name +
      (@params ? "(#{@params})" : "") +
      (@block ? " #{@block}" : "") +
      (@type ? " -> #{@type}" : "")
end