scanner.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. """JSON token scanner
  2. """
  3. import re
  4. from .errors import JSONDecodeError
  5. def _import_c_make_scanner():
  6. try:
  7. from ._speedups import make_scanner
  8. return make_scanner
  9. except ImportError:
  10. return None
  11. c_make_scanner = _import_c_make_scanner()
  12. __all__ = ['make_scanner', 'JSONDecodeError']
  13. NUMBER_RE = re.compile(
  14. r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?',
  15. (re.VERBOSE | re.MULTILINE | re.DOTALL))
  16. def py_make_scanner(context):
  17. parse_object = context.parse_object
  18. parse_array = context.parse_array
  19. parse_string = context.parse_string
  20. match_number = NUMBER_RE.match
  21. encoding = context.encoding
  22. strict = context.strict
  23. parse_float = context.parse_float
  24. parse_int = context.parse_int
  25. parse_constant = context.parse_constant
  26. object_hook = context.object_hook
  27. object_pairs_hook = context.object_pairs_hook
  28. memo = context.memo
  29. def _scan_once(string, idx):
  30. errmsg = 'Expecting value'
  31. try:
  32. nextchar = string[idx]
  33. except IndexError:
  34. raise JSONDecodeError(errmsg, string, idx)
  35. if nextchar == '"':
  36. return parse_string(string, idx + 1, encoding, strict)
  37. elif nextchar == '{':
  38. return parse_object((string, idx + 1), encoding, strict,
  39. _scan_once, object_hook, object_pairs_hook, memo)
  40. elif nextchar == '[':
  41. return parse_array((string, idx + 1), _scan_once)
  42. elif nextchar == 'n' and string[idx:idx + 4] == 'null':
  43. return None, idx + 4
  44. elif nextchar == 't' and string[idx:idx + 4] == 'true':
  45. return True, idx + 4
  46. elif nextchar == 'f' and string[idx:idx + 5] == 'false':
  47. return False, idx + 5
  48. m = match_number(string, idx)
  49. if m is not None:
  50. integer, frac, exp = m.groups()
  51. if frac or exp:
  52. res = parse_float(integer + (frac or '') + (exp or ''))
  53. else:
  54. res = parse_int(integer)
  55. return res, m.end()
  56. elif nextchar == 'N' and string[idx:idx + 3] == 'NaN':
  57. return parse_constant('NaN'), idx + 3
  58. elif nextchar == 'I' and string[idx:idx + 8] == 'Infinity':
  59. return parse_constant('Infinity'), idx + 8
  60. elif nextchar == '-' and string[idx:idx + 9] == '-Infinity':
  61. return parse_constant('-Infinity'), idx + 9
  62. else:
  63. raise JSONDecodeError(errmsg, string, idx)
  64. def scan_once(string, idx):
  65. if idx < 0:
  66. # Ensure the same behavior as the C speedup, otherwise
  67. # this would work for *some* negative string indices due
  68. # to the behavior of __getitem__ for strings. #98
  69. raise JSONDecodeError('Expecting value', string, idx)
  70. try:
  71. return _scan_once(string, idx)
  72. finally:
  73. memo.clear()
  74. return scan_once
  75. make_scanner = c_make_scanner or py_make_scanner