Skip to content

Instantly share code, notes, and snippets.

@mattetti
Created November 8, 2011 20:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattetti/1349038 to your computer and use it in GitHub Desktop.
Save mattetti/1349038 to your computer and use it in GitHub Desktop.
A dumb parser to parse a header file generated via sdef/sdp. The parsed info is saved to a local json file.
#!/usr/bin/env ruby
require 'json'
file = ARGV.first
raise "Can't find #{file.inspect}" unless File.exist?(file)
header = File.open(file)
doc = {}
properties = {}
File.foreach(header) do |line|
if line =~ /^@interface /
@current_class = line[/^@interface\s(.*)\s:/, 1]
elsif line =~ /^- /
doc[@current_class] ||= []
returned_class = line[/^- \((.*?)\)/, 1].gsub(' *', '')
selector = line[/^- \(.*?\)(.*);/, 1].strip
selector =~ /(.*?):/
method_name = $1 || selector
types = selector.scan(/\((.*?)\)/).flatten
selector_args = [method_name] + selector.scan(/\s(\w.*?):/).flatten
ruby_method = Hash[selector_args.zip(types)]
comment = line[/\/\/(.*)/, 1]
comment = comment.strip unless comment.nil?
doc[@current_class] << {:returned => returned_class, :selector => selector, :method => ruby_method, :comment => comment}
elsif line =~ /^@property/
properties[@current_class] ||= []
property = line[/\s([a-z|\*|[0-9]]*?);/i, 1].gsub('*', '')
comment = line[/\/\/(.*)/, 1]
comment = comment.strip unless comment.nil?
properties[@current_class] << {:name => property, :comment => comment}
end
end
puts doc.inspect
File.open("doc.json", "w"){|w| w << doc.to_json; w << "\n" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment