Debian Codegraph @

json/parse.go

raw
1 // Package json is a JSON parser following the specifications at http://json.org/.
2 package json
3
4 import (
5 "strconv"
6
7 "github.com/tdewolff/parse/v2"
8 )
9
10 // GrammarType determines the type of grammar
11 type GrammarType uint32
12
13 // GrammarType values.
14 const (
15 ErrorGrammar GrammarType = iota // extra grammar when errors occur
16 WhitespaceGrammar
17 LiteralGrammar
18 NumberGrammar
19 StringGrammar
20 StartObjectGrammar // {
21 EndObjectGrammar // }
22 StartArrayGrammar // [
23 EndArrayGrammar // ]
24 )
25
26 // String returns the string representation of a GrammarType.
27 func (gt GrammarType) String() string {
28 switch gt {
29 case ErrorGrammar:
30 return "Error"
31 case WhitespaceGrammar:
32 return "Whitespace"
33 case LiteralGrammar:
34 return "Literal"
35 case NumberGrammar:
36 return "Number"
37 case StringGrammar:
38 return "String"
39 case StartObjectGrammar:
40 return "StartObject"
41 case EndObjectGrammar:
42 return "EndObject"
43 case StartArrayGrammar:
44 return "StartArray"
45 case EndArrayGrammar:
46 return "EndArray"
47 }
48 return "Invalid(" + strconv.Itoa(int(gt)) + ")"
49 }
50
51 ////////////////////////////////////////////////////////////////
52
53 // State determines the current state the parser is in.
54 type State uint32
55
56 // State values.
57 const (
58 ValueState State = iota // extra token when errors occur
59 ObjectKeyState
60 ObjectValueState
61 ArrayState
62 )
63
64 // String returns the string representation of a State.
65 func (state State) String() string {
66 switch state {
67 case ValueState:
68 return "Value"
69 case ObjectKeyState:
70 return "ObjectKey"
71 case ObjectValueState:
72 return "ObjectValue"
73 case ArrayState:
74 return "Array"
75 }
76 return "Invalid(" + strconv.Itoa(int(state)) + ")"
77 }
78
79 ////////////////////////////////////////////////////////////////
80
81 // Parser is the state for the lexer.
82 type Parser struct {
83 r *parse.Input
84 state []State
85 err error
86
87 needComma bool
88 }
89
90 // NewParser returns a new Parser for a given io.Reader.
91 func NewParser(r *parse.Input) *Parser {
92 return &Parser{
93 r: r,
94 state: []State{ValueState},
95 }
96 }
97
98 // Err returns the error encountered during tokenization, this is often io.EOF but also other errors can be returned.
99 func (p *Parser) Err() error {
100 if p.err != nil {
101 return p.err
102 }
103 return p.r.Err()
104 }
105
106 // State returns the state the parser is currently in (ie. which token is expected).
107 func (p *Parser) State() State {
108 return p.state[len(p.state)-1]
109 }
110
111 // Next returns the next Grammar. It returns ErrorGrammar when an error was encountered. Using Err() one can retrieve the error message.
112 func (p *Parser) Next() (GrammarType, []byte) {
113 p.moveWhitespace()
114 c := p.r.Peek(0)
115 state := p.state[len(p.state)-1]
116 if c == ',' {
117 if state != ArrayState && state != ObjectKeyState {
118 p.err = parse.NewErrorLexer(p.r, "unexpected comma character")
119 return ErrorGrammar, nil
120 }
121 p.r.Move(1)
122 p.moveWhitespace()
123 p.needComma = false
124 c = p.r.Peek(0)
125 }
126 p.r.Skip()
127
128 if p.needComma && c != '}' && c != ']' && c != 0 {
129 p.err = parse.NewErrorLexer(p.r, "expected comma character or an array or object ending")
130 return ErrorGrammar, nil
131 } else if c == '{' {
132 p.state = append(p.state, ObjectKeyState)
133 p.r.Move(1)
134 return StartObjectGrammar, p.r.Shift()
135 } else if c == '}' {
136 if state != ObjectKeyState {
137 p.err = parse.NewErrorLexer(p.r, "unexpected right brace character")
138 return ErrorGrammar, nil
139 }
140 p.needComma = true
141 p.state = p.state[:len(p.state)-1]
142 if p.state[len(p.state)-1] == ObjectValueState {
143 p.state[len(p.state)-1] = ObjectKeyState
144 }
145 p.r.Move(1)
146 return EndObjectGrammar, p.r.Shift()
147 } else if c == '[' {
148 p.state = append(p.state, ArrayState)
149 p.r.Move(1)
150 return StartArrayGrammar, p.r.Shift()
151 } else if c == ']' {
152 p.needComma = true
153 if state != ArrayState {
154 p.err = parse.NewErrorLexer(p.r, "unexpected right bracket character")
155 return ErrorGrammar, nil
156 }
157 p.state = p.state[:len(p.state)-1]
158 if p.state[len(p.state)-1] == ObjectValueState {
159 p.state[len(p.state)-1] = ObjectKeyState
160 }
161 p.r.Move(1)
162 return EndArrayGrammar, p.r.Shift()
163 } else if state == ObjectKeyState {
164 if c != '"' || !p.consumeStringToken() {
165 p.err = parse.NewErrorLexer(p.r, "expected object key to be a quoted string")
166 return ErrorGrammar, nil
167 }
168 n := p.r.Pos()
169 p.moveWhitespace()
170 if c := p.r.Peek(0); c != ':' {
171 p.err = parse.NewErrorLexer(p.r, "expected colon character after object key")
172 return ErrorGrammar, nil
173 }
174 p.r.Move(1)
175 p.state[len(p.state)-1] = ObjectValueState
176 return StringGrammar, p.r.Shift()[:n]
177 } else {
178 p.needComma = true
179 if state == ObjectValueState {
180 p.state[len(p.state)-1] = ObjectKeyState
181 }
182 if c == '"' && p.consumeStringToken() {
183 return StringGrammar, p.r.Shift()
184 } else if p.consumeNumberToken() {
185 return NumberGrammar, p.r.Shift()
186 } else if p.consumeLiteralToken() {
187 return LiteralGrammar, p.r.Shift()
188 }
189 c := p.r.Peek(0) // pick up movement from consumeStringToken to detect NULL or EOF
190 if c == 0 && p.r.Err() == nil {
191 p.err = parse.NewErrorLexer(p.r, "unexpected NULL character")
192 return ErrorGrammar, nil
193 } else if c == 0 { // EOF
194 return ErrorGrammar, nil
195 }
196 }
197 p.err = parse.NewErrorLexer(p.r, "unexpected character '%c'", c)
198 return ErrorGrammar, nil
199 }
200
201 ////////////////////////////////////////////////////////////////
202
203 /*
204 The following functions follow the specifications at http://json.org/
205 */
206
207 func (p *Parser) moveWhitespace() {
208 for {
209 if c := p.r.Peek(0); c != ' ' && c != '\n' && c != '\r' && c != '\t' {
210 break
211 }
212 p.r.Move(1)
213 }
214 }
215
216 func (p *Parser) consumeLiteralToken() bool {
217 c := p.r.Peek(0)
218 if c == 't' && p.r.Peek(1) == 'r' && p.r.Peek(2) == 'u' && p.r.Peek(3) == 'e' {
219 p.r.Move(4)
220 return true
221 } else if c == 'f' && p.r.Peek(1) == 'a' && p.r.Peek(2) == 'l' && p.r.Peek(3) == 's' && p.r.Peek(4) == 'e' {
222 p.r.Move(5)
223 return true
224 } else if c == 'n' && p.r.Peek(1) == 'u' && p.r.Peek(2) == 'l' && p.r.Peek(3) == 'l' {
225 p.r.Move(4)
226 return true
227 }
228 return false
229 }
230
231 func (p *Parser) consumeNumberToken() bool {
232 mark := p.r.Pos()
233 if p.r.Peek(0) == '-' {
234 p.r.Move(1)
235 }
236 c := p.r.Peek(0)
237 if c >= '1' && c <= '9' {
238 p.r.Move(1)
239 for {
240 if c := p.r.Peek(0); c < '0' || c > '9' {
241 break
242 }
243 p.r.Move(1)
244 }
245 } else if c != '0' {
246 p.r.Rewind(mark)
247 return false
248 } else {
249 p.r.Move(1) // 0
250 }
251 if c := p.r.Peek(0); c == '.' {
252 p.r.Move(1)
253 if c := p.r.Peek(0); c < '0' || c > '9' {
254 p.r.Move(-1)
255 return true
256 }
257 for {
258 if c := p.r.Peek(0); c < '0' || c > '9' {
259 break
260 }
261 p.r.Move(1)
262 }
263 }
264 mark = p.r.Pos()
265 if c := p.r.Peek(0); c == 'e' || c == 'E' {
266 p.r.Move(1)
267 if c := p.r.Peek(0); c == '+' || c == '-' {
268 p.r.Move(1)
269 }
270 if c := p.r.Peek(0); c < '0' || c > '9' {
271 p.r.Rewind(mark)
272 return true
273 }
274 for {
275 if c := p.r.Peek(0); c < '0' || c > '9' {
276 break
277 }
278 p.r.Move(1)
279 }
280 }
281 return true
282 }
283
284 func (p *Parser) consumeStringToken() bool {
285 // assume to be on "
286 p.r.Move(1)
287 for {
288 c := p.r.Peek(0)
289 if c == '"' {
290 escaped := false
291 for i := p.r.Pos() - 1; i >= 0; i-- {
292 if p.r.Lexeme()[i] == '\\' {
293 escaped = !escaped
294 } else {
295 break
296 }
297 }
298 if !escaped {
299 p.r.Move(1)
300 break
301 }
302 } else if c == 0 {
303 return false
304 }
305 p.r.Move(1)
306 }
307 return true
308 }