home *** CD-ROM | disk | FTP | other *** search
/ com!online 2005 April / com_0405_1.iso / opensource / BTpp-0.5.4-bin.exe / $INSTDIR / BT++.exe / tokenize.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-19  |  8.7 KB  |  243 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.2)
  3.  
  4. from __future__ import generators
  5. __author__ = 'Ka-Ping Yee <ping@lfw.org>'
  6. __credits__ = 'GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro'
  7. import string
  8. import re
  9. from token import *
  10. import token
  11. __all__ = None if x[0] != '_' else [] + [
  12.     'COMMENT',
  13.     'tokenize',
  14.     'NL']
  15. del token
  16. COMMENT = N_TOKENS
  17. tok_name[COMMENT] = 'COMMENT'
  18. NL = N_TOKENS + 1
  19. tok_name[NL] = 'NL'
  20. N_TOKENS += 2
  21.  
  22. def group(*choices):
  23.     return '(' + '|'.join(choices) + ')'
  24.  
  25.  
  26. def any(*choices):
  27.     return apply(group, choices) + '*'
  28.  
  29.  
  30. def maybe(*choices):
  31.     return apply(group, choices) + '?'
  32.  
  33. Whitespace = '[ \\f\\t]*'
  34. Comment = '#[^\\r\\n]*'
  35. Ignore = Whitespace + any('\\\\\\r?\\n' + Whitespace) + maybe(Comment)
  36. Name = '[a-zA-Z_]\\w*'
  37. Hexnumber = '0[xX][\\da-fA-F]*[lL]?'
  38. Octnumber = '0[0-7]*[lL]?'
  39. Decnumber = '[1-9]\\d*[lL]?'
  40. Intnumber = group(Hexnumber, Octnumber, Decnumber)
  41. Exponent = '[eE][-+]?\\d+'
  42. Pointfloat = group('\\d+\\.\\d*', '\\.\\d+') + maybe(Exponent)
  43. Expfloat = '\\d+' + Exponent
  44. Floatnumber = group(Pointfloat, Expfloat)
  45. Imagnumber = group('\\d+[jJ]', Floatnumber + '[jJ]')
  46. Number = group(Imagnumber, Floatnumber, Intnumber)
  47. Single = "[^'\\\\]*(?:\\\\.[^'\\\\]*)*'"
  48. Double = '[^"\\\\]*(?:\\\\.[^"\\\\]*)*"'
  49. Single3 = "[^'\\\\]*(?:(?:\\\\.|'(?!''))[^'\\\\]*)*'''"
  50. Double3 = '[^"\\\\]*(?:(?:\\\\.|"(?!""))[^"\\\\]*)*"""'
  51. Triple = group("[uU]?[rR]?'''", '[uU]?[rR]?"""')
  52. String = group("[uU]?[rR]?'[^\\n'\\\\]*(?:\\\\.[^\\n'\\\\]*)*'", '[uU]?[rR]?"[^\\n"\\\\]*(?:\\\\.[^\\n"\\\\]*)*"')
  53. Operator = group('\\*\\*=?', '>>=?', '<<=?', '<>', '!=', '//=?', '[+\\-*/%&|^=<>]=?', '~')
  54. Bracket = '[][(){}]'
  55. Special = group('\\r?\\n', '[:;.,`]')
  56. Funny = group(Operator, Bracket, Special)
  57. PlainToken = group(Number, Funny, String, Name)
  58. Token = Ignore + PlainToken
  59. ContStr = group("[uU]?[rR]?'[^\\n'\\\\]*(?:\\\\.[^\\n'\\\\]*)*" + group("'", '\\\\\\r?\\n'), '[uU]?[rR]?"[^\\n"\\\\]*(?:\\\\.[^\\n"\\\\]*)*' + group('"', '\\\\\\r?\\n'))
  60. PseudoExtras = group('\\\\\\r?\\n', Comment, Triple)
  61. PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name)
  62. (tokenprog, pseudoprog, single3prog, double3prog) = map(re.compile, (Token, PseudoToken, Single3, Double3))
  63. endprogs = {
  64.     "'": re.compile(Single),
  65.     '"': re.compile(Double),
  66.     "'''": single3prog,
  67.     '"""': double3prog,
  68.     "r'''": single3prog,
  69.     'r"""': double3prog,
  70.     "u'''": single3prog,
  71.     'u"""': double3prog,
  72.     "ur'''": single3prog,
  73.     'ur"""': double3prog,
  74.     "R'''": single3prog,
  75.     'R"""': double3prog,
  76.     "U'''": single3prog,
  77.     'U"""': double3prog,
  78.     "uR'''": single3prog,
  79.     'uR"""': double3prog,
  80.     "Ur'''": single3prog,
  81.     'Ur"""': double3prog,
  82.     "UR'''": single3prog,
  83.     'UR"""': double3prog,
  84.     'r': None,
  85.     'R': None,
  86.     'u': None,
  87.     'U': None }
  88. tabsize = 8
  89.  
  90. class TokenError(Exception):
  91.     pass
  92.  
  93.  
  94. class StopTokenizing(Exception):
  95.     pass
  96.  
  97.  
  98. def printtoken(type, token, .4, .6, line):
  99.     (srow, scol) = .4
  100.     (erow, ecol) = .6
  101.     print '%d,%d-%d,%d:\t%s\t%s' % (srow, scol, erow, ecol, tok_name[type], repr(token))
  102.  
  103.  
  104. def tokenize(readline, tokeneater = printtoken):
  105.     
  106.     try:
  107.         tokenize_loop(readline, tokeneater)
  108.     except StopTokenizing:
  109.         pass
  110.  
  111.  
  112.  
  113. def tokenize_loop(readline, tokeneater):
  114.     for token_info in generate_tokens(readline):
  115.         apply(tokeneater, token_info)
  116.     
  117.  
  118.  
  119. def generate_tokens(readline):
  120.     lnum = parenlev = continued = 0
  121.     (namechars, numchars) = (string.ascii_letters + '_', '0123456789')
  122.     (contstr, needcont) = ('', 0)
  123.     contline = None
  124.     indents = [
  125.         0]
  126.     while 1:
  127.         line = readline()
  128.         lnum = lnum + 1
  129.         (pos, max) = (0, len(line))
  130.         if contstr:
  131.             if not line:
  132.                 raise TokenError, ('EOF in multi-line string', strstart)
  133.             
  134.             endmatch = endprog.match(line)
  135.             if endmatch:
  136.                 pos = end = endmatch.end(0)
  137.                 yield (STRING, contstr + line[:end], strstart, (lnum, end), contline + line)
  138.                 (contstr, needcont) = ('', 0)
  139.                 contline = None
  140.             elif needcont and line[-2:] != '\\\n' and line[-3:] != '\\\r\n':
  141.                 yield (ERRORTOKEN, contstr + line, strstart, (lnum, len(line)), contline)
  142.                 contstr = ''
  143.                 contline = None
  144.                 continue
  145.             else:
  146.                 contstr = contstr + line
  147.                 contline = contline + line
  148.         elif parenlev == 0 and not continued:
  149.             if not line:
  150.                 break
  151.             
  152.             column = 0
  153.             while pos < max:
  154.                 if line[pos] == ' ':
  155.                     column = column + 1
  156.                 elif line[pos] == '\t':
  157.                     column = (column / tabsize + 1) * tabsize
  158.                 elif line[pos] == '\x0c':
  159.                     column = 0
  160.                 else:
  161.                     break
  162.                 pos = pos + 1
  163.             if pos == max:
  164.                 break
  165.             
  166.             if line[pos] in '#\r\n':
  167.                 yield ((NL, COMMENT)[line[pos] == '#'], line[pos:], (lnum, pos), (lnum, len(line)), line)
  168.                 continue
  169.             
  170.             if column > indents[-1]:
  171.                 indents.append(column)
  172.                 yield (INDENT, line[:pos], (lnum, 0), (lnum, pos), line)
  173.             
  174.             while column < indents[-1]:
  175.                 indents = indents[:-1]
  176.                 yield (DEDENT, '', (lnum, pos), (lnum, pos), line)
  177.         elif not line:
  178.             raise TokenError, ('EOF in multi-line statement', (lnum, 0))
  179.         
  180.         continued = 0
  181.         while pos < max:
  182.             pseudomatch = pseudoprog.match(line, pos)
  183.             if pseudomatch:
  184.                 (start, end) = pseudomatch.span(1)
  185.                 (spos, epos, pos) = ((lnum, start), (lnum, end), end)
  186.                 (token, initial) = (line[start:end], line[start])
  187.                 if initial in numchars and initial == '.' and token != '.':
  188.                     yield (NUMBER, token, spos, epos, line)
  189.                 elif initial in '\r\n':
  190.                     if not parenlev > 0 and NL:
  191.                         pass
  192.                     yield (NEWLINE, token, spos, epos, line)
  193.                 elif initial == '#':
  194.                     yield (COMMENT, token, spos, epos, line)
  195.                 elif token in ("'''", '"""', "r'''", 'r"""', "R'''", 'R"""', "u'''", 'u"""', "U'''", 'U"""', "ur'''", 'ur"""', "Ur'''", 'Ur"""', "uR'''", 'uR"""', "UR'''", 'UR"""'):
  196.                     endprog = endprogs[token]
  197.                     endmatch = endprog.match(line, pos)
  198.                     if endmatch:
  199.                         pos = endmatch.end(0)
  200.                         token = line[start:pos]
  201.                         yield (STRING, token, spos, (lnum, pos), line)
  202.                     else:
  203.                         strstart = (lnum, start)
  204.                         contstr = line[start:]
  205.                         contline = line
  206.                         break
  207.                 elif initial in ("'", '"') and token[:2] in ("r'", 'r"', "R'", 'R"', "u'", 'u"', "U'", 'U"') or token[:3] in ("ur'", 'ur"', "Ur'", 'Ur"', "uR'", 'uR"', "UR'", 'UR"'):
  208.                     if token[-1] == '\n':
  209.                         strstart = (lnum, start)
  210.                         if not endprogs[initial] and endprogs[token[1]]:
  211.                             pass
  212.                         endprog = endprogs[token[2]]
  213.                         (contstr, needcont) = (line[start:], 1)
  214.                         contline = line
  215.                         break
  216.                     else:
  217.                         yield (STRING, token, spos, epos, line)
  218.                 elif initial in namechars:
  219.                     yield (NAME, token, spos, epos, line)
  220.                 elif initial == '\\':
  221.                     continued = 1
  222.                 elif initial in '([{':
  223.                     parenlev = parenlev + 1
  224.                 elif initial in ')]}':
  225.                     parenlev = parenlev - 1
  226.                 
  227.                 yield (OP, token, spos, epos, line)
  228.             else:
  229.                 yield (ERRORTOKEN, line[pos], (lnum, pos), (lnum, pos + 1), line)
  230.                 pos = pos + 1
  231.     for indent in indents[1:]:
  232.         yield (DEDENT, '', (lnum, 0), (lnum, 0), '')
  233.     
  234.     yield (ENDMARKER, '', (lnum, 0), (lnum, 0), '')
  235.  
  236. if __name__ == '__main__':
  237.     import sys
  238.     if len(sys.argv) > 1:
  239.         tokenize(open(sys.argv[1]).readline)
  240.     else:
  241.         tokenize(sys.stdin.readline)
  242.  
  243.