4 def _import_c_make_scanner():
6 from simplejson
._speedups
import make_scanner
10 c_make_scanner
= _import_c_make_scanner()
12 __all__
= ['make_scanner']
14 NUMBER_RE
= re
.compile(
15 r
'(-?(?:0|[1-9]\d*))(\.\d+)?([eE][-+]?\d+)?',
16 (re
.VERBOSE | re
.MULTILINE | re
.DOTALL
))
18 def py_make_scanner(context
):
19 parse_object
= context
.parse_object
20 parse_array
= context
.parse_array
21 parse_string
= context
.parse_string
22 match_number
= NUMBER_RE
.match
23 encoding
= context
.encoding
24 strict
= context
.strict
25 parse_float
= context
.parse_float
26 parse_int
= context
.parse_int
27 parse_constant
= context
.parse_constant
28 object_hook
= context
.object_hook
29 object_pairs_hook
= context
.object_pairs_hook
32 def _scan_once(string
, idx
):
34 nextchar
= string
[idx
]
39 return parse_string(string
, idx
+ 1, encoding
, strict
)
41 return parse_object((string
, idx
+ 1), encoding
, strict
,
42 _scan_once
, object_hook
, object_pairs_hook
, memo
)
44 return parse_array((string
, idx
+ 1), _scan_once
)
45 elif nextchar
== 'n' and string
[idx
:idx
+ 4] == 'null':
47 elif nextchar
== 't' and string
[idx
:idx
+ 4] == 'true':
49 elif nextchar
== 'f' and string
[idx
:idx
+ 5] == 'false':
52 m
= match_number(string
, idx
)
54 integer
, frac
, exp
= m
.groups()
56 res
= parse_float(integer
+ (frac
or '') + (exp
or ''))
58 res
= parse_int(integer
)
60 elif nextchar
== 'N' and string
[idx
:idx
+ 3] == 'NaN':
61 return parse_constant('NaN'), idx
+ 3
62 elif nextchar
== 'I' and string
[idx
:idx
+ 8] == 'Infinity':
63 return parse_constant('Infinity'), idx
+ 8
64 elif nextchar
== '-' and string
[idx
:idx
+ 9] == '-Infinity':
65 return parse_constant('-Infinity'), idx
+ 9
69 def scan_once(string
, idx
):
71 return _scan_once(string
, idx
)
77 make_scanner
= c_make_scanner
or py_make_scanner