home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Demo / scripts / h2py.py < prev    next >
Text File  |  1994-05-03  |  2KB  |  79 lines

  1. #! /usr/local/bin/python
  2.  
  3. # Read #define's from stdin and translate to Python code on stdout.
  4. # Very primitive: non-#define's are ignored.
  5. # You will have to edit the output in some cases.
  6. # If one or more filenames are given, output is written to corresponding
  7. # filenames in the local directory, translated to all uppercase, with
  8. # the extension replaced by ".py".
  9.  
  10. # XXX To do:
  11. # - turn trailing C comments into Python comments
  12. # - turn C string quotes into Python comments
  13. # - turn C Boolean operators "&& || !" into Python "and or not"
  14. # - what to do about #if(def)?
  15. # - what to do about macros with parameters?
  16. # - reject definitions with semicolons in them
  17.  
  18. import sys, regex, string, getopt, os
  19.  
  20. p_define = regex.compile('^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
  21.  
  22. p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?')
  23.  
  24. def main():
  25.     opts, args = getopt.getopt(sys.argv[1:], '')
  26.     if not args:
  27.         args = ['-']
  28.     for filename in args:
  29.         if filename == '-':
  30.             sys.stdout.write('# Generated by h2py from stdin\n')
  31.             process(sys.stdin, sys.stdout)
  32.         else:
  33.             fp = open(filename, 'r')
  34.             outfile = os.path.basename(filename)
  35.             i = string.rfind(outfile, '.')
  36.             if i > 0: outfile = outfile[:i]
  37.             outfile = string.upper(outfile)
  38.             outfile = outfile + '.py'
  39.             outfp = open(outfile, 'w')
  40.             outfp.write('# Generated by h2py from %s\n' % filename)
  41.             process(fp, outfp)
  42.             outfp.close()
  43.             fp.close()
  44.  
  45. def process(fp, outfp):
  46.     env = {}
  47.     lineno = 0
  48.     while 1:
  49.         line = fp.readline()
  50.         if not line: break
  51.         lineno = lineno + 1
  52.         if p_define.match(line) >= 0:
  53.             # gobble up continuation lines
  54.             while line[-2:] == '\\\n':
  55.                 nextline = fp.readline()
  56.                 if not nextline: break
  57.                 lineno = lineno + 1
  58.                 line = line + nextline
  59.             regs = p_define.regs
  60.             a, b = regs[1] # where the macro name is
  61.             name = line[a:b]
  62.             a, b = regs[0] # the whole match
  63.             body = line[b:]
  64.             # replace comments by spaces
  65.             while p_comment.search(body) >= 0:
  66.                 a, b = p_comment.regs[0]
  67.                 body = body[:a] + ' ' + body[b:]
  68.             stmt = '%s = %s\n' % (name, string.strip(body))
  69.             ok = 0
  70.             try:
  71.                 exec stmt in env
  72.                 ok = 1
  73.             except:
  74.                 sys.stderr.write('Skipping: %s' % stmt)
  75.             if ok:
  76.                 outfp.write(stmt)
  77.  
  78. main()
  79.