home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / simplejson / decoder.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  7.2 KB  |  309 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import re
  5. import sys
  6. import struct
  7. from simplejson.scanner import make_scanner
  8.  
  9. try:
  10.     from simplejson._speedups import scanstring as c_scanstring
  11. except ImportError:
  12.     c_scanstring = None
  13.  
  14. __all__ = [
  15.     'JSONDecoder']
  16. FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
  17.  
  18. def _floatconstants():
  19.     _BYTES = '7FF80000000000007FF0000000000000'.decode('hex')
  20.     if sys.byteorder != 'big':
  21.         _BYTES = _BYTES[:8][::-1] + _BYTES[8:][::-1]
  22.     
  23.     (nan, inf) = struct.unpack('dd', _BYTES)
  24.     return (nan, inf, -inf)
  25.  
  26. (NaN, PosInf, NegInf) = _floatconstants()
  27.  
  28. def linecol(doc, pos):
  29.     lineno = doc.count('\n', 0, pos) + 1
  30.     if lineno == 1:
  31.         colno = pos
  32.     else:
  33.         colno = pos - doc.rindex('\n', 0, pos)
  34.     return (lineno, colno)
  35.  
  36.  
  37. def errmsg(msg, doc, pos, end = None):
  38.     (lineno, colno) = linecol(doc, pos)
  39.     if end is None:
  40.         return '%s: line %d column %d (char %d)' % (msg, lineno, colno, pos)
  41.     
  42.     (endlineno, endcolno) = linecol(doc, end)
  43.     return '%s: line %d column %d - line %d column %d (char %d - %d)' % (msg, lineno, colno, endlineno, endcolno, pos, end)
  44.  
  45. _CONSTANTS = {
  46.     '-Infinity': NegInf,
  47.     'Infinity': PosInf,
  48.     'NaN': NaN }
  49. STRINGCHUNK = re.compile('(.*?)(["\\\\\\x00-\\x1f])', FLAGS)
  50. BACKSLASH = {
  51.     '"': u'"',
  52.     '\\': u'\\',
  53.     '/': u'/',
  54.     'b': u'\x08',
  55.     'f': u'\x0c',
  56.     'n': u'\n',
  57.     'r': u'\r',
  58.     't': u'\t' }
  59. DEFAULT_ENCODING = 'utf-8'
  60.  
  61. def py_scanstring(s, end, encoding = None, strict = True, _b = BACKSLASH, _m = STRINGCHUNK.match):
  62.     if encoding is None:
  63.         encoding = DEFAULT_ENCODING
  64.     
  65.     chunks = []
  66.     _append = chunks.append
  67.     begin = end - 1
  68.     while None:
  69.         chunk = _m(s, end)
  70.         if chunk is None:
  71.             raise ValueError(errmsg('Unterminated string starting at', s, begin))
  72.         
  73.         end = chunk.end()
  74.         (content, terminator) = chunk.groups()
  75.         if content:
  76.             if not isinstance(content, unicode):
  77.                 content = unicode(content, encoding)
  78.             
  79.             _append(content)
  80.         
  81.         if terminator == '"':
  82.             break
  83.         elif terminator != '\\':
  84.             if strict:
  85.                 raise ValueError(errmsg('Invalid control character %r at', s, end))
  86.             else:
  87.                 _append(terminator)
  88.         
  89.         
  90.         try:
  91.             esc = s[end]
  92.         except IndexError:
  93.             raise ValueError(errmsg('Unterminated string starting at', s, begin))
  94.  
  95.         if esc != 'u':
  96.             
  97.             try:
  98.                 m = _b[esc]
  99.             except KeyError:
  100.                 raise ValueError(errmsg('Invalid \\escape: %r' % (esc,), s, end))
  101.  
  102.             end += 1
  103.         else:
  104.             esc = s[end + 1:end + 5]
  105.             next_end = end + 5
  106.             msg = 'Invalid \\uXXXX escape'
  107.             
  108.             try:
  109.                 if len(esc) != 4:
  110.                     raise ValueError
  111.                 
  112.                 uni = int(esc, 16)
  113.                 if uni <= uni:
  114.                     pass
  115.                 elif uni <= 56319 and sys.maxunicode > 65535:
  116.                     msg = 'Invalid \\uXXXX\\uXXXX surrogate pair'
  117.                     if not s[end + 5:end + 7] == '\\u':
  118.                         raise ValueError
  119.                     
  120.                     esc2 = s[end + 7:end + 11]
  121.                     if len(esc2) != 4:
  122.                         raise ValueError
  123.                     
  124.                     uni2 = int(esc2, 16)
  125.                     uni = 65536 + (uni - 55296 << 10 | uni2 - 56320)
  126.                     next_end += 6
  127.                 
  128.                 m = unichr(uni)
  129.             except ValueError:
  130.                 raise ValueError(errmsg(msg, s, end))
  131.  
  132.             end = next_end
  133.         continue
  134.         return (u''.join(chunks), end)
  135.  
  136. if not c_scanstring:
  137.     pass
  138. scanstring = py_scanstring
  139. WHITESPACE = re.compile('[ \\t\\n\\r]*', FLAGS)
  140. WHITESPACE_STR = ' \t\n\r'
  141.  
  142. def JSONObject(.0, encoding, strict, scan_once, object_hook, _w = WHITESPACE.match, _ws = WHITESPACE_STR):
  143.     (s, end) = .0
  144.     pairs = { }
  145.     nextchar = s[end:end + 1]
  146.     if nextchar != '"':
  147.         if nextchar in _ws:
  148.             end = _w(s, end).end()
  149.             nextchar = s[end:end + 1]
  150.         
  151.         if nextchar == '}':
  152.             return (pairs, end + 1)
  153.         elif nextchar != '"':
  154.             raise ValueError(errmsg('Expecting property name', s, end))
  155.         
  156.     
  157.     end += 1
  158.     while True:
  159.         (key, end) = scanstring(s, end, encoding, strict)
  160.         if s[end:end + 1] != ':':
  161.             end = _w(s, end).end()
  162.             if s[end:end + 1] != ':':
  163.                 raise ValueError(errmsg('Expecting : delimiter', s, end))
  164.             
  165.         
  166.         end += 1
  167.         
  168.         try:
  169.             if s[end] in _ws:
  170.                 end += 1
  171.                 if s[end] in _ws:
  172.                     end = _w(s, end + 1).end()
  173.                 
  174.         except IndexError:
  175.             pass
  176.  
  177.         
  178.         try:
  179.             (value, end) = scan_once(s, end)
  180.         except StopIteration:
  181.             raise ValueError(errmsg('Expecting object', s, end))
  182.  
  183.         pairs[key] = value
  184.         
  185.         try:
  186.             nextchar = s[end]
  187.             if nextchar in _ws:
  188.                 end = _w(s, end + 1).end()
  189.                 nextchar = s[end]
  190.         except IndexError:
  191.             nextchar = ''
  192.  
  193.         end += 1
  194.         if nextchar == '}':
  195.             break
  196.         elif nextchar != ',':
  197.             raise ValueError(errmsg('Expecting , delimiter', s, end - 1))
  198.         
  199.         
  200.         try:
  201.             nextchar = s[end]
  202.             if nextchar in _ws:
  203.                 end += 1
  204.                 nextchar = s[end]
  205.                 if nextchar in _ws:
  206.                     end = _w(s, end + 1).end()
  207.                     nextchar = s[end]
  208.                 
  209.         except IndexError:
  210.             nextchar = ''
  211.  
  212.         end += 1
  213.         if nextchar != '"':
  214.             raise ValueError(errmsg('Expecting property name', s, end - 1))
  215.             continue
  216.     if object_hook is not None:
  217.         pairs = object_hook(pairs)
  218.     
  219.     return (pairs, end)
  220.  
  221.  
  222. def JSONArray(.0, scan_once, _w = WHITESPACE.match, _ws = WHITESPACE_STR):
  223.     (s, end) = .0
  224.     values = []
  225.     nextchar = s[end:end + 1]
  226.     if nextchar in _ws:
  227.         end = _w(s, end + 1).end()
  228.         nextchar = s[end:end + 1]
  229.     
  230.     if nextchar == ']':
  231.         return (values, end + 1)
  232.     
  233.     _append = values.append
  234.     while True:
  235.         
  236.         try:
  237.             (value, end) = scan_once(s, end)
  238.         except StopIteration:
  239.             raise ValueError(errmsg('Expecting object', s, end))
  240.  
  241.         _append(value)
  242.         nextchar = s[end:end + 1]
  243.         if nextchar in _ws:
  244.             end = _w(s, end + 1).end()
  245.             nextchar = s[end:end + 1]
  246.         
  247.         end += 1
  248.         if nextchar == ']':
  249.             break
  250.         elif nextchar != ',':
  251.             raise ValueError(errmsg('Expecting , delimiter', s, end))
  252.         
  253.         
  254.         try:
  255.             if s[end] in _ws:
  256.                 end += 1
  257.                 if s[end] in _ws:
  258.                     end = _w(s, end + 1).end()
  259.                 
  260.         continue
  261.         except IndexError:
  262.             continue
  263.         
  264.  
  265.         None<EXCEPTION MATCH>IndexError
  266.     return (values, end)
  267.  
  268.  
  269. class JSONDecoder(object):
  270.     
  271.     def __init__(self, encoding = None, object_hook = None, parse_float = None, parse_int = None, parse_constant = None, strict = True):
  272.         self.encoding = encoding
  273.         self.object_hook = object_hook
  274.         if not parse_float:
  275.             pass
  276.         self.parse_float = float
  277.         if not parse_int:
  278.             pass
  279.         self.parse_int = int
  280.         if not parse_constant:
  281.             pass
  282.         self.parse_constant = _CONSTANTS.__getitem__
  283.         self.strict = strict
  284.         self.parse_object = JSONObject
  285.         self.parse_array = JSONArray
  286.         self.parse_string = scanstring
  287.         self.scan_once = make_scanner(self)
  288.  
  289.     
  290.     def decode(self, s, _w = WHITESPACE.match):
  291.         (obj, end) = self.raw_decode(s, idx = _w(s, 0).end())
  292.         end = _w(s, end).end()
  293.         if end != len(s):
  294.             raise ValueError(errmsg('Extra data', s, end, len(s)))
  295.         
  296.         return obj
  297.  
  298.     
  299.     def raw_decode(self, s, idx = 0):
  300.         
  301.         try:
  302.             (obj, end) = self.scan_once(s, idx)
  303.         except StopIteration:
  304.             raise ValueError('No JSON object could be decoded')
  305.  
  306.         return (obj, end)
  307.  
  308.  
  309.