Class: IB::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ib/parser.rb

Constant Summary collapse

NAME_REGEX =
/[a-zA-Z][_a-zA-Z0-9]*/
SUPERCLASS_REGEX =
/(#{NAME_REGEX}):{0,2}(#{NAME_REGEX})/
CLASS_REGEX =
/^[ \t]*class[ \t]+(#{NAME_REGEX})([ \t]*<[ \t]*(#{SUPERCLASS_REGEX}))?/
OUTLET_REGEX =
/^[ \t]+(ib_)?outlet(_accessor)?[ \t]+:(#{NAME_REGEX})[ \t]*?(,[ \t]*['"]?(#{NAME_REGEX}))?/
OUTLET_COLLECTION_REGEX =
/^[ \t]+(ib_)?outlet_collection(_accessor)?[ \t]+:(#{NAME_REGEX})[ \t]*?(,[ \t]*['"]?(#{NAME_REGEX}))?/
METHOD_ARGUMENT_REGEX =
/(#{NAME_REGEX})(?:[ \t]*=[^,#)]*)?/
METHOD_REF_REGEX =
/^[ \t]+(ib_action)[ \t]:(#{NAME_REGEX})[ \t]*?(,[ \t]*['"]?(#{NAME_REGEX}))?/
METHOD_DEF_REGEX =
/^[ \t]+(def)[ \t]#{METHOD_ARGUMENT_REGEX}([ \t(]+)?#{METHOD_ARGUMENT_REGEX}?([ \t)]*)(#.*)?$/
ACTION_REGEX =
Regexp.union METHOD_DEF_REGEX, METHOD_REF_REGEX

Instance Method Summary collapse

Constructor Details

#initialize(motion_template_type) ⇒ Parser

Returns a new instance of Parser.



16
17
18
19
20
# File 'lib/ib/parser.rb', line 16

def initialize(motion_template_type)
  # NOTE: motion_template_type equal to Motion::Project::App.template
  #       but, this class use its value for judging build platform.
  @build_platform = motion_template_type
end

Instance Method Details

#find(path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ib/parser.rb', line 41

def find(path)
  src = File.read(path)
  offsets = []
  src.scan CLASS_REGEX do |b|
    offsets << $~.offset(0)
  end

  return [] if offsets.length == 0
  infos = []
  pairs = offsets.map {|o| o[0]}

  pairs << src.length
  (pairs.length - 1).times do |i|
    s = src[pairs[i], pairs[i+1]]
    info = {:class => find_class(s)}

    info[:outlets] = find_outlets(s)
    info[:outlet_collections] = find_outlet_collections(s)
    info[:actions] = find_actions(s)

    info[:path] = path
    info[:build_platform] = @build_platform

    infos << IB::OCInterface.new(info)
  end

  infos
end

#find_actions(src) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ib/parser.rb', line 88

def find_actions src
  src.scan(ACTION_REGEX).map do |groups|
    if groups[0] == "def"
      [groups[1], groups[3], nil]
    elsif groups[6] == "ib_action"
      [groups[7], 'sender', groups[9]]
    else
      nil
    end
  end.compact.uniq {|action| action[0]}
end

#find_all(dir_or_files) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ib/parser.rb', line 22

def find_all(dir_or_files)
  all = {}
  files = case dir_or_files
  when Array
    dir_or_files.flatten
  else
    Dir.glob("#{dir_or_files.to_s}/**/*.rb").to_a
  end

  files.each do |file|
    infos = find(file)
    if infos.length > 0
      all[file] = infos
    end
  end

  IB::DependencyResolver.new(all).sort
end

#find_class(src) ⇒ Object



70
71
72
73
74
# File 'lib/ib/parser.rb', line 70

def find_class src
  src.scan(CLASS_REGEX).map do |groups|
    [groups[0], groups[2]]
  end
end

#find_outlet_collections(src) ⇒ Object



82
83
84
85
86
# File 'lib/ib/parser.rb', line 82

def find_outlet_collections src
  src.scan(OUTLET_COLLECTION_REGEX).map do |groups|
    [groups[2], groups[4] || "id"]
  end
end

#find_outlets(src) ⇒ Object



76
77
78
79
80
# File 'lib/ib/parser.rb', line 76

def find_outlets src
  src.scan(OUTLET_REGEX).map do |groups|
    [groups[2], groups[4] || "id"]
  end
end