PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/simplejson/scanner.py

http://github.com/midgetspy/Sick-Beard
Python | 65 lines | 57 code | 6 blank | 2 comment | 17 complexity | edae70feecb908c3b1c5b801d3980323 MD5 | raw file
Possible License(s): Unlicense
  1. """JSON token scanner
  2. """
  3. import re
  4. try:
  5. from lib.simplejson._speedups import make_scanner as c_make_scanner
  6. except ImportError:
  7. c_make_scanner = None
  8. __all__ = ['make_scanner']
  9. NUMBER_RE = re.compile(
  10. r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?',
  11. (re.VERBOSE | re.MULTILINE | re.DOTALL))
  12. def py_make_scanner(context):
  13. parse_object = context.parse_object
  14. parse_array = context.parse_array
  15. parse_string = context.parse_string
  16. match_number = NUMBER_RE.match
  17. encoding = context.encoding
  18. strict = context.strict
  19. parse_float = context.parse_float
  20. parse_int = context.parse_int
  21. parse_constant = context.parse_constant
  22. object_hook = context.object_hook
  23. def _scan_once(string, idx):
  24. try:
  25. nextchar = string[idx]
  26. except IndexError:
  27. raise StopIteration
  28. if nextchar == '"':
  29. return parse_string(string, idx + 1, encoding, strict)
  30. elif nextchar == '{':
  31. return parse_object((string, idx + 1), encoding, strict, _scan_once, object_hook)
  32. elif nextchar == '[':
  33. return parse_array((string, idx + 1), _scan_once)
  34. elif nextchar == 'n' and string[idx:idx + 4] == 'null':
  35. return None, idx + 4
  36. elif nextchar == 't' and string[idx:idx + 4] == 'true':
  37. return True, idx + 4
  38. elif nextchar == 'f' and string[idx:idx + 5] == 'false':
  39. return False, idx + 5
  40. m = match_number(string, idx)
  41. if m is not None:
  42. integer, frac, exp = m.groups()
  43. if frac or exp:
  44. res = parse_float(integer + (frac or '') + (exp or ''))
  45. else:
  46. res = parse_int(integer)
  47. return res, m.end()
  48. elif nextchar == 'N' and string[idx:idx + 3] == 'NaN':
  49. return parse_constant('NaN'), idx + 3
  50. elif nextchar == 'I' and string[idx:idx + 8] == 'Infinity':
  51. return parse_constant('Infinity'), idx + 8
  52. elif nextchar == '-' and string[idx:idx + 9] == '-Infinity':
  53. return parse_constant('-Infinity'), idx + 9
  54. else:
  55. raise StopIteration
  56. return _scan_once
  57. make_scanner = c_make_scanner or py_make_scanner