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 jsonutil
= require("json.util")
8 local merge
= jsonutil
.merge
9 local util
= require("json.decode.util")
13 local digit
= lpeg
.R("09")
14 local digits
= digit^
1
16 -- Illegal octal declaration
17 local illegal_octal_detect
= #(lpeg
.P('0') * digits
) * util
.denied("Octal numbers")
19 local int
= (lpeg
.P('-') + 0) * (lpeg
.R("19") * digits
+ illegal_octal_detect
+ 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")
27 local ninf
= lpeg
.P('-') * lpeg
.S("Ii") * lpeg
.P("nfinity")
28 local hex
= (lpeg
.P("0x") + lpeg
.P("0X")) * lpeg
.R("09","AF","af")^
1
30 local defaultOptions
= {
38 local modeOptions
= {}
40 modeOptions
.strict
= {
47 local ninf_value
= -1/0
50 Options: configuration options for number rules
53 frac: match fraction portion (.0)
54 exp: match exponent portion (e1)
55 DEFAULT: nan, inf, frac, exp
57 local function mergeOptions(options
, mode
)
58 jsonutil
.doOptionMerge(options
, false, 'number', defaultOptions
, mode
and modeOptions
[mode
])
61 local function generateLexer(options
)
62 options
= options
.number
65 ret
= ret
* (frac
+ 0)
67 ret
= ret
* (#frac
* util
.denied("Fractions", "number.frac") + 0)
72 ret
= ret
* (#exp * util
.denied("Exponents", "number.exp") + 0)
77 ret
= #hex
* util
.denied("Hexadecimal", "number.hex") + ret
82 ret
= ret
+ nan
/ function() return nan_value
end
84 ret
= ret
+ #nan
* util
.denied("NaN", "number.nan")
87 ret
= ret
+ ninf
/ function() return ninf_value
end + inf
/ function() return inf_value
end
89 ret
= ret
+ (#ninf
+ #inf
) * util
.denied("+/-Inf", "number.inf")
96 mergeOptions
= mergeOptions
,
97 generateLexer
= generateLexer