Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
L
lua-language-server-srb2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LJ Sonic
lua-language-server-srb2
Commits
242d5ed3
Commit
242d5ed3
authored
4 years ago
by
最萌小汐
Browse files
Options
Downloads
Patches
Plain Diff
update json.lua
parent
67945d4e
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
script/json-beautify.lua
+12
-5
12 additions, 5 deletions
script/json-beautify.lua
script/json.lua
+103
-75
103 additions, 75 deletions
script/json.lua
with
115 additions
and
80 deletions
script/json-beautify.lua
+
12
−
5
View file @
242d5ed3
...
...
@@ -25,17 +25,22 @@ local function encode_newline()
end
local
encode_map
=
{}
for
k
,
v
in
next
,
json
.
encode_map
do
local
encode_string
=
json
.
_encode_string
for
k
,
v
in
next
,
json
.
_encode_map
do
encode_map
[
k
]
=
v
end
local
encode_string
=
json
.
encode_map
.
string
local
function
encode
(
v
)
local
res
=
encode_map
[
type
(
v
)](
v
)
statusQue
[
#
statusQue
+
1
]
=
res
end
function
encode_map
.
string
(
v
)
statusQue
[
#
statusQue
+
1
]
=
'"'
statusQue
[
#
statusQue
+
1
]
=
encode_string
(
v
)
return
'"'
end
function
encode_map
.
table
(
t
)
local
first_val
=
next
(
t
)
if
first_val
==
nil
then
...
...
@@ -62,15 +67,17 @@ function encode_map.table(t)
statusDep
=
statusDep
+
1
encode_newline
()
local
k
=
key
[
1
]
statusQue
[
#
statusQue
+
1
]
=
'"'
statusQue
[
#
statusQue
+
1
]
=
encode_string
(
k
)
statusQue
[
#
statusQue
+
1
]
=
":
"
statusQue
[
#
statusQue
+
1
]
=
'
":
'
encode
(
t
[
k
])
for
i
=
2
,
#
key
do
local
k
=
key
[
i
]
statusQue
[
#
statusQue
+
1
]
=
","
encode_newline
()
statusQue
[
#
statusQue
+
1
]
=
'"'
statusQue
[
#
statusQue
+
1
]
=
encode_string
(
k
)
statusQue
[
#
statusQue
+
1
]
=
":
"
statusQue
[
#
statusQue
+
1
]
=
'
":
'
encode
(
t
[
k
])
end
statusDep
=
statusDep
-
1
...
...
This diff is collapsed.
Click to expand it.
script/json.lua
+
103
−
75
View file @
242d5ed3
...
...
@@ -16,14 +16,17 @@ local string_format = string.format
local
math_type
=
math.type
local
setmetatable
=
setmetatable
local
getmetatable
=
getmetatable
local
Inf
=
math.huge
local
huge
=
math.huge
local
tiny
=
-
huge
local
json
=
{}
json
.
object
=
{}
json
.
supportSparseArray
=
true
-- json.encode --
local
status
Mark
local
status
Que
local
status
Visited
local
status
Builder
local
encode_map
=
{}
...
...
@@ -54,17 +57,22 @@ end
local
function
encode
(
v
)
local
res
=
encode_map
[
type
(
v
)](
v
)
status
Que
[
#
status
Que
+
1
]
=
res
status
Builder
[
#
status
Builder
+
1
]
=
res
end
encode_map
[
"nil"
]
=
function
()
return
"null"
end
local
function
encode_string
(
v
)
return
string_gsub
(
v
,
'[\0-\31\\"]'
,
encode_escape_map
)
end
function
encode_map
.
string
(
v
)
return
'"'
..
string_gsub
(
v
,
'[\0-\31\\"]'
,
encode_escape_map
)
..
'"'
statusBuilder
[
#
statusBuilder
+
1
]
=
'"'
statusBuilder
[
#
statusBuilder
+
1
]
=
encode_string
(
v
)
return
'"'
end
local
encode_string
=
encode_map
.
string
local
function
convertreal
(
v
)
local
g
=
string_format
(
'%.16g'
,
v
)
...
...
@@ -74,11 +82,18 @@ local function convertreal(v)
return
string_format
(
'%.17g'
,
v
)
end
if
string_match
(
tostring
(
1
/
2
),
"%p"
)
==
","
then
local
_convertreal
=
convertreal
function
convertreal
(
v
)
return
string_gsub
(
_convertreal
(
v
),
','
,
'.'
)
end
end
function
encode_map
.
number
(
v
)
if
v
~=
v
or
v
<=
-
Inf
or
v
>=
Inf
then
if
v
~=
v
or
v
<=
tiny
or
v
>=
huge
then
error
(
"unexpected number value '"
..
tostring
(
v
)
..
"'"
)
end
return
string_gsub
(
convertreal
(
v
)
,
','
,
'.'
)
return
convertreal
(
v
)
end
function
encode_map
.
boolean
(
v
)
...
...
@@ -98,50 +113,55 @@ function encode_map.table(t)
return
"[]"
end
end
if
status
Mark
[
t
]
then
if
status
Visited
[
t
]
then
error
(
"circular reference"
)
end
status
Mark
[
t
]
=
true
status
Visited
[
t
]
=
true
if
type
(
first_val
)
==
'string'
then
local
key
=
{}
local
key
s
=
{}
for
k
in
next
,
t
do
if
type
(
k
)
~=
"string"
then
error
(
"invalid table: mixed or invalid key types"
)
end
key
[
#
key
+
1
]
=
k
key
s
[
#
key
s
+
1
]
=
k
end
table_sort
(
key
)
statusQue
[
#
statusQue
+
1
]
=
"{"
local
k
=
key
[
1
]
status
Que
[
#
status
Que
+
1
]
=
encode_string
(
k
)
status
Que
[
#
status
Que
+
1
]
=
":
"
table_sort
(
key
s
)
local
k
=
keys
[
1
]
statusBuilder
[
#
statusBuilder
+
1
]
=
'{"'
status
Builder
[
#
status
Builder
+
1
]
=
encode_string
(
k
)
status
Builder
[
#
status
Builder
+
1
]
=
'
":
'
encode
(
t
[
k
])
for
i
=
2
,
#
key
do
local
k
=
key
[
i
]
status
Que
[
#
status
Que
+
1
]
=
"
,"
status
Que
[
#
status
Que
+
1
]
=
encode_string
(
k
)
status
Que
[
#
status
Que
+
1
]
=
":
"
for
i
=
2
,
#
key
s
do
local
k
=
key
s
[
i
]
status
Builder
[
#
status
Builder
+
1
]
=
'
,"
'
status
Builder
[
#
status
Builder
+
1
]
=
encode_string
(
k
)
status
Builder
[
#
status
Builder
+
1
]
=
'
":
'
encode
(
t
[
k
])
end
status
Mark
[
t
]
=
nil
status
Visited
[
t
]
=
nil
return
"}"
else
local
count
=
0
local
max
=
0
for
k
in
next
,
t
do
if
math_type
(
k
)
~=
"integer"
or
k
<=
0
then
error
(
"invalid table: mixed or invalid key types"
)
end
count
=
count
+
1
if
max
<
k
then
max
=
k
end
end
statusQue
[
#
statusQue
+
1
]
=
"["
if
not
json
.
supportSparseArray
and
count
~=
max
then
error
(
"sparse array are not supported"
)
end
statusBuilder
[
#
statusBuilder
+
1
]
=
"["
encode
(
t
[
1
])
for
i
=
2
,
max
do
status
Que
[
#
status
Que
+
1
]
=
","
status
Builder
[
#
status
Builder
+
1
]
=
","
encode
(
t
[
i
])
end
status
Mark
[
t
]
=
nil
status
Visited
[
t
]
=
nil
return
"]"
end
end
...
...
@@ -158,13 +178,14 @@ encode_map[ "userdata" ] = encode_unexpected
encode_map
[
"thread"
]
=
encode_unexpected
function
json
.
encode
(
v
)
status
Mark
=
{}
status
Que
=
{}
status
Visited
=
{}
status
Builder
=
{}
encode
(
v
)
return
table_concat
(
status
Que
)
return
table_concat
(
status
Builder
)
end
json
.
encode_map
=
encode_map
json
.
_encode_map
=
encode_map
json
.
_encode_string
=
encode_string
-- json.decode --
...
...
@@ -200,12 +221,20 @@ local function get_word()
end
local
function
next_byte
()
statusPos
=
string_find
(
statusBuf
,
"[^ \t\r\n]"
,
statusPos
)
if
statusPos
then
return
string_byte
(
statusBuf
,
statusPos
)
local
pos
=
string_find
(
statusBuf
,
"[^ \t\r\n]"
,
statusPos
)
if
pos
then
statusPos
=
pos
return
string_byte
(
statusBuf
,
pos
)
end
return
-
1
end
local
function
consume_byte
(
c
)
local
_
,
pos
=
string_find
(
statusBuf
,
c
,
statusPos
)
if
pos
then
statusPos
=
pos
+
1
return
true
end
statusPos
=
#
statusBuf
+
1
decode_error
(
"unexpected character '<eol>'"
)
end
local
function
expect_byte
(
c
)
...
...
@@ -272,49 +301,46 @@ local function decode_string()
end
local
function
decode_number
()
local
word
=
get_word
()
if
not
(
string_match
(
word
,
'^.[0-9]*$'
)
or
string_match
(
word
,
'^.[0-9]*%.[0-9]+$'
)
or
string_match
(
word
,
'^.[0-9]*[Ee][+-]?[0-9]+$'
)
or
string_match
(
word
,
'^.[0-9]*%.[0-9]+[Ee][+-]?[0-9]+$'
)
)
then
decode_error
(
"invalid number '"
..
word
..
"'"
)
local
num
,
c
=
string_match
(
statusBuf
,
'^([0-9]+%.?[0-9]*)([eE]?)'
,
statusPos
)
if
not
num
or
string_byte
(
num
,
-
1
)
==
0x2E
--[[ "." ]]
then
decode_error
(
"invalid number '"
..
get_word
()
..
"'"
)
end
statusPos
=
statusPos
+
#
word
return
tonumber
(
word
)
if
c
~=
''
then
num
=
string_match
(
statusBuf
,
'^([^eE]*[eE][-+]?[0-9]+)[ \t\r\n%]},]'
,
statusPos
)
if
not
num
then
decode_error
(
"invalid number '"
..
get_word
()
..
"'"
)
end
local
function
decode_number_negative
()
local
word
=
get_word
()
if
not
(
string_match
(
word
,
'^.[1-9][0-9]*$'
)
or
string_match
(
word
,
'^.[1-9][0-9]*%.[0-9]+$'
)
or
string_match
(
word
,
'^.[1-9][0-9]*[Ee][+-]?[0-9]+$'
)
or
string_match
(
word
,
'^.[1-9][0-9]*%.[0-9]+[Ee][+-]?[0-9]+$'
)
or
word
==
"-0"
or
string_match
(
word
,
'^.0%.[0-9]+$'
)
or
string_match
(
word
,
'^.0[Ee][+-]?[0-9]+$'
)
or
string_match
(
word
,
'^.0%.[0-9]+[Ee][+-]?[0-9]+$'
)
)
then
decode_error
(
"invalid number '"
..
word
..
"'"
)
end
statusPos
=
statusPos
+
#
word
return
tonumber
(
word
)
end
statusPos
=
statusPos
+
#
num
return
tonumber
(
num
)
end
local
function
decode_number_zero
()
local
word
=
get_word
()
if
not
(
#
word
==
1
or
string_match
(
word
,
'^.%.[0-9]+$'
)
or
string_match
(
word
,
'^.[Ee][+-]?[0-9]+$'
)
or
string_match
(
word
,
'^.%.[0-9]+[Ee][+-]?[0-9]+$'
)
)
then
decode_error
(
"invalid number '"
..
word
..
"'"
)
local
num
,
c
=
string_match
(
statusBuf
,
'^(.%.?[0-9]*)([eE]?)'
,
statusPos
)
if
not
num
or
string_byte
(
num
,
-
1
)
==
0x2E
--[[ "." ]]
or
string_match
(
statusBuf
,
'^.[0-9]+'
,
statusPos
)
then
decode_error
(
"invalid number '"
..
get_word
()
..
"'"
)
end
if
c
~=
''
then
num
=
string_match
(
statusBuf
,
'^([^eE]*[eE][-+]?[0-9]+)[ \t\r\n%]},]'
,
statusPos
)
if
not
num
then
decode_error
(
"invalid number '"
..
get_word
()
..
"'"
)
end
end
statusPos
=
statusPos
+
#
num
return
tonumber
(
num
)
end
local
function
decode_number_negative
()
statusPos
=
statusPos
+
1
local
c
=
string_byte
(
statusBuf
,
statusPos
)
if
c
then
if
c
==
0x30
then
return
-
decode_number_zero
()
elseif
c
>
0x30
and
c
<
0x3A
then
return
-
decode_number
()
end
statusPos
=
statusPos
+
#
wor
d
return
tonumber
(
word
)
en
d
decode_error
(
"invalid number '"
..
get_word
()
..
"'"
)
end
local
function
decode_true
()
...
...
@@ -344,8 +370,7 @@ end
local
function
decode_array
()
statusPos
=
statusPos
+
1
local
res
=
{}
if
next_byte
()
==
93
--[[ "]" ]]
then
statusPos
=
statusPos
+
1
if
consume_byte
"^[ \t\r\n]*%]"
then
return
res
end
statusTop
=
statusTop
+
1
...
...
@@ -357,8 +382,7 @@ end
local
function
decode_object
()
statusPos
=
statusPos
+
1
local
res
=
{}
if
next_byte
()
==
125
--[[ "}" ]]
then
statusPos
=
statusPos
+
1
if
consume_byte
"^[ \t\r\n]*}"
then
return
setmetatable
(
res
,
json
.
object
)
end
statusTop
=
statusTop
+
1
...
...
@@ -389,11 +413,15 @@ local decode_uncompleted_map = {
local
function
unexpected_character
()
decode_error
(
"unexpected character '"
..
string_sub
(
statusBuf
,
statusPos
,
statusPos
)
..
"'"
)
end
local
function
unexpected_eol
()
decode_error
(
"unexpected character '<eol>'"
)
end
local
decode_map
=
{}
for
i
=
0
,
255
do
decode_map
[
i
]
=
decode_uncompleted_map
[
i
]
or
unexpected_character
end
decode_map
[
-
1
]
=
unexpected_eol
local
function
decode
()
return
decode_map
[
next_byte
()]()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment