2 Licensed according to the included 'LICENSE' document
3 Author: Thomas Harning Jr <harningt@gmail.com>
5 local lpeg
= require("lpeg")
6 local jsonutil
= require("json.util")
7 local merge
= jsonutil
.merge
8 local util
= require("json.decode.util")
10 -- Container module for other JavaScript types (bool, null, undefined)
14 -- For null and undefined, use the util.null value to preserve null-ness
15 local booleanCapture
=
16 lpeg
.P("true") * lpeg
.Cc(true)
17 + lpeg
.P("false") * lpeg
.Cc(false)
19 local nullCapture
= lpeg
.P("null")
20 local undefinedCapture
= lpeg
.P("undefined")
22 local defaultOptions
= {
23 allowUndefined
= true,
25 undefined
= jsonutil
.undefined
28 local modeOptions
= {}
30 modeOptions
.simple
= {
31 null
= false, -- Mapped to nil
32 undefined
= false -- Mapped to nil
34 modeOptions
.strict
= {
35 allowUndefined
= false
38 local function mergeOptions(options
, mode
)
39 jsonutil
.doOptionMerge(options
, false, 'others', defaultOptions
, mode
and modeOptions
[mode
])
42 local function generateLexer(options
)
43 -- The 'or nil' clause allows false to map to a nil value since 'nil' cannot be merged
44 options
= options
.others
45 local valueCapture
= (
47 + nullCapture
* lpeg
.Cc(options
.null
or nil)
49 if options
.allowUndefined
then
50 valueCapture
= valueCapture
+ undefinedCapture
* lpeg
.Cc(options
.undefined
or nil)
52 valueCapture
= valueCapture
+ #undefinedCapture
* util
.denied("undefined", "others.allowUndefined")
58 mergeOptions
= mergeOptions
,
59 generateLexer
= generateLexer