# 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.
(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"),
# 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):
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:
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 = []
if debug >= 2:
print "Input is: %s" % `code`
text = ctopy(code)
+ if debug:
+ print "Output:"
sys.stdout.write(text)