]> thyrsus.com Git - ctopy.git/commitdiff
Enumeration processing is almost done -- now all we have to do is
authorEric S. Raymond <esr@thyrsus.com>
Mon, 2 Oct 2006 13:50:01 +0000 (13:50 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Mon, 2 Oct 2006 13:50:01 +0000 (13:50 +0000)
renumber,

ctopy

diff --git a/ctopy b/ctopy
index 185d123ed6c7c2e69742a54c4a379acca5763c8e..02f6744b17bc058ff5d79ba43b47572d630b20f7 100755 (executable)
--- a/ctopy
+++ b/ctopy
@@ -33,6 +33,8 @@ file_transformations = (
     # Simple initializers to tuples
     (r"(?:static)?\s+(?:%(type)s)\s+(%(id)s)\[[a-zA-Z0-9._]*]\s*=\s*{([^}]+)}",
      r"\1 = (\2)"),
+    (r"enum\s+(%(id)s\s*){\s*",  r"enum \1{\n"),
+    (r"enum\s+{\s*",  r"enum {\n"),
     )
 
 # These need to be applied repeatedly within regions.
@@ -167,6 +169,9 @@ post_transformations = (
     (r"#include \<time.h\>\n", r"import time\n"),
     (r"#include \<regex.h\>\n", r"import re\n"),
     (r"#include \<curses.h\>\n", r"import curses\n"),
+)
+
+final_transformations = (
     # block delimiters -- do these as late as possible, they're useful
     (r";\n", r"\n"),
     (r"\n[ \t]*}\n", r"\n"),
@@ -181,7 +186,7 @@ def single_apply(transforms, text):
         # be used to recognize start of line.
         text = re.sub(fro % shorthands, to, "\n" + oldtext)[1:]
         if debug >= 2 and text != oldtext:
-            print "%s transforms %s to %s: groups" % ((fro, to), oldtext, text)
+            print "%s transforms '%s' to '%s'" % ((fro, to), oldtext, text)
     return text
 
 def repeat_apply(transforms, text):
@@ -222,6 +227,17 @@ def ctopy(input):
                     print "Open comment starts at %d" % i
                 boundaries.append(i)
                 state = "opencomment"
+            elif input[i:].startswith("typedef enum ") or input[i:].startswith("enum "):
+                if debug >= 2:
+                    print "enumeration starts at %d" % i
+                if input[i-2:i+1] != 'f e':
+                    boundaries.append(i)
+                boundaries.append(i + input[i:].find("enum") + 5)
+            elif input[i:].startswith("\n}"):
+                if debug >= 2:
+                    print "start-of-line brace at %d" % i
+                boundaries.append(i)
+                boundaries.append(i+2)
         elif state == "stringliteral":
             if input[i] == '"' and (i == 0 or input[i-1] != '\\'):
                 if debug >= 2:
@@ -283,6 +299,44 @@ def ctopy(input):
     except IndexError:
         sys.stderr.write("ctopy: pathological string literal at %d.\n" % boundaries[i])
         raise SystemExit, 1
+    if debug:
+        "Enumeration processing"
+    state = "plain"
+    for (i, region) in enumerate(regions):
+        # first compute a parse state
+        if region.startswith("typedef enum"):
+            state = "typedef"
+        elif region.startswith("enum"):
+            state = "enum"
+        elif region == "\n}":
+            if state in ("typedef", "enum"):
+                regions[i] = ''
+                state = "plain"
+        # now do something useful with it
+        if debug >= 2:
+            print "Enumeration processing: state = %s, region = %s" % (state, `regions[i]`)
+        if state in ("enum", "typedef"):
+            if regions[i] == "enum ":
+                m = re.match("(%(id)s) {" % shorthands, regions[i+1])
+                if m:
+                    shorthands['type'] += "|" + m.group(1)
+                    regions[i] = '# From enumerated type %s\n' % m.group(1)
+                    regions[i+1] = regions[i+1][m.end(0)+1:]
+                else: 
+                    regions[i] = '# From anonymous enumerated type\n'
+            elif regions[i] == "typedef enum ":
+                m = re.match("\s* (%(id)s)\s*;?" % shorthands, regions[i+3])
+                if m:
+                    shorthands['type'] += "|" + m.group(1)
+                    regions[i] = '# From enumerated type %s\n' % m.group(1)
+                    regions[i+3] = regions[i+3][:m.start(0)] + "\n\n"
+                else: 
+                    regions[i] = '# From anonymous typedefed enum (?!)\n'
+            else:
+                regions[i] = re.sub(",\s*", "\n", regions[i])
+    if debug:
+        "Final transformations begin"
+    regions = map(lambda r: single_apply(final_transformations, r),regions)
     if debug:
         print "Import checks begin"
     importlist = []
@@ -319,4 +373,6 @@ if __name__ == "__main__":
     if debug >= 2:
         print "Input is: %s" % `code`
     text = ctopy(code)
+    if debug:
+        print "Output:"
     sys.stdout.write(text)