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