| 398 | } |
| 399 | |
| 400 | func parseString(p *peeker) (node, hcl.Diagnostics) { |
| 401 | tok := p.Read() |
| 402 | var str string |
| 403 | err := json.Unmarshal(tok.Bytes, &str) |
| 404 | |
| 405 | if err != nil { |
| 406 | var errRange hcl.Range |
| 407 | if serr, ok := err.(*json.SyntaxError); ok { |
| 408 | errOfs := serr.Offset |
| 409 | errPos := tok.Range.Start |
| 410 | errPos.Byte += int(errOfs) |
| 411 | |
| 412 | // TODO: Use the byte offset to properly count unicode |
| 413 | // characters for the column, and mark the whole of the |
| 414 | // character that was wrong as part of our range. |
| 415 | errPos.Column += int(errOfs) |
| 416 | |
| 417 | errEndPos := errPos |
| 418 | errEndPos.Byte++ |
| 419 | errEndPos.Column++ |
| 420 | |
| 421 | errRange = hcl.Range{ |
| 422 | Filename: tok.Range.Filename, |
| 423 | Start: errPos, |
| 424 | End: errEndPos, |
| 425 | } |
| 426 | } else { |
| 427 | errRange = tok.Range |
| 428 | } |
| 429 | |
| 430 | var contextRange *hcl.Range |
| 431 | if errRange != tok.Range { |
| 432 | contextRange = &tok.Range |
| 433 | } |
| 434 | |
| 435 | // FIXME: Eventually we should parse strings directly here so |
| 436 | // we can produce a more useful error message in the face fo things |
| 437 | // such as invalid escapes, etc. |
| 438 | return nil, hcl.Diagnostics{ |
| 439 | { |
| 440 | Severity: hcl.DiagError, |
| 441 | Summary: "Invalid JSON string", |
| 442 | Detail: "There is a syntax error in the given JSON string.", |
| 443 | Subject: &errRange, |
| 444 | Context: contextRange, |
| 445 | }, |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | return &stringVal{ |
| 450 | Value: str, |
| 451 | SrcRange: tok.Range, |
| 452 | }, nil |
| 453 | } |
| 454 | |
| 455 | func parseKeyword(p *peeker) (node, hcl.Diagnostics) { |
| 456 | tok := p.Read() |