Class: ReFe::ReferenceRDParser

Inherits:
Object
  • Object
show all
Defined in:
lib/refe/refrdparser.rb

Instance Method Summary collapse

Instance Method Details

#_get_method_name(line) ⇒ Object



107
108
109
# File 'lib/refe/refrdparser.rb', line 107

def _get_method_name( line )
  line.slice(/\A: (\w+[?!=]?|==|\[\]|\[\]=|\+|\-|<=>|=~|~)/, 1)
end

#find_same_method(table, mnames) ⇒ Object



91
92
93
94
95
# File 'lib/refe/refrdparser.rb', line 91

def find_same_method( table, mnames )
  a = mnames.map {|n| table[n] }.compact.uniq
  raise 'fatal: inconsistent document; cannot parse' if a.length > 1
  a[0]
end

#get_method_name(line, f) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/refe/refrdparser.rb', line 97

def get_method_name( line, f )
  n = _get_method_name(line)
  unless n
    p f.lineno
    p line
    raise 'cannot get method name'
  end
  n
end

#parse(f) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/refe/refrdparser.rb', line 18

def parse( f )
  classes = {}   # {class => description}
  methods = {}   # {class[#.] => {method => description}}
  current_class = nil
  s_table = nil
  m_table = nil
  tbl = nil

  f = LineInput.new(f)
  while line = f.gets
    case line
    when /\A=+\s/
      case line
      when /\A==? (?:class|module) ([A-Z][\w:]+)/
        current_class = $1
        raise "class duplicated: #{current_class}" if classes.key?(current_class)
        classes[current_class] = read_class_document(f)
        s_table = (methods[current_class + '.'] ||= {})
        m_table = (methods[current_class + '#'] ||= {})
        tbl = nil
      when /\A===? (?:Class Methods|Module Functions)/
        tbl = s_table
      when /\A===? Instance Methods/
        tbl = m_table
      when /\A= /
        current_class = nil
        s_table = m_table = tbl = nil
      end

    when /\A: /
      next unless tbl
      mnames, ent = read_entry(f, line)
      if desc = find_same_method(tbl, mnames)
        desc << ent
      else
        desc = ent
      end
      mnames.each do |name|
        tbl[name] = desc
      end
    end
  end

  return classes, methods
end

#read_class_document(f) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/refe/refrdparser.rb', line 64

def read_class_document( f )
  buf = ''
  f.until_match(/\A[\=\-]/) do |line|
    buf << line
  end
  buf.strip
end

#read_entry(f, first_line) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/refe/refrdparser.rb', line 72

def read_entry( f, first_line )
  buf = ''
  buf << first_line
  mnames = [get_method_name(first_line, f)]

  # check method aliases
  f.while_match(/\A: /) do |line|
    buf << line
    mnames.push get_method_name(line, f)
  end

  # read description
  f.until_match(/\A(?:=+ |: )/) do |line|
    buf << line
  end

  return mnames, buf.strip + "\n\n"
end