| 355 | } |
| 356 | |
| 357 | func parseNumber(p *peeker) (node, hcl.Diagnostics) { |
| 358 | tok := p.Read() |
| 359 | |
| 360 | // Use encoding/json to validate the number syntax. |
| 361 | // TODO: Do this more directly to produce better diagnostics. |
| 362 | var num json.Number |
| 363 | err := json.Unmarshal(tok.Bytes, &num) |
| 364 | if err != nil { |
| 365 | return nil, hcl.Diagnostics{ |
| 366 | { |
| 367 | Severity: hcl.DiagError, |
| 368 | Summary: "Invalid JSON number", |
| 369 | Detail: "There is a syntax error in the given JSON number.", |
| 370 | Subject: &tok.Range, |
| 371 | }, |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | // We want to guarantee that we parse numbers the same way as cty (and thus |
| 376 | // native syntax HCL) would here, so we'll use the cty parser even though |
| 377 | // in most other cases we don't actually introduce cty concepts until |
| 378 | // decoding time. We'll unwrap the parsed float immediately afterwards, so |
| 379 | // the cty value is just a temporary helper. |
| 380 | nv, err := cty.ParseNumberVal(string(num)) |
| 381 | if err != nil { |
| 382 | // Should never happen if above passed, since JSON numbers are a subset |
| 383 | // of what cty can parse... |
| 384 | return nil, hcl.Diagnostics{ |
| 385 | { |
| 386 | Severity: hcl.DiagError, |
| 387 | Summary: "Invalid JSON number", |
| 388 | Detail: "There is a syntax error in the given JSON number.", |
| 389 | Subject: &tok.Range, |
| 390 | }, |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return &numberVal{ |
| 395 | Value: nv.AsBigFloat(), |
| 396 | SrcRange: tok.Range, |
| 397 | }, nil |
| 398 | } |
| 399 | |
| 400 | func parseString(p *peeker) (node, hcl.Diagnostics) { |
| 401 | tok := p.Read() |