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
# 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.
# 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*", ""),
),
)
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()