MCPcopy
hub / github.com/hashicorp/hcl / parseArray

Function parseArray

json/parser.go:255–355  ·  view source on GitHub ↗
(p *peeker)

Source from the content-addressed store, hash-verified

253}
254
255func parseArray(p *peeker) (node, hcl.Diagnostics) {
256 var diags hcl.Diagnostics
257
258 open := p.Read()
259 vals := []node{}
260
261 // recover is used to shift the peeker to what seems to be the end of
262 // our array, so that when we encounter an error we leave the peeker
263 // at a reasonable point in the token stream to continue parsing.
264 recover := func(tok token) {
265 open := 1
266 for {
267 switch tok.Type {
268 case tokenBrackO:
269 open++
270 case tokenBrackC:
271 open--
272 if open <= 1 {
273 return
274 }
275 case tokenEOF:
276 // Ran out of source before we were able to recover,
277 // so we'll bail here and let the caller deal with it.
278 return
279 }
280 tok = p.Read()
281 }
282 }
283
284Token:
285 for {
286 if p.Peek().Type == tokenBrackC {
287 break Token
288 }
289
290 valNode, valDiags := parseValue(p)
291 diags = diags.Extend(valDiags)
292 if valNode == nil {
293 return nil, diags
294 }
295
296 vals = append(vals, valNode)
297
298 switch p.Peek().Type {
299 case tokenComma:
300 comma := p.Read()
301 if p.Peek().Type == tokenBrackC {
302 // Special error message for this common mistake
303 return nil, diags.Append(&hcl.Diagnostic{
304 Severity: hcl.DiagError,
305 Summary: "Trailing comma in array",
306 Detail: "JSON does not permit a trailing comma after the final value in an array.",
307 Subject: &comma.Range,
308 })
309 }
310 continue Token
311 case tokenColon:
312 recover(p.Read())

Callers 1

parseValueFunction · 0.85

Calls 6

ExtendMethod · 0.95
AppendMethod · 0.95
parseValueFunction · 0.85
PtrMethod · 0.80
ReadMethod · 0.45
PeekMethod · 0.45

Tested by

no test coverage detected