/
old_glspec.rb
executable file
·258 lines (224 loc) · 8.21 KB
/
old_glspec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#----------------------------------------------------------------------------------------------
#
# old_glspec.rb
# GenGGL (OpenGL extension glue code generator in C)
#
# Copyright 2010 Ju Hyung Lee. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of
# conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
# of conditions and the following disclaimer in the documentation and/or other materials
# provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY JU HYUNG LEE ``AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JU HYUNG LEE OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are those of the
# authors and should not be interpreted as representing official policies, either expressed
# or implied, of Ju Hyung Lee.
#
#----------------------------------------------------------------------------------------------
require 'net/http'
class GLCommandParam
attr_reader :name, :typename, :pointer, :const
attr_writer :name, :typename, :pointer, :const
def to_s
(const ? "const " : "") + typename + (pointer ? "* " : " ") + name
end
end
class GLCommand
attr_reader :name, :category, :vectorequiv, :alias, :original, :return_type, :params, :deprecated
attr_writer :name, :category, :vectorequiv, :alias, :original, :return_type, :params, :deprecated
def initialize(name, spec)
@name = name
@params = Array.new
@spec = spec
end
def core?
@category =~ /^VERSION_/ ? true : false
end
def core_version
@category =~ /^VERSION_(\d+_\d+)/ ? $1.sub('_', '.').to_f : nil
end
def core_extension? # 코어화된 extension (non post-fix)
@category !~ /^VERSION_/ && @name !~ /(#{@spec.extension_group_names.join('|')})$/ ? true : false
end
def extension? # 일반적인 extension
@category !~ /^VERSION_/ && @name =~ /(#{@spec.extension_group_names.join('|')})$/ ? true : false
end
def valid?(version)
return true if core? && core_version <= version
return true if core_extension?
return true if extension? && (!(a_command = aliased) || !a_command.core? || (a_command.core? && a_command.core_version > version))
return false
end
def aliased
alias_name = self.alias
command = nil
# alias might be recursive
while alias_name do
command = @spec.command_array.find { |x| x.name == alias_name }
puts "WARNING: alias \"#{alias_name}\" not found" if !command
alias_name = command.alias
end
return command
end
end
class GLDefine
attr_reader :name, :value
def initialize(name, value)
@name = name
@value = value
end
end
class GLSpecElement
attr_reader :name, :data
def initialize(name, data)
@name = name
@data = data
end
end
class GLSpec
attr_reader :versions, :enumext_elements, :glspec_elements, :type_hash, :categories, :extension_group_names, :command_array
def initialize(url)
@enumext_elements = []
@glspec_elements = []
@type_hash = {}
@command_array = []
@command_hash = {}
@categories = []
puts "parsing #{url[:typemap]}..."
parse_typemap_url(url[:typemap])
puts "parsing #{url[:enumext]}..."
parse_enumext_url(url[:enumext])
puts "parsing #{url[:glspec]}..."
parse_glspec_url(url[:glspec])
end
def new_command(command_name)
if !@command_hash[command_name]
@command_array << command = GLCommand.new(command_name, self)
@command_hash[command_name] = command
end
end
def del_command(command_name)
if command = @command_hash[command_name]
@command_array.delete(command)
@command_hash[command_name] = nil
end
end
def find_command(command_name)
@command_hash[command_name]
end
def typemap(typename)
return "void" if typename == "void"
@type_hash[typename]
end
def parse_typemap_url(url)
parse_url_each_line(url) do |line|
ar = line.split(',')
@type_hash[ar[0]] = ar[3].to_s.strip
end
end
def parse_enumext_url(url)
parse_url_each_line(url) do |line|
if line =~ /(^pass(thru|end)):/
@enumext_elements << GLSpecElement.new($1.to_sym, line.sub(/^\w+: ?/, ''))
elsif line =~ /(^\w+)\senum:/
@enumext_elements << GLSpecElement.new(:enum, $1)
elsif line =~ /^\s+(\w+)\s+\=\s+(\-?\w+)/
@enumext_elements << GLSpecElement.new(:define, GLDefine.new($1, $2))
elsif line =~ /^\s+use\s+\w+\s+(\w+)/
@enumext_elements << GLSpecElement.new(:use, $1)
end
end
end
def parse_glspec_url(url)
command = nil
parse_url_each_line(url) do |line|
if line =~ /^category:\s+/
@categories.concat(line.sub(/^category:\s+/, '').split)
elsif line =~ /(^pass(thru|end)):/
@glspec_elements << GLSpecElement.new($1.to_sym, line.sub(/^\w+: ?/, ''))
elsif line =~ /^newcategory:\s+(\w+)/
@categories << $1
@glspec_elements << GLSpecElement.new(:newcategory, $1)
elsif line =~ /^version:\s+/
@versions = line.sub(/^version:\s+/, '').split
elsif line =~ /(^\w+?)\(.*\)/ # command
@command_array << command = GLCommand.new($1, self)
@command_hash[$1] = command
@glspec_elements << GLSpecElement.new(:command, command)
elsif line =~ /\s+(\w+)\s+(.*)/ # command property
case $1
when 'return'
command.return_type = typemap($2)
when 'param'
x = $2
puts "WARNING: unmatched \"#{x}\"" if (x =~ /(\w+)\s+(\w+)\s+/) == nil
param_name = $1
# prevent using C keywords
param_name = "z#{param_name}" if param_name == "near" || param_name == "far"
param_type = $2
x.sub!($&, '')
#param_type.sub!(/\w+\b/, "PixelInternalFormat") if command.name =~ /^TexImage(1|2|3)D/ && param_name == "internalformat"
puts "WARNING: unmatched \"#{x}\"" if (x =~ /(in|out)\s+(value|reference|array)/) == nil
param = GLCommandParam.new
param.name = param_name
param.typename = typemap(param_type)
if $2 == "reference" || $2 == "array"
param.pointer = true
param.const = true if $1 == "in"
end
command.params << param
when 'category'
command.category = $2
when 'deprecated'
command.deprecated = $2
when 'vectorequiv'
command.vectorequiv = $2
when 'alias'
command.alias = $2
command.alias << 's' if command.alias == 'GenVertexArray' # 's' is omitted in spec file
end
end
end
# EXT_texture_compression_s3tc is omitted in spec file
@categories << "EXT_texture_compression_s3tc"
@categories.uniq!.sort!
@extension_group_names = @categories.grep(/(^[A-Z0-9]+?)_/) { |x| $1 }.reject { |x| x =~ /^VERSION/ }.uniq
@command_array.each do |command|
if command.alias
@command_hash[command.alias].original = command
elsif !command.core? && command.vectorequiv
ve_command = @command_hash[command.vectorequiv]
if ve_command.core?
command.alias = command.vectorequiv.chop
@command_hash[command.alias].original = command
end
end
end
end
def parse_url_each_line(url)
Net::HTTP.get_response(URI.parse(url)) do |resp|
resp.body.each_line do |line|
if line !~ /^pass(thru|end):/
line.sub!(/#.*/, '')
line.rstrip!
end
yield line
end
end
end
end