home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Mac / Tools / IDE / PyFontify.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  4.1 KB  |  129 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. """Module to analyze Python source code; for syntax coloring tools.
  5.  
  6. Interface:
  7. \ttags = fontify(pytext, searchfrom, searchto)
  8.  
  9. The 'pytext' argument is a string containing Python source code.
  10. The (optional) arguments 'searchfrom' and 'searchto' may contain a slice in pytext. 
  11. The returned value is a list of tuples, formatted like this:
  12. \t[('keyword', 0, 6, None), ('keyword', 11, 17, None), ('comment', 23, 53, None), etc. ]
  13. The tuple contents are always like this:
  14. \t(tag, startindex, endindex, sublist)
  15. tag is one of 'keyword', 'string', 'comment' or 'identifier'
  16. sublist is not used, hence always None. 
  17. """
  18. __version__ = '0.3.1'
  19. import string
  20. import regex
  21. import string
  22.  
  23. def replace(where, what, with):
  24.     return string.join(string.split(where, what), with)
  25.  
  26. keywordsList = [
  27.     'assert',
  28.     'del',
  29.     'from',
  30.     'lambda',
  31.     'return',
  32.     'and',
  33.     'elif',
  34.     'global',
  35.     'not',
  36.     'try',
  37.     'break',
  38.     'else',
  39.     'if',
  40.     'or',
  41.     'while',
  42.     'class',
  43.     'except',
  44.     'import',
  45.     'pass',
  46.     'continue',
  47.     'finally',
  48.     'in',
  49.     'print',
  50.     'def',
  51.     'for',
  52.     'is',
  53.     'raise']
  54. commentPat = '#.*'
  55. pat = 'q[^\\q\n]*\\(\\\\[\x00-\xff][^\\q\n]*\\)*q'
  56. quotePat = replace(pat, 'q', "'") + '\\|' + replace(pat, 'q', '"')
  57. pat = '\n\tqqq\n\t[^\\q]*\n\t\\(\n\t\t\\(\t\\\\[\x00-\xff]\n\t\t\\|\tq\n\t\t\t\\(\t\\\\[\x00-\xff]\n\t\t\t\\|\t[^\\q]\n\t\t\t\\|\tq\n\t\t\t\t\\(\t\\\\[\x00-\xff]\n\t\t\t\t\\|\t[^\\q]\n\t\t\t\t\\)\n\t\t\t\\)\n\t\t\\)\n\t\t[^\\q]*\n\t\\)*\n\tqqq\n'
  58. pat = string.join(string.split(pat), '')
  59. tripleQuotePat = replace(pat, 'q', "'") + '\\|' + replace(pat, 'q', '"')
  60. nonKeyPat = '\\(^\\|[^a-zA-Z0-9_."\']\\)'
  61. keyPat = nonKeyPat + '\\('
  62. for keyword in keywordsList:
  63.     keyPat = keyPat + keyword + '\\|'
  64.  
  65. keyPat = keyPat[:-2] + '\\)' + nonKeyPat
  66. matchPat = keyPat + '\\|' + commentPat + '\\|' + tripleQuotePat + '\\|' + quotePat
  67. matchRE = regex.compile(matchPat)
  68. idKeyPat = '[ \t]*[A-Za-z_][A-Za-z_0-9.]*'
  69. idRE = regex.compile(idKeyPat)
  70.  
  71. def fontify(pytext, searchfrom = 0, searchto = None):
  72.     if searchto is None:
  73.         searchto = len(pytext)
  74.     
  75.     search = matchRE.search
  76.     group = matchRE.group
  77.     idSearch = idRE.search
  78.     idGroup = idRE.group
  79.     tags = []
  80.     tags_append = tags.append
  81.     commentTag = 'comment'
  82.     stringTag = 'string'
  83.     keywordTag = 'keyword'
  84.     identifierTag = 'identifier'
  85.     start = 0
  86.     end = searchfrom
  87.     while 1:
  88.         start = search(pytext, end)
  89.         if start < 0 or start >= searchto:
  90.             break
  91.         
  92.         match = group(0)
  93.         end = start + len(match)
  94.         c = match[0]
  95.         if c not in '#\'"':
  96.             if start != searchfrom:
  97.                 match = match[1:-1]
  98.                 start = start + 1
  99.             else:
  100.                 match = match[:-1]
  101.             end = end - 1
  102.             tags_append((keywordTag, start, end, None))
  103.             if match in [
  104.                 'def',
  105.                 'class']:
  106.                 start = idSearch(pytext, end)
  107.                 if start == end:
  108.                     match = idGroup(0)
  109.                     end = start + len(match)
  110.                     tags_append((identifierTag, start, end, None))
  111.                 
  112.             
  113.         elif c == '#':
  114.             tags_append((commentTag, start, end, None))
  115.         else:
  116.             tags_append((stringTag, start, end, None))
  117.     return tags
  118.  
  119.  
  120. def test(path):
  121.     f = open(path)
  122.     text = f.read()
  123.     f.close()
  124.     tags = fontify(text)
  125.     for tag, start, end, sublist in tags:
  126.         print tag, `text[start:end]`
  127.     
  128.  
  129.