home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / tokenize.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  10.3 KB  |  361 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. __author__ = 'Ka-Ping Yee <ping@lfw.org>'
  5. __credits__ = 'GvR, ESR, Tim Peters, Thomas Wouters, Fred Drake, Skip Montanaro, Raymond Hettinger'
  6. import string
  7. import re
  8. from token import *
  9. import token
  10. __all__ = _[1] + [
  11.     'COMMENT',
  12.     'tokenize',
  13.     'generate_tokens',
  14.     'NL',
  15.     'untokenize']
  16. del x
  17. del token
  18. COMMENT = N_TOKENS
  19. tok_name[COMMENT] = 'COMMENT'
  20. NL = N_TOKENS + 1
  21. tok_name[NL] = 'NL'
  22. N_TOKENS += 2
  23.  
  24. def group(*choices):
  25.     return '(' + '|'.join(choices) + ')'
  26.  
  27.  
  28. def any(*choices):
  29.     return group(*choices) + '*'
  30.  
  31.  
  32. def maybe(*choices):
  33.     return group(*choices) + '?'
  34.  
  35. Whitespace = '[ \\f\\t]*'
  36. Comment = '#[^\\r\\n]*'
  37. Ignore = Whitespace + any('\\\\\\r?\\n' + Whitespace) + maybe(Comment)
  38. Name = '[a-zA-Z_]\\w*'
  39. Hexnumber = '0[xX][\\da-fA-F]+[lL]?'
  40. Octnumber = '(0[oO][0-7]+)|(0[0-7]*)[lL]?'
  41. Binnumber = '0[bB][01]+[lL]?'
  42. Decnumber = '[1-9]\\d*[lL]?'
  43. Intnumber = group(Hexnumber, Binnumber, Octnumber, Decnumber)
  44. Exponent = '[eE][-+]?\\d+'
  45. Pointfloat = group('\\d+\\.\\d*', '\\.\\d+') + maybe(Exponent)
  46. Expfloat = '\\d+' + Exponent
  47. Floatnumber = group(Pointfloat, Expfloat)
  48. Imagnumber = group('\\d+[jJ]', Floatnumber + '[jJ]')
  49. Number = group(Imagnumber, Floatnumber, Intnumber)
  50. Single = "[^'\\\\]*(?:\\\\.[^'\\\\]*)*'"
  51. Double = '[^"\\\\]*(?:\\\\.[^"\\\\]*)*"'
  52. Single3 = "[^'\\\\]*(?:(?:\\\\.|'(?!''))[^'\\\\]*)*'''"
  53. Double3 = '[^"\\\\]*(?:(?:\\\\.|"(?!""))[^"\\\\]*)*"""'
  54. Triple = group("[uU]?[rR]?'''", '[uU]?[rR]?"""')
  55. String = group("[uU]?[rR]?'[^\\n'\\\\]*(?:\\\\.[^\\n'\\\\]*)*'", '[uU]?[rR]?"[^\\n"\\\\]*(?:\\\\.[^\\n"\\\\]*)*"')
  56. Operator = group('\\*\\*=?', '>>=?', '<<=?', '<>', '!=', '//=?', '[+\\-*/%&|^=<>]=?', '~')
  57. Bracket = '[][(){}]'
  58. Special = group('\\r?\\n', '[:;.,`@]')
  59. Funny = group(Operator, Bracket, Special)
  60. PlainToken = group(Number, Funny, String, Name)
  61. Token = Ignore + PlainToken
  62. ContStr = group("[uU]?[rR]?'[^\\n'\\\\]*(?:\\\\.[^\\n'\\\\]*)*" + group("'", '\\\\\\r?\\n'), '[uU]?[rR]?"[^\\n"\\\\]*(?:\\\\.[^\\n"\\\\]*)*' + group('"', '\\\\\\r?\\n'))
  63. PseudoExtras = group('\\\\\\r?\\n', Comment, Triple)
  64. PseudoToken = Whitespace + group(PseudoExtras, Number, Funny, ContStr, Name)
  65. (tokenprog, pseudoprog, single3prog, double3prog) = map(re.compile, (Token, PseudoToken, Single3, Double3))
  66. endprogs = {
  67.     "'": re.compile(Single),
  68.     '"': re.compile(Double),
  69.     "'''": single3prog,
  70.     '"""': double3prog,
  71.     "r'''": single3prog,
  72.     'r"""': double3prog,
  73.     "u'''": single3prog,
  74.     'u"""': double3prog,
  75.     "ur'''": single3prog,
  76.     'ur"""': double3prog,
  77.     "R'''": single3prog,
  78.     'R"""': double3prog,
  79.     "U'''": single3prog,
  80.     'U"""': double3prog,
  81.     "uR'''": single3prog,
  82.     'uR"""': double3prog,
  83.     "Ur'''": single3prog,
  84.     'Ur"""': double3prog,
  85.     "UR'''": single3prog,
  86.     'UR"""': double3prog,
  87.     "b'''": single3prog,
  88.     'b"""': double3prog,
  89.     "br'''": single3prog,
  90.     'br"""': double3prog,
  91.     "B'''": single3prog,
  92.     'B"""': double3prog,
  93.     "bR'''": single3prog,
  94.     'bR"""': double3prog,
  95.     "Br'''": single3prog,
  96.     'Br"""': double3prog,
  97.     "BR'''": single3prog,
  98.     'BR"""': double3prog,
  99.     'r': None,
  100.     'R': None,
  101.     'u': None,
  102.     'U': None,
  103.     'b': None,
  104.     'B': None }
  105. triple_quoted = { }
  106. for t in ("'''", '"""', "r'''", 'r"""', "R'''", 'R"""', "u'''", 'u"""', "U'''", 'U"""', "ur'''", 'ur"""', "Ur'''", 'Ur"""', "uR'''", 'uR"""', "UR'''", 'UR"""', "b'''", 'b"""', "B'''", 'B"""', "br'''", 'br"""', "Br'''", 'Br"""', "bR'''", 'bR"""', "BR'''", 'BR"""'):
  107.     triple_quoted[t] = t
  108.  
  109. single_quoted = { }
  110. for t in ("'", '"', "r'", 'r"', "R'", 'R"', "u'", 'u"', "U'", 'U"', "ur'", 'ur"', "Ur'", 'Ur"', "uR'", 'uR"', "UR'", 'UR"', "b'", 'b"', "B'", 'B"', "br'", 'br"', "Br'", 'Br"', "bR'", 'bR"', "BR'", 'BR"'):
  111.     single_quoted[t] = t
  112.  
  113. tabsize = 8
  114.  
  115. class TokenError(Exception):
  116.     pass
  117.  
  118.  
  119. class StopTokenizing(Exception):
  120.     pass
  121.  
  122.  
  123. def printtoken(type, token, srow_scol, erow_ecol, line):
  124.     (srow, scol) = srow_scol
  125.     (erow, ecol) = erow_ecol
  126.     print '%d,%d-%d,%d:\t%s\t%s' % (srow, scol, erow, ecol, tok_name[type], repr(token))
  127.  
  128.  
  129. def tokenize(readline, tokeneater = printtoken):
  130.     
  131.     try:
  132.         tokenize_loop(readline, tokeneater)
  133.     except StopTokenizing:
  134.         pass
  135.  
  136.  
  137.  
  138. def tokenize_loop(readline, tokeneater):
  139.     for token_info in generate_tokens(readline):
  140.         tokeneater(*token_info)
  141.     
  142.  
  143.  
  144. class Untokenizer:
  145.     
  146.     def __init__(self):
  147.         self.tokens = []
  148.         self.prev_row = 1
  149.         self.prev_col = 0
  150.  
  151.     
  152.     def add_whitespace(self, start):
  153.         (row, col) = start
  154.         col_offset = col - self.prev_col
  155.         if col_offset:
  156.             self.tokens.append(' ' * col_offset)
  157.         
  158.  
  159.     
  160.     def untokenize(self, iterable):
  161.         for t in iterable:
  162.             if len(t) == 2:
  163.                 self.compat(t, iterable)
  164.                 break
  165.             
  166.             (tok_type, token, start, end, line) = t
  167.             self.add_whitespace(start)
  168.             self.tokens.append(token)
  169.             (self.prev_row, self.prev_col) = end
  170.             if tok_type in (NEWLINE, NL):
  171.                 self.prev_row += 1
  172.                 self.prev_col = 0
  173.                 continue
  174.             self
  175.         
  176.         return ''.join(self.tokens)
  177.  
  178.     
  179.     def compat(self, token, iterable):
  180.         startline = False
  181.         indents = []
  182.         toks_append = self.tokens.append
  183.         (toknum, tokval) = token
  184.         if toknum in (NAME, NUMBER):
  185.             tokval += ' '
  186.         
  187.         if toknum in (NEWLINE, NL):
  188.             startline = True
  189.         
  190.         prevstring = False
  191.         for tok in iterable:
  192.             (toknum, tokval) = tok[:2]
  193.             if toknum in (NAME, NUMBER):
  194.                 tokval += ' '
  195.             
  196.             if toknum == STRING:
  197.                 if prevstring:
  198.                     tokval = ' ' + tokval
  199.                 
  200.                 prevstring = True
  201.             else:
  202.                 prevstring = False
  203.             if toknum == INDENT:
  204.                 indents.append(tokval)
  205.                 continue
  206.             elif toknum == DEDENT:
  207.                 indents.pop()
  208.                 continue
  209.             elif toknum in (NEWLINE, NL):
  210.                 startline = True
  211.             elif startline and indents:
  212.                 toks_append(indents[-1])
  213.                 startline = False
  214.             
  215.             toks_append(tokval)
  216.         
  217.  
  218.  
  219.  
  220. def untokenize(iterable):
  221.     ut = Untokenizer()
  222.     return ut.untokenize(iterable)
  223.  
  224.  
  225. def generate_tokens(readline):
  226.     lnum = parenlev = continued = 0
  227.     namechars = string.ascii_letters + '_'
  228.     numchars = '0123456789'
  229.     (contstr, needcont) = ('', 0)
  230.     contline = None
  231.     indents = [
  232.         0]
  233.     while None:
  234.         
  235.         try:
  236.             line = readline()
  237.         except StopIteration:
  238.             line = ''
  239.  
  240.         lnum = lnum + 1
  241.         pos = 0
  242.         max = len(line)
  243.         if contstr:
  244.             if not line:
  245.                 raise TokenError, ('EOF in multi-line string', strstart)
  246.             line
  247.             endmatch = endprog.match(line)
  248.             if endmatch:
  249.                 pos = end = endmatch.end(0)
  250.                 yield (STRING, contstr + line[:end], strstart, (lnum, end), contline + line)
  251.                 (contstr, needcont) = ('', 0)
  252.                 contline = None
  253.             elif needcont and line[-2:] != '\\\n' and line[-3:] != '\\\r\n':
  254.                 yield (ERRORTOKEN, contstr + line, strstart, (lnum, len(line)), contline)
  255.                 contstr = ''
  256.                 contline = None
  257.                 continue
  258.             else:
  259.                 contstr = contstr + line
  260.                 contline = contline + line
  261.         elif parenlev == 0 and not continued:
  262.             if not line:
  263.                 break
  264.             
  265.             column = 0
  266.             while pos < max:
  267.                 if line[pos] == ' ':
  268.                     column = column + 1
  269.                 elif line[pos] == '\t':
  270.                     column = (column / tabsize + 1) * tabsize
  271.                 elif line[pos] == '\x0c':
  272.                     column = 0
  273.                 else:
  274.                     break
  275.                 pos = pos + 1
  276.             if pos == max:
  277.                 break
  278.             
  279.             if line[pos] in '#\r\n':
  280.                 if line[pos] == '#':
  281.                     comment_token = line[pos:].rstrip('\r\n')
  282.                     nl_pos = pos + len(comment_token)
  283.                     yield (COMMENT, comment_token, (lnum, pos), (lnum, pos + len(comment_token)), line)
  284.                     yield (NL, line[nl_pos:], (lnum, nl_pos), (lnum, len(line)), line)
  285.                     continue
  286.                 yield ((NL, COMMENT)[line[pos] == '#'], line[pos:], (lnum, pos), (lnum, len(line)), line)
  287.                 continue
  288.             
  289.             if column > indents[-1]:
  290.                 indents.append(column)
  291.                 yield (INDENT, line[:pos], (lnum, 0), (lnum, pos), line)
  292.             
  293.             while column < indents[-1]:
  294.                 if column not in indents:
  295.                     raise IndentationError('unindent does not match any outer indentation level', ('<tokenize>', lnum, pos, line))
  296.                 column not in indents
  297.                 indents = indents[:-1]
  298.                 yield (DEDENT, '', (lnum, pos), (lnum, pos), line)
  299.         elif not line:
  300.             raise TokenError, ('EOF in multi-line statement', (lnum, 0))
  301.         
  302.         continued = 0
  303.         while pos < max:
  304.             pseudomatch = pseudoprog.match(line, pos)
  305.             if pseudomatch:
  306.                 (start, end) = pseudomatch.span(1)
  307.                 spos = (lnum, start)
  308.                 epos = (lnum, end)
  309.                 pos = end
  310.                 token = line[start:end]
  311.                 initial = line[start]
  312.                 if (initial in numchars or initial == '.') and token != '.':
  313.                     yield (NUMBER, token, spos, epos, line)
  314.                 elif initial in '\r\n':
  315.                     yield (None if parenlev > 0 else NEWLINE, token, spos, epos, line)
  316.                 elif initial == '#':
  317.                     yield (COMMENT, token, spos, epos, line)
  318.                 elif token in triple_quoted:
  319.                     endprog = endprogs[token]
  320.                     endmatch = endprog.match(line, pos)
  321.                     if endmatch:
  322.                         pos = endmatch.end(0)
  323.                         token = line[start:pos]
  324.                         yield (STRING, token, spos, (lnum, pos), line)
  325.                     else:
  326.                         strstart = (lnum, start)
  327.                         contstr = line[start:]
  328.                         contline = line
  329.                         break
  330.                 elif initial in single_quoted and token[:2] in single_quoted or token[:3] in single_quoted:
  331.                     if token[-1] == '\n':
  332.                         strstart = (lnum, start)
  333.                         if not endprogs[initial] and endprogs[token[1]]:
  334.                             pass
  335.                         endprog = endprogs[token[2]]
  336.                         contstr = line[start:]
  337.                         needcont = 1
  338.                         contline = line
  339.                         break
  340.                     else:
  341.                         yield (STRING, token, spos, epos, line)
  342.                 elif initial in namechars:
  343.                     yield (NAME, token, spos, epos, line)
  344.                 elif initial == '\\':
  345.                     continued = 1
  346.                 elif initial in '([{':
  347.                     parenlev = parenlev + 1
  348.                 elif initial in ')]}':
  349.                     parenlev = parenlev - 1
  350.                 
  351.                 yield (OP, token, spos, epos, line)
  352.                 continue
  353.             yield (ERRORTOKEN, line[pos], (lnum, pos), (lnum, pos + 1), line)
  354.             pos = pos + 1
  355.         continue
  356.         for indent in indents[1:]:
  357.             yield (DEDENT, '', (lnum, 0), (lnum, 0), '')
  358.         
  359.     yield (ENDMARKER, '', (lnum, 0), (lnum, 0), '')
  360.  
  361.