2 Licensed according to the included 'LICENSE' document
3 Author: Thomas Harning Jr <harningt@gmail.com>
5 local lpeg
= require("lpeg")
6 local tonumber = tonumber
7 local util
= require("json.decode.util")
9 module("json.decode.number")
11 local space
= util
.space
13 local digit
= lpeg
.R("09")
14 local digits
= digit^
1
16 -- Deviation from JSON spec: Leading zeroes, inf number negatives w/ space
17 int
= lpeg
.P('-')^
0 * space^
0 * digits
19 local strictInt
= (lpeg
.P('-') + 0) * (lpeg
.R("19") * digits
+ digit
)
21 local frac
= lpeg
.P('.') * digits
23 local exp = lpeg
.S("Ee") * (lpeg
.S("-+") + 0) * digits
25 local nan
= lpeg
.S("Nn") * lpeg
.S("Aa") * lpeg
.S("Nn")
26 local inf
= lpeg
.S("Ii") * lpeg
.P("nfinity")
28 local defaultOptions
= {
42 Options: configuration options for number rules
45 strict: for integer portion, only match [-]([0-9]|[1-9][0-9]*)
46 frac: match fraction portion (.0)
47 exp: match exponent portion (e1)
48 DEFAULT: nan, inf, frac, exp
51 function buildMatch(options
)
52 options
= util
.merge({}, defaultOptions
, options
)
53 local ret
= options
.strict
and strictInt
or int
55 ret
= ret
* (frac
+ 0)
69 function buildCapture(options
)
70 return buildMatch(options
) / tonumber