2017-05-15 15:35:32 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2017-05-16 15:00:07 +00:00
|
|
|
"encoding/json"
|
2017-05-16 02:37:20 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-09-09 23:08:19 +00:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2019-01-24 23:06:28 +00:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
2017-05-15 15:35:32 +00:00
|
|
|
)
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
func parseFileContent(buf []byte, filename string) (node, hcl.Diagnostics) {
|
2017-05-15 15:35:32 +00:00
|
|
|
tokens := scan(buf, pos{
|
|
|
|
Filename: filename,
|
2017-09-11 23:40:37 +00:00
|
|
|
Pos: hcl.Pos{
|
2017-05-15 15:35:32 +00:00
|
|
|
Byte: 0,
|
|
|
|
Line: 1,
|
|
|
|
Column: 1,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
p := newPeeker(tokens)
|
2017-05-16 15:00:07 +00:00
|
|
|
node, diags := parseValue(p)
|
2017-05-17 14:59:41 +00:00
|
|
|
if len(diags) == 0 && p.Peek().Type != tokenEOF {
|
2017-09-11 23:40:37 +00:00
|
|
|
diags = diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2017-05-17 14:59:41 +00:00
|
|
|
Summary: "Extraneous data after value",
|
|
|
|
Detail: "Extra characters appear after the JSON value.",
|
|
|
|
Subject: p.Peek().Range.Ptr(),
|
|
|
|
})
|
|
|
|
}
|
2017-05-16 15:00:07 +00:00
|
|
|
return node, diags
|
2017-05-15 15:35:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
func parseValue(p *peeker) (node, hcl.Diagnostics) {
|
2017-05-15 15:35:32 +00:00
|
|
|
tok := p.Peek()
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
wrapInvalid := func(n node, diags hcl.Diagnostics) (node, hcl.Diagnostics) {
|
2017-05-20 20:32:12 +00:00
|
|
|
if n != nil {
|
|
|
|
return n, diags
|
|
|
|
}
|
|
|
|
return invalidVal{tok.Range}, diags
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:35:32 +00:00
|
|
|
switch tok.Type {
|
|
|
|
case tokenBraceO:
|
2017-05-20 20:32:12 +00:00
|
|
|
return wrapInvalid(parseObject(p))
|
2017-05-15 15:35:32 +00:00
|
|
|
case tokenBrackO:
|
2017-05-20 20:32:12 +00:00
|
|
|
return wrapInvalid(parseArray(p))
|
2017-05-15 15:35:32 +00:00
|
|
|
case tokenNumber:
|
2017-05-20 20:32:12 +00:00
|
|
|
return wrapInvalid(parseNumber(p))
|
2017-05-15 15:35:32 +00:00
|
|
|
case tokenString:
|
2017-05-20 20:32:12 +00:00
|
|
|
return wrapInvalid(parseString(p))
|
2017-05-15 15:35:32 +00:00
|
|
|
case tokenKeyword:
|
2017-05-20 20:32:12 +00:00
|
|
|
return wrapInvalid(parseKeyword(p))
|
2017-05-15 15:35:32 +00:00
|
|
|
case tokenBraceC:
|
2017-09-11 23:40:37 +00:00
|
|
|
return wrapInvalid(nil, hcl.Diagnostics{
|
2017-05-15 15:35:32 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Severity: hcl.DiagError,
|
2018-07-18 22:41:35 +00:00
|
|
|
Summary: "Missing JSON value",
|
2017-05-15 15:35:32 +00:00
|
|
|
Detail: "A JSON value must start with a brace, a bracket, a number, a string, or a keyword.",
|
|
|
|
Subject: &tok.Range,
|
|
|
|
},
|
2017-05-20 20:32:12 +00:00
|
|
|
})
|
2017-05-15 15:35:32 +00:00
|
|
|
case tokenBrackC:
|
2017-09-11 23:40:37 +00:00
|
|
|
return wrapInvalid(nil, hcl.Diagnostics{
|
2017-05-15 15:35:32 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Severity: hcl.DiagError,
|
2017-05-15 15:35:32 +00:00
|
|
|
Summary: "Missing array element value",
|
|
|
|
Detail: "A JSON value must start with a brace, a bracket, a number, a string, or a keyword.",
|
|
|
|
Subject: &tok.Range,
|
|
|
|
},
|
2017-05-20 20:32:12 +00:00
|
|
|
})
|
2017-05-17 14:55:21 +00:00
|
|
|
case tokenEOF:
|
2017-09-11 23:40:37 +00:00
|
|
|
return wrapInvalid(nil, hcl.Diagnostics{
|
2017-05-17 14:55:21 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Severity: hcl.DiagError,
|
2017-05-17 14:55:21 +00:00
|
|
|
Summary: "Missing value",
|
|
|
|
Detail: "The JSON data ends prematurely.",
|
|
|
|
Subject: &tok.Range,
|
|
|
|
},
|
2017-05-20 20:32:12 +00:00
|
|
|
})
|
2017-05-15 15:35:32 +00:00
|
|
|
default:
|
2017-09-11 23:40:37 +00:00
|
|
|
return wrapInvalid(nil, hcl.Diagnostics{
|
2017-05-15 15:35:32 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Severity: hcl.DiagError,
|
2017-05-15 15:35:32 +00:00
|
|
|
Summary: "Invalid start of value",
|
|
|
|
Detail: "A JSON value must start with a brace, a bracket, a number, a string, or a keyword.",
|
|
|
|
Subject: &tok.Range,
|
|
|
|
},
|
2017-05-20 20:32:12 +00:00
|
|
|
})
|
2017-05-15 15:35:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tokenCanStartValue(tok token) bool {
|
|
|
|
switch tok.Type {
|
|
|
|
case tokenBraceO, tokenBrackO, tokenNumber, tokenString, tokenKeyword:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
func parseObject(p *peeker) (node, hcl.Diagnostics) {
|
|
|
|
var diags hcl.Diagnostics
|
2017-05-15 15:35:32 +00:00
|
|
|
|
|
|
|
open := p.Read()
|
2018-02-17 18:26:58 +00:00
|
|
|
attrs := []*objectAttr{}
|
2017-05-15 15:35:32 +00:00
|
|
|
|
2017-05-20 20:32:12 +00:00
|
|
|
// recover is used to shift the peeker to what seems to be the end of
|
|
|
|
// our object, so that when we encounter an error we leave the peeker
|
|
|
|
// at a reasonable point in the token stream to continue parsing.
|
|
|
|
recover := func(tok token) {
|
|
|
|
open := 1
|
|
|
|
for {
|
|
|
|
switch tok.Type {
|
|
|
|
case tokenBraceO:
|
|
|
|
open++
|
|
|
|
case tokenBraceC:
|
|
|
|
open--
|
|
|
|
if open <= 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case tokenEOF:
|
|
|
|
// Ran out of source before we were able to recover,
|
|
|
|
// so we'll bail here and let the caller deal with it.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tok = p.Read()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-15 15:35:32 +00:00
|
|
|
Token:
|
|
|
|
for {
|
|
|
|
if p.Peek().Type == tokenBraceC {
|
|
|
|
break Token
|
|
|
|
}
|
|
|
|
|
|
|
|
keyNode, keyDiags := parseValue(p)
|
|
|
|
diags = diags.Extend(keyDiags)
|
|
|
|
if keyNode == nil {
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
keyStrNode, ok := keyNode.(*stringVal)
|
|
|
|
if !ok {
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2018-07-18 22:41:35 +00:00
|
|
|
Summary: "Invalid object property name",
|
|
|
|
Detail: "A JSON object property name must be a string",
|
2017-05-15 15:35:32 +00:00
|
|
|
Subject: keyNode.StartRange().Ptr(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
key := keyStrNode.Value
|
|
|
|
|
|
|
|
colon := p.Read()
|
2017-05-16 15:00:07 +00:00
|
|
|
if colon.Type != tokenColon {
|
2017-05-20 20:32:12 +00:00
|
|
|
recover(colon)
|
|
|
|
|
2017-05-15 15:35:32 +00:00
|
|
|
if colon.Type == tokenBraceC || colon.Type == tokenComma {
|
|
|
|
// Catch common mistake of using braces instead of brackets
|
2017-05-20 20:32:12 +00:00
|
|
|
// for an object.
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2017-05-15 15:35:32 +00:00
|
|
|
Summary: "Missing object value",
|
|
|
|
Detail: "A JSON object attribute must have a value, introduced by a colon.",
|
|
|
|
Subject: &colon.Range,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-20 20:32:12 +00:00
|
|
|
if colon.Type == tokenEquals {
|
2018-01-27 18:51:26 +00:00
|
|
|
// Possible confusion with native HCL syntax.
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2018-07-18 22:41:35 +00:00
|
|
|
Summary: "Missing property value colon",
|
2017-05-20 20:32:12 +00:00
|
|
|
Detail: "JSON uses a colon as its name/value delimiter, not an equals sign.",
|
|
|
|
Subject: &colon.Range,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2018-07-18 22:41:35 +00:00
|
|
|
Summary: "Missing property value colon",
|
|
|
|
Detail: "A colon must appear between an object property's name and its value.",
|
2017-05-15 15:35:32 +00:00
|
|
|
Subject: &colon.Range,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
valNode, valDiags := parseValue(p)
|
|
|
|
diags = diags.Extend(valDiags)
|
2017-05-17 14:55:21 +00:00
|
|
|
if valNode == nil {
|
2017-05-15 15:35:32 +00:00
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
|
2018-02-17 18:26:58 +00:00
|
|
|
attrs = append(attrs, &objectAttr{
|
2017-05-15 15:35:32 +00:00
|
|
|
Name: key,
|
|
|
|
Value: valNode,
|
|
|
|
NameRange: keyStrNode.SrcRange,
|
2018-02-17 18:26:58 +00:00
|
|
|
})
|
2017-05-15 15:35:32 +00:00
|
|
|
|
|
|
|
switch p.Peek().Type {
|
|
|
|
case tokenComma:
|
2017-05-17 14:55:21 +00:00
|
|
|
comma := p.Read()
|
2017-05-15 15:35:32 +00:00
|
|
|
if p.Peek().Type == tokenBraceC {
|
|
|
|
// Special error message for this common mistake
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2017-05-15 15:35:32 +00:00
|
|
|
Summary: "Trailing comma in object",
|
2018-07-18 22:41:35 +00:00
|
|
|
Detail: "JSON does not permit a trailing comma after the final property in an object.",
|
2017-05-17 14:55:21 +00:00
|
|
|
Subject: &comma.Range,
|
2017-05-15 15:35:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
continue Token
|
2017-05-16 15:00:07 +00:00
|
|
|
case tokenEOF:
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2017-05-16 15:00:07 +00:00
|
|
|
Summary: "Unclosed object",
|
|
|
|
Detail: "No closing brace was found for this JSON object.",
|
|
|
|
Subject: &open.Range,
|
|
|
|
})
|
|
|
|
case tokenBrackC:
|
2017-05-20 20:32:12 +00:00
|
|
|
// Consume the bracket anyway, so that we don't return with the peeker
|
|
|
|
// at a strange place.
|
|
|
|
p.Read()
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2017-05-16 15:00:07 +00:00
|
|
|
Summary: "Mismatched braces",
|
|
|
|
Detail: "A JSON object must be closed with a brace, not a bracket.",
|
|
|
|
Subject: p.Peek().Range.Ptr(),
|
|
|
|
})
|
2017-05-15 15:35:32 +00:00
|
|
|
case tokenBraceC:
|
|
|
|
break Token
|
|
|
|
default:
|
2017-05-20 20:32:12 +00:00
|
|
|
recover(p.Read())
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2017-05-15 15:35:32 +00:00
|
|
|
Summary: "Missing attribute seperator comma",
|
2018-07-18 22:41:35 +00:00
|
|
|
Detail: "A comma must appear between each property definition in an object.",
|
2017-05-17 14:55:21 +00:00
|
|
|
Subject: p.Peek().Range.Ptr(),
|
2017-05-15 15:35:32 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
close := p.Read()
|
|
|
|
return &objectVal{
|
2017-05-21 18:46:58 +00:00
|
|
|
Attrs: attrs,
|
2017-09-11 23:40:37 +00:00
|
|
|
SrcRange: hcl.RangeBetween(open.Range, close.Range),
|
2017-05-21 18:46:58 +00:00
|
|
|
OpenRange: open.Range,
|
|
|
|
CloseRange: close.Range,
|
2017-05-15 15:35:32 +00:00
|
|
|
}, diags
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
func parseArray(p *peeker) (node, hcl.Diagnostics) {
|
|
|
|
var diags hcl.Diagnostics
|
2017-05-17 14:55:21 +00:00
|
|
|
|
|
|
|
open := p.Read()
|
|
|
|
vals := []node{}
|
|
|
|
|
2017-05-20 20:32:12 +00:00
|
|
|
// recover is used to shift the peeker to what seems to be the end of
|
|
|
|
// our array, so that when we encounter an error we leave the peeker
|
|
|
|
// at a reasonable point in the token stream to continue parsing.
|
|
|
|
recover := func(tok token) {
|
|
|
|
open := 1
|
|
|
|
for {
|
|
|
|
switch tok.Type {
|
|
|
|
case tokenBrackO:
|
|
|
|
open++
|
|
|
|
case tokenBrackC:
|
|
|
|
open--
|
|
|
|
if open <= 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
case tokenEOF:
|
|
|
|
// Ran out of source before we were able to recover,
|
|
|
|
// so we'll bail here and let the caller deal with it.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tok = p.Read()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 14:55:21 +00:00
|
|
|
Token:
|
|
|
|
for {
|
|
|
|
if p.Peek().Type == tokenBrackC {
|
|
|
|
break Token
|
|
|
|
}
|
|
|
|
|
|
|
|
valNode, valDiags := parseValue(p)
|
|
|
|
diags = diags.Extend(valDiags)
|
|
|
|
if valNode == nil {
|
|
|
|
return nil, diags
|
|
|
|
}
|
|
|
|
|
|
|
|
vals = append(vals, valNode)
|
|
|
|
|
|
|
|
switch p.Peek().Type {
|
|
|
|
case tokenComma:
|
|
|
|
comma := p.Read()
|
|
|
|
if p.Peek().Type == tokenBrackC {
|
|
|
|
// Special error message for this common mistake
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2017-05-17 14:55:21 +00:00
|
|
|
Summary: "Trailing comma in array",
|
2018-07-18 22:41:35 +00:00
|
|
|
Detail: "JSON does not permit a trailing comma after the final value in an array.",
|
2017-05-17 14:55:21 +00:00
|
|
|
Subject: &comma.Range,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
continue Token
|
|
|
|
case tokenColon:
|
2017-05-20 20:32:12 +00:00
|
|
|
recover(p.Read())
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2017-05-17 14:55:21 +00:00
|
|
|
Summary: "Invalid array value",
|
|
|
|
Detail: "A colon is not used to introduce values in a JSON array.",
|
|
|
|
Subject: p.Peek().Range.Ptr(),
|
|
|
|
})
|
|
|
|
case tokenEOF:
|
2017-05-20 20:32:12 +00:00
|
|
|
recover(p.Read())
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2017-05-17 14:55:21 +00:00
|
|
|
Summary: "Unclosed object",
|
|
|
|
Detail: "No closing bracket was found for this JSON array.",
|
|
|
|
Subject: &open.Range,
|
|
|
|
})
|
|
|
|
case tokenBraceC:
|
2017-05-20 20:32:12 +00:00
|
|
|
recover(p.Read())
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2017-05-17 14:55:21 +00:00
|
|
|
Summary: "Mismatched brackets",
|
|
|
|
Detail: "A JSON array must be closed with a bracket, not a brace.",
|
|
|
|
Subject: p.Peek().Range.Ptr(),
|
|
|
|
})
|
|
|
|
case tokenBrackC:
|
|
|
|
break Token
|
|
|
|
default:
|
2017-05-20 20:32:12 +00:00
|
|
|
recover(p.Read())
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, diags.Append(&hcl.Diagnostic{
|
|
|
|
Severity: hcl.DiagError,
|
2017-05-17 14:55:21 +00:00
|
|
|
Summary: "Missing attribute seperator comma",
|
|
|
|
Detail: "A comma must appear between each value in an array.",
|
|
|
|
Subject: p.Peek().Range.Ptr(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
close := p.Read()
|
|
|
|
return &arrayVal{
|
|
|
|
Values: vals,
|
2017-09-11 23:40:37 +00:00
|
|
|
SrcRange: hcl.RangeBetween(open.Range, close.Range),
|
2017-05-17 14:55:21 +00:00
|
|
|
OpenRange: open.Range,
|
|
|
|
}, diags
|
2017-05-15 15:35:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
func parseNumber(p *peeker) (node, hcl.Diagnostics) {
|
2017-05-17 14:34:33 +00:00
|
|
|
tok := p.Read()
|
|
|
|
|
|
|
|
// Use encoding/json to validate the number syntax.
|
|
|
|
// TODO: Do this more directly to produce better diagnostics.
|
|
|
|
var num json.Number
|
|
|
|
err := json.Unmarshal(tok.Bytes, &num)
|
|
|
|
if err != nil {
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, hcl.Diagnostics{
|
2017-05-17 14:34:33 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Severity: hcl.DiagError,
|
2017-05-17 14:34:33 +00:00
|
|
|
Summary: "Invalid JSON number",
|
|
|
|
Detail: fmt.Sprintf("There is a syntax error in the given JSON number."),
|
|
|
|
Subject: &tok.Range,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 23:06:28 +00:00
|
|
|
// We want to guarantee that we parse numbers the same way as cty (and thus
|
|
|
|
// native syntax HCL) would here, so we'll use the cty parser even though
|
|
|
|
// in most other cases we don't actually introduce cty concepts until
|
|
|
|
// decoding time. We'll unwrap the parsed float immediately afterwards, so
|
|
|
|
// the cty value is just a temporary helper.
|
|
|
|
nv, err := cty.ParseNumberVal(string(num))
|
2017-05-17 14:34:33 +00:00
|
|
|
if err != nil {
|
|
|
|
// Should never happen if above passed, since JSON numbers are a subset
|
2019-01-24 23:06:28 +00:00
|
|
|
// of what cty can parse...
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, hcl.Diagnostics{
|
2017-05-17 14:34:33 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Severity: hcl.DiagError,
|
2017-05-17 14:34:33 +00:00
|
|
|
Summary: "Invalid JSON number",
|
|
|
|
Detail: fmt.Sprintf("There is a syntax error in the given JSON number."),
|
|
|
|
Subject: &tok.Range,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &numberVal{
|
2019-01-24 23:06:28 +00:00
|
|
|
Value: nv.AsBigFloat(),
|
2017-05-17 14:34:33 +00:00
|
|
|
SrcRange: tok.Range,
|
|
|
|
}, nil
|
2017-05-15 15:35:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
func parseString(p *peeker) (node, hcl.Diagnostics) {
|
2017-05-16 15:00:07 +00:00
|
|
|
tok := p.Read()
|
|
|
|
var str string
|
|
|
|
err := json.Unmarshal(tok.Bytes, &str)
|
|
|
|
|
|
|
|
if err != nil {
|
2017-09-11 23:40:37 +00:00
|
|
|
var errRange hcl.Range
|
2017-05-16 15:00:07 +00:00
|
|
|
if serr, ok := err.(*json.SyntaxError); ok {
|
|
|
|
errOfs := serr.Offset
|
|
|
|
errPos := tok.Range.Start
|
|
|
|
errPos.Byte += int(errOfs)
|
|
|
|
|
|
|
|
// TODO: Use the byte offset to properly count unicode
|
|
|
|
// characters for the column, and mark the whole of the
|
|
|
|
// character that was wrong as part of our range.
|
|
|
|
errPos.Column += int(errOfs)
|
|
|
|
|
|
|
|
errEndPos := errPos
|
|
|
|
errEndPos.Byte++
|
|
|
|
errEndPos.Column++
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
errRange = hcl.Range{
|
2017-05-16 15:00:07 +00:00
|
|
|
Filename: tok.Range.Filename,
|
|
|
|
Start: errPos,
|
|
|
|
End: errEndPos,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
errRange = tok.Range
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
var contextRange *hcl.Range
|
2017-05-16 15:00:07 +00:00
|
|
|
if errRange != tok.Range {
|
|
|
|
contextRange = &tok.Range
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Eventually we should parse strings directly here so
|
|
|
|
// we can produce a more useful error message in the face fo things
|
|
|
|
// such as invalid escapes, etc.
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, hcl.Diagnostics{
|
2017-05-16 15:00:07 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Severity: hcl.DiagError,
|
2017-05-16 15:00:07 +00:00
|
|
|
Summary: "Invalid JSON string",
|
|
|
|
Detail: fmt.Sprintf("There is a syntax error in the given JSON string."),
|
|
|
|
Subject: &errRange,
|
|
|
|
Context: contextRange,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &stringVal{
|
|
|
|
Value: str,
|
|
|
|
SrcRange: tok.Range,
|
|
|
|
}, nil
|
2017-05-15 15:35:32 +00:00
|
|
|
}
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
func parseKeyword(p *peeker) (node, hcl.Diagnostics) {
|
2017-05-16 02:37:20 +00:00
|
|
|
tok := p.Read()
|
|
|
|
s := string(tok.Bytes)
|
|
|
|
|
|
|
|
switch s {
|
|
|
|
case "true":
|
|
|
|
return &booleanVal{
|
|
|
|
Value: true,
|
|
|
|
SrcRange: tok.Range,
|
|
|
|
}, nil
|
|
|
|
case "false":
|
|
|
|
return &booleanVal{
|
|
|
|
Value: false,
|
|
|
|
SrcRange: tok.Range,
|
|
|
|
}, nil
|
|
|
|
case "null":
|
|
|
|
return &nullVal{
|
|
|
|
SrcRange: tok.Range,
|
|
|
|
}, nil
|
|
|
|
case "undefined", "NaN", "Infinity":
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, hcl.Diagnostics{
|
2017-05-16 02:37:20 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Severity: hcl.DiagError,
|
2017-05-16 02:37:20 +00:00
|
|
|
Summary: "Invalid JSON keyword",
|
|
|
|
Detail: fmt.Sprintf("The JavaScript identifier %q cannot be used in JSON.", s),
|
|
|
|
Subject: &tok.Range,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
var dym string
|
|
|
|
if suggest := keywordSuggestion(s); suggest != "" {
|
|
|
|
dym = fmt.Sprintf(" Did you mean %q?", suggest)
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:40:37 +00:00
|
|
|
return nil, hcl.Diagnostics{
|
2017-05-16 02:37:20 +00:00
|
|
|
{
|
2017-09-11 23:40:37 +00:00
|
|
|
Severity: hcl.DiagError,
|
2017-05-16 02:37:20 +00:00
|
|
|
Summary: "Invalid JSON keyword",
|
|
|
|
Detail: fmt.Sprintf("%q is not a valid JSON keyword.%s", s, dym),
|
|
|
|
Subject: &tok.Range,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 15:35:32 +00:00
|
|
|
}
|