blob: 74e6805155d85399fd5a6a67d5b35765270f63db [file] [log] [blame]
Bob Ippolitod914e3f2009-03-17 23:19:00 +00001"""JSON token scanner
Brett Cannon4b964f92008-05-05 20:21:38 +00002"""
Brett Cannon4b964f92008-05-05 20:21:38 +00003import re
Bob Ippolitod914e3f2009-03-17 23:19:00 +00004try:
5 from _json import make_scanner as c_make_scanner
6except ImportError:
7 c_make_scanner = None
Brett Cannon4b964f92008-05-05 20:21:38 +00008
Bob Ippolitod914e3f2009-03-17 23:19:00 +00009__all__ = ['make_scanner']
Brett Cannon4b964f92008-05-05 20:21:38 +000010
Bob Ippolitod914e3f2009-03-17 23:19:00 +000011NUMBER_RE = re.compile(
12 r'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?',
13 (re.VERBOSE | re.MULTILINE | re.DOTALL))
Brett Cannon4b964f92008-05-05 20:21:38 +000014
Bob Ippolitod914e3f2009-03-17 23:19:00 +000015def py_make_scanner(context):
16 parse_object = context.parse_object
17 parse_array = context.parse_array
18 parse_string = context.parse_string
19 match_number = NUMBER_RE.match
20 encoding = context.encoding
21 strict = context.strict
22 parse_float = context.parse_float
23 parse_int = context.parse_int
24 parse_constant = context.parse_constant
25 object_hook = context.object_hook
Bob Ippolito76a982a2009-03-29 22:33:58 +000026 object_pairs_hook = context.object_pairs_hook
Brett Cannon4b964f92008-05-05 20:21:38 +000027
Bob Ippolitod914e3f2009-03-17 23:19:00 +000028 def _scan_once(string, idx):
29 try:
30 nextchar = string[idx]
31 except IndexError:
32 raise StopIteration
Brett Cannon4b964f92008-05-05 20:21:38 +000033
Bob Ippolitod914e3f2009-03-17 23:19:00 +000034 if nextchar == '"':
35 return parse_string(string, idx + 1, encoding, strict)
36 elif nextchar == '{':
37 return parse_object((string, idx + 1), encoding, strict,
Bob Ippolito76a982a2009-03-29 22:33:58 +000038 _scan_once, object_hook, object_pairs_hook)
Bob Ippolitod914e3f2009-03-17 23:19:00 +000039 elif nextchar == '[':
40 return parse_array((string, idx + 1), _scan_once)
41 elif nextchar == 'n' and string[idx:idx + 4] == 'null':
42 return None, idx + 4
43 elif nextchar == 't' and string[idx:idx + 4] == 'true':
44 return True, idx + 4
45 elif nextchar == 'f' and string[idx:idx + 5] == 'false':
46 return False, idx + 5
Brett Cannon4b964f92008-05-05 20:21:38 +000047
Bob Ippolitod914e3f2009-03-17 23:19:00 +000048 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 StopIteration
Brett Cannon4b964f92008-05-05 20:21:38 +000064
Bob Ippolitod914e3f2009-03-17 23:19:00 +000065 return _scan_once
Brett Cannon4b964f92008-05-05 20:21:38 +000066
Bob Ippolitod914e3f2009-03-17 23:19:00 +000067make_scanner = c_make_scanner or py_make_scanner