decoder: Added simple LPEG version parser "x.y.z.." => x.y
[luajson.git] / src / json / decode / util.lua
blob8dd5b2647ded49d9664cf56aee5235c40574927a
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 select = select
7 local pairs, ipairs = pairs, ipairs
8 local tonumber = tonumber
9 module("json.decode.util")
11 space = lpeg.S(" \n\r\t\f")
13 identifier = lpeg.R("AZ","az","__") * lpeg.R("AZ","az", "__", "09") ^0
15 hex = lpeg.R("09","AF","af")
16 hexpair = hex * hex
18 comments = {
19 cpp = lpeg.P("//") * (1 - lpeg.P("\n"))^0 * lpeg.P("\n"),
20 c = lpeg.P("/*") * (1 - lpeg.P("*/"))^0 * lpeg.P("*/")
23 comment = comments.cpp + comments.c
25 ignored = (space + comment)^0
27 VALUE, TABLE, ARRAY = 2, 3, 4
28 function clone(t)
29 local ret = {}
30 for k,v in pairs(t) do
31 ret[k] = v
32 end
33 return ret
34 end
36 function merge(t, ...)
37 for i = 1,select('#', ...) do
38 local currentTable = select(i, ...)
39 if currentTable then
40 for k,v in pairs(currentTable) do
41 t[k] = v
42 end
43 end
44 end
45 return t
46 end
48 inits = {}
50 function doInit()
51 for _, v in ipairs(inits) do
52 v()
53 end
54 end
56 -- Current depth is persistent
57 -- If more complex depth management needed, a new system would need to be setup
58 local currentDepth = 0
60 function buildDepthLimit(limit)
61 local function init()
62 currentDepth = 0
63 end
64 inits[#inits + 1] = init
66 local function incDepth(s, i)
67 currentDepth = currentDepth + 1
68 return currentDepth < limit and i or false
69 end
70 local function decDepth(s, i)
71 currentDepth = currentDepth - 1
72 return i
73 end
74 return {incDepth, decDepth}
75 end
78 -- Parse the lpeg version skipping patch-values
79 -- LPEG <= 0.7 have no version value... so 0.7 is value
80 DecimalLpegVersion = lpeg.version and tonumber(lpeg.version():match("^(%d+%.%d+)")) or 0.7