summaryrefslogtreecommitdiffstats
path: root/Tools/scripts/h2py.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1992-03-02 16:20:32 (GMT)
committerGuido van Rossum <guido@python.org>1992-03-02 16:20:32 (GMT)
commit2ba9f30489326dcaea8e0c1fdc395805fb618c97 (patch)
treed360c7225310833094e7dc40be5289f9d95ea3e9 /Tools/scripts/h2py.py
parentbecdad3d5e512b08e0dd5994ac7c7ec3e6f84ebc (diff)
downloadcpython-2ba9f30489326dcaea8e0c1fdc395805fb618c97.zip
cpython-2ba9f30489326dcaea8e0c1fdc395805fb618c97.tar.gz
cpython-2ba9f30489326dcaea8e0c1fdc395805fb618c97.tar.bz2
Initial revision
Diffstat (limited to 'Tools/scripts/h2py.py')
-rwxr-xr-xTools/scripts/h2py.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/Tools/scripts/h2py.py b/Tools/scripts/h2py.py
new file mode 100755
index 0000000..ff2afc2
--- /dev/null
+++ b/Tools/scripts/h2py.py
@@ -0,0 +1,50 @@
+#! /ufs/guido/bin/sgi/python
+#! /usr/local/python
+
+# Read #define's from stdin and translate to Python code on stdout.
+# Very primitive: non-#define's are ignored, no check for valid Python
+# syntax is made -- you will have to edit the output in most cases.
+
+# XXX To do:
+# - accept filename arguments
+# - turn trailing C comments into Python comments
+# - turn C string quotes into Python comments
+# - turn C Boolean operators "&& || !" into Python "and or not"
+# - what to do about #if(def)?
+# - what to do about macros with parameters?
+# - reject definitions with semicolons in them
+
+import sys, regex, string
+
+p_define = regex.compile('^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
+
+p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\*+/')
+
+def main():
+ process(sys.stdin)
+
+def process(fp):
+ lineno = 0
+ while 1:
+ line = fp.readline()
+ if not line: break
+ lineno = lineno + 1
+ if p_define.match(line) >= 0:
+ # gobble up continuation lines
+ while line[-2:] == '\\\n':
+ nextline = fp.readline()
+ if not nextline: break
+ lineno = lineno + 1
+ line = line + nextline
+ regs = p_define.regs
+ a, b = regs[1] # where the macro name is
+ name = line[a:b]
+ a, b = regs[0] # the whole match
+ body = line[b:]
+ # replace comments by spaces
+ while p_comment.search(body) >= 0:
+ a, b = p_comment.regs[0]
+ body = body[:a] + ' ' + body[b:]
+ print name, '=', string.strip(body)
+
+main()