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 merge
= require("json.util").merge
8 local util
= require("json.decode.util")
10 module("json.decode.number")
12 local digit
= lpeg
.R("09")
13 local digits
= digit^
1
15 int
= (lpeg
.P('-') + 0) * (lpeg
.R("19") * digits
+ digit
)
18 local frac
= lpeg
.P('.') * digits
20 local exp = lpeg
.S("Ee") * (lpeg
.S("-+") + 0) * digits
22 local nan
= lpeg
.S("Nn") * lpeg
.S("Aa") * lpeg
.S("Nn")
23 local inf
= lpeg
.S("Ii") * lpeg
.P("nfinity")
24 local ninf
= lpeg
.P('-') * lpeg
.S("Ii") * lpeg
.P("nfinity")
25 local hex
= (lpeg
.P("0x") + lpeg
.P("0X")) * lpeg
.R("09","AF","af")^
1
27 local defaultOptions
= {
35 default
= nil -- Let the buildCapture optimization take place
43 local ninf_value
= -1/0
46 Options: configuration options for number rules
49 frac: match fraction portion (.0)
50 exp: match exponent portion (e1)
51 DEFAULT: nan, inf, frac, exp
53 local function buildCapture(options
)
54 options
= options
and merge({}, defaultOptions
, options
) or defaultOptions
57 ret
= ret
* (frac
+ 0)
68 ret
= ret
+ nan
/ function() return nan_value
end
71 ret
= ret
+ ninf
/ function() return ninf_value
end + inf
/ function() return inf_value
end
76 function register_types()
77 util
.register_type("INTEGER")
80 function load_types(options
, global_options
, grammar
)
81 local integer_id
= util
.types
.INTEGER
82 local capture
= buildCapture(options
)
83 util
.append_grammar_item(grammar
, "VALUE", capture
)
84 grammar
[integer_id
] = int
/ tonumber