annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/json/scanner.py @ 68:5028fdace37b

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 16:23:26 -0400
parents
children
rev   line source
jpayne@68 1 """JSON token scanner
jpayne@68 2 """
jpayne@68 3 import re
jpayne@68 4 try:
jpayne@68 5 from _json import make_scanner as c_make_scanner
jpayne@68 6 except ImportError:
jpayne@68 7 c_make_scanner = None
jpayne@68 8
jpayne@68 9 __all__ = ['make_scanner']
jpayne@68 10
jpayne@68 11 NUMBER_RE = re.compile(
jpayne@68 12 r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?',
jpayne@68 13 (re.VERBOSE | re.MULTILINE | re.DOTALL))
jpayne@68 14
jpayne@68 15 def py_make_scanner(context):
jpayne@68 16 parse_object = context.parse_object
jpayne@68 17 parse_array = context.parse_array
jpayne@68 18 parse_string = context.parse_string
jpayne@68 19 match_number = NUMBER_RE.match
jpayne@68 20 strict = context.strict
jpayne@68 21 parse_float = context.parse_float
jpayne@68 22 parse_int = context.parse_int
jpayne@68 23 parse_constant = context.parse_constant
jpayne@68 24 object_hook = context.object_hook
jpayne@68 25 object_pairs_hook = context.object_pairs_hook
jpayne@68 26 memo = context.memo
jpayne@68 27
jpayne@68 28 def _scan_once(string, idx):
jpayne@68 29 try:
jpayne@68 30 nextchar = string[idx]
jpayne@68 31 except IndexError:
jpayne@68 32 raise StopIteration(idx) from None
jpayne@68 33
jpayne@68 34 if nextchar == '"':
jpayne@68 35 return parse_string(string, idx + 1, strict)
jpayne@68 36 elif nextchar == '{':
jpayne@68 37 return parse_object((string, idx + 1), strict,
jpayne@68 38 _scan_once, object_hook, object_pairs_hook, memo)
jpayne@68 39 elif nextchar == '[':
jpayne@68 40 return parse_array((string, idx + 1), _scan_once)
jpayne@68 41 elif nextchar == 'n' and string[idx:idx + 4] == 'null':
jpayne@68 42 return None, idx + 4
jpayne@68 43 elif nextchar == 't' and string[idx:idx + 4] == 'true':
jpayne@68 44 return True, idx + 4
jpayne@68 45 elif nextchar == 'f' and string[idx:idx + 5] == 'false':
jpayne@68 46 return False, idx + 5
jpayne@68 47
jpayne@68 48 m = match_number(string, idx)
jpayne@68 49 if m is not None:
jpayne@68 50 integer, frac, exp = m.groups()
jpayne@68 51 if frac or exp:
jpayne@68 52 res = parse_float(integer + (frac or '') + (exp or ''))
jpayne@68 53 else:
jpayne@68 54 res = parse_int(integer)
jpayne@68 55 return res, m.end()
jpayne@68 56 elif nextchar == 'N' and string[idx:idx + 3] == 'NaN':
jpayne@68 57 return parse_constant('NaN'), idx + 3
jpayne@68 58 elif nextchar == 'I' and string[idx:idx + 8] == 'Infinity':
jpayne@68 59 return parse_constant('Infinity'), idx + 8
jpayne@68 60 elif nextchar == '-' and string[idx:idx + 9] == '-Infinity':
jpayne@68 61 return parse_constant('-Infinity'), idx + 9
jpayne@68 62 else:
jpayne@68 63 raise StopIteration(idx)
jpayne@68 64
jpayne@68 65 def scan_once(string, idx):
jpayne@68 66 try:
jpayne@68 67 return _scan_once(string, idx)
jpayne@68 68 finally:
jpayne@68 69 memo.clear()
jpayne@68 70
jpayne@68 71 return scan_once
jpayne@68 72
jpayne@68 73 make_scanner = c_make_scanner or py_make_scanner