home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Demo / scripts / h2py.py < prev    next >
Encoding:
Python Source  |  1994-10-03  |  3.4 KB  |  123 lines  |  [TEXT/R*ch]

  1. #! /usr/local/bin/python
  2.  
  3. # Read #define's and translate to Python code.
  4. # Handle #include statements.
  5. # Handle #define macros with one argument.
  6. # Anything that isn't recognized or doesn't translate into valid
  7. # Python is ignored.
  8.  
  9. # Without filename arguments, acts as a filter.
  10. # If one or more filenames are given, output is written to corresponding
  11. # filenames in the local directory, translated to all uppercase, with
  12. # the extension replaced by ".py".
  13.  
  14. # By passing one or more options of the form "-i regular_expression"
  15. # you can specify additional strings to be ignored.  This is useful
  16. # e.g. to ignore casts to u_long: simply specify "-i '(u_long)'".
  17.  
  18. # XXX To do:
  19. # - turn trailing C comments into Python comments
  20. # - turn C Boolean operators "&& || !" into Python "and or not"
  21. # - what to do about #if(def)?
  22. # - what to do about macros with multiple parameters?
  23.  
  24. import sys, regex, regsub, string, getopt, os
  25.  
  26. p_define = regex.compile('^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)[\t ]+')
  27.  
  28. p_macro = regex.compile(
  29.   '^#[\t ]*define[\t ]+\([a-zA-Z0-9_]+\)(\([_a-zA-Z][_a-zA-Z0-9]*\))[\t ]+')
  30.  
  31. p_include = regex.compile('^#[\t ]*include[\t ]+<\([a-zA-Z0-9_/\.]+\)')
  32.  
  33. p_comment = regex.compile('/\*\([^*]+\|\*+[^/]\)*\(\*+/\)?')
  34.  
  35. ignores = [p_comment]
  36.  
  37. p_char = regex.compile("'\(\\\\.[^\\\\]*\|[^\\\\]\)'")
  38.  
  39. filedict = {}
  40.  
  41. def main():
  42.     opts, args = getopt.getopt(sys.argv[1:], 'i:')
  43.     for o, a in opts:
  44.         if o == '-i':
  45.             ignores.append(regex.compile(a))
  46.     if not args:
  47.         args = ['-']
  48.     for filename in args:
  49.         if filename == '-':
  50.             sys.stdout.write('# Generated by h2py from stdin\n')
  51.             process(sys.stdin, sys.stdout)
  52.         else:
  53.             fp = open(filename, 'r')
  54.             outfile = os.path.basename(filename)
  55.             i = string.rfind(outfile, '.')
  56.             if i > 0: outfile = outfile[:i]
  57.             outfile = string.upper(outfile)
  58.             outfile = outfile + '.py'
  59.             outfp = open(outfile, 'w')
  60.             outfp.write('# Generated by h2py from %s\n' % filename)
  61.             filedict = {}
  62.             if filename[:13] == '/usr/include/':
  63.                 filedict[filename[13:]] = None
  64.             process(fp, outfp)
  65.             outfp.close()
  66.             fp.close()
  67.  
  68. def process(fp, outfp, env = {}):
  69.     lineno = 0
  70.     while 1:
  71.         line = fp.readline()
  72.         if not line: break
  73.         lineno = lineno + 1
  74.         n = p_define.match(line)
  75.         if n >= 0:
  76.             # gobble up continuation lines
  77.             while line[-2:] == '\\\n':
  78.                 nextline = fp.readline()
  79.                 if not nextline: break
  80.                 lineno = lineno + 1
  81.                 line = line + nextline
  82.             name = p_define.group(1)
  83.             body = line[n:]
  84.             # replace ignored patterns by spaces
  85.             for p in ignores:
  86.                 body = regsub.gsub(p, ' ', body)
  87.             # replace char literals by ord(...)
  88.             body = regsub.gsub(p_char, 'ord(\\0)', body)
  89.             stmt = '%s = %s\n' % (name, string.strip(body))
  90.             ok = 0
  91.             try:
  92.                 exec stmt in env
  93.             except:
  94.                 sys.stderr.write('Skipping: %s' % stmt)
  95.             else:
  96.                 outfp.write(stmt)
  97.         n =p_macro.match(line)
  98.         if n >= 0:
  99.             macro, arg = p_macro.group(1, 2)
  100.             body = line[n:]
  101.             for p in ignores:
  102.                 body = regsub.gsub(p, ' ', body)
  103.             body = regsub.gsub(p_char, 'ord(\\0)', body)
  104.             stmt = 'def %s(%s): return %s\n' % (macro, arg, body)
  105.             try:
  106.                 exec stmt in env
  107.             except:
  108.                 sys.stderr.write('Skipping: %s' % stmt)
  109.             else:
  110.                 outfp.write(stmt)
  111.         if p_include.match(line) >= 0:
  112.             regs = p_include.regs
  113.             a, b = regs[1]
  114.             filename = line[a:b]
  115.             if not filedict.has_key(filename):
  116.                 filedict[filename] = None
  117.                 outfp.write(
  118.                     '\n# Included from %s\n' % filename)
  119.                 inclfp = open('/usr/include/' + filename, 'r')
  120.                 process(inclfp, outfp, env)
  121. main()
  122.  
  123.