base: fixes regression test lua path
[luajson.git] / lua / json / decode / number.lua
blobefc7fd62a722630687ac67cbadf7c6ea863c4ac0
1 --[[
2 Licensed according to the included 'LICENSE' document
3 Author: Thomas Harning Jr <harningt@gmail.com>
4 ]]
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)
16 local int = int
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 = {
28 nan = true,
29 inf = true,
30 frac = true,
31 exp = true,
32 hex = false
35 default = nil -- Let the buildCapture optimization take place
36 strict = {
37 nan = false,
38 inf = false
41 local nan_value = 0/0
42 local inf_value = 1/0
43 local ninf_value = -1/0
45 --[[
46 Options: configuration options for number rules
47 nan: match NaN
48 inf: match Infinity
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
55 local ret = int
56 if options.frac then
57 ret = ret * (frac + 0)
58 end
59 if options.exp then
60 ret = ret * (exp + 0)
61 end
62 if options.hex then
63 ret = hex + ret
64 end
65 -- Capture number now
66 ret = ret / tonumber
67 if options.nan then
68 ret = ret + nan / function() return nan_value end
69 end
70 if options.inf then
71 ret = ret + ninf / function() return ninf_value end + inf / function() return inf_value end
72 end
73 return ret
74 end
76 function register_types()
77 util.register_type("INTEGER")
78 end
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
85 end