Better support for loading extensions:
[cl-glfw.git] / generators / sexpize-gl-spec.rb
blob9b7c0d2b533eec1547fb16ac3bd25c4240398ec8
1 #!/usr/bin/env ruby
3 require 'generators/string-lispify'
4 require 'pp'
6 BASE=File::dirname($0)+'/..'
8 function_names=[]
9 open("#{BASE}/src/gl.spec","r") do |i|
10   for l in i
11     case l
12     when /^(\w+)\((.*)\)$/ 
13       function_names << $1
14     end
15   end
16 end
17 function_name_map=function_names.lispify.inject(Hash.new) do |h,cl|
18   #puts "processing #{cl.inspect}"
19   lisp_name=cl[1..-1].join('-').gsub(/-+/,'-').sub(/-$/,'').downcase
20   #puts "injecting #{cl[0].inspect} as #{lisp_name.inspect}"
21   h[cl[0]]=lisp_name
22   h
23 end
25 open("#{BASE}/src/gl.spec.lisp","w") do |o|
26   o.puts "("
27   open("#{BASE}/src/gl.spec","r") do |i|
28     o.puts(";;;; File: gl.spec")
29     o.puts ":functions (("
30     for l in i
31       case l
32         when /^\s*(#+)(.*)/ then o.puts((';'*$1.length)+$2)
33         when /^[a-z-]*:/ then next
34         when /^(\w+)\((.*)\)$/ 
35           o.puts %{) (("#{$1}" #{function_name_map[$1]}) :args (#{$2.split(',').collect{|s|'|'+s.strip+'|'}.join(' ')})}
36         when /^\tparam\s+(\w+)\s+(\w+)\s+(in|out)\s+(array|value)\s*(.*)\s*$/i
37           o.write(%{:param (:name |#{$1}| :type |#{$2}| :direction :#{$3}})
38           if $4=='array'
39             o.write(%{ :array t})
40             if rest=$5.match(/\[(\S*)\](\s+retained)?/)
41               if rest[1]!=''
42                 if size=rest[1].match(/COMPSIZE\(([^)]*)\)/)
43                   computes=size[1].split(/\W+/)
44                   if computes.length>0
45                     o.write(%{ :size (#{computes.collect{|c|"|#{c}|"}.join(' ')})})
46                   end
47                 else
48                   o.write(%{ :size #{rest[1]}})
49                 end
50               end
51               o.write(%{ :retained t}) if rest[2] and rest[2]!=''
52             end
53           end
54           o.puts(%{)})
55         when /^\t([a-z0-9-]*)\s*(.*?)(#|$)/
56           o.puts(":"+$1+" ("+$2.split(/\s+/).compact.collect{|s|'"'+s+'"'}.join(' ')+")")
57         when /^$/ then o.puts
58         else o.puts(";?"+l)
59       end
60     end
61     o.puts "))"
62   end
63   open(BASE+"/src/gl.tm","r") do |i|
64     o.puts(";;;; File: gl.tm")
65     o.puts ":type-map ("
66     for l in i
67       case l
68         when /^\s*(#+)(.*)/ then o.puts((';'*$1.length)+$2)
69         when /^(.*,){5}.*$/
70           ts=l.strip.split(',')
71           o.puts("|"+ts[0]+"| |"+ts[3].strip+"|")
72         when /^$/ then o.puts
73         else o.puts ";? #{l}"
74       end
75     end
76     o.puts ")"
77   end
78   o.puts ":enum-spec ("
79   for spec in ["enum.spec","enumext.spec"]
80     open(BASE+'/src/'+spec) do |i|
81       o.puts(";;;; File: #{spec}")
82       for l in i
83         case l
84           when /^Extensions define:$/
85             o.puts(':extensions (')
86           when /^(\w*) enum:$/
87             o.puts(') |'+$1.gsub(/\s+/,'-')+'| (')
88           when /^(?:\t| {4,})(\w+)\s*=\s*0x([a-fA-F0-9]+)/
89             o.puts(%{|#{$1}| #x#{$2}})
90           when /^(?:\t| {4,})(\w+)\s*=\s*GL_(\w+)(?:\s+#.*)?/
91             o.puts(%{|#{$1}| |#{$2}|})
92           when /^(?:\t| {4,})(\w+)\s*=\s*(\d+)(?:\s+#.*)?/
93             o.puts(%{|#{$1}| #{$2}})
94           when /^(?:\t| {4,})(\w+)\s*=\s*(.+)(?:\s+#.*)/
95             o.puts(%{|#{$1}| #{$2}})
96           when /^(?:\t| {4,})use\s+(\w+)\s+(\w+)/   
97             o.puts(%{|#{$2}| (:use |#{$1}|)})
98 #          when /^(\w+)(,\*)*,\s+(\w+)(,\*)*/
99 #            o.puts(%{("#{$1}" "#{$3}")})
100           when /^\s*(#+)(.*)/ then o.puts((';'*$1.length)+$2)
101           when /^$/ then o.puts
102           else o.puts ";;? #{l}"
103         end
104       end
105     end
106   end
107   o.puts "))"
108   o.puts ")"