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