-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
208 lines (196 loc) · 4.14 KB
/
json.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package jsonlp
import (
"errors"
"strconv"
"github.com/shellyln/go-loose-json-parser/jsonlp/class"
. "github.com/shellyln/takenoco/base"
. "github.com/shellyln/takenoco/string"
)
var (
jsonParser ParserFn
)
func init() {
jsonParser = jsonDocument()
}
func listValue() ParserFn {
return Trans(
FlatGroup(
erase((Seq("["))),
sp0(),
ZeroOrOnce(
FlatGroup(
First(
primitiveValue(),
Indirect(listValue),
Indirect(objectValue),
),
sp0(),
),
ZeroOrMoreTimes(
erase((Seq(","))),
sp0(),
First(
primitiveValue(),
Indirect(listValue),
Indirect(objectValue),
LookAhead(Seq("]")),
FlatGroup(
sp0(),
Error("Expect array closing parenthesis ')' or value"),
),
),
sp0(),
),
),
ZeroOrOnce(
erase((Seq(","))),
sp0(),
),
First(
erase((Seq("]"))),
FlatGroup(
sp0(),
Error("Expect array closing parenthesis ')'"),
),
),
sp0(),
),
func(ctx ParserContext, asts AstSlice) (AstSlice, error) {
length := len(asts)
v := make([]interface{}, length)
for i := 0; i < length; i++ {
v[i] = asts[i].Value
}
return AstSlice{{
ClassName: class.Array,
Type: AstType_Any,
Value: v,
}}, nil
},
)
}
func objectKey(allowLb bool) ParserFn {
return Trans(
First(
dottedIdentifier(allowLb),
stringValue(),
identifier(),
),
)
}
func objectKeyValuePair() ParserFn {
return FlatGroup(
objectKey(true),
sp0(),
erase(First(CharClass(":"), CharClass("=>"), CharClass("="))),
sp0(),
First(
primitiveValue(),
Indirect(listValue),
Indirect(objectValue),
Error("Expect object property value"),
),
sp0(),
)
}
func objectValue() ParserFn {
return Trans(
FlatGroup(
erase((Seq("{"))),
sp0(),
ZeroOrOnce(
objectKeyValuePair(),
ZeroOrMoreTimes(
erase((Seq(","))),
sp0(),
First(
objectKeyValuePair(),
LookAhead(Seq("}")),
FlatGroup(
sp0(),
Error("Expect object closing bracket '}' or key-value pair"),
),
),
),
ZeroOrOnce(
erase((Seq(","))),
sp0(),
),
),
First(
erase((Seq("}"))),
FlatGroup(
sp0(),
Error("Expect object closing bracket '}'"),
),
),
sp0(),
),
tableTransformer,
)
}
func jsonDocument() ParserFn {
return FlatGroup(
Start(),
sp0(),
First(
primitiveValue(),
listValue(),
objectValue(),
),
sp0(),
First(
End(),
Error("Expect terminatiion"),
),
)
}
// src: Loose JSON
//
// plafLb:
// Platform-dependent line break. (`Linebreak_Lf` | `Linebreak_CrLf` | `Linebreak_Cr`)
// Line break codes in multi-line string are replaced by this specified line break.
// (Excluding line breaks by escape sequences)
//
// interop:
// If Interop_JSON is set, replace NaN, Infinity, complex number by `{nan:true}`, `{inf:+/-1}`, `{re:re,im:im}`.
// If Interop_TOML is set, replace complex number by `{re:re,im:im}`.
// If Interop_JSON_AsNull is set, replace NaN, Infinity, complex number by null.
// If Interop_TOML_AsNull is set, replace complex number by null.
//
// parsed:
// nil | []any | map[string]any | float64 | int64 | uint64 | complex128 | string | bool | time.Time
func ParseJSON(s string, plafLb PlatformLinebreakType, interop InteropType) (interface{}, error) {
ctx := *NewStringParserContext(s)
opts := parseOptions{
interop: interop,
platformLinebreak: "\n",
isTOML: false,
}
switch plafLb {
case Linebreak_CrLf:
opts.platformLinebreak = "\r\n"
case Linebreak_Cr:
opts.platformLinebreak = "\r"
}
ctx.Tag = opts
out, err := jsonParser(ctx)
if err != nil {
pos := GetLineAndColPosition(s, out.SourcePosition, 4)
return nil, errors.New(
err.Error() +
"\n --> Line " + strconv.Itoa(pos.Line) +
", Col " + strconv.Itoa(pos.Col) + "\n" +
pos.ErrSource)
}
if out.MatchStatus == MatchStatus_Matched {
return out.AstStack[0].Value, nil
} else {
pos := GetLineAndColPosition(s, out.SourcePosition, 4)
return nil, errors.New(
"Parse failed" +
"\n --> Line " + strconv.Itoa(pos.Line) +
", Col " + strconv.Itoa(pos.Col) + "\n" +
pos.ErrSource)
}
}