-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.lua
More file actions
193 lines (155 loc) · 5.95 KB
/
parser.lua
File metadata and controls
193 lines (155 loc) · 5.95 KB
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
--[[
Copyright (C) 2011 by Joshua Simmons
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
--]]
-- A somewhat simplified OpenGL spec file parser designed to make it possible
-- to generate OpenGL bindings from your language of choice. It could also be
-- used to transcode spec files into something more reasonable like JSON.
-- A generic parser structure for parsing gl registry files.
-- Takes a path to the file and a pattern->callback table and then for each
-- non-comment line in the file tries to apply each pattern until one succeeds
-- at which point the captures are passed to the callback.
local function basic_parser(path, patterns)
for line in io.lines(path) do
-- Remove the comment part from any place in the line.
line = line:gsub('#.*', '')
-- If there was nothing but comment or an empty line there'll only be
-- whitespace left in which case let's break out.
if line:find('%S') then
for pattern, callback in pairs(patterns) do
local matches = {line:match(pattern)}
-- Ensure that the pattern was successfully matched before
-- invoking the callback.
if matches[1] ~= nil then
callback(unpack(matches))
break
end
end
end
end
end
local function typemap(path)
local map = {}
local patterns = {
['([^,]+)%W+([^,]+)'] = function(a, b)
-- 'void' causes trouble so let's fix it.
if b == '*' then
map[a] = a
else
map[a] = b
end
end;
}
basic_parser(path, patterns)
return map
end
local function parse_enums(path, extpath, version, whitelist)
local enums, drop = {}, false
local patterns = {
['(%S+)%s*=%s*(%S+)'] = function(symbol, value)
if drop then return end
-- Expand references here as the hash table being unordered will
-- otherwise muddle everything up.
value = value:gsub('GL_', '')
enums[symbol] = enums[value] or value
end;
['^(%S+) enum:'] = function(name)
if whitelist[name] then
drop = false
return
end
drop = true
local major, minor, dep = name:match('VERSION_(%d)_(%d)_?(.*)')
if major == nil then return end
-- Drop all deprecated items for now.
if dep ~= 'DEPRECATED' and (major * 10 + minor <= version) then
drop = false
end
end;
}
basic_parser(path, patterns)
basic_parser(extpath, patterns)
return enums
end
local function parse_funcs(path, tm, version, whitelist)
local functions = {}, current_function, current_name
local patterns = {
['^(%w+)%(.*%)'] = function(name)
-- Store the last function if finished
print(current_name)
if current_name then functions[current_name] = current_function end
current_name = name
current_function = {params = {}}
end;
-- TODO: Capture array length information in a useful manner.
['param%s+(%S+)%s+(%S+) (%S+) (%S+)'] = function(name, type, dir, mode)
table.insert(current_function.params, {
name = name;
type = tm[type];
dir = dir;
mode = mode;
})
end;
['^\treturn%s+(%S+)'] = function(return_type)
current_function.return_type = tm[return_type]
end;
['^\tdeprecated%s+(%S+)'] = function(as_of)
if (as_of * 10) <= version then
current_name = nil
end
end;
['^\tversion%s+(%S+)'] = function(from)
if (from * 10) > version then
current_name = nil
end
end;
['^\talias%s+(%S+)'] = function(as)
current_function.alias = as
end;
['^\tcategory%s+(%S+)'] = function(name)
-- Use the version information from the version specifier to save
-- duplication.
if not name:find('VERSION') and not whitelist[name] then
current_name = nil
end
end;
}
basic_parser(path, patterns)
return functions
end
local function gl(base, version, whitelist)
whitelist = whitelist or {}
local tm = typemap(base .. 'gl.tm')
return {
enums = parse_enums(base .. 'glenum.spec', base .. 'glenumext.spec',
version, whitelist);
funcs = parse_funcs(base .. 'gl.spec', tm, version, whitelist);
}
end
local function glx(base, version, whitelist)
whitelist = whitelist or {}
local tm = typemap(base .. 'glx.tm')
return {
enums = parse_enums(base .. 'glxenum.spec', base .. 'glxenumext.spec',
version, whitelist);
funcs = parse_funcs(base .. 'glx.spec', tm, version, whitelist);
}
end
return {
gl = gl;
glx = glx;
}