Guido van Rossum | 41ffccb | 1993-04-01 20:50:35 +0000 | [diff] [blame] | 1 | #! /usr/local/bin/python |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 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 | |
| 16 | import sys, regex, string |
| 17 | |
| 18 | p_define = regex.compile('^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+') |
| 19 | |
Guido van Rossum | 047979e | 1992-06-05 15:13:53 +0000 | [diff] [blame] | 20 | p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?') |
Guido van Rossum | 2ba9f30 | 1992-03-02 16:20:32 +0000 | [diff] [blame] | 21 | |
| 22 | def main(): |
| 23 | process(sys.stdin) |
| 24 | |
| 25 | def 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 | |
| 49 | main() |