]> git.cworth.org Git - apitrace/blob - specs/scripts/gltxt.py
Move all API specs to a separate dir.
[apitrace] / specs / scripts / gltxt.py
1 #!/usr/bin/env python
2 ##########################################################################
3 #
4 # Copyright 2010 VMware, Inc.
5 # All Rights Reserved.
6 #
7 # Permission is hereby granted, free of charge, to any person obtaining a copy
8 # of this software and associated documentation files (the "Software"), to deal
9 # in the Software without restriction, including without limitation the rights
10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 # copies of the Software, and to permit persons to whom the Software is
12 # furnished to do so, subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be included in
15 # all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 # THE SOFTWARE.
24 #
25 ##########################################################################/
26
27
28 import sys
29 import re
30 import optparse
31
32
33 def stderr(x):
34     sys.stderr.write(str(x) + '\n')
35
36
37 class Parser:
38
39     def __init__(self, stream):
40         pass
41
42
43 class LineParser:
44     """Base class for parsers that read line-based formats."""
45
46     def __init__(self, stream):
47         self._stream = stream
48         self._line = None
49         self._eof = False
50         # read lookahead
51         self.readline()
52     
53     def parse(self):
54         raise NotImplementedError
55
56     def readline(self):
57         line = self._stream.readline()
58         if not line:
59             self._line = ''
60             self._eof = True
61         self._line = line.rstrip('\r\n')
62
63     def lookahead(self):
64         assert self._line is not None
65         return self._line
66
67     def consume(self):
68         assert self._line is not None
69         line = self._line
70         self.readline()
71         return line
72
73     def eof(self):
74         assert self._line is not None
75         return self._eof
76     
77     def skip_whitespace(self):
78         while not self.eof() and self.match_whitespace() or self.match_comment():
79             self.consume()
80
81     def match_whitespace(self):
82         line = self.lookahead()
83         return not line.strip()
84
85     def match_comment(self):
86         return False
87
88
89 class TxtParser(LineParser):
90
91     property_re = re.compile(r'^\w+:')
92     prototype_re = re.compile(r'^(\w+)\((.*)\)$')
93
94     def __init__(self, stream, prefix=''):
95         LineParser.__init__(self, stream)
96         self.prefix = prefix
97
98     def parse(self):
99         line = self.consume()
100         while not line.startswith("New Procedures and Functions"):
101             line = self.consume()
102         self.parse_procs()
103
104     def parse_procs(self):
105         lines = []
106         while True:
107             line = self.consume()
108             if not line.strip():
109                 continue
110             if not line.startswith(' '):
111                 break
112             lines.append(line.strip())
113             if line.endswith(';'):
114                 self.parse_proc(' '.join(lines))
115                 lines = []
116
117     token_re = re.compile(r'(\w+|\s+|.)')
118     get_function_re = re.compile(r'^Get[A-Z]\w+')
119
120     def parse_proc(self, prototype):
121         #print prototype
122         tokens = self.token_re.split(prototype)
123         self.tokens = [token for token in tokens if token.strip()]
124         #print self.tokens
125
126         ret = self.parse_type()
127
128         name = self.tokens.pop(0)
129         extra = ''
130         if self.get_function_re.match(name):
131             extra += ', sideeffects=False'
132         name = self.prefix + name
133
134         assert self.tokens.pop(0) == '('
135         args = []
136         while self.tokens[0] != ')':
137             arg = self.parse_arg()
138             args.append(arg)
139             if self.tokens[0] == ',':
140                 self.tokens.pop(0)
141         print '    GlFunction(%s, "%s", [%s]%s),' % (ret, name, ', '.join(args), extra)
142
143     def parse_arg(self):
144         type = self.parse_type()
145         name = self.tokens.pop(0)
146         return '(%s, "%s")' % (type, name)
147
148     def parse_type(self):
149         token = self.tokens.pop(0)
150         if token == 'const':
151             return 'Const(%s)' % self.parse_type()
152         if token == 'void':
153             type = 'Void'
154         else:
155             type = 'GL' + token
156         while self.tokens[0] == '*':
157             type = 'OpaquePointer(%s)' % type
158             self.tokens.pop(0)
159         return type
160
161
162 def main():
163     for arg in sys.argv[1:]:
164         parser = TxtParser(open(arg, 'rt'), prefix='gl')
165         parser.parse()
166     
167
168 if __name__ == '__main__':
169     main()