]> git.cworth.org Git - apitrace/blobdiff - specs/scripts/gltxt.py
Minor cleanups and better documentation for the spec generation scripts.
[apitrace] / specs / scripts / gltxt.py
index 7fc62f557f0823c2ee6949c58943a79bc11af478..4ce92320114b1d52ba101739310a78baf7837b40 100755 (executable)
@@ -31,6 +31,7 @@
 import sys
 import re
 import optparse
+from urllib2 import urlopen
 
 
 def stderr(x):
@@ -91,6 +92,8 @@ class LineParser:
 
 class TxtParser(LineParser):
 
+    section_re = re.compile(r'^([A-Z]\w+)( \w+)*$')
+
     property_re = re.compile(r'^\w+:')
     prototype_re = re.compile(r'^(\w+)\((.*)\)$')
 
@@ -99,22 +102,51 @@ class TxtParser(LineParser):
         self.prefix = prefix
 
     def parse(self):
-        line = self.consume()
-        while not line.startswith("New Procedures and Functions"):
+        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_procs()
+            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 True:
-            line = self.consume()
+        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.endswith(';'):
-                self.parse_proc(' '.join(lines))
+            if line[-1] in (';', ')'):
+                prototype = ' '.join(lines)
+                self.parse_proc(prototype)
                 lines = []
 
     token_re = re.compile(r'(\w+|\s+|.)')
@@ -163,8 +195,21 @@ class TxtParser(LineParser):
 
 
 def main():
-    for arg in sys.argv[1:]:
-        parser = TxtParser(open(arg, 'rt'), prefix='gl')
+    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()