Need to export the extension loader
[cl-glfw.git] / generators / string-lispify.rb
blobd2c43873ca8f28b0b1642686ccebd2ae5d577f49
2 class String
3   def lispify prefix=nil
4     s=self.dup
5     # remove prefix if it's there
6     s=s.slice(prefix.length..-1) if prefix and s[0...prefix.length]==prefix
8     # prefix underscore->alpha or uppercase->lowercase with dash
9     s.gsub!(/(_+[a-zA-Z]|[A-Z])(?=[a-z])/) do |m|
10       '-'+m.downcase[-1,1]
11     end
12     # split off any numerical groups
13     s.gsub!(/\d+/,"-\\0")
14     # split off any two or more-letter acronyms at the end (eg. EXT NV ARB ATI SGIX etc)
15     s.sub!(/[A-Z]{2,}$/,"-\\0")
16     # group together any bouts of -s and _s into a single dash
17     s.gsub!(/[-_]+/,"-")
18     s.downcase!
19     if s[0,1]=='-'
20       s[1..-1]
21     else
22       s
23     end
24   end
25 end
27 class Array
28   GL_SUFFIXES=Regexp.new('('+["IBM","3DFX","SGIS","SUNX","HP","GREMEDY","APPLE","MESA","SUN","INTEL","NV","ARB","SGIX","EXT","ATI"].join('|')+')$')
30   def lispify
31     stems={}
32     split=
33     collect do |e|
34       [e,*e.sub(GL_SUFFIXES,'-\\1').split('-')]
35     end.collect do |orig,e,vendor_suffix|
36       #puts("Expanding e #{e.inspect} to #{e.sub(/([0-9])?(u?(i|s|b)|h|f|d)v?$/,'-\\0')}")
37       stem,arg_suffix=e.sub(/([0-9])?(u?(i|s|b)|h|f|d)v?$/,'-\\0').split('-')
38       stems[stem]||={}
39       if arg_suffix && arg_suffix!=''
40         stems[stem][arg_suffix]||=0
41         stems[stem][arg_suffix]+=1
42       end
43       #pp [orig,stem,arg_suffix||'',vendor_suffix]
44       [orig,stem,arg_suffix||'',vendor_suffix]
45     end
46     # re-join endings that do not share a stem with another function name
47     split.collect do |orig,e,arg_suffix,vendor_suffix|
48       # also keep those ending in 'v', as these are never (probably) going to be at the end of words
49       # also keep 'i' as these are all integer suffices
50       # other special-case words
51       shared_stem=((stems[e].keys.length > 1) or arg_suffix.match(/v$/) or arg_suffix=='i' \
52                    or e.match(/Bounds$/) or e.match(/Depth$/))
53       # special case, things ending in edv (ie. participle-v), reattach 'd' and remove from arg_suffix
54       if (e.match(/e$/) and arg_suffix.match(/^d.*v$/))
55         e+='d'
56         arg_suffix=arg_suffix[1..-1]
57         puts "Fixing participle-v case: #{orig.inspect}, #{e.inspect} suffix: #{arg_suffix.inspect}"
58       end
59       e_split=(shared_stem ? e : e+arg_suffix).gsub(/([a-z])([A-Z0-9])/,'\\1-\\2').split('-')
60       if arg_suffix!='' and not shared_stem
61         puts "Re-attaching arg_suffix for #{orig.inspect}, #{e.inspect}+#{arg_suffix.inspect}"
62       end
63       [orig,*e_split].concat([shared_stem ? arg_suffix : '',vendor_suffix])
64     end
65   end
66 end