]> thyrsus.com Git - ctopy.git/commitdiff
Halfway to class processing. Doesn't work, but doesn't break any
authorEric S. Raymond <esr@thyrsus.com>
Mon, 2 Oct 2006 01:33:39 +0000 (01:33 +0000)
committerEric S. Raymond <esr@thyrsus.com>
Mon, 2 Oct 2006 01:33:39 +0000 (01:33 +0000)
existing rewrites either.  Tinkering with the class regexps ought
to get us the rest of the way.

ctopy

diff --git a/ctopy b/ctopy
index 55f51d8412e84549a9d2cf3492ad72c1073dd72a..30ce14743d1ba9c3449899202e743bdc3606f041 100755 (executable)
--- a/ctopy
+++ b/ctopy
@@ -24,7 +24,8 @@ import sys, re
 shorthands = {
     'id' : r"[a-zA-Z_][a-zA-Z0-9._]*",
     'exp' : r"[a-zA-Z0-9._\-+\*/]+",
-    'type' : r"void|int|bool|char|short|double|long|float|time_t|FILE",
+    'type' : r"\bvoid\b|\bint\b|\bbool\b|\bchar\b|\bshort\b|\bdouble\b|\blong\b|\bfloat\b|\btime_t\b|\bFILE\b",
+    'cls' : "\b__dummyclass__\b",
 }
 
 # These have to be applied to the entire file without breaking it into regions
@@ -44,21 +45,27 @@ repeat_transformations = (
     # simplifying both function formal parameter lists and variable
     # declaration lines.  We'll get rid of the remaining type stuff
     # in a later pass.
-    (r"(%(type)s)\s*\*", r"\1"),                       # Pointer type decls
-    (r"(%(type)s)\s*\((%(id)s)\)", r"\1 \2"),  # Function pointer wrappers
-    (r"(%(type)s)\s*(%(id)s)\[\]", r"\1 \2"),  # Array declarators
+    (r"(%(type)s|%(cls)s)\s*\*", r"\1"),       # Pointer type decls
+    (r"(%(type)s|%(cls)s)\s*\((%(id)s)\)\(\)", r"\1 \2"),      # Function pointer wrappers
+    (r"(%(type)s|%(cls)s)\s*(%(id)s)\[\]", r"\1 \2"),  # Array declarators
     ),
     (
     # What we're doing here is trying to get rid of variable
     # declarations that don't have initializer parts.
     # First, bare ids between a comma and a trailing semicolon go away.
     (r"(\n[ \t]*)((?:%(type)s).*)(,\s*%(id)s\s*);", r"\1\2;"),
-    # Next, bare ids between commas in a declaration line go away.
+    # But bare classes between comma and semicolon get a following initializer 
+    (r"(\n[ \t]*)(%(cls)s)(.*,\s)*(%(id)s)(\s*;)", r"\1\2\3\4 = \2()\5;"),
+    # Bare ids between commas in a declaration line go away.
     (r"(\n[ \t]*(?:%(type)s)\s+.*,)(%(id)s),(.*);", r"\1\3"),
-    # Next, bare ids just after the type and before a comma go away.
+    # But bare class names between commas get an initializer
+    (r"(\n[ \t]*)(%(cls)s)(\s+.*,)(%(id)s),(.*);", r"\1\2\3\4 =\2(),\5"),
+    # Bare ids just after the type and before a comma go away.
     (r"(\n[ \t]*(?:%(type)s))(\s*%(id)s),(.*);", r"\1\3"),
+    # Bare class names after the type and before comma get an initializer.
+    (r"(\n[ \t]*)(%(cls)s)(\s*%(id)s),(.*);", r"\1\2\3 = \2(),\4"),
     # Any declaration line not containing an initializer goes away.
-    (r"\n[ \t]*(?:%(type)s)[^=;]*;", ""),
+    (r"\n[ \t]*(?:%(type)s|%(cls)s)[^=;]*;", ""),
     ),
     (
     # At this point in the game all declarations left have initializers.
@@ -66,14 +73,14 @@ repeat_transformations = (
     # barf later because it thinks that, e.g., a=4, b=5  is an attempt to
     # assign a constant to a tuple.
     # FIXME: This will fail messily on GCC structure intializers.
-    (r"(\n[ \t]*(?:%(type)s).*),(.*);", r"\1;\2"),
+    (r"(\n[ \t]*(?:%(type)s|%(cls)s).*),(.*);", r"\1;\2"),
     ),
     (
     # Now rip out all remaining base type information.
     # This will strip remaining type declarators out of formal parameter lists.
     # It will also remove types from the beginning of declaration lines.
     # Won't nuke casts because it looks for at least one following whitespace.
-    (r"(?:%(type)s)\s\s*", ""),
+    (r"(?:%(type)s|%(cls)s)\s\s*", ""),
     ),
 )
 
@@ -293,15 +300,19 @@ def ctopy(input):
     if importlist:
         importlist = "import " + ",".join(importlist) + "\n"
         text = importlist + text
-    # Emit what we got.  Preverve imports in case this is a .h file 
+    # Emit what we got.  Preserve imports in case this is a .h file 
     return text
 
 if __name__ == "__main__":
     import getopt
-    (options, arguments) = getopt.getopt(sys.argv[1:], "d")
+    (options, arguments) = getopt.getopt(sys.argv[1:], "c:d")
     debug = 0;
+    classes = ["__dummyclass__"]
     for (switch, val) in options:
-       if (switch == '-d'):
+       if (switch == '-c'):
+           classes.append(val)
+            shorthands["cls"] = "|".join(filter(lambda x: r"\b"+x+r"\b", classes))
+       elif (switch == '-d'):
            debug += 1
 
     code = sys.stdin.read()