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
|
#!/usr/bin/env python
##########################################################################
#
# Copyright 2010 VMware, Inc.
# All Rights Reserved.
#
# 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.
#
##########################################################################/
"""Parser for OpenGL .txt extensions specification."""
import sys
import re
import optparse
from urllib2 import urlopen
def stderr(x):
sys.stderr.write(str(x) + '\n')
class Parser:
def __init__(self, stream):
pass
class LineParser:
"""Base class for parsers that read line-based formats."""
def __init__(self, stream):
self._stream = stream
self._line = None
self._eof = False
# read lookahead
self.readline()
def parse(self):
raise NotImplementedError
def readline(self):
line = self._stream.readline()
if not line:
self._line = ''
self._eof = True
self._line = line.rstrip('\r\n')
def lookahead(self):
assert self._line is not None
return self._line
def consume(self):
assert self._line is not None
line = self._line
self.readline()
return line
def eof(self):
assert self._line is not None
return self._eof
def skip_whitespace(self):
while not self.eof() and self.match_whitespace() or self.match_comment():
self.consume()
def match_whitespace(self):
line = self.lookahead()
return not line.strip()
def match_comment(self):
return False
class TxtParser(LineParser):
section_re = re.compile(r'^([A-Z]\w+)( \w+)*$')
property_re = re.compile(r'^\w+:')
prototype_re = re.compile(r'^(\w+)\((.*)\)$')
def __init__(self, stream, prefix=''):
LineParser.__init__(self, stream)
self.prefix = prefix
def parse(self):
while not self.eof():
while not self.eof():
line = self.lookahead()
if self.eof():
return
mo = self.section_re.match(line)
if mo:
break
self.consume()
line = self.consume()
self.parse_section(line)
print
def parse_section(self, name):
if name == 'Name Strings':
self.parse_strings()
if name == 'New Procedures and Functions':
self.parse_procs()
def parse_strings(self):
while not self.eof():
line = self.lookahead()
if not line.strip():
self.consume()
continue
if not line.startswith(' '):
break
self.consume()
name = line.strip()
print ' # %s' % name
def parse_procs(self):
lines = []
while not self.eof():
line = self.lookahead()
if not line.strip():
self.consume()
continue
if not line.startswith(' '):
break
self.consume()
lines.append(line.strip())
if line[-1] in (';', ')'):
prototype = ' '.join(lines)
self.parse_proc(prototype)
lines = []
token_re = re.compile(r'(\w+|\s+|.)')
get_function_re = re.compile(r'^Get[A-Z]\w+')
def parse_proc(self, prototype):
#print prototype
tokens = self.token_re.split(prototype)
self.tokens = [token for token in tokens if token.strip()]
#print self.tokens
ret = self.parse_type()
name = self.tokens.pop(0)
extra = ''
if self.get_function_re.match(name):
extra += ', sideeffects=False'
name = self.prefix + name
assert self.tokens.pop(0) == '('
args = []
while self.tokens[0] != ')':
arg = self.parse_arg()
args.append(arg)
if self.tokens[0] == ',':
self.tokens.pop(0)
print ' GlFunction(%s, "%s", [%s]%s),' % (ret, name, ', '.join(args), extra)
def parse_arg(self):
type = self.parse_type()
name = self.tokens.pop(0)
return '(%s, "%s")' % (type, name)
def parse_type(self):
token = self.tokens.pop(0)
if token == 'const':
return 'Const(%s)' % self.parse_type()
if token == 'void':
type = 'Void'
else:
type = self.prefix.upper() + token
while self.tokens[0] == '*':
type = 'OpaquePointer(%s)' % type
self.tokens.pop(0)
return type
def main():
optparser = optparse.OptionParser(
usage="\n\t%prog [options] [SPEC] ")
optparser.add_option(
'-p', '--prefix', metavar='STRING',
type="string", dest="prefix", default='gl',
help="function prefix [default: %default]")
(options, args) = optparser.parse_args(sys.argv[1:])
for arg in args:
if arg.startswith('http://'):
stream = urlopen(arg, 'rt')
else:
stream = open(arg, 'rt')
parser = TxtParser(stream, prefix = options.prefix)
parser.parse()
if __name__ == '__main__':
main()
|