blob: 2fc26d81f86ffa4534cdc0969edaea48fb36c071 [file] [log] [blame]
Guido van Rossum2ba9f301992-03-02 16:20:32 +00001#! /usr/local/python
2
3# Read #define's from stdin and translate to Python code on stdout.
4# Very primitive: non-#define's are ignored, no check for valid Python
5# syntax is made -- you will have to edit the output in most cases.
6
7# XXX To do:
8# - accept filename arguments
9# - turn trailing C comments into Python comments
10# - turn C string quotes into Python comments
11# - turn C Boolean operators "&& || !" into Python "and or not"
12# - what to do about #if(def)?
13# - what to do about macros with parameters?
14# - reject definitions with semicolons in them
15
16import sys, regex, string
17
18p_define = regex.compile('^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
19
Guido van Rossum047979e1992-06-05 15:13:53 +000020p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?')
Guido van Rossum2ba9f301992-03-02 16:20:32 +000021
22def main():
23 process(sys.stdin)
24
25def process(fp):
26 lineno = 0
27 while 1:
28 line = fp.readline()
29 if not line: break
30 lineno = lineno + 1
31 if p_define.match(line) >= 0:
32 # gobble up continuation lines
33 while line[-2:] == '\\\n':
34 nextline = fp.readline()
35 if not nextline: break
36 lineno = lineno + 1
37 line = line + nextline
38 regs = p_define.regs
39 a, b = regs[1] # where the macro name is
40 name = line[a:b]
41 a, b = regs[0] # the whole match
42 body = line[b:]
43 # replace comments by spaces
44 while p_comment.search(body) >= 0:
45 a, b = p_comment.regs[0]
46 body = body[:a] + ' ' + body[b:]
47 print name, '=', string.strip(body)
48
49main()